(Python, OpenCV, pytesseract) ValueError: not enough values to unpack (expected 3, got 2) , same script and img, PC works and other PC (TX2) failed

I’m making python script that can do img to text
but main problem is same pic, same python script

can run successfully on PC python 3.9.12 , Ubuntu 22.04.1

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

and TX2 , I used “python3 my_script.py” to run

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

the error message

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)

this is my first time using nvidia TX2, but seems instead of normal PC runiing python code/script , that factory production lines more tend to use tx2 and raspberry pi

I thought I made script run on linux pc is fine, should no worry with TX2 cause they are both ubuntu, I’m too naive.
╮( ̄▽ ̄)╭

therefore, I’m not sure is the code issue or machine issue, or I should change the script/code on TX2

Your case seems complex and not easy to understand.
You may better narrow it down to a simpler case for better help.
As the error seems to be with findContours, get traces of the inputs you give to it in both PC and TX2 and compare, then follow the track to the culprit and provide a simple script and data for reproducing your case.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.