Skip to content

Commit 3c3c9e2

Browse files
committed
OMR Reader using Python
1 parent bc668bd commit 3c3c9e2

27 files changed

+1121
-0
lines changed

Category.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import cv2
2+
import numpy as np
3+
4+
def category(cat):
5+
cate=""
6+
orginal=np.uint8(cat)
7+
orginal=cv2.resize(orginal,(202,626))
8+
c = ["GEN","OBC1","OBC2","SC","ST","PH"]
9+
10+
h,w = orginal.shape
11+
crop= orginal[140:h-5,140:w-5]
12+
# cv2.imshow('l',crop)
13+
14+
h1,w1 = crop.shape
15+
th, im_th = cv2.threshold(crop,127,255,0)
16+
17+
im_th=~im_th
18+
kernel = np.ones((5,5), np.uint8)
19+
binary = cv2.erode(im_th, kernel, iterations=2)
20+
21+
count = 0
22+
for x in range(0, h1,np.uint(np.floor(h1/6))):
23+
24+
if (x+int(h1/6) > h1):
25+
break
26+
row = binary[x:x+int(h1/6),:]
27+
visr=crop[x:x+int(h1/6),:]
28+
count+=1
29+
# cv2.imshow("Foreground", visr)
30+
# cv2.waitKey(0)
31+
_,cnts, _ = cv2.findContours(row, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
32+
33+
if len(cnts) == 1:
34+
cate=c[count-1]
35+
36+
37+
return cate

Center_code.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import cv2
2+
import numpy as np
3+
4+
def code(center):
5+
6+
orginal=np.uint8(center)
7+
orginal=cv2.resize(orginal,(267,583))
8+
9+
h,w = orginal.shape
10+
crop= orginal[158:h-5,18:w-18]
11+
12+
13+
th, im_th = cv2.threshold(crop,127,255,0)
14+
im_th=~im_th
15+
kernel = np.ones((5,5), np.uint8)
16+
binary = cv2.erode(im_th, kernel, iterations=2)
17+
18+
h1,w1 = crop.shape
19+
code = []
20+
for y in range(0, w1,np.uint(np.floor(w1/4))):
21+
if (y +int(w1/4) > w1):
22+
break
23+
column = binary[0:h1, y: y +int(w1/4)]
24+
visc=crop[0:h1, y: y +int(w1/4)]
25+
count=0
26+
for x in range(0, h1,np.uint(np.floor(h1/10))):
27+
28+
if (x+int(h1/10) > h1):
29+
break
30+
row = column[x:x+int(h1/10),:]
31+
visr=visc[x:x+int(h1/10),:]
32+
count+=1
33+
34+
# cv2.imshow("Foreground", visr)
35+
# cv2.waitKey(0)
36+
37+
_,cnts, _ = cv2.findContours(row, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
38+
if len(cnts) == 1:
39+
code.append(str(count))
40+
if len(code)>0:
41+
code=['0' if x == '10' else x for x in code]
42+
code=''.join(code)
43+
code=int(code)
44+
else:
45+
code=""
46+
return code
47+
48+

Input/10.jpg

978 KB
Loading

Input/11.jpg

973 KB
Loading

Input/12.jpg

1000 KB
Loading

Input/2.jpg

738 KB
Loading

Input/3.jpg

984 KB
Loading

Input/4.jpg

2.3 MB
Loading

Input/5.jpg

2.29 MB
Loading

Input/6.jpg

2.32 MB
Loading

Input/7.jpg

2.3 MB
Loading

Input/8.jpg

980 KB
Loading

Input/9.jpg

984 KB
Loading

Main.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Aug 25 23:40:17 2020
4+
5+
@author: Koshy G
6+
"""
7+
8+
9+
import os
10+
import csv
11+
import Question_recog
12+
import segments
13+
import Name_recog
14+
import Mobile_recog
15+
import Roll_no
16+
import Center_code
17+
import Category
18+
19+
20+
21+
In_path ="Input/"
22+
Score_path = "Score/"
23+
valid_images = [".jpg",".gif",".png",".tga"]
24+
data=[]
25+
data.append(['Centre Code','Roll No','Name','Category','Mobile Number','question'])
26+
27+
28+
29+
#
30+
i=0
31+
for s in os.listdir(In_path):
32+
33+
print(i)
34+
i=i+1
35+
ext = os.path.splitext(s)[1]
36+
if ext.lower() not in valid_images:
37+
continue
38+
input_img=os.path.join(In_path,s)
39+
40+
q1,q2,q3,q4,q5,name,mob,roll,center,cat = segments.segment(input_img)
41+
quest1=Question_recog.question(q1)
42+
quest2=Question_recog.question(q2)
43+
quest3=Question_recog.question(q3)
44+
quest4=Question_recog.question(q4)
45+
quest5=Question_recog.question(q5)
46+
quest=quest1+quest2+quest3+quest4+quest5
47+
name = Name_recog.nameread(name)
48+
mobile = Mobile_recog.mobile(mob)
49+
rollno = Roll_no.roll_no(roll)
50+
code=Center_code.code(center)
51+
cate = Category.category(cat)
52+
53+
data.append([code,rollno,name,cate,mobile,quest])
54+
55+
56+
57+
with open('output.csv', 'w') as writeFile:
58+
writer = csv.writer(writeFile)
59+
writer.writerows(data)
60+
61+

Mainsheet/2.png

9.88 MB
Loading

Mobile_recog.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import cv2
2+
import numpy as np
3+
4+
def mobile(mob):
5+
6+
orginal=np.uint8(mob)
7+
orginal=cv2.resize(orginal,(555,629))
8+
# cv2.imshow('a',orginal)
9+
10+
h,w = orginal.shape
11+
crop= orginal[200:h-9,15:w-15]
12+
h1,w1 = crop.shape
13+
th, im_th0 = cv2.threshold(crop,220,255,0)
14+
im_th0=~im_th0
15+
kernel1= np.ones((5,30), np.uint8)
16+
im_th0=cv2.morphologyEx(im_th0, cv2.MORPH_CLOSE, kernel1)
17+
18+
kernel1=np.ones((5,30), np.uint8)
19+
im_th0 = cv2.erode(im_th0, kernel1, iterations=1)
20+
21+
kernel1=np.ones((5,w1), np.uint8)
22+
im_th0=cv2.morphologyEx(im_th0, cv2.MORPH_OPEN, kernel1)
23+
24+
_,cnts0, _ = cv2.findContours(im_th0, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
25+
if len(cnts0)==9:
26+
crop=orginal[150:h-9,15:w-15]
27+
h1,w1=crop.shape
28+
29+
th, im_th = cv2.threshold(crop,127,255,0)
30+
im_th=~im_th
31+
kernel = np.ones((5,5), np.uint8)
32+
number=[]
33+
binary = cv2.erode(im_th, kernel, iterations=2)
34+
# cv2.imshow('a',binary)
35+
# cv2.waitKey(0)
36+
37+
for y in range(0, w1,np.uint(np.floor(w1/10))):
38+
39+
if (y +int(w1/10) > w1):
40+
break
41+
column = binary[0:h1, y: y +int(w1/10)]
42+
# cv2.imshow('aa',column)
43+
# cv2.waitKey(0)
44+
# visc=crop[0:h1, y: y +int(w1/10)]
45+
countn = 0
46+
for x in range(0, h1,np.uint(np.floor(h1/10))):
47+
48+
if (x+int(h1/10) > h1):
49+
break
50+
row = column[x:x+int(h1/10),:]
51+
# visr=visc[x:x+int(h1/10),:]
52+
countn+=1
53+
54+
# cv2.imshow("Foreground", row)
55+
# cv2.waitKey(0)
56+
57+
_,cnts, _ = cv2.findContours(row, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
58+
if len(cnts) > 0:
59+
number.append(str(countn))
60+
number=['0' if x == '10' else x for x in number]
61+
number=''.join(number)
62+
63+
if len(number)>0:
64+
number=number
65+
else:
66+
number=""
67+
# print(number)
68+
return number
69+
70+
71+
72+

0 commit comments

Comments
 (0)