-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisAgent.py
81 lines (58 loc) · 3.69 KB
/
VisAgent.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
import time
from PIL import Image, ImageChops, ImageOps, ImageStat, ImageFilter
import math, operator
class VisAgent:
def Solve(self, problem, answers):
problem_figures={}
transforms={}
for figureName in problem.figures:
figure = problem.figures[figureName]
image = Image.open(figure.visualFilename).convert('L')
problem_figures[figureName] = ImageOps.invert(image).filter(ImageFilter.GaussianBlur(3))
scores={}
row_scores ={}
col_scores={}
avg_score={}
for x in answers:
candidate = problem_figures[str(x)]
if problem.problemType=='3x3':
transforms['CF'] = self.generateTransform(problem_figures['C'], problem_figures['F'])
transforms['GH'] = self.generateTransform(problem_figures['G'], problem_figures['H'])
transforms['AB'] = self.generateTransform(problem_figures['A'], problem_figures['B'])
transforms['AD'] = self.generateTransform(problem_figures['A'], problem_figures['D'])
transforms['BC'] = self.generateTransform(problem_figures['B'], problem_figures['C'])
transforms['DE'] = self.generateTransform(problem_figures['D'], problem_figures['E'])
transforms['BE'] = self.generateTransform(problem_figures['B'], problem_figures['E'])
transforms['DG'] = self.generateTransform(problem_figures['D'], problem_figures['G'])
transforms['EF'] = self.generateTransform(problem_figures['E'], problem_figures['F'])
transforms['EH'] = self.generateTransform(problem_figures['E'], problem_figures['H'])
transforms['F'+ str(x)] = self.generateTransform(problem_figures['F'], candidate)
transforms['H'+ str(x)] = self.generateTransform(problem_figures['H'], candidate)
row_scores[x]=(transforms['AB']+transforms['BC'] )- (transforms['DE']+transforms['EF'] )\
-(transforms['DE']+transforms['EF']-
(transforms['GH']+transforms['H'+ str(x)] ))
col_scores[x]=(transforms['AD']+transforms['DG'] )\
- (transforms['BE']+transforms['EH'] )\
-(transforms['CF']+transforms['F'+ str(x)])
scores[x]= abs(((transforms['CF']-transforms['F'+ str(x)])+\
(transforms['GH']-transforms['H'+ str(x)])))/2
avg_score[x]=(abs(row_scores[x]+scores[x]))/2
if problem.problemType=='2x2':
transforms['AB'] = self.generateTransform(problem_figures['A'], problem_figures['B'])
transforms['AC'] = self.generateTransform(problem_figures['A'], problem_figures['B'])
transforms['C'+ str(x)] = self.generateTransform(problem_figures['C'], candidate)
transforms['B'+ str(x)] = self.generateTransform(problem_figures['B'], candidate)
row_scores[x]=(transforms['AB'])- transforms['C'+ str(x)]
col_scores[x]=(transforms['AC']-transforms['B'+ str(x)])
scores[x]= abs(transforms['B'+ str(x)])+ abs(transforms['C'+ str(x)])/2
avg_score[x]=(abs(row_scores[x]+scores[x]))/2
return min(scores, key=scores.get)
def generateTransform(self, srcImage, destImage):
return self.rmsdiff(srcImage, destImage)
def rmsdiff(self, im1, im2):
"Calculate the root-mean-square difference between two images"
h = ImageChops.difference(im2, im1).histogram()
# calculate rms
return math.sqrt(reduce(operator.add,
map(lambda h, i: h*(i**2), h, range(256))
) / (float(im2.size[0]) * im2.size[1]))