-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
116 lines (90 loc) · 3.18 KB
/
main.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import easyocr
import cv2
import numpy as np
import docx
import os
import csv
# import pandas as pd
# Creating dataset directory if does not exist
if not os.path.exists('Dataset'):
os.mkdir('Dataset')
def color(i):
if result[i][2]*100 >= 0 and result[i][2]*100 <= 25:
(r, g, b) = (255, 106, 0)
elif result[i][2]*100 > 25 and result[i][2]*100 <= 50:
(r, g, b) = (255, 213, 0)
elif result[i][2]*100 > 50 and result[i][2]*100 <= 75:
(r, g, b) = (191, 255, 0)
elif result[i][2]*100 > 75 and result[i][2]*100 <= 100:
(r, g, b) = (85, 255, 0)
return (b, g, r)
# this needs to run only once to load the model into memory
imG = cv2.imread('1.jpg')
reader = easyocr.Reader(['en'], gpu=True)
result = reader.readtext(imG, detail=1)
# Creating document template
doc = docx.Document()
for i in range(len(result)):
topLeft = np.round(result[i][0][0]).astype(int) # [146, 134]
bottomRight = np.round(result[i][0][2]).astype(int) # [537, 268]
# if type(topLeft) == list or type(bottomRight) == list:
# # continue
# list_inttl = ''
# list_intbr = ''
# for j in topLeft:
# list_inttl = list_inttl+str(j)
# list_intbr = list_intbr+str(j)
# topLeft = int(list_inttl)
# bottomRight = int(list_intbr)
# drawing rectanlge
rect = cv2.rectangle(imG, topLeft, bottomRight, color(i), thickness=3)
# rect = cv2.rectangle(imG, topLeft, bottomRight, (255, 255, 0), thickness=3)
doc.add_paragraph(result[i][1])
doc.save('result_text.docx')
cv2.imshow('result', rect)
if os.path.exists(f'{os.getcwd()}/Dataset/labels.csv'):
labels = open(f'{os.getcwd()}/Dataset/labels.csv', 'r+', newline='')
lastLabel = 0
for line in csv.reader(labels):
pass
lastLabel = line[0]
i = len(lastLabel)-1
s = 0
j = 0
while (i >= 0):
if lastLabel[i].isdigit(): # if file is empty or there's just column names no entries
s += int(lastLabel[i])*(10**j)
j += 1
i -= 1
lastLabel = s
writerObj = csv.DictWriter(labels, ['filename', 'words'])
else:
labels = open(f'{os.getcwd()}/Dataset/labels.csv', 'a', newline='')
writerObj = csv.writer(labels)
writerObj.writerow(['filename', 'words'])
writerObj = csv.DictWriter(labels, ['filename', 'words'])
lastLabel = 0
# # caputering pressed key
k = cv2.waitKey(0)
if k == 27:
for i in range(len(result)):
# labeling
writerObj.writerow(
{'filename': f'{lastLabel+i+1}.jpg', 'words': result[i][1]})
# cropping image
topLeft = np.round(result[i][0][0]).astype(int)
bottomRight = np.round(result[i][0][2]).astype(int)
croppedImg = imG[topLeft[1]: bottomRight[1],
topLeft[0]: bottomRight[0]]
# saving cropped image
cv2.imwrite(f'{os.getcwd()}/Dataset/{lastLabel+i+1}.jpg',
croppedImg) # f-strings
# closing all opened windows and files
cv2.destroyAllWindows()
labels.close()
# cmd command to open ms word of result.docx
opt = input('Show document?(y or n)= ')
if opt == 'y':
os.system('cmd /c "start result_text.docx"')
else:
SystemExit()