├── AnswerSheet ├── 12345_Q1.txt ├── 12345_Q2.txt ├── 12345_Q3.txt ├── 201811036_Q1.txt ├── 201851112_Q1.txt ├── 201851112_Q2.txt └── 201851112_Q3.txt ├── Images ├── Roi.png ├── StudentQP.png ├── admin1.png ├── admin2.png ├── home.png ├── ivcam.png ├── lined page.png ├── pen.png ├── setup.jpeg ├── student login.png ├── thick nib.png └── training.png ├── MainPage.py ├── Marksheet.xlsx ├── Project Presentation.pdf ├── Project_Report.pdf ├── Questions.txt ├── README.md ├── ans.xlsx ├── final.py ├── main.py └── setCamera2.py /AnswerSheet/12345_Q1.txt: -------------------------------------------------------------------------------- 1 | 201851112 2 | SATYAPRAKASH 3 | 4 | SIX INT CHAR 800L 5 | VorID DOUBLE FLOAT 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AnswerSheet/12345_Q2.txt: -------------------------------------------------------------------------------- 1 | THREE 2 | PHYSICAL ORGANC 3 | INORGANIC ORGANIC 4 | 5 | eo 6 | -------------------------------------------------------------------------------- /AnswerSheet/12345_Q3.txt: -------------------------------------------------------------------------------- 1 | 20185I/(2 2 | SHIVANSHI DAVE 3 | INT CHAR FLOAT 4 | Bool #B DovBre VorId 5 | S1X 6 | 7 | DINYSYO >INvbyonT 8 | NYbYo HI; 9 | 332) 10 | z 11 | -------------------------------------------------------------------------------- /AnswerSheet/201811036_Q1.txt: -------------------------------------------------------------------------------- 1 | 201851112 2 | SATYAPRAKASH 3 | 4 | SIX INT CHAR B00L 5 | Vorp DOUBLE FLOAT 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /AnswerSheet/201851112_Q1.txt: -------------------------------------------------------------------------------- 1 | 201851112 2 | SATYAPRAKASH 3 | 4 | SIX INT CHAR BOoL 5 | VoID DOUBLE FLOAT 6 | -------------------------------------------------------------------------------- /AnswerSheet/201851112_Q2.txt: -------------------------------------------------------------------------------- 1 | 201851112 SATYA 2 | THERE ARE THREE 3 | PARTS OF CHEMISTRY 4 | PHYSICAL ORGANIC 5 | INORGANIC 6 | 7 | -------------------------------------------------------------------------------- /AnswerSheet/201851112_Q3.txt: -------------------------------------------------------------------------------- 1 | FULL FORM oF spp 2 | SUMMER DESIGN 3 | PROTECT 4 | 5 | 201851112 6 | SATYAPRAIKASH 7 | -------------------------------------------------------------------------------- /Images/Roi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/Roi.png -------------------------------------------------------------------------------- /Images/StudentQP.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/StudentQP.png -------------------------------------------------------------------------------- /Images/admin1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/admin1.png -------------------------------------------------------------------------------- /Images/admin2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/admin2.png -------------------------------------------------------------------------------- /Images/home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/home.png -------------------------------------------------------------------------------- /Images/ivcam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/ivcam.png -------------------------------------------------------------------------------- /Images/lined page.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/lined page.png -------------------------------------------------------------------------------- /Images/pen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/pen.png -------------------------------------------------------------------------------- /Images/setup.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/setup.jpeg -------------------------------------------------------------------------------- /Images/student login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/student login.png -------------------------------------------------------------------------------- /Images/thick nib.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/thick nib.png -------------------------------------------------------------------------------- /Images/training.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Images/training.png -------------------------------------------------------------------------------- /MainPage.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import * 3 | import subprocess as sb_p 4 | from xlrd import * 5 | import openpyxl 6 | import pandas as pd 7 | import win32com.client 8 | from setCamera2 import get_answer 9 | import sys 10 | import PIL.Image as Image 11 | import numpy as np 12 | import pytesseract 13 | import cv2 14 | import re 15 | import array as ar 16 | import os 17 | # import the time module 18 | import time 19 | 20 | 21 | 22 | def verify(res,id): 23 | number = 0 24 | for i in range(2): 25 | for j in res: 26 | 27 | if j == str(id): 28 | number = 1 29 | 30 | return number 31 | 32 | def set_exam(root,frame1): 33 | path_q='questions.txt' 34 | path_k='ans.xlsx' 35 | path_m='Marksheet.xlsx' 36 | 37 | for widget in frame1.winfo_children(): 38 | widget.destroy() 39 | 40 | Label(frame1, text="Set Examination", font=('Helvetica', 25, 'bold')).grid(row = 0, column = 1, rowspan=1) 41 | Label(frame1, text="").grid(row = 1,column = 0) 42 | #Add Questions 43 | set_q = Button(frame1, text="Add Questions", width=15, command = lambda: os.system(path_q)) 44 | 45 | #Set Keywords 46 | set_k = Button(frame1, text="Set Keywords", width=15, command = lambda: os.system(path_k)) 47 | 48 | #Open Marksheet 49 | set_m = Button(frame1, text="Manage Marksheet", width=15, command = lambda: os.system(path_m)) 50 | 51 | Label(frame1, text="").grid(row = 2,column = 0) 52 | Label(frame1, text="").grid(row = 4,column = 0) 53 | Label(frame1, text="").grid(row = 6,column = 0) 54 | set_q.grid(row = 3, column = 1, columnspan = 2) 55 | set_k.grid(row = 5, column = 1, columnspan = 2) 56 | set_m.grid(row = 7, column = 1, columnspan = 2) 57 | 58 | frame1.pack() 59 | root.mainloop() 60 | 61 | 62 | def Evaluator(root, frame1): 63 | root.title("Set Examination") 64 | for widget in frame1.winfo_children(): 65 | widget.destroy() 66 | Label(frame1, text="Enter Valid ID and password", font=('Helvetica', 24, 'bold')).grid(row=0, column=2, rowspan=1) 67 | Label(frame1, text="").grid(row=1, column=0) 68 | Label(frame1, text="ID: ", anchor="e", justify=LEFT).grid(row=2, column=0) 69 | Label(frame1, text="Password: ", anchor="e", justify=LEFT).grid(row=3, column=0) 70 | 71 | ID = tk.StringVar() 72 | password = tk.StringVar() 73 | 74 | e1 = Entry(frame1, textvariable=ID) 75 | e1.grid(row=2, column=2) 76 | e2 = Entry(frame1, textvariable=password) 77 | e2.grid(row=3, column=2) 78 | 79 | checking = Button(frame1, text="NEXT", width=15, command=lambda: eval_check(root, frame1, str(ID.get()), str(password.get()))) 80 | Label(frame1, text="").grid(row=4, column=0) 81 | checking.grid(row=5, column=3, columnspan=2) 82 | frame1.pack() 83 | root.mainloop() 84 | 85 | 86 | def eval_check(root, frame1, id, passw): 87 | if passw == "admin" and id == "admin": 88 | set_exam(root, frame1) 89 | else: 90 | Evaluator(root, frame1) 91 | 92 | 93 | def Student(root, frame1): 94 | root.title("Start Examination") 95 | for widget in frame1.winfo_children(): 96 | widget.destroy() 97 | Label(frame1, text="Enter Valid ID and your name", font=('Helvetica', 24, 'bold')).grid(row=0, column=2, rowspan=1) 98 | Label(frame1, text="").grid(row=1, column=0) 99 | Label(frame1, text="Student ID: ", anchor="e", justify=LEFT).grid(row=2, column=0) 100 | Label(frame1, text="Name: ", anchor="e", justify=LEFT).grid(row=3, column=0) 101 | 102 | s_ID = tk.StringVar() 103 | name = tk.StringVar() 104 | 105 | e1 = Entry(frame1, textvariable=s_ID) 106 | e1.grid(row=2, column=2) 107 | e2 = Entry(frame1, textvariable=name) 108 | e2.grid(row=3, column=2) 109 | 110 | path = 'questions.txt' 111 | checking = Button(frame1, text="START", width=15, 112 | command=lambda: Q_paper(s_ID.get(), name.get(), root, frame1, load_questions(path))) 113 | Label(frame1, text="").grid(row=4, column=0) 114 | checking.grid(row=5, column=3, columnspan=2) 115 | frame1.pack() 116 | root.mainloop() 117 | 118 | 119 | def check(id,qno): 120 | name = "C:/Users/SATYAPRAKASH/PycharmProjects/Test/AnswerSheet/"+str(id)+'_Q'+str(qno)+'.txt' 121 | file = open(name , 'r') 122 | test = file.read().replace("\n", " ") 123 | file.close() 124 | 125 | xlApp = win32com.client.Dispatch("Excel.Application") 126 | filename,password = r"C:/Users/SATYAPRAKASH/PycharmProjects/Test/ans.xlsx", '1234' 127 | xlwb = xlApp.Workbooks.Open(filename, False, True, None, password) 128 | # xlwb.Sheets().Select 129 | 130 | res = re.findall(r'\w+', test) 131 | flag = verify(res,id) 132 | if(flag == 0): 133 | print("Not Valid") 134 | number = 0 135 | 136 | k = 1 137 | while True : 138 | c=xlwb.Sheets(qno).Cells(k,1).Value 139 | print("form Sheet : " + str(c)) 140 | if c is None: 141 | break 142 | k=k+1 143 | 144 | for i in range(k-2): 145 | for j in res: 146 | 147 | if j.lower() == xlwb.Sheets(qno).Cells(i+2,1).Value.lower(): 148 | print(xlwb.Sheets(qno).Cells(i+2,2).Value) 149 | number = number+xlwb.Sheets(qno).Cells(i+2,2).Value 150 | 151 | print(number) 152 | wrkbk = openpyxl.load_workbook('Marksheet.xlsx') 153 | sh = wrkbk.active 154 | i = 1 155 | while True : 156 | c=sh.cell(row=i,column=1) 157 | if c.value == str(id): 158 | break 159 | i=i+1 160 | writer = pd.ExcelWriter('Marksheet.xlsx', engine='openpyxl') 161 | df = pd.read_excel('Marksheet.xlsx', header=None) 162 | df1 = pd.DataFrame([[number]]) 163 | df.to_excel(writer, header=None, index=False) 164 | df1.to_excel(writer, header=None, index=False, 165 | startcol=qno+1,startrow=i-1) 166 | writer.save() 167 | 168 | xlwb.Close(SaveChanges=0) 169 | xlApp.Quit() 170 | 171 | 172 | 173 | def load_questions(path): 174 | f = open(path, 'r') 175 | s=f.read() 176 | a=s.split('\n') 177 | return a 178 | 179 | 180 | def Q_paper(s_ID,name,root,frame1,questions): 181 | 182 | for frame in root.winfo_children(): 183 | for widget in frame.winfo_children(): 184 | widget.destroy() 185 | 186 | wrkbk = openpyxl.load_workbook('Marksheet.xlsx') 187 | sh = wrkbk.active 188 | i = 1 189 | while True : 190 | c=sh.cell(row=i,column=1) 191 | if c.value is None: 192 | break 193 | i=i+1 194 | writer = pd.ExcelWriter('Marksheet.xlsx', engine='openpyxl') 195 | df = pd.read_excel('Marksheet.xlsx', header=None) 196 | df1 = pd.DataFrame([[s_ID , name]]) 197 | df.to_excel(writer, header=None, index=False) 198 | df1.to_excel(writer, header=None, index=False, 199 | startcol=0,startrow=i-1) 200 | writer.save() 201 | 202 | n = len(questions) 203 | q = [] 204 | 205 | for j in range(0, n): 206 | 207 | Label(frame1, text="Q-" + str(j+1) + " :" + str(questions[j]) , font=('Helvetica', 24, 'bold')).grid(row = 2+2*j,column = 0) 208 | Label(frame1, text="").grid(row = 3+2*j,column = 0) 209 | 210 | q.append(None) 211 | q[j] = Button(frame1, text="START WRITING", width=20, command = lambda k=j :action(q,k,s_ID)) 212 | q[j].grid(row = 2+2*j, column = 3, columnspan = 2) 213 | 214 | quit = Button(frame1, text="FINISH", width=20, command = lambda: root.quit()) 215 | Label(frame1, text="").grid(row = 18,column = 0) 216 | quit.grid(row = 20, column = 0, columnspan = 2) 217 | 218 | def action(q, j, s_ID): 219 | q[j].configure(state=DISABLED) 220 | get_answer(s_ID , j+1) 221 | #sb_p.call('start python setCamera.py', shell=True) 222 | #file_create(s_ID,j,string) 223 | check(s_ID,j+1) 224 | 225 | 226 | # def file_create(s_ID,qno,string): 227 | # f = open(str(s_ID)+"_Q"+str(qno)+".txt", "w") 228 | # f.write(string) 229 | 230 | 231 | 232 | def Home(root, frame1, frame2): 233 | 234 | for frame in root.winfo_children(): 235 | for widget in frame.winfo_children(): 236 | widget.destroy() 237 | 238 | Button(frame2, text="Home", command = lambda: Home(root, frame1, frame2)).grid(row=0,column=0) 239 | Label(frame2, text=" ").grid(row = 0,column = 1) 240 | Label(frame2, text=" ").grid(row = 0,column = 2) 241 | Label(frame2, text=" ").grid(row = 1,column = 1) 242 | frame2.pack(side=TOP) 243 | 244 | Label(frame1, text="Home", font=('Helvetica', 25, 'bold')).grid(row = 0, column = 1, rowspan=1) 245 | Label(frame1, text="").grid(row = 1,column = 0) 246 | #Evaluator Login 247 | eval = Button(frame1, text="Evaluator Login", width=15, command = lambda: Evaluator(root, frame1)) 248 | 249 | #Student Login 250 | student = Button(frame1, text="Student Login", width=15, command = lambda: Student(root, frame1)) 251 | 252 | Label(frame1, text="").grid(row = 2,column = 0) 253 | Label(frame1, text="").grid(row = 4,column = 0) 254 | eval.grid(row = 3, column = 1, columnspan = 2) 255 | student.grid(row = 5, column = 1, columnspan = 2) 256 | 257 | frame1.pack() 258 | root.mainloop() 259 | 260 | 261 | root = Tk() 262 | root.geometry('2000x2000') 263 | root.title("Welcome...") 264 | frame1 = Frame(root) 265 | frame2 = Frame(root) 266 | Home(root, frame1, frame2) 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | -------------------------------------------------------------------------------- /Marksheet.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Marksheet.xlsx -------------------------------------------------------------------------------- /Project Presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Project Presentation.pdf -------------------------------------------------------------------------------- /Project_Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/Project_Report.pdf -------------------------------------------------------------------------------- /Questions.txt: -------------------------------------------------------------------------------- 1 | How many fundamental data types in C are there? Write names. 2 | how many parts of chemistry? write name. 3 | full Form of SDP. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Handwritten-answersheet-detection-and-evaluation 2 | The project detects and recognize handwritten characters and evaluates them and updates marks of students for teachers automatically. 3 | # Handwritten-answersheet-detection-and-evaluation 4 | The project detects and recognize handwritten characters and evaluates them and updates marks of students for teachers automatically. 5 | The developed system here will also be helpful for the students writing the examination. It is a time consuming process when a student writes first and then scan it and makes a pdf document of it. 6 | 7 | This is Setup required for project. 8 | ![SetUP](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/setup.jpeg) 9 | 10 | 11 | Home Page 12 | 13 | ![Home Page](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/home.png) 14 | 15 | 16 | 17 | Admin/Evaluator LogIn Page 18 | 19 | ![Admin/Evaluator Login Page](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/admin1.png) 20 | 21 | 22 | 23 | Admin/Evaluator Home Page 24 | 25 | ![Admin/Evaluator Home Page](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/admin2.png) 26 | 27 | 28 | 29 | Student Login Page 30 | 31 | ![Student Login Page](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/student%20login.png) 32 | 33 | 34 | 35 | Student Exam 36 | 37 | ![Student Exam Page](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/StudentQP.png) 38 | 39 | When Student will click on start Writting, he would write answer on the paper which is under android camera. and simultenouslt he will get a new 40 | window of ROI(Region of Interest) on his screen. 41 | 42 | ![ROI](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/Roi.png) 43 | 44 | iVcam Used to transfer video fram from mobile too laptop. 45 | 46 | ![iVcam](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/ivcam.png) 47 | 48 | For better accuracy, training on particular handwriting was also done by Jtessboxeditor. 49 | 50 | ![Training](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/training.png) 51 | 52 | One example of accuracy is given below. 53 | 54 | ![Example](https://github.com/phet2309/Handwritten-answersheet-detection-and-evaluation/blob/master/Images/thick%20nib.png) 55 | -------------------------------------------------------------------------------- /ans.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phet2309/Handwritten-answersheet-detection-and-evaluation/65b62b6f27fee476ee25bf26e93c5e6df43851cb/ans.xlsx -------------------------------------------------------------------------------- /final.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import pytesseract as tess 3 | from pytesseract import Output 4 | import numpy as np 5 | import random 6 | import PIL.Image as Image 7 | from autocorrect import Speller 8 | 9 | def correct_sentence(p): 10 | p = p.strip().split('\n') 11 | lines=[] 12 | for i in p: 13 | if len(i)>0: 14 | lines.append(i) 15 | # print(l) 16 | #spell=Speller(lang='en') 17 | # lines = line.strip().split(' ') 18 | new_line = "" 19 | # similar_word = {} 20 | ans=[] 21 | for l in lines: 22 | l=l.strip().split(' ') 23 | for j in l: 24 | new_line += j + " " 25 | #new_line += spell(j) + " " 26 | ans.append(new_line) 27 | new_line='' 28 | # print(ans) 29 | return ans 30 | # # similar_word[l]=spell.candidates(l) 31 | # return new_line 32 | 33 | 34 | 35 | def preprocess(image) : 36 | 37 | # Method - 1 38 | ConcatenatedImage = np.concatenate((image, image, image, image, image, image), axis = 0) # axis=0 is the concatenation at bottom 39 | ConcatenatedImage = Image.fromarray(ConcatenatedImage) 40 | #print('M-1:') 41 | #print(pytesseract.image_to_string(ConcatenatedImage)) 42 | # Method - 2 43 | image = cv2.fastNlMeansDenoisingColored(np.array(ConcatenatedImage)) 44 | #print('M-2:') 45 | #print(pytesseract.image_to_string(image)) 46 | Image.fromarray(image) 47 | # Method - 3 48 | imageBorderRemoved = np.array(image)[40:310, 20:480] 49 | imageBorderRemoved = cv2.fastNlMeansDenoisingColored(imageBorderRemoved) 50 | Image.fromarray(imageBorderRemoved) 51 | #print('M-3:') 52 | #print(pytesseract.image_to_string(imageBorderRemoved)) 53 | # Method - 4 54 | gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY) 55 | #print('M-4:') 56 | #print(pytesseract.image_to_string(gray)) 57 | Image.fromarray(gray) 58 | # Method - 5 59 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 60 | ret1,th1 = cv2.threshold( gray,127,255,cv2.THRESH_BINARY) # global thresholding 61 | Image.fromarray(th1) 62 | #print('M-5:') 63 | #print(pytesseract.image_to_string(th1)) 64 | # Method - 6 65 | DenoisedImage = cv2.fastNlMeansDenoisingColored(np.array(image)) 66 | grayDenoisedImage = cv2.cvtColor(DenoisedImage, cv2.COLOR_BGR2GRAY) 67 | th2 = cv2.adaptiveThreshold(grayDenoisedImage,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ 68 | cv2.THRESH_BINARY,11,2) 69 | Image.fromarray(th2) 70 | #print('M-6:') 71 | #print(pytesseract.image_to_string(th2)) 72 | return th2 73 | 74 | 75 | 76 | def text_detection(path): 77 | # Load image, grayscale, Gaussian blur, adaptive threshold 78 | tess.pytesseract.tesseract_cmd = r'C:\Users\SATYAPRAKASH\AppData\Local\Programs\Tesseract-OCR\tesseract.exe' 79 | 80 | 81 | # path='D:\Sem5\Design Project\Mypart/test11.jpeg' 82 | img = cv2.imread(path) 83 | 84 | # img=preprocess(img) 85 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 86 | 87 | blur = cv2.GaussianBlur(gray, (3,3), 0) 88 | thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] 89 | 90 | # Morph open to remove noise and invert image 91 | kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) 92 | opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1) 93 | img = 255 - opening 94 | 95 | 96 | #target = pytesseract.image_to_string(img, lang='eng', config='--psm 6 --oem 3 -c tessedit_char_whitelist= ABCDEFGHIJKLNMOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz\\,\\.') 97 | # target=correct_sentence(target) 98 | target = tess.pytesseract.image_to_string(img) 99 | #a=correct_sentence(target) 100 | # for i in a: 101 | # print(i) 102 | return target 103 | # cv2.imshow('img', img) 104 | # cv2.waitKey(0) 105 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import pytesseract 3 | import os 4 | 5 | def text_rec(x): 6 | img = cv2.imread(x) 7 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 8 | target = pytesseract.image_to_string(img, lang='eng', config='--psm 6 --oem 3 -c tessedit_char_whitelist= ABCDEFGHIJKLNMOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz\\,\\.') 9 | #target = pytesseract.image_to_string(img, lang='eng', config='--psm 6 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLNMOPQRSTUVWXYZ1234567890') 10 | # print(target) 11 | # print("-----------") 12 | tp = '' 13 | for i in target: 14 | if ord(i) >= 48 and ord(i) <= 57: 15 | tp = tp + i 16 | if len(tp)==0: 17 | return -1 18 | return int(tp) 19 | 20 | def pageNo(path): 21 | image = cv2.imread(path) 22 | #image = cv2.resize(image, (765, 1049)) 23 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 24 | blur = cv2.GaussianBlur(gray, (9, 9), 0) 25 | thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 30) 26 | 27 | # Dilate to combine adjacent text contours 28 | kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 9)) 29 | dilate = cv2.dilate(thresh, kernel, iterations=4) 30 | 31 | # Find contours, highlight text areas, and extract ROIs 32 | cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 33 | 34 | cnts = cnts[0] if len(cnts) == 2 else cnts[1] 35 | if len(cnts)==0: 36 | return -1 37 | # temp=cnts[len(cnts)-1] 38 | i = 0; 39 | # ROI_number = 0 40 | for c in cnts: 41 | area = cv2.contourArea(c) 42 | if area > 1000: 43 | x, y, w, h = cv2.boundingRect(c) 44 | cv2.rectangle(image, (x, y), (x + w, y + h), (36, 255, 12), 3) 45 | # if i==0: 46 | # ROI = image[y:y+h, x:x+w] 47 | # cv2.imwrite("PageNo" + str(i) + ".jpg", ROI) 48 | # i=100; 49 | # ROI_number += 1 50 | ROI = image[y:y + h, x:x + w] 51 | cv2.imwrite("PageNo" + str(i) + ".jpg", ROI) 52 | retVal = text_rec("PageNo" + str(i) + ".jpg") 53 | 54 | try: 55 | os.remove("PageNo" + str(i) + ".jpg") 56 | except: 57 | pass 58 | return retVal 59 | -------------------------------------------------------------------------------- /setCamera2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy 3 | import numpy as np 4 | import imutils 5 | import os 6 | import pytesseract as tess 7 | from main import pageNo 8 | from final import correct_sentence 9 | 10 | from final import text_detection , correct_sentence 11 | from time import sleep 12 | #_________________________________________________________________________________ 13 | #prachi's work character recognition 14 | tess.pytesseract.tesseract_cmd=r'C:\Users\SATYAPRAKASH\AppData\Local\Programs\Tesseract-OCR\tesseract.exe' 15 | def text_rec(x): 16 | img = cv2.imread(x) 17 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 18 | target = tess.pytesseract.image_to_string(img , lang ="eng+eng1" ) 19 | #target = pytesseract.image_to_string(img, lang='eng', config='--psm 6 --oem 3 -c tessedit_char_whitelist= ABCDEFGHIJKLNMOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz\\,\\.') 20 | #target = pytesseract.image_to_string(img, lang='eng', config='--psm 6 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLNMOPQRSTUVWXYZ1234567890') 21 | #print(target) 22 | #a = correct_sentence(target) 23 | return target 24 | 25 | 26 | def correct_sentence(p): 27 | p = p.strip().split('\n') 28 | lines = [] 29 | for i in p: 30 | if len(i) > 0: 31 | lines.append(i) 32 | 33 | new_line = "" 34 | 35 | ans = [] 36 | for l in lines: 37 | l = l.strip().split(' ') 38 | for j in l: 39 | new_line += j + " " 40 | ans.append(new_line) 41 | new_line = '' 42 | fin = '' 43 | for i in ans: 44 | fin += i + '\n' 45 | return fin 46 | 47 | def final_file(id,q): 48 | f1=open("C:/Users/SATYAPRAKASH/PycharmProjects/Test/AnswerSheet/"+str(id)+'_Q'+str(q)+'.txt','r') 49 | s=f1.read() 50 | s=correct_sentence(s) 51 | f1.close() 52 | # print(s) 53 | f1=open("C:/Users/SATYAPRAKASH/PycharmProjects/Test/AnswerSheet/"+str(id)+'_Q'+str(q)+'.txt','w') 54 | f1.write(s) 55 | f1.close() 56 | 57 | 58 | 59 | 60 | #_______________________________________________________________________________________ 61 | def get_answer(ID , QN): 62 | url = 'http://192.168.43.58:8080/video' 63 | cap = cv2.VideoCapture(1) # cap is a camera object 64 | j = 0; 65 | #oldPage = 3425 66 | #newPage = 0; 67 | from matplotlib import pyplot as plt 68 | file1 = open("C:/Users/SATYAPRAKASH/PycharmProjects/Test/AnswerSheet/"+str(ID)+"_Q"+str(QN)+".txt", 'w') 69 | 70 | while True: 71 | ret, frame = cap.read() # made a frame object 72 | blurred_frame = cv2.blur(frame, (5, 5), 0) 73 | hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV) # convert blurred_frame into hsv color 74 | 75 | lower_white = np.array([40, 0, 132]) 76 | upper_white = np.array([145, 75, 245]) 77 | 78 | mask = cv2.inRange(hsv, lower_white, upper_white) 79 | res = cv2.bitwise_and(frame, frame, mask=mask) 80 | r, thresh = cv2.threshold(mask, 40, 255, 0) 81 | 82 | # check to see if we are using OpenCV 2.X or OpenCV 4 83 | if imutils.is_cv2() or imutils.is_cv4(): 84 | contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 85 | # check to see if we are using OpenCV 3 86 | elif imutils.is_cv3(): 87 | im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 88 | 89 | for contour in contours: 90 | area = cv2.contourArea(contour) 91 | # if area > 100: 92 | # cv2.drawContours(frame, contours, -1, 255, 3) 93 | # find the biggest countour (c) by the area 94 | c = max(contours, key=cv2.contourArea) 95 | x, y, w, h = cv2.boundingRect(c) 96 | 97 | # draw the biggest contour (c) in green 98 | cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) 99 | roi = frame[y + 10:y + h - 5, x + 10:x + w - 8] 100 | cv2.imshow("Roi", roi) # show image 101 | 102 | if j % 150 == 0: 103 | cv2.imwrite("Frame" + str(j) + ".jpg", roi) 104 | content = text_rec("Frame" + str(j) + ".jpg") 105 | # content = text_detection("Frame"+str(j)+".jpg") 106 | # newPage = pageNo("Frame"+str(j)+".jpg") 107 | # if newPage==-1: 108 | # continue 109 | print(content) 110 | file1.seek(0) 111 | file1.write(content) 112 | # for i in content: 113 | # file1.write(i) 114 | # file1.write("\n") 115 | # print(i) 116 | 117 | # if newPage!=oldPage: 118 | # print("page no = "+ str(newPage) ) 119 | # oldPage=newPage 120 | # print(content) 121 | try: 122 | os.remove("Frame" + str(j) + ".jpg") 123 | except: 124 | pass 125 | break; 126 | 127 | j = j + 1 128 | 129 | # sleep(5) 130 | 131 | # show the images 132 | # cv2.imshow("Result", np.hstack([frame, res])) 133 | cv2.imshow("Roi", roi) # show image 134 | # cv2.imshow("Frame", frame) # show image 135 | # cv2.imshow("Mask", mask) # show image 136 | # cv2.imshow("res", res) # show image 137 | 138 | if cv2.waitKey(1) & 0xFF == ord('q'): 139 | break 140 | file1.close() 141 | #final_file(ID , QN) 142 | cap.release() 143 | cv2.destroyAllWindows() 144 | 145 | 146 | 147 | 148 | --------------------------------------------------------------------------------