forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_detection.py
101 lines (76 loc) · 2.91 KB
/
Word_detection.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'''
output : word detection on document (Simple OCR type of application)
'''
import cv2
import numpy as np
import imutils
# frame read
frame = cv2.imread('test.jpeg')
# resize
frame = cv2.resize(frame, (600, 600))
# grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# remove noise
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# otsu thresh (bimodel thresold)
thresh = cv2.threshold(blur, 0, 255,
cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# get structuring element
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 1))
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 25))
print('horizontal kernel : {}'.format(horizontal_kernel))
print('vertical kernel : {}'.format(vertical_kernel))
# opening (erosion followed by dilation)
horizontal_lines = cv2.morphologyEx(thresh,
cv2.MORPH_OPEN,
horizontal_kernel,
iterations=2)
vertical_lines = cv2.morphologyEx(thresh,
cv2.MORPH_OPEN,
vertical_kernel,
iterations=2)
# contours apply on detected lines
# First one is source image, second is contour retrieval mode, third is contour approximation method
cnts = cv2.findContours(horizontal_lines, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cntsv = cv2.findContours(vertical_lines, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# find contours
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cntsv = cntsv[0] if len(cntsv) == 2 else cntsv[1]
for c in cnts:
cv2.drawContours(frame, [c], -1, (255, 255, 255), 2)
for c in cntsv:
cv2.drawContours(frame, [c], -1, (255, 255, 255), 2)
# imshow
cv2.imshow('thresh', thresh)
cv2.imshow('horizontal_lines', horizontal_lines)
cv2.imshow('vertical_lines', vertical_lines)
cv2.imshow('frame', frame)
# grayscale
gray1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
thresh1 = cv2.adaptiveThreshold(gray1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 23, 30)
canny = imutils.auto_canny(thresh1)
output = cv2.bitwise_not(canny)
kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
dilation = cv2.dilate(canny, kernel, iterations=1)
contour, hierachy = cv2.findContours(dilation, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
for i in contour:
area = cv2.contourArea(i)
if area > 20:
x, y, w, h = cv2.boundingRect(i)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 120, 255), 2)
cv2.imshow('output', output)
cv2.imshow('dilate', dilation)
cv2.imshow('opening', opening)
cv2.imshow('original_frame', frame)
cv2.imshow('canny', canny)
cv2.imshow('thresh1', thresh1)
# Saving output image
cv2.imwrite('output.jpg', frame)
# destroy all window
cv2.waitKey(0)
cv2.destroyAllWindows()