Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm trying to run the paddleOCR framework on the cropped ROI of a scanned image, but when I do, I always get the error message mentioned below. I have no idea what's gone wrong.

Python
UFuncTypeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_37324\165684197.py in <module>
      1 # Displaying output.
----> 2 save_ocr(img_path, out_path, result2, font)

~\AppData\Local\Temp\ipykernel_37324\1042196559.py in save_ocr(img_path, out_path, result, font)
      1 # Function to plot and save the results.
      2 def save_ocr(img_path, out_path, result, font):
----> 3   save_path = os.path.join(out_path, img_path + '-output.jpg')
      4 
      5   image = cv2.imread(img_path)

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U11'), dtype('<U11')) -> dtype('<U11')


What I have tried:

Python
import cv2
inp = '.../inputimg.jpg'
image2 = cv2.imread(inp)
x,y,w,h = x1,y1,w1,h1 
ROI3 = image2[y:y+h, x:x+w]
cv2.imshow('cropped for treatment', ROI3)
# imfinal = cv2.imwrite('..../Crop_image/result/trfinal.jpg',ROI3)     # To save the ROI
cv2.waitKey(0)
cv2.destroyAllWindows()

# Applying OCR
img_path = ROI3
out_path = '----/output_images'
font = '---/simfang.ttf'
result2 = ocr.ocr(img_path)

# Function to plot and save the results.
def save_ocr(img_path, out_path, result, font):
  save_path = os.path.join(out_path, img_path + '-output.jpg')

  image = cv2.imread(img_path)

  # Extracting boxes, texts and its score from the output list.
  boxes = [line[0] for line in result]
  txts = [line[1][0] for line in result]
  scores = [line[1][1] for line in result]

  # Plotting the outputs using PaddleOCR in-built function.
  im_show = draw_ocr(image, boxes, txts, scores, font_path=font)
  
  # Saving and displaying the output.
  cv2.imwrite(save_path, im_show)

  img = cv2.cvtColor(im_show, cv2.COLOR_BGR2RGB)
  plt.imshow(img)

# Displaying output.
save_ocr(img_path, out_path, result2, font)

What am I doing wrong as I cannot save and display the final result?
Posted
Updated 6-Oct-22 22:11pm
v3

1 solution

You have the following lines in your code:
Python
ROI3 = image2[y:y+h, x:x+w] # this looks like part of the image

# ...

img_path = ROI3

# ...
  save_path = os.path.join(out_path, img_path + '-output.jpg')

You need to look at the content of img_path, which I suspect is some numpy data, and not a path to a file.
 
Share this answer
 
v3
Comments
M@153 7-Oct-22 4:32am    
Yeah...But I want only the cropped part (ROI3) as an input path for my ocr and it comes in the form of NumPy data. How to update the code to take it as input path?
Richard MacCutchan 7-Oct-22 5:10am    
But you are trying to use image data as part of a (save) file name, and then read that file back; that makes no sense. You need to create a valid filename to save the partial image, and then write the image data to that file.
M@153 7-Oct-22 8:36am    
Hii...I just saw what mistake I was doing, and fixed it by saving that ROI image and then calling the filename as suggested by you. Thank you for your valuable suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900