-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredict.py
54 lines (42 loc) · 1.22 KB
/
predict.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
from PIL import Image
import os
import math
import time
import segmenter
import preprocessor
import pickle
class VectorCompare:
def magnitude(self,concordance):
total = 0
for word,count in concordance.iteritems():
total += count ** 2
return math.sqrt(total)
def relation(self,concordance1, concordance2):
relevance = 0
topvalue = 0
for word, count in concordance1.iteritems():
if concordance2.has_key(word):
topvalue += count * concordance2[word]
return topvalue / (self.magnitude(concordance1) * self.magnitude(concordance2))
def buildvector(im):
d1 = {}
count = 0
for i in im.getdata():
d1[count] = i
count += 1
return d1
v = VectorCompare()
imageset = pickle.load(open("./imgdata.data","r"))
def predict(im):
im = preprocessor.process_image(im)
segments = segmenter.segment_image(im)
output = ""
for i in segments:
guess = []
for image in imageset:
for x,y in image.iteritems():
if(len(y)!=0):
guess.append((v.relation(y[0],buildvector(i)), x))
guess.sort(reverse=True)
output += str(guess[0][1])
return output