当前位置: 首页 > article >正文

将PHP函数转换为Python

将 PHP 函数转换为 Python 函数时,需要注意两者之间语法和特性上的差异。

在这里插入图片描述

问题背景

我们试图将下面的 PHP 函数移植到 Python。但是,在执行移植过程中,我们遇到了一个错误:line 189, in detectOnSubImage
rect = rects[i_rect]
IndexError: list index out of range

rects 接受位于 current_node[1] 的列表

rects = current_node[1]

而且,当循环变量 i_rect 大于或等于 rects 列表长度时,while 循环将不退出。

在我们移植这个 PHP 函数到 Python 时遗漏了什么?正确的 Python 代码等价物是什么?

PHP 代码如下:

protected function detectOnSubImage($x, $y, $scale, $ii, $ii2, $w, $iiw, $inv_area)
{
    $inv_area";
    $mean  = ($ii[($y+$w)*$iiw + $x + $w] + $ii[$y*$iiw+$x] - $ii[($y+$w)*$iiw+$x] - $ii[$y*$iiw+$x+$w])*$inv_area;
    $vnorm = ($ii2[($y+$w)*$iiw + $x + $w]
              + $ii2[$y*$iiw+$x]
              - $ii2[($y+$w)*$iiw+$x]
              - $ii2[$y*$iiw+$x+$w])*$inv_area - ($mean*$mean);
    $vnorm = $vnorm > 1 ? sqrt($vnorm) : 1;
    $passed = true;
    for ($i_stage = 0; $i_stage < count($this->detection_data); $i_stage++) {
        $stage = $this->detection_data[$i_stage];
        $trees = $stage[0];

        $stage_thresh = $stage[1];
        $stage_sum = 0;

        for ($i_tree = 0; $i_tree < count($trees); $i_tree++) {
            $tree = $trees[$i_tree];
            $current_node = $tree[0];
            $tree_sum = 0;
            while ($current_node != null) {
                $vals = $current_node[0];
                $node_thresh = $vals[0];
                $leftval = $vals[1];
                $rightval = $vals[2];
                $leftidx = $vals[3];
                $rightidx = $vals[4];
                $rects = $current_node[1];

                $rect_sum = 0;
                for ($i_rect = 0; $i_rect < count($rects); $i_rect++) {
                    $s = $scale;
                    $rect = $rects[$i_rect];
                    $rx = ($rect[0]*$s+$x)>>0;
                    $ry = ($rect[1]*$s+$y)>>0;
                    $rw = ($rect[2]*$s)>>0;
                    $rh = ($rect[3]*$s)>>0;
                    $wt = $rect[4];
                    $r_sum = ($ii[($ry+$rh)*$iiw + $rx + $rw]
                              + $ii[$ry*$iiw+$rx]
                              - $ii[($ry+$rh)*$iiw+$rx]
                              - $ii[$ry*$iiw+$rx+$rw])*$wt;
                    $rect_sum += $r_sum;
                }
                $rect_sum *= $inv_area;
                $current_node = null;
                if ($rect_sum >= $node_thresh*$vnorm) {
                    if ($rightidx == -1) {
                        $tree_sum = $rightval;
                    } else {
                        $current_node = $tree[$rightidx];
                    }
                } else {
                    if ($leftidx == -1) {
                        $tree_sum = $leftval;
                    } else {
                        $current_node = $tree[$leftidx];
                    }
                }
            }
            $stage_sum += $tree_sum;
        }
        if ($stage_sum < $stage_thresh) {
            return false;
        }
    }
    return true;
 }
}

Python 代码如下:

