Search code examples
opencvocrtesseractpython-tesseracteasyocr

How to write recognized hangul text on a image in Ubuntu using python 3 opencv2


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


Solution

  • 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)