(Python、OpenCV、pytesseract)ValueError:解包值不足(预期 3 个,实际为 2 个),相同的脚本和图像,PC 可运行,其他 PC(TX2)失败

(Python、OpenCV、pytesseract)ValueError:解包值不足(预期 3 个,实际为 2 个),相同的脚本和图像,PC 可运行,其他 PC(TX2)失败

我正在编写可以将 img 转换为文本的 python 脚本
,但主要问题是相同的图片,相同的 Python 脚本

可以在PC上成功运行python 3.9.12Ubuntu 22.04.1

ValueError: not enough values to unpack (expected 3, got 2)
在 nvidia TX2上python3 3.6.9python 2.7.17Ubuntu 18.04.1

和 TX2 ,我使用“python3 my_script.py”来运行

import os
import cv2
import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import pytesseract



image_2x2 = cv2.imread("/home/jetson/Desktop/test_12.26/original_duplicate.png")
x = 0
y = 0
small_w = 250
small_h = 334
new_crop_for_2x2_xy_distance = image_2x2[y:y+small_h, x:x+small_w]
cv2.imwrite(f"/home/jetson/Desktop/test_12.26/2x2_xy_distance.png",new_crop_for_2x2_xy_distance)

gray = cv2.cvtColor(new_crop_for_2x2_xy_distance, cv2.COLOR_BGR2GRAY)


ret, thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY_INV)
blur = cv2.medianBlur(thresh, 1)
kernel = np.ones((10, 20), np.uint8)
img_dilation = cv2.dilate(blur, kernel, iterations=1)
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

xy_list = []
listOfElems = []
listOfDuplicate = []
list_for_duplicate_x_and_y = [ ]

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)
    roi = new_crop_for_2x2_xy_distance[y:y + h, x:x + w]
    if (h > 50 and w > 50) and h < 200:

        cv2.rectangle(new_crop_for_2x2_xy_distance, (x, y), (x + w, y + h), (255, 255, 255), 1)        
        
        print("= = = = = = = =   ")
        print("左上 右下" )        
        print(x , y )            # 左上 (x , y ) 
        print(x + w , y + h )    # 右下 (x , y ) = (x + w , y + h )


# (x,y)標準化
        for xc in (45,150,255,360,465,570):
            if xc-20 < x < xc+20:
                x = xc + 26
                break
        else:
            x = 0
                    
        for yc in (132, 243,586,357,470):
            if yc-20 < y < yc+20:
                y = yc + 48
                break
             
        else:
            y = 0           
        
        print("new number" , x , y )
        
        tem_list_x_and_y = [ ] 
        tem_list_for_duplicate_x_and_y = [ ] 

        if (x != 0) and (y != 0):
            # cv2.imwrite(f"/home/joy/桌面/optimization_11_10/output_11_10__001/output_y:{y}_x:{x}.png", roi)
            tem_list_x_and_y.append(x)
            tem_list_x_and_y.append(y)
            
            xy_list.append(tem_list_x_and_y)

print(xy_list)    
sorted_xy_list = (sorted(xy_list , key=lambda k: [k[1], k[0]]))   
print(sorted_xy_list)   

first_xy= sorted_xy_list[0]
second_xy= sorted_xy_list[1]
third_xy= sorted_xy_list[2]
fourth_xy= sorted_xy_list[3]

# each x
first_xy_x = first_xy[0]
second_xy_x = second_xy[0]
third_xy_x = third_xy[0]
fourth_xy_x = fourth_xy[0]

# each y
first_xy_y = first_xy[1] 
second_xy_y = second_xy[1]
third_xy_y = third_xy[1]
fourth_xy_y = fourth_xy[1]

x_distance = ((second_xy_x - first_xy_x) + (fourth_xy_x - third_xy_x) )/2
y_distance = ((third_xy_y - first_xy_y) + (fourth_xy_y - second_xy_y) )/2

print(x_distance)
print(y_distance)


predict_xy_list = []
tem_predict_xy_list = [ ] 

tem_predict_xy_list.append(first_xy_x)
tem_predict_xy_list.append(first_xy_y)
predict_xy_list.append(tem_predict_xy_list)

last_x = first_xy_x
last_y = first_xy_y

tem_predict_xy_list = [ ] 
for y in range(4):
    tem_predict_xy_list = [ ] 
    last_y = int(last_y) + int(y_distance)
    tem_predict_xy_list.append(first_xy_x)
    tem_predict_xy_list.append(last_y)
    predict_xy_list.append(tem_predict_xy_list)

