-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOCRRead.py
More file actions
66 lines (59 loc) · 2.88 KB
/
OCRRead.py
File metadata and controls
66 lines (59 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pytesseract
from PIL import Image
import os
from os import listdir
import imageio
import time
import glob
questions_dir = "C:\\Users\\dongd\\Downloads\\Testin\\SplitSet\\"
answers_dir = "C:\\Users\\dongd\\Downloads\\Testin\\AnswersSet\\"
Output_dir = "C:\\Users\\dongd\\Downloads\\Testin\\"
pytesseract.pytesseract.tesseract_cmd = r"C:\\Users\\dongd\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe"
def OCR_Questions():
with open('C:\\Users\\dongd\\Downloads\\Testin\\test.txt', 'w') as f:
for images in os.listdir(questions_dir):
if images.endswith('.jpg'):
#f.write(pytesseract.image_to_string(Image.open(folder_dir + images)))
pyt = pytesseract.image_to_string(Image.open(questions_dir + images), lang='eng',config='--psm 6')
f.write(pyt)
print(pyt)
f.write("\n")
print("Job finished")
def OCR_Answers():
with open('C:\\Users\\dongd\\Downloads\\Testin\\AnswersData.txt', 'w') as f:
for i in range(84,144):
try:
img = Image.open(answers_dir + str(i) + '.jpg')
img = img.crop((200, 370, 1550, 700)) #--> Skill Crop
imageio.imwrite(Output_dir + "answerTemp"+ '.jpg', img)
pyt = pytesseract.image_to_string(Image.open(Output_dir + "answerTemp"+ '.jpg'), lang='eng',config='--psm 6')
f.write(pyt)
print(pyt)
f.write("\n")
img = Image.open(answers_dir + str(i) + '.jpg')
img = img.crop((1580, 370, 2450, 700)) # ---> Learning Objective Crop
imageio.imwrite(Output_dir + "answerTemp"+ '.jpg', img)
time.sleep(1)
pyt = pytesseract.image_to_string(Image.open(Output_dir + "answerTemp"+ '.jpg'), lang='eng',config='--psm 6')
f.write(pyt)
print(pyt)
f.write("\n")
img = Image.open(answers_dir + str(i) + '.jpg')
img = img.crop((2480, 470, 2650, 640)) # --> Unit Crop
imageio.imwrite(Output_dir + "answerTemp"+ '.jpg', img)
pyt = pytesseract.image_to_string(Image.open(Output_dir + "answerTemp"+ '.jpg'), lang='eng',config='--psm 10')
f.write("Unit" + pyt)
print(pyt)
f.write("\n")
img = Image.open(answers_dir + str(i) + '.jpg')
img = img.crop((200, 685, 2700, 3700)) # --> Question Crop
imageio.imwrite(Output_dir + "answerTemp"+ '.jpg', img)
pyt = pytesseract.image_to_string(Image.open(Output_dir + "answerTemp"+ '.jpg'), lang='eng',config='--psm 6')
f.write(pyt)
print(pyt)
f.write("\n")
except:
print("Missing File " + str(i))
continue
print("Job finished")
OCR_Answers()