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:

Monday, 12 April 2021

China's Mini EV

Daily Dose of Satisfaction

opencv 29 super resolution

original image, butterfly is region of interest

zoomed in 4 times, butterfly is pixelated

render with super resolution, image is still sharp



#super_resolution.py
import cv2
import argparse
import os
import time
from cv2 import dnn_superres

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image we want to increase resolution of")
args = vars(ap.parse_args())

orignal_iamge = cv2.imread(args["image"])


def super_resolution(model_path, image):
# extract the model name and model scale from the file path
modelName = model_path.split(os.path.sep)[-1].split("_")[0].lower()
modelScale = model_path.split("_x")[-1]
modelScale = int(modelScale[:modelScale.find(".")])

# initialize OpenCV's super resolution DNN object, load the super
# resolution model from disk, and set the model name and scale
print("[INFO] loading super resolution model: {}".format(
model_path))
print("[INFO] model name: {}".format(modelName))
print("[INFO] model scale: {}".format(modelScale))
sr = dnn_superres.DnnSuperResImpl_create()
sr.readModel(model_path)
sr.setModel(modelName, modelScale)

# load the input image from disk and display its spatial dimensions
print("[INFO] w: {}, h: {}".format(image.shape[1], image.shape[0]))

# use the super resolution model to upscale the image, timing how
# long it takes
start = time.time()
upscaled = sr.upsample(image)
end = time.time()
print("[INFO] super resolution took {:.6f} seconds".format(
end - start))

# show the spatial dimensions of the super resolution image
print("[INFO] w: {}, h: {}".format(upscaled.shape[1],
   upscaled.shape[0]))

cv2.imshow(modelName, upscaled)


xl, yl, xr, yr = (0, 0, 0, 0)
btn_down = False
img = orignal_iamge.copy()

def drag_event(event, x, y, flags, param):
global btn_down, xl, yl, xr, yr, img

if event == cv2.EVENT_LBUTTONUP and btn_down:
btn_down = False

cv2.rectangle(img, (xl - 2, yl - 2), (xr + 2, yr + 2), (0, 0, 255), 2)
cv2.imshow('original', img)

roi = orignal_iamge[yl:yr, xl:xr]

zoom_img = cv2.resize(roi, (0, 0), fx=4, fy=4)
cv2.imshow('zoom in', zoom_img)

super_resolution("models\ESPCN_x4.pb", roi)
super_resolution("models\FSRCNN_x4.pb", roi)
super_resolution("models\LapSRN_x4.pb", roi)


elif event == cv2.EVENT_MOUSEMOVE and btn_down:
xr, yr = (x, y)
cv2.rectangle(img, (xl - 2, yl - 2), (xr + 2, yr + 2), (0, 0, 255), 2)
cv2.imshow('original', img)
img = orignal_iamge.copy()

elif event == cv2.EVENT_LBUTTONDOWN:
btn_down = True
xl, yl = (x, y)

cv2.imshow("Image", orignal_iamge)
cv2.setMouseCallback('Image', drag_event)

cv2.waitKey(0)

--------------------
#logs
(venv) C:\Users\zchen\PycharmProjects\opencv>python super_resolution.py --image assets/butterfly.jpeg
[INFO] loading super resolution model: models\ESPCN_x4.pb
[INFO] model name: espcn
[INFO] model scale: 4
[INFO] w: 94, h: 73
[INFO] super resolution took 0.004987 seconds
[INFO] w: 376, h: 292
[INFO] loading super resolution model: models\FSRCNN_x4.pb
[INFO] model name: fsrcnn
[INFO] model scale: 4
[INFO] w: 94, h: 73
[INFO] super resolution took 0.006982 seconds
[INFO] w: 376, h: 292
[INFO] loading super resolution model: models\LapSRN_x4.pb
[INFO] model name: lapsrn
[INFO] model scale: 4
[INFO] w: 94, h: 73
[INFO] super resolution took 0.077327 seconds
[INFO] w: 376, h: 292

reference:

mouse drag event

super resolution model download

Sunday, 11 April 2021

test flight April

expense

Apirl 7 - lunch, lowes, driving 70km
April 11 - lunch, driving 70km

timesheet
April 7 - install tabi 1 - 8h
April 11 - bundle, uninstall - 8h