forked from OlympicCode/vHackXTBot-Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocr.py
More file actions
Latest commit
86 lines (78 loc) · 1.94 KB
/
Copy pathocr.py
File metadata and controls
86 lines (78 loc) · 1.94 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
fromPILimportImage
fromioimportBytesIO
importbase64
importsys
importdifflib
classOCR:
defanalyze(self, im):
#im = Image.open(name)
pix=im.load()
l= []
fori1inrange(0,im.size[0]):
count=0
fori2inrange (0, im.size[1]):
ifpix[i1,i2] !=0:
count+=1
l.append(count)
returnl
defsplitnumbers(self, l):
nrs= []
i1=0
whilei1<len(l):
ifl[i1] >3:
temp= []
add=0
fori2inrange(0,9):
ifnotl[i1+i2] <4:
temp.append(l[i1+i2])
else:
add=i2
break
ifadd==0:
add=8
i1+=add
nrs.append(temp)
i1+=1
returnnrs
defreadit(self, l):
nrs= []
fori1inl:
ratio=0.0
current=0
fori2inrange(0,len(self.nrlist)):
sm=difflib.SequenceMatcher(None,i1,self.nrlist[i2])
trat=sm.ratio()
iftrat>ratio:
ratio=trat
current=i2
nrs.append(current)
returnnrs
defrightSolution(self, nr, possies):
current=0
ratio=0.0
fori1inrange(0,len(possies)):
temp= []
fori2inpossies[i1]:
temp.append(int(i2))
sm=difflib.SequenceMatcher(None,nr,temp)
trat=sm.ratio()
iftrat>ratio:
ratio=trat
current=i1
return"p"+str(current+1)
defbase64toImage(self, string):
im=Image.open(BytesIO(base64.b64decode(string)))
returnim
defgetSolution(self, response):
string=response.split(',')[0].split('{"img":"')[1].split('"')[0]
possies= []
fori1inrange(1,7):
possies.append(response.split(',')[i1].split(':')[1])
im=self.base64toImage(string)
l=self.analyze(im)
l=self.splitnumbers(l)
l=self.readit(l)
s=self.rightSolution(l, possies)
returns
def__init__(self):
self.nrlist= [[13,14,15,8,6,7,15,15,12],[4,4,15,15,15],[9,12,13,11,14,14,13,9],[7,9,9,10,10,15,15,14],[5,8,8,11,10,12,15,15],[12,12,11,9,9,13,13,12],[13,15,10,9,9,12,14,11],[4,11,13,15,9,6,4],[14,15,12,9,10,15,15,14],[10,13,14,10,11,15,15,14]]