2013-10-15 53 views
0

我試圖將以下PHP函數移植到Python。但是我收到以下錯誤:線189,在detectOnSubImage RECT = rects [i_rect] IndexError:列表索引超出範圍將PHP函數轉換爲Python

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_dumps (2){[0] =>數組(2){[0] =>數組(5){[0] =>浮點數(0.00432723)[1]數組(2){0} => array [0]
] => float(0.0383819)[2] => f loat(-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 ){[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) =>數組(5){[0] => int(8)[1] => int(-1)[4] => => int(4)[2] => int(3)[3] => int(14)[4] => int(-1)} [1] => array(5){[0] => int(8)[1] => int(3)=> int(7)[4] => int(2)}}}}

$ current_node (0)= $ tree [0]
array(2){[0] => array(5){[0] => float(0.00432723)[1] => float(0.0383819)[2] => )[1] =>數組(2){[0] =>數組(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 ){[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)}

+0

您將'current_node'設置爲包含'tree [0]'的值,因此其長度爲1(僅存在元素0)。在php中'tree'的結構是什麼,它在python中是什麼? ('current_node'賦值之前的'print tree')。如果這是一個特殊的對象,它可能被「打包」不同。 (例如,'[[1,2,3]]'不會有元素1.) – beroe

+0

在PHP $ tree中,顯示爲長度爲2的數組$ tree = $ trees [$ i_tree]; \t \t \t \t print「Tree:$ tree」; \t \t \t \t $ length = count($ tree); \t \t \t \t print「Tree Array Length:$ length」; – bfalz

+0

這個結構是否能在python中生存?列表作爲多維數組的替代品,Python並不是很好。我認爲我們需要看到在Python中定義或導入'trees'的部分。 – beroe

回答

0

爲了調試這個,建議在Python和PHP代碼中打印出treetrees變量(及其他)的整個結構。這將讓您比較您需要做什麼來使代碼兼容的分配和索引。過去我遇到過一些問題,只是在數組賦值的末尾添加了一個[0],因爲它在PHP或JSON中嵌套了更深層次,或者超出了我的預期。

祝你好運!