0

我正在尝试将以下 PHP 函数移植到 Python。但是我收到以下错误:第 189 行,在 detectOnSubImage rect = rects[i_rect] IndexError: list index out of range

Rects 接收位于 current_node[1] 的列表

rects = current_node[1]

并且 while 循环不会超出列表 rects 的长度范围

while i_rect < len(rects):    
       i_rect = i_rect+1    
       rect = rects[i_rect]   

将此 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

这是 PHP 代码中的树结构和其他 var_dump,它显示了一个多维数组

$tree = $trees[0]
array(2) { [0]=> array(2) { [0]=> array(5) { [0]=> float(0.00432723) [1]=> float(0.0383819 ) [2]=> float(-1) [3]=> int(-1) [4]=> int(1) } [1]=> array(2) { [0]=> array(5) { [0]=> int(2) [1]=> int(7) [2]=> int(16) [3]=> int(4) [4]=> int(-1) } [1 ]=> array(5) { [0]=> int(2) [1]=> int(9) [2]=> int(16) [3]=> int(2) [4]=> int (2) } } } [1]=> 数组(2) { [0]=> 数组(5) { [0]=> 浮点数(0.0130762) [1]=> 浮点数(0.896526) [2]=> 浮点数(0.262931) [3]=> int(-1) [4]=> int(-1) } [1]=> array(2) { [0]=> array(5) { [0]=> int (8) [1]=> int(4) [2]=> int(3) [3]=> int(14) [4]=> int(-1) } [1]=> array(5) { [0]=> int(8) [1]=> int(11) [2]=> int(3) [3]=> int(7) [4]=> int(2) } } } }

$current_node = $tree[0]
array(2) { [0]=> array(5) { [0]=> float(0.00432723) [1]=> float(0.0383819) [2]=> float(-1 ) [3]=> int(-1) [4]=> int(1) } [1]=> array(2) { [0]=> array(5) { [0]=> int(2) [1]=> int(7) [2]=> int(16) [3]=> int(4) [4]=> int(-1) } [1]=> array(5) { [0 ]=> int(2) [1]=> int(9) [2]=> int(16) [3]=> int(2) [4]=> int(2) } } }

$vals = $current_node[0]
array(5) { [0]=> float(0.00432723) [1]=> float(0.0383819) [2]=> float(-1) [3]=> int(-1 ) [4]=> int(1) }

$rects = $current_node[1]
array(2) { [0]=> array(5) { [0]=> int(2) [1]=> int(7) [2]=> int(16) [3]=> int(4) [4]=> int(-1) } [1]=> array(5) { [0]=> int(2) [1]=> int(9) [2 ]=> int(16) [3]=> int(2) [4]=> int(2) } }

$rect = $rects[0]
array(5) { [0]=> int(2) [1]=> int(7) [2]=> int(16) [3]=> int(4) [ 4]=> int(-1) }

4

1 回答 1

0

为了调试它,我建议在 Python 和 PHP 代码中打印出tree和变量(和其他)的整个结构。trees这将让您比较需要执行哪些分配和索引以使代码兼容。过去我遇到过问题,只需在[0]数组赋值的末尾再添加一个,因为它在 PHP 或 JSON 或其他任何东西中的嵌套比我预期的要深一层。

祝你好运!

于 2013-10-15T22:00:55.963 回答