Tuesday 13 April 2021

opencv 30 text recognition


detect word

detect character
#download and install Tesseract 

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"

img = cv2.imread("assets/text.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

#print(pytesseract.image_to_string(img))
#print(pytesseract.image_to_boxes(img))

#detecting characters
"""
hImg, wImg, _ = img.shape
boxes = pytesseract.image_to_boxes(img)

for b in boxes.splitlines():
    b = b.split(' ')
    print(b)
    x, y, w, h = (int(b[1]), int(b[2]), int(b[3]), int(b[4]))
    cv2.rectangle(img, (x, hImg-y), (w, hImg-h), (0, 0, 255), 1)
    cv2.putText(img, b[0], (x, hImg-y+25), cv2.FONT_HERSHEY_COMPLEX, 1, (50, 50, 255), 2)
"""

#detecting words
hImg, wImg, _ = img.shape

#detect only number
#conf = r"--oem 3 --psm 6 outputbase digits"
#boxes = pytesseract.image_to_data(img, config=conf)

boxes = pytesseract.image_to_data(img)
print(boxes)

for i, b in enumerate( boxes.splitlines()):
    if i != 0:
        b = b.split()
        if len(b) == 12:
            x, y, w, h = (int(b[6]), int(b[7]), int(b[8]), int(b[9]))
            cv2.rectangle(img, (x, y), (w+x, h+y), (0, 0, 255), 1)
            cv2.putText(img, b[11], (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (50, 50, 255), 2)

cv2.imshow('img', img)
cv2.waitKey(0)

------------------------
#logs for word recognition
(venv) C:\Users\zchen\PycharmProjects\opencv>python text_detection.py
level   page_num        block_num       par_num line_num        word_num        left    top     width   height  conf    text
1       1       0       0       0       0       0       0       1327    412     -1
2       1       1       0       0       0       3       50      1304    282     -1
3       1       1       1       0       0       3       50      1304    282     -1
4       1       1       1       1       0       3       50      1304    74      -1
5       1       1       1       1       1       3       55      138     55      95      Text
5       1       1       1       1       2       171     53      325     57      96      Detection
5       1       1       1       1       3       525     50      146     60      93      with
5       1       1       1       1       4       701     54      258     70      90      Opencv
5       1       1       1       1       5       987     53      52      56      96      in
5       1       1       1       1       6       1072    50      235     74      96      Python
4       1       1       1       2       0       6       165     686     71      -1
5       1       1       1       2       1       6       165     142     56      96      OCR
5       1       1       1       2       2       177     165     175     71      96      using
5       1       1       1       2       3       374     166     318     55      96      Tesseract
4       1       1       1       3       0       10      277     1172    55      -1
5       1       1       1       3       1       10      277     1172    55      88      123456789101112131415

-------------------------------
#logs for character recognition
(venv) C:\Users\zchen\PycharmProjects\opencv>python text_detection.py
['T', '3', '303', '43', '357', '0']
['e', '41', '302', '75', '344', '0']
['x', '80', '303', '113', '343', '0']
['t', '117', '302', '141', '353', '0']
['D', '171', '303', '212', '357', '0']
['e', '220', '302', '254', '344', '0']
['t', '259', '302', '284', '353', '0']
['e', '289', '302', '324', '344', '0']
['c', '332', '302', '361', '344', '0']
...

['0', '615', '80', '652', '135', '0']
['1', '682', '80', '713', '135', '0']
['1', '725', '80', '757', '135', '0']
['1', '788', '80', '819', '135', '0']
['2', '829', '80', '863', '135', '0']
['1', '894', '80', '925', '135', '0']
['3', '935', '80', '968', '135', '0']
['1', '1001', '80', '1032', '135', '0']
['4', '1039', '80', '1078', '135', '0']
['1', '1107', '80', '1138', '135', '0']
['5', '1148', '80', '1182', '135', '0']

reference:

No comments:

Post a Comment