-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgtotext.py
46 lines (37 loc) · 1.16 KB
/
imgtotext.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
import numpy as np
import cv2
import tensorflow as tf
from wordsegment import load, segment
from ocrModelLoader import abc,alphabat,number
from charsegment import charsegment
class_names = ['0','1']
def predict_alpa(img):
for i in range (len(alphabat)):
pred = alphabat[i].predict(img)
result=class_names[np.argmax(pred)]
if result == "1":
return chr(97+i)
return ""
def predict_num(img):
for i in range (len(number)):
pred = number[i].predict(img)
result=class_names[np.argmax(pred)]
if result == "1":
return str(i)
return ""
def img_to_text(imgdata):
text = ""
for img in imgdata:
dim = (16,16)
test = cv2.resize(img, dim, interpolation =cv2.INTER_AREA)
test = (np.expand_dims(test,0))
test = test /255.0
pred = abc.predict(test)
result=class_names[np.argmax(pred)]
if result == "1":
word = predict_alpa(test)
text = text + word
else:
word = predict_num(test)
text = text + word
return text