我正在编写以下函数:
def model(image):
texts,points =text_extractor(image)
pts=points[0]
pt1,pt2,pt3=pts[0],pts[1],pts[2]
return pt1,pt2,pt3
然而,上面的代码给出了错误:
18 texts,points =text_extractor(image)
19 pts=points[0]
---> 20 pt1,pt2,pt3=pts[0],pts[1],pts[2]
21 image=skew_correction(image,(pt1,pt2,pt3))
UnboundLocalError: local variable 'pts' referenced before assignment
我不明白为什么会出现这种情况,因为变量pts已经在第19行中定义了。
当我在一个单独的单元格中单独运行代码片段时,代码工作正常,并按预期进行。有人能说出这里出了什么问题吗。当我在函数中打印变量"pts"的值时,如下所示:
def model(image):
texts,points =text_extractor(image)
pts=points[0]
print(pts)
pt1,pt2,pt3=pts[0],pts[1],pts[2]
return pt1,pt2,pt3
相应输出:
[[211 482]
[242 479]
[260 668]
[230 671]]
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-28-ac65bafa631e> in <module>()
----> 1 model(im)
1 frames
<ipython-input-27-0a3790f540d2> in model(image)
17 texts,points =text_extractor(image)
18 pts=points[0]
---> 19 print(pts)
20 pt1,pt2,pt3=pts[0],pts[1],pts[2]
UnboundLocalError: local variable 'pts' referenced before assignment
这意味着"pts"的值被正确打印,但同时显示为未分配。为什么会发生这种情况,如何解决?
点[0]似乎没有分配给pts。
只需检查text_extractor((中的"points"是什么。