-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_documented.py
More file actions
140 lines (110 loc) · 4.03 KB
/
main_documented.py
File metadata and controls
140 lines (110 loc) · 4.03 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
# , recog_network='best_accuracy'
imG = cv2.imread('test case.jpg')
reader = easyocr.Reader(['en'], gpu=True,)
result = reader.readtext(imG, detail=1)
# for this image, result is a list containing 2 tuples. each for detected text
# tuples contain (Coordinates,Text,Confidence)
#result=[([[146, 134], [537, 134], [537, 268], [146, 268]], 'Sample', 0.9549119391612874), ([[442, 400], [516, 400], [516, 430], [442, 430]], ' ;', 0.1835381164882384)]
# coordinates are line start and end points
# cv2.line(image, start_point, end_point, color, thickness)
# ([[top left-x,y],top right[x,y],bottom right[x,y],bottom left-[x,y]],'Sample',0.9549119391612874)
# 4th quadrant
# 0--------------255
# |TL TR
# |
# |
# |BL BR
# 255
# line = cv2.line(imG, (146, 134), (537, 134), (255, 0, 0), 3)
# cv2.imshow('result', line)
# k = cv2.waitKey(0)
# cv2.destroyAllWindows()
# Creating document template
doc = docx.Document()
for i in range(len(result)-3):
topLeft = result[i][0][0] # [146, 134]
bottomRight = result[i][0][2] # [537, 268]
# drawing rectanlge
rect = cv2.rectangle(imG, topLeft,
bottomRight, color(i), 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)-3):
# labeling
writerObj.writerow(
{'filename': f'{lastLabel+i+1}.jpg', 'words': result[i][1]})
# cropping image
# roi = image[startY:endY, startX:endX]
# https://pyimagesearch.com/2021/01/19/crop-image-with-opencv/
topLeft = result[i][0][0]
bottomRight = result[i][0][2]
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()
# Once in the directory containing the document, use the start command to start the document in Windows.
# For example, if the document is called "example.doc," you would type the following command.
# start example.doc
# -------------Cmd Command execution---------------
# (1) CMD /K – execute a command and then remain:
# import os
# os.system('cmd /k "Your Command Prompt Command"')
# (2) CMD /C – execute a command and then terminate:
# import os
# os.system('cmd /c "Your Command Prompt Command"')
opt = input('Show document?(y or n)= ')
if opt == 'y':
os.system('cmd /c "start result_text.docx"')
else:
SystemExit()