嘗試將以下PHP函數轉換爲Python,但出現以下錯誤。工作Python與下面的PHP函數等效的是什麼?將PHP函數轉換爲Python
線140,在doDetectBigToSmall 在x範圍規模(start_scale,比例> 1,標度=規模* scale_update): UnboundLocalError:分配之前引用的本地變量 '規模'
PHP CODE:
protected function doDetectBigToSmall($ii, $ii2, $width, $height)
{
$s_w = $width/20.0;
$s_h = $height/20.0;
$start_scale = $s_h < $s_w ? $s_h : $s_w;
$scale_update = 1/1.2;
for ($scale = $start_scale; $scale > 1; $scale *= $scale_update) {
$w = (20*$scale) >> 0;
$endx = $width - $w - 1;
$endy = $height - $w - 1;
$step = max($scale, 2) >> 0;
$inv_area = 1/($w*$w);
for ($y = 0; $y < $endy; $y += $step) {
for ($x = 0; $x < $endx; $x += $step) {
$passed = $this->detectOnSubImage($x, $y, $scale, $ii, $ii2, $w, $width+1, $inv_area);
if ($passed) {
return array('x'=>$x, 'y'=>$y, 'w'=>$w);
}
} // end x
} // end y
} // end scale
return null;
}
Python代碼:
def doDetectBigToSmall(self,ii, ii2, width, height):
s_w = width/20.0
s_h = height/20.0
start_scale = s_h if s_h < s_w else s_w
scale_update = 1/1.2
for scale in xrange(start_scale, scale > 1,scale = scale* scale_update):
w = (20*scale) >> 0
endx = width - w - 1
endy = height - w - 1
step = max(scale, 2) >> 0
inv_area = 1/(w*w)
for y in xrange(0,y < endy,y = y + step):
for x in xrange(0, x < endx, x= x + step):
passed = self.detectOnSubImage(x, y, scale, ii, ii2, w, width+1, inv_area)
if (passed):
return {'x': x, 'y': y, 'w': w}
在盲目編寫Python代碼之前:學習Python,特別是不要使用顯式從未檢查過其sementatics的方法。 –
我沒有檢查語義,但我承認我沒有完全理解For For循環與範圍和Xrange相比,For Next循環如何與我一起工作的其他語言。我正在嘗試學習Python,並沿着我將犯錯的方式授予,並且可能需要一些指針。 – bfalz