forked from theWellHopeErr/ocr-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.py
60 lines (43 loc) · 1.48 KB
/
ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
##############################
# Usage:
# python ocr.py img.ext
##############################
import sys
import os
import json
import cv2
import pytesseract
from pytesseract import Output
from dotenv import load_dotenv
load_dotenv(verbose=True)
pytesseract.pytesseract.tesseract_cmd = os.getenv("TESSARACT_PATH")
print("[INFO] loading image...")
img = cv2.imread(sys.argv[1])
print("[INFO] processing image...")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(
gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
dilation = cv2.dilate(thresh1, rect_kernel, iterations=1)
contours, hierarchy = cv2.findContours(
dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
im2 = img.copy()
file = open("recognized.json", "w+")
file.write("[")
file.close()
for idx, cnt in enumerate(contours):
x, y, w, h = cv2.boundingRect(cnt)
rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
cropped = im2[y:y + h, x:x + w]
file = open("recognized.json", "a")
print("[INFO] reading text...")
text = pytesseract.image_to_data(cropped, output_type=Output.DICT)
print("[INFO] writing text...")
file.write(json.dumps(text))
if idx != len(contours) - 1:
file.write(",")
else:
file.write("]")
# print(text)
file.close()
print("[Finished] Data written to the file .\\recognised.json")