Agent.py - from PIL import Image from PIL import ImageMath...

This preview shows page 1 - 4 out of 23 pages.

from PIL import Imagefrom PIL import ImageMathfrom PIL import ImageOpsfrom PIL import ImageChopsfrom PIL import ImageDrawimport numpy as np# Your Agent for solving Raven's Progressive Matrices. You MUST modify this file.## You may also create and submit new files in addition to modifying this file.## Make sure your file retains methods with the signatures:# def __init__(self)# def Solve(self,problem)## These methods will be necessary for the project's main method to run.# Install Pillow and uncomment this line to access image processing.#from PIL import Image#import numpyclass Agent:# The default constructor for your Agent. Make sure to execute any# processing necessary before your Agent starts solving problems here.## Do not add any variables to this signature; they will not be used by# main().def __init__(self):passdef similarity_score(self,A,B):A = A.convert('1')B = B.convert('1')diff = ImageChops.difference(A,B)pixels = diff.getdata()#print type(pixels)whitepixelcount = 0for pixel in pixels:if pixel != 0:whitepixelcount += 1totalpixels = len(pixels)#print 'whitePixelCount' , whitePixelCount#print 'totalPixels' ,totalPixels#print 'whitePixelCount/float(totalPixels)', whitePixelCount/float(totalPixels)score = 100 - 100*(whitepixelcount/float(totalpixels))#print 'score', scorereturn score def sse(self, A,B):'''#A[A == 255] = 1#B[B == 255] = 1A *= 1.0/A.max() B *= 1.0/B.max() intersect = np.sum(np.multiply(A,B))print intersect#print np.amax(intersect)union = np.sum(np.maximum(A,B))print uniona_minus_b = np.sum(np.subtract(A,B))
b_minus_a = np.sum(np.subtract(B,A))print a_minus_bprint b_minus_aA_equals_B = intersect/union #alpha and beta =1A_subset_B = intersect/float((intersect + a_minus_b)) #alpha=1, beta =0 ie A is a subset of BB_subset_A = intersect/float((intersect + b_minus_a)) #alpha=0, beta =1 ie B is a subset of Aprint A_equals_B, A_subset_B, B_subset_A'''A = np.array(A,dtype='float64')B = np.array(B,dtype='float64')err = np.sum((A-B) ** 2)err /= float(A.shape[0] * B.shape[1])return errdef get_pixel_ratio(self,A):A = np.array(A)whitepixelcount = 0blackpixelcount = 0for i in range(A.shape[0]):for j in range(A.shape[1]):if(A[i,j]!=0):whitepixelcount += 1else:blackpixelcount +=1return blackpixelcount/float(whitepixelcount)def weightage_metrics(self, dark_pixel, intersection_pixel,weightage):return float((dark_pixel*weightage)) + float((intersection_pixel*(1-weightage)))def check_transformation(self, A,B):#identity,mirror(vertical flip),flip(horizontal),rot90,rot180,rot270scores = []scores_new = []#check identityscores.append(self.sse(A,B)*0.4)#mirrorA_mirror = A.transpose(Image.FLIP_LEFT_RIGHT)#A_mirror.save("./Problems/Basic Problems B/Basic Problem B-05/transformed.png")#print 'mirror diff ', self.sse(A_mirror,B)scores.append(self.sse(A_mirror,B)*0.6)#flipA_flip = A.transpose(Image.FLIP_TOP_BOTTOM)scores.append(self.sse(A_flip,B)*0.8)
#rotate 90A_90 = A.transpose(Image.ROTATE_90)scores.append(self.sse(A_90,B)*1.5)#rotate 180A_180 = A.rotate(180)scores.append(self.sse(A_180,B)*1.5)return scores.index(min(scores))def get_transformation(self,C, transform_res):if transform_res == 0:return Celif transform_res == 1:C = C.transpose(Image.FLIP_LEFT_RIGHT)#C = C.convert('1')#C.save("./Problems/Basic Problems B/Basic Problem B-05/C_transformed.png")

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture