Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Aniket025
GitHub Repository: Aniket025/Medical-Prescription-OCR
Path: blob/master/Model-4/ocr/words.py
427 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, join=False):
12
""" Detecting the words bounding boxes """
13
# Preprocess image for word detection
14
blurred = cv2.GaussianBlur(image, (5, 5), 18)
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, join)
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, join=False):
43
small = resize(img, 2000)
44
45
# Finding contours
46
mask = np.zeros(small.shape, np.uint8)
47
im2, cnt, hierarchy = cv2.findContours(np.copy(small),
48
cv2.RETR_CCOMP,
49
cv2.CHAIN_APPROX_SIMPLE)
50
index = 0
51
boundingBoxes = np.array([0,0,0,0])
52
bBoxes = []
53
54
# image for drawing bounding boxes
55
small = cv2.cvtColor(small, cv2.COLOR_GRAY2RGB)
56
57
# Go through all contours in top level
58
while (index >= 0):
59
x,y,w,h = cv2.boundingRect(cnt[index])
60
cv2.drawContours(mask, cnt, index, (255, 255, 255), cv2.FILLED)
61
maskROI = mask[y:y+h, x:x+w]
62
# Ratio of white pixels to area of bounding rectangle
63
r = cv2.countNonZero(maskROI) / (w * h)
64
65
# Limits for text
66
if r > 0.1 and 1600 > w > 10 and 1600 > h > 10 and h/w < 3 and w/h < 10 and (60 // h) * w < 1000:
67
bBoxes += [[x, y, w, h]]
68
69
index = hierarchy[0][index][0]
70
71
# Need more work
72
if join:
73
bBoxes = groupRectangles(bBoxes)
74
for (x, y, w, h) in bBoxes:
75
cv2.rectangle(small, (x, y),(x+w,y+h), (0, 255, 0), 2)
76
boundingBoxes = np.vstack((boundingBoxes,
77
np.array([x, y, x+w, y+h])))
78
79
implt(small, t='Bounding rectangles')
80
81
bBoxes = boundingBoxes.dot(ratio(image, small.shape[0])).astype(np.int64)
82
return bBoxes[1:]
83
84
85
def textDetectWatershed(thresh):
86
""" Text detection using watershed algorithm - NOT IN USE """
87
# According to: http://docs.opencv.org/trunk/d3/db4/tutorial_py_watershed.html
88
img = cv2.cvtColor(cv2.imread("data/textdet/%s.jpg" % IMG),
89
cv2.COLOR_BGR2RGB)
90
img = resize(img, 3000)
91
thresh = resize(thresh, 3000)
92
# noise removal
93
kernel = np.ones((3,3),np.uint8)
94
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 3)
95
96
# sure background area
97
sure_bg = cv2.dilate(opening,kernel,iterations=3)
98
99
# Finding sure foreground area
100
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
101
ret, sure_fg = cv2.threshold(dist_transform,
102
0.01*dist_transform.max(), 255, 0)
103
104
# Finding unknown region
105
sure_fg = np.uint8(sure_fg)
106
unknown = cv2.subtract(sure_bg,sure_fg)
107
108
# Marker labelling
109
ret, markers = cv2.connectedComponents(sure_fg)
110
111
# Add one to all labels so that sure background is not 0, but 1
112
markers += 1
113
114
# Now, mark the region of unknown with zero
115
markers[unknown == 255] = 0
116
117
markers = cv2.watershed(img, markers)
118
implt(markers, t='Markers')
119
image = img.copy()
120
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
121
122
for mark in np.unique(markers):
123
# mark == 0 --> background
124
if mark == 0:
125
continue
126
127
# Draw it on mask and detect biggest contour
128
mask = np.zeros(gray.shape, dtype="uint8")
129
mask[markers == mark] = 255
130
131
cnts = cv2.findContours(mask.copy(),
132
cv2.RETR_EXTERNAL,
133
cv2.CHAIN_APPROX_SIMPLE)[-2]
134
c = max(cnts, key=cv2.contourArea)
135
136
# Draw a bounding rectangle if it contains text
137
x,y,w,h = cv2.boundingRect(c)
138
cv2.drawContours(mask, c, 0, (255, 255, 255), cv2.FILLED)
139
maskROI = mask[y:y+h, x:x+w]
140
# Ratio of white pixels to area of bounding rectangle
141
r = float(cv2.countNonZero(maskROI)) / (w * h)
142
143
# Limits for text
144
if r > 0.2 and 2000 > w > 15 and 1500 > h > 15:
145
cv2.rectangle(image, (x, y),(x+w,y+h), (0, 255, 0), 2)
146
147
implt(image)
148
149