-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounding_boxes.py
39 lines (39 loc) · 1.47 KB
/
bounding_boxes.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
import pytesseract
import cv2
image = cv2.imread("data/index_02.JPG")
base_image = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite("temp/index_gray.png", gray)
blur = cv2.GaussianBlur(gray, (7, 7), 0)
cv2.imwrite("temp/index_blur.png", blur)
thresh = cv2.threshold(
blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cv2.imwrite("temp/index_thresh.png", thresh)
kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 13))
cv2.imwrite("temp/index_kernal.png", kernal)
dilate = cv2.dilate(thresh, kernal, iterations=1)
cv2.imwrite("temp/index_dilate.png", dilate)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cents[1]
cnts = sorted(cnts, key=lambda x: cv2.boundingRect(x)[0])
results = []
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
if h > 200 and w > 20:
roi = image[y:y+h, x:x+h]
cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 2)
ocr_result = pytesseract.image_to_string(roi)
ocr_result = ocr_result.split("\n")
for item in ocr_result:
results.append(item)
cv2.imwrite("temp/index_bbox_new.png", image)
entities = []
for item in results:
item = item.strip().replace("\n", "")
item = item.split(" ")[0]
if len(item) > 2:
if item[0] == "A" and "-" not in item:
item = item.split(".")[0].replace(",", "").replace(";", "")
entities.append(item)
entities = list(set(entities))
entities.sort()