作为最后一年项目的一部分,我编写了一个程序,该程序接收图像,对其进行分析,并将其与数据字典进行比较。最终,程序会打印出图片中出现的人的姓名。然而,该计划似乎在承认方面犯了相当多的错误,因此我的求助和/或想法的请求。
它是如何运作的?
首先,4张特定男性/女性的照片(奥巴马,特朗普,希拉里克林顿......)(每张有4张照片)都是进口的。使用Pil,程序然后分析每个图片的像素,不同的函数为每张图片产生不同的“属性”:
此时,名为“Massavg”的函数会计算这3个属性的平均值。 (换句话说,此时每张照片都由3维常数[x,y,z]表示,这是3个属性的平均值。)
然后,对于每个人(例如奥巴马),一个函数计算导入的不同照片的平均值。
我还创建了一个“Faces词典”,看起来像这样:
facesdictionary= {'trump': ([151.60, 107.10, 89.58],31.53) , 'obama': ([121.53, 84.71, 65.4],24.60), 'willsmith': ([118.70, 78.91, 54.58],27.26),'clinton':([137.78, 107.65, 86.53],23.69)}
称为“面部识别”的最终功能然后接收照片并立即计算到字典中每个条目的距离。然后打印“Key”,其值是最接近的匹配。
正如我所说,承认不是很好,并且犯了很多错误。我非常希望听到有关更多属性的更多想法。
附件是代码。非常感谢你!
from PIL import Image
import math
import numpy as np
humansdb = open('HumansDB.txt', "ab+")
img =Image.open( './Obama.jpg' )
trump =Image.open( './Trump.jpg' )
clinton = Image.open( './Clinton.jpg' )
boyega = Image.open( './Boyega.jpg' )
obama2 = Image.open( './Obama2.jpg' )
obama3 = Image.open( './Obama3.jpg' )
obama4 = Image.open( './Obama4.jpg' )
obama5 = Image.open( './Obama5.jpg' )
obamaface = Image.open( './ObamaFace.jpg' )
obamacopy = Image.open( './ObamaCopy.jpg' )
obamatest = Image.open( './ObamaTest.jpg' )
obamalaugh = Image.open( './obamatestlaugh.png' )
obamatest2 = Image.open( './ObamaTest2.jpg' )
obamatest3 = Image.open( './ObamaTest3.jpg' )
obamatest4 = Image.open( './ObamaTest4.jpg' )
willsmith = Image.open( './WillSmith.jpg' )
willsmith2 = Image.open( './WillSmith2.jpg' )
willsmith3 = Image.open( './WillSmith3.jpg' )
willsmith4 = Image.open( './WillSmith4.jpg' )
trump2 = Image.open( './Trump2.jpg' )
trump3 = Image.open( './Trump3.jpg' )
trump4 = Image.open( './Trump4.jpg' )
trumpstranger = Image.open( './trumpstranger.jpg' )
trumpstranger2 = Image.open( './trumpstranger2.jpg' )
willsmithstranger = Image.open( './Willsmithstranger.jpg' )
clinton2 = Image.open( './Clinton2.jpg' )
clinton3 = Image.open( './Clinton3.jpg' )
clinton4 = Image.open( './Clinton4.jpg' )
clinton5 = Image.open( './Clinton5.jpg' )
clintontest = Image.open( './ClintonTest.jpg' )
someone = Image.open( './someone.jpg' ) #will smith
someone2 = Image.open( './someone2.jpg' )#trump
someone3 = Image.open( './someone3.jpg' )#clinton
ninecolors = Image.open( './NineColors.jpg' )
someone4 = Image.open( './someone4.jpg' )#clinton
someone5 = Image.open( './someone5.jpg' )#trump
#PuttingPixels
'''
width, height = img.size
def putpixels(r,g,b):
for x in range(width):
for y in range(height):
img.putpixel((x,y),(r,g,b))
'''
#Average color attribution:
def averagecolor(wstart,wend,hstart,hend,image):
redcolor = []
bluecolor = []
greencolor = []
for x in range(wstart,wend):
for y in range(hstart,hend):
r,g,b = image.getpixel((x,y))
redcolor.append(r)
greencolor.append(g)
bluecolor.append(b)
red = sum(redcolor)/(len(redcolor))
green = sum(greencolor)/(len(greencolor))
blue = sum(bluecolor)/(len(bluecolor))
colorattribution = [red,green,blue]
return colorattribution
#obamavgcolor = averagecolor(0,width,0,height,img)
#Differnce Between up and down color attribution:
def differenceupdown(image):
width, height = image.size
avgdown = averagecolor(0,width,0,int(0.5*height),image)
avgup = averagecolor(0,width,int(0.5*height),height,image)
differenceupdown = [abs(avgdown[i]-avgup[i]) for i in range(0,3)]
return differenceupdown
#obamadifference = differenceupdown(img)
#Difference Between right and left color attribution:
def differencerightleft(image):
width, height = image.size
avgright = averagecolor(0,int(0.5*width),0,height,image)
avgleft = averagecolor(int(0.5*width),width,0,height,image)
differencerightleft = [abs(avgright[i]-avgleft[i]) for i in range(0,3)]
return differencerightleft
# NEW 12/06/2016 - 9 More Attributes, Dividing the picture into 9 parts and Calculating the AVG color for each part
def NinePartsAvgColors(image):
width, height = image.size
avg1 = averagecolor(0,int((1/3.0)*width),0,int((1/3.0)*height),image)
avg2 = averagecolor(int((1/3.0)*width),int((2/3.0)*width),0,int((1/3.0)*height),image)
avg3 = averagecolor(int((2/3.0)*width),width,0,int((1/3.0)*height),image)
avg4 = averagecolor(0,int((1/3.0)*width),int((1/3.0)*height),int((2/3.0)*height),image)
avg5 = averagecolor(int((1/3.0)*width),int((2/3.0)*width),int((1/3.0)*height),int((2/3.0)*height),image)
avg6 = averagecolor(int((2/3.0)*width),width,int((1/3.0)*height),int((2/3.0)*height),image)
avg7 = averagecolor(0,int((1/3.0)*width),int((2/3.0)*height),height,image)
avg8 = averagecolor(int((1/3.0)*width),int((2/3.0)*width),int((2/3.0)*height),height,image)
avg9 = averagecolor(int((2/3.0)*width),width,int((2/3.0)*height),height,image)
return avg1,avg2,avg3,avg4,avg5,avg6,avg7,avg8,avg9
#print NinePartsAvgColors(obama3)
#Calculating 3 dimensional average:
def massavg(image):
width, height = image.size
imgavgcolor = averagecolor(0,width,0,height,image)
diffupdown = differenceupdown(image)
diffrightleft = differencerightleft(image)
nine = NinePartsAvgColors(image)
massaverage = [float(imgavgcolor[i]+diffupdown[i]+diffrightleft[i]+nine[0][i]+nine[1][i]+nine[2][i]+nine[3][i]+nine[4][i]+nine[5][i]+nine[6][i]+nine[7][i]+nine[8][i])/12 for i in range(0,3)]
return massaverage
#widthtest, heighttest = trump2.size
#print averagecolor(0,widthtest,0,heighttest,trump2),differenceupdown(trump2),differencerightleft(trump2),NinePartsAvgColors(trump2)
def spacedistance((x1,y1,z1),(x2,y2,z2)):
xd = x2-x1
yd = y2-y1
zd = z2-z1
distance = math.sqrt(xd**2+yd**2+zd**2)
return distance
#print spacedistance(massavg(img),massavg(trump))
def avg_photos_mass(list_of_mass_averages):
stdphotos = np.std(list_of_mass_averages)
length = len(list_of_mass_averages)
onedimension = [list_of_mass_averages[i][0] for i in range(0,length)]
onedimensionavg = sum(onedimension)/float(len(onedimension))
twodimension = [list_of_mass_averages[i][1] for i in range(0,length)]
twodimensionavg = sum(twodimension)/float(len(twodimension))
threedimension = [list_of_mass_averages[i][2] for i in range(0,length)]
threedimensionavg = sum(threedimension)/float(len(threedimension))
avg_photos = []
avg_photos.append(onedimensionavg)
avg_photos.append(twodimensionavg)
avg_photos.append(threedimensionavg)
return avg_photos,stdphotos
#print avg_photos_mass([[1,2,3],[4,5,6],[7,8,9],[10,11,18]])
#print spacedistance(obamavg,massavg(boyega))
#print avg_photos_mass([[1,2,3],[4,5,6],[7,8,9]])[1]
'''
humansdb.write("\nhellooooo")
humansdb.close()
humansdb = open('HumansDB.txt', "r")
for line in humansdb.readlines():
print line
humansdb.close()
'''
def Ultimate_3d_avg(listofphotos):
averagesofphotos = map(massavg,listofphotos)
point = avg_photos_mass(averagesofphotos)[0]
std = avg_photos_mass(averagesofphotos)[1]
return point,std
#Calculating People's Averages:
'''
trumpavg = Ultimate_3d_avg([trump,trump2,trump3,trump4])[0]
print trumpavg
willsmithavg = Ultimate_3d_avg([willsmith,willsmith2,willsmith3,willsmith4])[0]
print willsmithavg
obamavg = Ultimate_3d_avg([img,obama2,obama3,obama4,obama5])[0]
print obamavg
clintonavg = Ultimate_3d_avg([clinton,clinton2,clinton3,clinton4,clinton5])[0]
print clintonavg
'''
#Calculating People's STD:
'''
willsmithstd = Ultimate_3d_avg([willsmith,willsmith2,willsmith3,willsmith4])[1]
print willsmithstd
obamastd = Ultimate_3d_avg([img,obama2,obama3,obama4,obama5])[1]
print obamastd
trumpstd = Ultimate_3d_avg([trump,trump2,trump3,trump4])[1]
print trumpstd
clintonstd = Ultimate_3d_avg([clinton,clinton2,clinton3,clinton4,clinton5])[1]
print clintonstd
'''
facesdictionary= {'trump': ([151.60, 107.10, 89.58],31.53) , 'obama': ([121.53, 84.71, 65.4],24.60), 'willsmith': ([118.70, 78.91, 54.58],27.26),'clinton':([137.78, 107.65, 86.53],23.69)}
#print facesdictionary.items()
def facial_recognition(imageofsomething):
imageavg = massavg(imageofsomething)
database = facesdictionary.items()
minkey = database[0][0]
bestmatch = (spacedistance(imageavg,database[0][1][0]))/(database[0][1][1]) # Distance/STD
for i in range(0,len(database)):
distance = (spacedistance(imageavg,database[i][1][0]))/(database[i][1][1])
if(distance<bestmatch):
bestmatch = distance
minkey = database[i][0]
return minkey,bestmatch
print facial_recognition(someone5)
#print massavg(trump),massavg(trump2),massavg(trump3),massavg(trump4)