New procedure using contour detection
This commit is contained in:
parent
074678eaa0
commit
3160dcba40
49
ocr.py
49
ocr.py
|
@ -6,21 +6,42 @@ import cv2
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
def process(image):
|
def process(image):
|
||||||
|
# Read image
|
||||||
img = cv2.imread(image)
|
img = cv2.imread(image)
|
||||||
norm_img = np.zeros((img.shape[0], img.shape[1]))
|
# Convert to single-channel grayscale
|
||||||
# Normalize
|
img_gs = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
img = cv2.normalize(img, norm_img, 0, 255, cv2.NORM_MINMAX)
|
# Binary threshold to get rid of blemishes
|
||||||
# Crop
|
_, img_gs = cv2.threshold(img_gs, 50, 255, cv2.THRESH_BINARY)
|
||||||
img = img[165:-120, 100:-100]
|
# OTSU threshold for contour detection
|
||||||
# Threshold
|
_, thr = cv2.threshold(img_gs, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
|
||||||
img = cv2.threshold(img, 80, 255, cv2.THRESH_BINARY)[1]
|
# Rectangular kernel for contour detection
|
||||||
# Blur
|
rect_k = cv2.getStructuringElement(cv2.MORPH_RECT, (32, 32))
|
||||||
img = cv2.GaussianBlur(img,(9,9),cv2.BORDER_DEFAULT)
|
# Dilation
|
||||||
# Write modified image
|
dil = cv2.dilate(thr, rect_k, iterations = 1)
|
||||||
cv2.imwrite("output/{}_tmp.png".format(image.split("/")[1].split(".")[0]),img)
|
# Finding contours
|
||||||
# Actual ocr in one call
|
conts, _ = cv2.findContours(dil, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||||
return pytesseract.image_to_string(img)
|
txt=""
|
||||||
|
# Iterate over found contours and attemt to ocr text within
|
||||||
|
for cnt in conts:
|
||||||
|
x, y, w, h = cv2.boundingRect(cnt) # location and size of found feature
|
||||||
|
if x == 0 or y == 0:
|
||||||
|
# Do not do anything if the feature is on image edges, expected text is more or less centered
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
||||||
|
# Crop only the text feature with some padding in horizontal direction to help with ocr
|
||||||
|
txt_ft = img_gs[y:y+h, x:x+w]
|
||||||
|
try:
|
||||||
|
# Write image with the detected text bounding box
|
||||||
|
cv2.imwrite("output/{}_tmp.png".format(image.split("/")[1].split(".")[0]),img)
|
||||||
|
# Apply blur to remove sharp edges and help with ocr
|
||||||
|
txt_ft = cv2.GaussianBlur(txt_ft,(5,5),cv2.BORDER_DEFAULT)
|
||||||
|
# Actual ocr in one call
|
||||||
|
txt += pytesseract.image_to_string(txt_ft)
|
||||||
|
except:
|
||||||
|
# Fail silently, only the emtpy string will be returned
|
||||||
|
pass
|
||||||
|
return txt
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if not os.path.exists("input"):
|
if not os.path.exists("input"):
|
||||||
|
|
Loading…
Reference in New Issue