0

I am working on Easy OCR and to some extent, it detects bounding box and recognizes the Korean words but it didn't show the recognized text on the top of bounding box. For KR, it shows ???????

Code

# OCR the input image using EasyOCR
print("[INFO] OCR'ing input image...")
reader = Reader(langs, gpu=args["gpu"] > 0)
results = reader.readtext(image)

# loop over the results
for (bbox, text, prob) in results:
    # display the OCR'd text and associated probability
    print("[INFO] {:.4f}: {}".format(prob, text))

    # unpack the bounding box
    (tl, tr, br, bl) = bbox
    tl = (int(tl[0]), int(tl[1]))
    tr = (int(tr[0]), int(tr[1]))
    br = (int(br[0]), int(br[1]))
    bl = (int(bl[0]), int(bl[1]))

    # cleanup the text and draw the box surrounding the text along
    # with the OCR'd text itself
    text = cleanup_text(text)
    cv2.rectangle(image, tl, br, (0, 255, 0), 2)
    cv2.putText(image, text, (tl[0], tl[1] - 10),
        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)

# show the output image
plt_imshow("Image", image)

Result

    [INFO] 0.0630: J2
[INFO] 0.9515: 인류학과의 조우
[INFO] 0.9760: 성사정
[INFO] 0.5252: 9 y
[INFO] 0.5667: Anthrop 0| 0 g y

Output

enter image description here

Khawar Islam
  • 2,556
  • 2
  • 34
  • 56
  • OpenCV's font, the Hershey font, does not support non-ascii characters. See this: https://stackoverflow.com/questions/71962098/python-opencv-puttext-show-non-ascii-unicode-utf-character-symbols – stateMachine Mar 22 '23 at 07:21
  • @stateMachine I am using cv2.putText method because after recognition i want to add add on the top of the rectangle. Do you have any idea how to add it? – Khawar Islam Mar 22 '23 at 07:26
  • Just follow the linked post's answer: Use PIL to load a font and its draw method to render text on the image. – stateMachine Mar 22 '23 at 07:35
  • I also want to use cv2.rectangle to draw rectangle box thats the problem – Khawar Islam Mar 22 '23 at 07:38
  • The two aren't mutually exclusive. You can draw the rectangle with OpenCV and the text with PIL, as shown in the linked post. You need to convert the numpy array to a PIL object and vice versa. – stateMachine Mar 22 '23 at 08:18
  • @stateMachine Still, I am unable to do please guide me how i can do it? – Khawar Islam Mar 23 '23 at 01:08

1 Answers1

0

March 2023: cv2.putText() only supports a small ascii subset of characters and not unicode, utf, or non-ascii characters.

# load the input image from disk
font = ImageFont.truetype("gulim.ttc", 20)
image = cv2.imread(args["image"])
cv2_im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im_rgb)
draw = ImageDraw.Draw(pil_im)

b,g,r,a = 139,0,0,0
# loop over the results
for (bbox, text, prob) in results:
    # display the OCR'd text and associated probability
    print("[INFO] {:.4f}: {}".format(prob, text))

    # unpack the bounding box
    (tl, tr, br, bl) = bbox
    tl = (int(tl[0]), int(tl[1]))
    tr = (int(tr[0]), int(tr[1]))
    br = (int(br[0]), int(br[1]))
    bl = (int(bl[0]), int(bl[1]))
    #img = cv2.rectangle(np.array(pil_im), tl, br, (0, 255, 0), 2)

    draw.text((tl[0], tl[1] - 20), text, font=font, fill=(b,g,r,a))
    cv2_im_processed = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)
    cv2.imwrite("resultb.png", cv2_im_processed)

# show the output image
cv2.imshow("Image", cv2_im_processed)
cv2.waitKey(0)
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56