-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtutorials.py
153 lines (114 loc) · 5.08 KB
/
tutorials.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from ocr_tamil.ocr import OCR
ocr = OCR()
# For a single image - text recognize
image_path = r"test_images\1.jpg" # insert your own path here
text_list = ocr.predict(image_path)
print("Single text recognize",text_list)
with open("outputs\output_text_recognize_single_image.txt","w",encoding="utf-8") as f:
for text in text_list:
f.write(text + " ")
# For multiple image - text recognize
image_path = [r"test_images\1.jpg",r"test_images\2.jpg",
r"test_images\3.jpg",r"test_images\4.jpg",
r"test_images\5.jpg",r"test_images\6.jpg",
r"test_images\7.jpg",r"test_images\8.jpg",
r"test_images\9.jpg",r"test_images\10.jpg",
r"test_images\10_180.jpg",r"test_images\11.jpg",
r"test_images\14.jpg"] # insert your own path here
text_list = ocr.predict(image_path)
print("Multiple text recognize",text_list)
with open("outputs\output_text_recognize_multiple_image.txt","w",encoding="utf-8") as f:
for text in text_list:
f.write(text + "\n")
ocr = OCR(detect=True)
# For single image - text detect + text recognize
image_path = r"test_images\0.jpg" # insert your own path here
text_list = ocr.predict(image_path)
print("Single text detect recognize",text_list)
with open("outputs\output_text_detect_recognize_single_image.txt","w",encoding="utf-8") as f:
for item in text_list:
for text in item:
f.write(text + " ")
f.write("\n")
ocr = OCR(detect=True,lang=["tamil"])
image_path = r"test_images\tamil_handwritten.jpg" # insert your own path here
text_list = ocr.predict(image_path)
print("Single text detect recognize",text_list)
with open("outputs\handwritten_image.txt","w",encoding="utf-8") as f:
for item in text_list:
for text in item:
f.write(text + " ")
f.write("\n")
# For multiple image - text detect + text recognize
ocr = OCR(detect=True)
image_path = [r"test_images\0.jpg",r"test_images\tamil_sentence.jpg",
r"test_images\tamil_sentence_1.png",
r"test_images\tamil_handwritten.jpg",
r"test_images\tamil_handwritten_1.jpg"] # insert your own path here
text_list = ocr.predict(image_path)
print("Multiple text detect recognize",text_list)
with open("outputs\output_text_detect_recognize_multiple_image.txt","w",encoding="utf-8") as f:
for item in text_list:
for text in item:
f.write(text + " ")
f.write("\n")
## For the details of 1
ocr = OCR(details=1)
# For a single image - text recognize
image_path = r"test_images\1.jpg" # insert your own path here
text_list = ocr.predict(image_path)
print("Single text recognize with confidence",text_list)
# For multiple image - text recognize
image_path = [r"test_images\1.jpg",r"test_images\2.jpg"] # insert your own path here
text_list = ocr.predict(image_path)
print("Multiple text recognize",text_list)
ocr = OCR(detect=True,details=1)
# For single image - text detect + text recognize
image_path = r"test_images\0.jpg" # insert your own path here
text_list = ocr.predict(image_path)
print("Single text detect recognize with conf",text_list)
# For multiple image - text detect + text recognize
image_path = [r"test_images\0.jpg",r"test_images\tamil_sentence.jpg"] # insert your own path here
text_list = ocr.predict(image_path)
print("Multiple text detect recognize with conf",text_list)
## For the details of 2
ocr = OCR(detect=True,details=2)
# For single image - text detect + text recognize
image_path = r"test_images\0.jpg" # insert your own path here
text_list = ocr.predict(image_path)
print("Single text detect recognize with confidence and bbox",text_list)
# For multiple image - text detect + text recognize
image_path = [r"test_images\0.jpg",r"test_images\tamil_sentence.jpg"] # insert your own path here
text_list = ocr.predict(image_path)
print("Multiple text detect recognize with confidence and bbox",text_list)
image_path = [r"test_images\tamil_handwritten.jpg"] # insert your own path here
text_list = ocr.predict(image_path)
print("Multiple text detect recognize with confidence and bbox",text_list)
with open(r"outputs\formatted_handwritten.txt","w",encoding="utf-8") as f:
for item in text_list:
current_line = 1
for info in item:
text,conf,bbox = info
line = bbox[1]
if line == current_line:
f.write(text + " ")
else:
f.write("\n"+text+ " ")
current_line = line
f.write("\n")
# For multiple image - text detect + text recognize
image_path = [r"test_images\tamil_sentence_2.jpg"] # insert your own path here
text_list = ocr.predict(image_path)
print("Multiple text detect recognize with confidence and bbox",text_list)
with open(r"outputs\english_tamil_combined.txt","w",encoding="utf-8") as f:
for item in text_list:
current_line = 1
for info in item:
text,conf,bbox = info
line = bbox[1]
if line == current_line:
f.write(text + " ")
else:
f.write("\n"+text+ " ")
current_line = line
f.write("\n")