def detectOnSubImage(self, x, y, scale, ii, ii2, w, iiw, inv_area):  
    mean  = (ii[(y+w)*iiw + x + w] + ii[y*iiw+x] - ii[(y+w)*iiw+x] - ii[y*iiw+x+w])*inv_area  
    vnorm = (ii2[(y+w)*iiw + x + w] + ii2[y*iiw+x] - ii2[(y+w)*iiw+x] - ii2[y*iiw+x+w])*inv_area - (mean*mean)  
    vnorm = sqrt(vnorm) if vnorm > 1 else 1  
    #var foo = (test) ? "True" : "False";  
    #foo = "True" if test else "False"  
    passed = True  
    #for i_stage in xrange(0, i_stage < (len(self.detection_data)), i_stage= i_stage+1):  
    i_stage=0  
    while i_stage < len(self.detection_data):  
        i_stage= i_stage+1  
        stage = self.detection_data[i_stage]  
        trees = stage[0]  
        stage_thresh = stage[1]
        stage_sum = 0

        #for i_tree in xrange( 0, i_tree < len(trees), i_tree= i_tree+1):
        i_tree=0
        while i_tree < len(trees):
            i_tree= i_tree+1
            tree = trees[i_tree]
            current_node = tree[0]
            tree_sum = 0
            while (current_node != None):
                vals = current_node[0]
                node_thresh = vals[0]
                leftval = vals[1]
                rightval = vals[2]
                leftidx = vals[3]
                rightidx = vals[4]
                rects = current_node[1]
                rect_sum = 0
                #for i_rect in xrange(0, i_rect < len(rects), i_rect = i_rec+1):
                i_rect = 0
                while i_rect < len(rects):
                    i_rect = i_rect+1
                    s = scale
                    rect = rects[i_rect]
                    rx = (rect[0]*s+x)>>0
                    ry = (rect[1]*s+y)>>0
                    rw = (rect[2]*s)>>0
                    rh = (rect[3]*s)>>0
                    wt = rect[4]

                    r_sum = (ii[(ry+rh)*iiw + rx + rw] + ii[ry*iiw+rx] - ii[(ry+rh)*iiw+rx] - ii[ry*iiw+rx+rw])*wt
                    rect_sum = rect_sum + r_sum
                rect_sum = rect_sum * inv_area
                current_node = None
                if (rect_sum >= node_thresh*vnorm):
                    if (rightidx == -1):
                        tree_sum = rightval
                    else: 
                        current_node = tree[rightidx]
                else:
                    if (leftidx == -1):
                        tree_sum = leftval
                    else:
                        current_node = tree[leftidx]
            stage_sum = stage_sum + tree_sum
        if (stage_sum < stage_thresh):
            return false
    return True

解决方法

  1. 在 Python 版本的 detectOnSubImage 方法中,使用 range() 函数代替 xrange() 函数。
  2. 在 Python 版本的 detectOnSubImage 方法中,使用 while 循环代替 for 循环。
  3. 在 Python 版本的 detectOnSubImage 方法中,使用 if 语句代替 var_dump() 函数。
  4. 在 Python 版本的 detectOnSubImage 方法中,使用 sqrt() 函数计算 vnorm。

代码例子

正确的 Python 代码等价物如下:

def detectOnSubImage(self, x, y, scale, ii, ii2, w, iiw, inv_area):  
    mean  = (ii[(y+w)*iiw + x + w] + ii[y*iiw+x] - ii[(y+w)*iiw+

如果有更复杂的 PHP 函数逻辑,请提供代码,我可以进一步帮助转换!


http://www.kler.cn/a/501327.html

相关文章:

  • 人才选拔中,如何优化面试流程
  • 项目开发版本控制Git流程规范
  • 【Linux】Linux软件包管理
  • 接口项目功能说明-thinkphp6-rabbitmq
  • pip工具安装第三方库
  • 【深度学习基础与pytorch基础】1机器学习的定义与分类以及机器学习、深度学习和人工智能之间的关系
  • 【PyQt】常用控件button
  • 在线工具箱源码优化版
  • 小白项目部署:anaconda环境+pycharm+yolov5(虚拟机环境)
  • Pulsar客户端如何控制内存使用
  • QCC3040主端音频蓝牙模块在跑步机(健身车)中的应用
  • 【python基础】python内置函数 zip用法介绍
  • Debian之Maven安装
  • Docker image
  • ️ 如何将 Julia 包切换为本地开发版本?以 Reactant 为例
  • OpenCV的对比度受限的自适应直方图均衡化算法
  • kafka原理解析
  • Python的pandas库基础知识(超详细教学)
  • 贪心算法详细讲解(沉淀中)
  • 论文阅读:Jailbreak Open-Sourced Large Language Models via Enforced Decoding