Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP函数转换为Python_Php_Python - Fatal编程技术网

将PHP函数转换为Python

将PHP函数转换为Python,php,python,Php,Python,我正在尝试将下面的PHP函数移植到Python。但是,我收到了以下错误:detectOnSubImage中的第189行 rect=rects[i_rect] 索引器:列表索引超出范围 Rects接收位于当前_节点[1]的列表 rects=当前_节点[1] while循环不会超出列表矩形的长度范围 while i_rect < len(rects): i_rect = i_rect+1 rect = rects[i_rect] 而i_rec

我正在尝试将下面的PHP函数移植到Python。但是,我收到了以下错误:detectOnSubImage中的第189行 rect=rects[i_rect] 索引器:列表索引超出范围

Rects接收位于当前_节点[1]的列表

rects=当前_节点[1]

while循环不会超出列表矩形的长度范围

while i_rect < len(rects):    
       i_rect = i_rect+1    
       rect = rects[i_rect]   
而i_rect
在将这个PHP函数移植到Python时,我遗漏了什么?正确的Python代码等价物是什么

PHP代码:(如下)

受保护功能检测子映像($x、$y、$scale、$ii、$ii2、$w、$iiw、$inv\U区域)
{
$inv_面积”;
$mean=($ii[($y+$w)*$iiw+$x+$w]+$ii[$y*$iiw+$x]-$ii[($y+$w)*$iiw+$x]-$ii[$y*$iiw+$x+$w])*$inv\U区域;
$vnorm=($ii2[($y+$w)*$iiw+$x+$w]
+$ii2[$y*$iiw+$x]
-$ii2[($y+$w)*$iiw+$x]
-$ii2[$y*$iiw+$x+$w])*$inv_面积-($MEANT*$MEANT);
$vnorm=$vnorm>1?sqrt($vnorm):1;
$passed=true;
对于($i_stage=0;$i_stagedetection_data);$i_stage++){
$stage=$this->detection_data[$i_stage];
$trees=$stage[0];
$stage_thresh=$stage[1];
$stage_sum=0;
对于($i_tree=0;$i_tree>0;
$ry=($rect[1]*$s+$y)>>0;
$rw=($rect[2]*$s)>>0;
$rh=($rect[3]*$s)>>0;
$wt=$rect[4];
$r_总和=($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_面积;
$current_node=null;
如果($rect\u sum>=$node\u thresh*$vnorm){
如果($rightidx==-1){
$tree_sum=$rightval;
}否则{
$current_node=$tree[$rightidx];
}
}否则{
如果($leftidx==-1){
$tree_sum=$leftval;
}否则{
$current_node=$tree[$leftidx];
}
}
}
$stage\u sum+=$tree\u sum;
}
如果($stage\u sum<$stage\u thresh){
返回false;
}
}
返回true;
}
}
Python代码:(如下)

def检测子图像(自身、x、y、比例、ii、ii2、w、iiw、inv_区域):
平均值=(ii[(y+w)*iiw+x+w]+ii[y*iiw+x]-ii[(y+w)*iiw+x]-ii[y*iiw+x+w])*inv\u面积
vnorm=(ii2[(y+w)*iiw+x+w]+ii2[y*iiw+x]-ii2[(y+w)*iiw+x]-ii2[y*iiw+x+w])*投资面积-(平均值*平均值)
如果vnorm>1,则vnorm=sqrt(vnorm),否则为1
#var foo=(测试)?“真”:“假”;
#foo=“True”如果测试结果为“False”
通过=正确
#对于X范围内的i_级(0,i_级<(len(自检测数据)),i_级=i_级+1):
i_阶段=0
当i_阶段>0
ry=(rect[1]*s+y)>>0
rw=(rect[2]*s)>>0
右=(矩形[3]*s)>>0
wt=rect[4]
r_和=(ii[(ry+rh)*iiw+rx+rw]+ii[ry*iiw+rx]-ii[(ry+rh)*iiw+rx]-ii[ry*iiw+rx+rw])*wt
rect\u sum=rect\u sum+r\u sum
rect\u sum=rect\u sum*inv\u区域
当前节点=无
如果(rect\u sum>=节点阈值*vnorm):
如果(rightidx==-1):
tree_sum=rightval
其他:
当前_节点=树[rightidx]
其他:
如果(leftidx==-1):
tree_sum=leftval
其他:
当前_节点=树[leftidx]
阶段总和=阶段总和+树总和
如果(阶段总和<阶段阈值):
返回错误
返回真值
下面是PHP代码中的树结构和其他var_转储,它显示了一个多维数组

$tree=$trees[0]
排列
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;
 }
}
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