for x in range(5):
    tem_predict_xy_list = [ ] 
    last_x = int(last_x) + int(x_distance)
    tem_predict_xy_list.append(last_x)
    tem_predict_xy_list.append(first_xy_y)
    predict_xy_list.append(tem_predict_xy_list)

print(predict_xy_list)
print(len(predict_xy_list))
print("= = = = = ")

for x in range(5):
    tem_predict_xy_list = [ ] 
    last_x = int(last_x) + int(x_distance)
    last_y = first_xy_y
    for y in range(4):
        tem_predict_xy_list = [ ] 
        last_y = int(last_y) + int(y_distance)
        tem_predict_xy_list.append(last_x)
        tem_predict_xy_list.append(last_y)
        predict_xy_list.append(tem_predict_xy_list)

print(predict_xy_list)
print(len(predict_xy_list))

sorted_predict_xy_list = (sorted(predict_xy_list , key=lambda k: [k[1], k[0]]))
print(sorted_predict_xy_list)   

sorted_predict_xy_list = [ [71,180], [176,180], [281,180], [386,180], [491,180], [596,180],
[71,291], [176,291], [281,291], [386,291], [491,291], [596,291],
[71,406], [176,406], [281,406], [386,406], [491,406], [596,406],
[71,522], [176,522], [281,522], [386,522], [491,522], [596,522],
[71,637], [176,637], [281,637], [386,637], [491,637], [596,637] ]

image = cv2.imread("/home/jetson/Desktop/test_12.26/original_duplicate.png")
j = 0
while j < len(sorted_predict_xy_list) :
    temp_xy = sorted_predict_xy_list[j]
    x = temp_xy[0]
    y = temp_xy[1]
    small_txt_size_w = 65
    small_txt_size_h = 16
    x2 = int(x) + int(small_txt_size_w)
    y2 = int(y) + int(small_txt_size_h)
    new_crop = image[y:y2, x:x2]



    text = pytesseract.image_to_string(new_crop, lang='eng').strip()
    print("x :" , x  , ",y :",y , ", x+small_txt_size_w", x2 , ", y+small_txt_size_h :" , y2)
    j+=1
 
 
    
    if text not in listOfElems:
        
        listOfElems.append(text)
        print(text)
        print("= = = = = = = =   ")
        print("  ")
    else:

        print("Duplicate text is here:")
        print(text)
        print("x :" , x , "y :",y)
        tem_list_for_duplicate_x_and_y.append(x)
        tem_list_for_duplicate_x_and_y.append(y)
        list_for_duplicate_x_and_y.append(tem_list_x_and_y)

        print("= = = = = = = =   ")
        print("  ")                

                            

                    # print("len for xy_list is : " ,len(xy_list))

                    # aaa_list = (sorted(xy_list , key=lambda k: [k[1], k[0]]))
                    # print(aaa_list)        

                    # print("list_for_duplicate_x_and_y is :")      

                    # print(list_for_duplicate_x_and_y)      

                    # print("listOfElems is :")
                    # print(listOfElems)    
    



    # cd /home/jetson/Desktop/test_12.26

# /home/jetson/Desktop/test_12.26/original_duplicate.png
# python 12_26_img_to_text.py

# python3 12_26_img_to_text.py

错误信息

Traceback (most recent call last):
  File "12_26_img_to_text.py", line 26, in <module>
    im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

这是我第一次使用 nvidia TX2,但似乎工厂生产线更倾向于使用 tx2 和 raspberry pi,而不是在普通 PC 上运行 python 代码/脚本

我以为我编写的脚本在 Linux PC 上运行没问题,不用担心 TX2,因为它们都是 ubuntu,我太天真了。╮
( ̄▽ ̄)╭

因此,我不确定是代码问题还是机器问题,或者我应该更改 TX2 上的脚本/代码


现在我知道 cv2 版本不同

jetson TX2 python3 3.6.9,Ubuntu 18.04.1 --- cv2 版本:4.1.1

装有 python 3.9.12、Ubuntu 22.04.1 的 PC --- cv2 版本:3.4.15

答案1

发生这种情况是因为您使用了两个不同版本的 OpenCV。

在你的 Ubuntu 18.04.1 上,尝试替换:

im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

和:

ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

也就是说,删除第一个“im2”返回变量,顺便说一下,该变量未在您的代码中使用。然后就没问题了。

注意:在每台机器上,你可以使用以下命令检查 OpenCV 版本:

pythonX.Y -c "import cv2; print(cv2.__version__)"

其中 XY 对应您正在使用的 Python 版本,因为您安装了多个 Python 版本(例如 Python3.9、Python3.6、Python2.7)

相关内容