Skip to content

Commit 807c50f

Browse files
committed
spaces added
1 parent 4650a3b commit 807c50f

File tree

8 files changed

+132
-12
lines changed

8 files changed

+132
-12
lines changed

__pycache__/segment.cpython-37.pyc

0 Bytes
Binary file not shown.
2.13 KB
Binary file not shown.

app.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import cv2
55
import numpy as np
66
from segment import segment_input
7+
from segment_spaces import segmentspaces_input
78
import io
89
from fastai.vision import *
910

10-
11+
# ROOT_DIR="D:\\Projects\\OcrKannada\\Application\\"
1112
app = Flask(__name__)
12-
app.config['UPLOAD_FOLDER'] = 'D:\\Projects\\OcrKannada\\Application\\static\\Upload'
13+
# app.config['UPLOAD_FOLDER'] = ROOT_DIR+'static\\Upload'
1314
app.secret_key = 'asrtarstaursdlarsn'
1415
#unicode for each dependent vowel
1516

@@ -108,15 +109,15 @@ def upload():
108109
print("No file")
109110
return render_template('./index.html',result='false',uploaded='false',imagesrc="/static/assets/img/1%20CBkz7f_KjNh_wVuqLp-m0A.png")
110111
file = request.files['file']
111-
# if user does not select file, browser also
112-
# submit an empty part without filename
113112
if file.filename == '':
114113
return render_template('./index.html',result='false',uploaded='false',imagesrc="/static/assets/img/1%20CBkz7f_KjNh_wVuqLp-m0A.png")
115114
if file:
116115
# print("Hello")
117116
filename = secure_filename(file.filename)
118-
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
119-
number_files=segment_input(os.path.join(app.config['UPLOAD_FOLDER'], filename),filename.split(".")[0])
117+
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
118+
file.save("./static/Upload/{}".format(filename))
119+
# number_files=segment_input(os.path.join(app.config['UPLOAD_FOLDER'], filename),filename.split(".")[0])
120+
number_files=segmentspaces_input("./static/Upload/{}".format(filename),filename.split(".")[0])
120121
#<HARIS CODE>
121122
s=''
122123
# with open('./static/output.txt','w') as outputFile:
@@ -131,6 +132,7 @@ def upload():
131132
#img with size 6X9 is a space
132133
if img.size[0]==6 and img.size[1]==9:
133134
# outputFile.write(' ')
135+
s+=' '
134136
print("Skipped: ",i+1)
135137
continue
136138

@@ -148,11 +150,6 @@ def upload():
148150
except:
149151
# outputFile.write('')
150152
s+='?'
151-
# f = io.open("./static/output.txt", mode="r", encoding="utf-8")
152-
# s=f.readlines()
153-
154-
# for i in s:
155-
# print(i)
156153
print('S: ',s)
157154
try:
158155
return render_template('./index.html',result='true',imagesrc="/static/Upload/{}".format(filename),data=s)

environment.yml

+1
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ dependencies:
109109
- wasabi==0.7.1
110110
- werkzeug==1.0.1
111111
- zipp==3.1.0
112+

readme.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
Kannada Character Recognition
1+
# KannadaOCR
2+
## Instructions to run Web Application:
3+
* Environment Setup
4+
* Open Anaconda Prompt
5+
* Enter the Project directory
6+
* Execute the following commands:
7+
8+
conda env create -f environment.yml
9+
10+
conda activate ocr
11+
* To run the application:
12+
13+
python app.py

