duangua5742 2013-10-15 00:50
浏览 39
已采纳

将PHP函数转换为Python

I am trying to port over the below PHP function to Python. However I am receiving the following error: line 189, in detectOnSubImage rect = rects[i_rect] IndexError: list index out of range

Rects receives the list located at current_node[1]

rects = current_node[1]

And the while loop would not go out of range past the length of the list rects

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

What did I miss when porting over this PHP function to Python and what would be the correct Python code equivalent?

PHP code: (below)

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 code: (below)

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

Here is the tree structure and other var_dumps from within the PHP code which shows a multi-dimensional array

$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]=> array(2) { [0]=> array(5) { [0]=> float(0.0130762) [1]=> float(0.896526) [2]=> float(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) }

  • 写回答

1条回答 默认 最新

  • duannei0044 2013-10-15 22:00
    关注

    To debug this, I recommend printing out the entire structure of the tree and trees variables (and others) within Python and in the PHP code. This will let you compare what assignments and indexing you need to do to make the code compatible. I have had issues in the past where it was just a matter of adding one more [0] to the end of an array assignment, because it was nested one level deeper in the PHP or JSON or whatever than I anticipated.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!