Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Aniket025
GitHub Repository: Aniket025/Medical-Prescription-OCR
Path: blob/master/Model-3/ocr/words.py
426 views
1
# -*- coding: utf-8 -*-
2
"""
3
Detect words on the page
4
return array of words' bounding boxes
5
"""
6
import numpy as np
7
import matplotlib.pyplot as plt
8
import cv2
9
from .helpers import *
10
11
def detection(image):
12
""" Detecting the words bounding boxes """
13
# Preprocess image for word detection
14
blurred = cv2.GaussianBlur(image, (5, 5), 10)
15
edgeImg = edgeDetect(blurred)
16
ret, edgeImg = cv2.threshold(edgeImg, 50, 255, cv2.THRESH_BINARY)
17
bwImage = cv2.morphologyEx(edgeImg, cv2.MORPH_CLOSE,
18
np.ones((15,15), np.uint8))
19
# Return detected bounding boxes
20
return textDetect(bwImage, image)
21
22
23
def edgeDetect(im):
24
"""
25
Edge detection
26
Sobel operator is applied for each image layer (RGB)
27
"""
28
return np.max(np.array([sobelDetect(im[:,:, 0]),
29
sobelDetect(im[:,:, 1]),
30
sobelDetect(im[:,:, 2])]), axis=0)
31
32
33
def sobelDetect(channel):
34
""" Sobel operator """
35
sobelX = cv2.Sobel(channel, cv2.CV_16S, 1, 0)
36
sobelY = cv2.Sobel(channel, cv2.CV_16S, 0, 1)
37
sobel = np.hypot(sobelX, sobelY)
38
sobel[sobel > 255] = 255
39
return np.uint8(sobel)
40
41
42
def textDetect(img, image):
43
""" Text detection using contours """
44
small_2 = resize(image,2000)
45
small = resize(img, 2000)
46
47
# Finding contours
48
mask = np.zeros(small.shape, np.uint8)
49
im2, cnt, hierarchy = cv2.findContours(np.copy(small), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
50
index = 0
51
boundingBoxes = np.array([0,0,0,0])
52
53
# image for drawing bounding boxes
54
small = cv2.cvtColor(small, cv2.COLOR_GRAY2RGB)
55
small_copy = small.copy()
56
implt(small_copy , t='All contours')
57
cv2.drawContours(small_copy, cnt, -1, (0,255,0), 3)
58
implt(small_copy, t='check')
59
cv2.imwrite('check.jpg',small_copy)
60
# Go through all contours in top level
61
while (index >= 0 and index<40):
62
x,y,w,h = cv2.boundingRect(cnt[index])
63
cv2.drawContours(mask, cnt, index, (255, 255, 255), cv2.FILLED)
64
maskROI = mask[y:y+h, x:x+w]
65
# Ratio of white pixels to area of bounding rectangle
66
r = float(cv2.countNonZero(maskROI)) / (w * h)
67
68
# Limits for text
69
#if r > 0.1 and 1600 > w > 10 and 1600 > h > 10 and (60 // h) * w < 1000:
70
if r > 0.1 and 2000 > w > 15 and 1500 > h > 15:
71
cv2.rectangle(small, (x, y),(x+w,y+h), (0, 255, 0), 2)
72
crop_img = small_2[y:y+h,x:x+w]
73
implt(crop_img, t='contours %s' % (index))
74
cv2.imwrite('check%s.jpg' % (index),crop_img)
75
boundingBoxes = np.vstack((boundingBoxes,np.array([x, y, x+w, y+h])))
76
77
index+=1
78
79
implt(small, t='Bounding rectangles')
80
cv2.imwrite('check2.jpg',small)
81
82
bBoxes = boundingBoxes.dot(ratio(image, 2000)).astype(np.int64)
83
return bBoxes[1:]
84
85
86
def textDetectWatershed(thresh):
87
""" Text detection using watershed algorithm - NOT IN USE """
88
# According to: http://docs.opencv.org/trunk/d3/db4/tutorial_py_watershed.html
89
img = cv2.cvtColor(cv2.imread("data/textdet/%s.jpg" % IMG),
90
cv2.COLOR_BGR2RGB)
91
img = resize(img, 3000)
92
thresh = resize(thresh, 3000)
93
# noise removal
94
kernel = np.ones((3,3),np.uint8)
95
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 3)
96
97
# sure background area
98
sure_bg = cv2.dilate(opening,kernel,iterations=3)
99
100
# Finding sure foreground area
101
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
102
ret, sure_fg = cv2.threshold(dist_transform,
103
0.01*dist_transform.max(), 255, 0)
104
105
# Finding unknown region
106
sure_fg = np.uint8(sure_fg)
107
unknown = cv2.subtract(sure_bg,sure_fg)
108
109
# Marker labelling
110
ret, markers = cv2.connectedComponents(sure_fg)
111
112
# Add one to all labels so that sure background is not 0, but 1
113
markers += 1
114
115
# Now, mark the region of unknown with zero
116
markers[unknown == 255] = 0
117
118
markers = cv2.watershed(img, markers)
119
implt(markers, t='Markers')
120
image = img.copy()
121
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
122
123
for mark in np.unique(markers):
124
# mark == 0 --> background
125
if mark == 0:
126
continue
127
128
# Draw it on mask and detect biggest contour
129
mask = np.zeros(gray.shape, dtype="uint8")
130
mask[markers == mark] = 255
131
132
cnts = cv2.findContours(mask.copy(),
133
cv2.RETR_EXTERNAL,
134
cv2.CHAIN_APPROX_SIMPLE)[-2]
135
c = max(cnts, key=cv2.contourArea)
136
137
# Draw a bounding rectangle if it contains text
138
x,y,w,h = cv2.boundingRect(c)
139
cv2.drawContours(mask, c, 0, (255, 255, 255), cv2.FILLED)
140
maskROI = mask[y:y+h, x:x+w]
141
# Ratio of white pixels to area of bounding rectangle
142
r = float(cv2.countNonZero(maskROI)) / (w * h)
143
144
# Limits for text
145
if r > 0.2 and 2000 > w > 15 and 1500 > h > 15:
146
cv2.rectangle(image, (x, y),(x+w,y+h), (0, 255, 0), 2)
147
148
implt(image)
149
150