segment_spaces.py

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import cv2
2+
import numpy as np
3+
4+
def segmentspaces_input(PATH,filename):
5+
# Insert location of image here
6+
image = cv2.imread(PATH)
7+
ret,image = cv2.threshold(image,220,255,cv2.THRESH_BINARY)
8+
9+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
10+
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
11+
12+
kernel = np.ones((5, 5), np.uint8)
13+
img_dilated = cv2.dilate(thresh, kernel, iterations = 1)
14+
15+
cnts, _ = cv2.findContours(img_dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
16+
17+
# Array of initial bounding rects
18+
rects = []
19+
20+
# Bool array indicating which initial bounding rect has
21+
# already been used
22+
rectsUsed = []
23+
24+
# Just initialize bounding rects and set all bools to false
25+
for cnt in cnts:
26+
rects.append(cv2.boundingRect(cnt))
27+
rectsUsed.append(False)
28+
29+
# Sort bounding rects by x coordinate
30+
def getXFromRect(item):
31+
return item[0]
32+
33+
rects.sort(key = getXFromRect)
34+
35+
# Array of accepted rects
36+
acceptedRects = []
37+
38+
# Merge threshold for x coordinate distance
39+
xThr = 5
40+
41+
# Iterate all initial bounding rects
42+
for supIdx, supVal in enumerate(rects):
43+
if (rectsUsed[supIdx] == False):
44+
45+
# Initialize current rect
46+
currxMin = supVal[0]
47+
currxMax = supVal[0] + supVal[2]
48+
curryMin = supVal[1]
49+
curryMax = supVal[1] + supVal[3]
50+
51+
# This bounding rect is used
52+
rectsUsed[supIdx] = True
53+
54+
# Iterate all initial bounding rects
55+
# starting from the next
56+
for subIdx, subVal in enumerate(rects[(supIdx+1):], start = (supIdx+1)):
57+
58+
# Initialize merge candidate
59+
candxMin = subVal[0]
60+
candxMax = subVal[0] + subVal[2]
61+
candyMin = subVal[1]
62+
candyMax = subVal[1] + subVal[3]
63+
64+
# Check if x distance between current rect
65+
# and merge candidate is small enough
66+
if (candxMin <= currxMax + xThr):
67+
68+
# Reset coordinates of current rect
69+
currxMax = candxMax
70+
curryMin = min(curryMin, candyMin)
71+
curryMax = max(curryMax, candyMax)
72+
73+
# Merge candidate (bounding rect) is used
74+
rectsUsed[subIdx] = True
75+
else:
76+
break
77+
78+
# No more merge candidates possible, accept current rect
79+
acceptedRects.append([currxMin, curryMin, currxMax - currxMin, curryMax - curryMin])
80+
81+
# for rect in acceptedRects:
82+
# print(rect[0],rect[1],rect[2],rect[3])
83+
# img = cv2.rectangle(image, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (121, 11, 189), 2)
84+
85+
# cv2_imshow(image)
86+
c=0
87+
c=0
88+
for i in range(len(acceptedRects)):
89+
flag=0
90+
stpt = [acceptedRects[i][0], acceptedRects[i][1]]
91+
endpt=[acceptedRects[i][0]+acceptedRects[i][2], acceptedRects[i][1]+acceptedRects[i][3]]
92+
if i<len(acceptedRects)-1:
93+
nstpt = [acceptedRects[i+1][0], acceptedRects[i+1][1]]
94+
nendpt=[acceptedRects[i+1][0]+acceptedRects[i+1][2], acceptedRects[i+1][1]+acceptedRects[i+1][3]]
95+
dis = nstpt[0]-endpt[0]
96+
print(dis)
97+
if dis > 100:
98+
temp1 = np.zeros((6,9), dtype=int)
99+
# cv2_imshow(temp1)
100+
flag=1
101+
cropp= image[stpt[1]:endpt[1], stpt[0]:endpt[0]]
102+
color = [255, 255, 255]
103+
pad=100
104+
new_im = cv2.copyMakeBorder(cropp, pad, pad, pad, pad, cv2.BORDER_CONSTANT,value=color)
105+
c+=1
106+
cv2.imwrite("./images/{}c{}.png".format(filename,c), new_im)
107+
if flag==1:
108+
c+=1
109+
cv2.imwrite("./images/{}c{}.png".format(filename,c), temp1)
110+
return c

static/Upload/test2.png

8.6 KB
Loading

static/Upload/test3.jpg

22.8 KB
Loading

0 commit comments

Comments
 (0)