Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenHuang2020 committed Jun 19, 2020
1 parent 9174d3f commit e5d682f
Show file tree
Hide file tree
Showing 30 changed files with 1,194 additions and 17 deletions.
File renamed without changes.
Binary file removed afterTraining/weights/trainFacialRecognition.h5
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1,003 changes: 1,003 additions & 0 deletions db/facial.csv

Large diffs are not rendered by default.

File renamed without changes.
106 changes: 106 additions & 0 deletions makeDB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import sys,os
import cv2
import numpy as np
import pandas as pd
from genLabel import loadImg,getImgHW,getFileName,getLabelFileLabels,pathsFiles

def distanceAB(a,b):
return np.sqrt(np.sum((a-b)**2))

def calculateRatio(pts,id1,id2,id3,id4):
assert(id1<len(pts))
assert(id2<len(pts))
assert(id3<len(pts))
assert(id4<len(pts))
return distanceAB(pts[id1], pts[id2])/distanceAB(pts[id3],pts[id4])

def calculateFeature(pts,H,W):
for i in range(len(pts)):
#print(pts[i][0],pts[i][1])
pts[i][0] = pts[i][0]*W
pts[i][1] = pts[i][1]*H
#print(pts)
pts = np.array(pts)
#print(pts.shape)

# print('pts24=',pts[24])
# print('pts18=',pts[18])
# print(pts[24][0],pts[18][0])

x = np.min([pts[24][0],pts[18][0]])
y = np.min([pts[24][1],pts[18][1]])
eyebrowCenter = np.array([x,y]) + np.abs(pts[24]-pts[18])/2
#print('eyebrowCenter=',eyebrowCenter)

#print(distanceAB(3,4))
#print(distanceAB(np.array([1,2]),np.array([4,6])))
#print(distanceAB(np.array([4,6]),np.array([1,2])))
F_Facial_Index = distanceAB(eyebrowCenter, pts[7])/distanceAB(pts[14],pts[0])
F_Mandibular_Index = calculateRatio(pts,66,7,4,10)

F_Intercanthal = calculateRatio(pts,37,45,32,27)
F_OrbitalWidth = calculateRatio(pts,27,29,37,45)
F_EyeFissure = calculateRatio(pts,28,30,27,29)
F_VermilionHeight = calculateRatio(pts,51,66,66,57)
F_MouthFaceWidth = calculateRatio(pts,48,54,0,14)
F_Noise1 = calculateRatio(pts,39,46,39,41)
F_Noise2 = calculateRatio(pts,39,40,39,41)
F_Noise3 = calculateRatio(pts,39,38,39,41)
F_Noise4 = calculateRatio(pts,39,38,67,41)
F_Noise5 = calculateRatio(pts,39,41,67,41)
#print('name:',name,'Features=',F_Facial_Index,F_Mandibular_Index,F_Intercanthal,F_OrbitalWidth,F_EyeFissure,F_VermilionHeight,F_MouthFaceWidth,F_Noise1,F_Noise2,F_Noise3,F_Noise4,F_Noise5)

return [F_Facial_Index,F_Mandibular_Index,F_Intercanthal,F_OrbitalWidth,F_EyeFissure,F_VermilionHeight,F_MouthFaceWidth,F_Noise1,F_Noise2,F_Noise3,F_Noise4,F_Noise5]

data = {'F_Facial_Index':F_Facial_Index, 'F_Mandibular_Index':F_Mandibular_Index,
'F_Intercanthal':F_Intercanthal,'F_OrbitalWidth':F_OrbitalWidth,'F_EyeFissure':F_EyeFissure,
'F_VermilionHeight':F_VermilionHeight,'F_MouthFaceWidth':F_MouthFaceWidth,'F_Noise1':F_Noise1,
'F_Noise2':F_Noise2,'F_Noise3':F_Noise3,'F_Noise4':F_Noise4,'F_Noise5':F_Noise5}
#df = pd.DataFrame(data=data,index=[0])
#print(df)
return data

dbFile = r'.\db\facial.csv'
def makeDb():
base = r'.\db\train\\'
base = os.path.abspath(base)
imgPath = base + r'\images'
LabelPath = base + r'\labels'

df = pd.DataFrame()
columns = ['F_Facial_Index', 'F_Mandibular_Index', 'F_Intercanthal',
'F_OrbitalWidth', 'F_EyeFissure', 'F_VermilionHeight',
'F_MouthFaceWidth', 'F_Noise1', 'F_Noise2', 'F_Noise3', 'F_Noise4',
'F_Noise5']

for i in pathsFiles(imgPath,'jpg'):
#print(i)
img = loadImg(i)
H,W = getImgHW(img)
fileName = getFileName(i)
fileName = fileName[:fileName.rfind('.')]

label = LabelPath + '\\' + fileName + '.pts'
pts = getLabelFileLabels(label)

print(fileName,'label=', label,'pts=', len(pts))
data = calculateFeature(pts,H,W)
data = np.array(data).reshape(-1,len(data))
line = pd.DataFrame(data,columns=columns)
line.insert(0, "Id", fileName, True)

df = df.append(line)

df.set_index(["Id"], inplace=True)
print(df.head())
print(df.columns)
df.to_csv(dbFile)

def getDb():
return pd.read_csv(dbFile)

def main():
makeDb()

if __name__ == '__main__':
main()
7 changes: 2 additions & 5 deletions afterTraining/predictKeyPoints.py → predictKeyPoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from commonModule.ImageBase import *
from genLabel import writeAnotationFile

def getImg(file):
return loadImg(file)

def argCmdParse():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source', help = 'source image')
Expand All @@ -27,6 +24,7 @@ def preditImg(img, modelName = r'./weights/trainFacialRecognition.h5'):
x = x.reshape((1,x.shape[0],x.shape[1],1))
print(x.shape)
pts = model.predict(x)
pts = pts.reshape((68,2))
return pts

def main():
Expand All @@ -35,7 +33,7 @@ def main():
dstFile = arg.dst
print(file,dstFile)

img = getImg(file)
img = loadImg(file)
pts = preditImg(img)
'''
print(img.shape)
Expand All @@ -51,7 +49,6 @@ def main():
pts = pts.reshape((68,2))
print('pts=',len(pts),pts.shape,pts)


writeAnotationFile(dstFile,pts) #'./res/myface_gray.pts'

if __name__=='__main__':
Expand Down
52 changes: 52 additions & 0 deletions predictPerson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os,sys
from commonModule.ImageBase import *
from predictKeyPoints import *
from makeDB import calculateFeature,getDb,distanceAB
from testLabel import testFaceLabelPredict,showimage

def argCmdParse():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source', help = 'source image')
#parser.add_argument('-d', '--dst', help = 'save iamge')
return parser.parse_args()

def predictPerson(feature):
df = getDb()
print(df.head())
#allFeatures = df.iloc[:,1:].values
#print('allFeatures.shape=',allFeatures.shape)
allDistance=[]
allIds = []
for i in range(df.shape[0]):
id = df.iloc[i,0]
i = df.iloc[i,1:].values
#print(id,i)
allIds.append(id)

dis = distanceAB(i,feature)
#print('dis=',dis)
allDistance.append(dis)

#print('allDistance=',allDistance)
disMin = min(allDistance)
minIndex = allDistance.index(disMin)
print('disMin=',disMin,'minIndex=',minIndex,'id=',allIds[minIndex])


def main():
arg = argCmdParse()
file = r'./res/001A29.jpg' #arg.source #

img = loadImg(file)
H,W = getImgHW(img)
pts = preditImg(img)
#print('pts=',len(pts),pts.shape,pts)

showimage(testFaceLabelPredict(file))

feature = calculateFeature(pts,H,W)
print('feature=',len(feature),feature)
predictPerson(feature)

if __name__=='__main__':
main()
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
43 changes: 31 additions & 12 deletions testLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
sys.path.append(r'.\afterTraining\\')

from genLabel import getLabelFileLabels
from afterTraining.predictKeyPoints import preditImg
from afterTraining.commonModule.imagePlot import plotImagList
from afterTraining.commonModule.ImageBase import changeBgr2Rbg
from predictKeyPoints import preditImg
from commonModule.imagePlot import plotImagList
from commonModule.ImageBase import changeBgr2Rbg

def argCmdParse():
parser = argparse.ArgumentParser()
Expand All @@ -34,11 +34,10 @@ def showimage(img,str='image',autoSize=False):
def writeImg(img,filePath):
cv2.imwrite(filePath,img)

def drawPointIndexImg(img, i,pt):
def drawPointIndexImg(img, i,pt,color = (255, 0, 0),fontsize=0.6):
font = cv2.FONT_HERSHEY_SIMPLEX
org =(int(pt[0]), int(pt[1]))
fontScale = 1
color = (255, 0, 0)
fontScale = fontsize
thickness = 1
return cv2.putText(img, str(i), org, font, fontScale, color, thickness, cv2.LINE_AA)

Expand All @@ -49,8 +48,14 @@ def drawPointImgFromPts(img,pts,color):
for i,pt in enumerate(pts):
img = drawPointImg(img,pt,color=color)

# if i%2:
# img = drawPointIndexImg(img,i,pt)
#img = drawPointIndexImg(img,i,pt,fontsize=0.5)
if 1:
if i<27:
img = drawPointIndexImg(img,i,pt,fontsize=0.5)
elif i<48:
img = drawPointIndexImg(img,i-27,pt,color=(0,255,0),fontsize=0.5)
else:
img = drawPointIndexImg(img,i-47,pt,color=(0,255,255),fontsize=0.5)
return img

def loadImg(file,mode=cv2.IMREAD_COLOR):
Expand All @@ -76,11 +81,11 @@ def getLabelFileLabels(fileLabel):

def testFaceLabelPredict(file):
img = loadImg(file)
pts = preditImg(img,modelName=r'.\afterTraining\weights\trainFacialRecognition.h5')
pts = preditImg(img)
#print(pts)
pts = pts.reshape((68,2))
#pts = pts.reshape((68,2))
return testFaceLabelPts(img,pts)

def testFaceLabel(file,label,color=(0,0,255),locCod=False): #locCod coordinats or ratio
img = loadImg(file)
return testFaceLabelImg(img,label,color=color,locCod=locCod)
Expand Down Expand Up @@ -122,7 +127,7 @@ def testFromDrawpt():

predictImg = changeBgr2Rbg(predictImg)
trueImg = changeBgr2Rbg(trueImg)
c1Img = changeBgr2Rbg(c1Img)
c1Img = changeBgr2Rbg(c1Img) #true and predict overlap

imgList,nameList = [],[]
imgList.append(predictImg),nameList.append('Predict')
Expand All @@ -136,8 +141,22 @@ def testFromDrawpt():
#showimage(c1Img)
#showimage(trueImg)

def drawPtsAndNumber():
file = r'.\afterTraining\res\\' + '001A29.jpg'
labelTrue = r'.\afterTraining\res\\' + '001A29_True2.pts'

#file = r'.\afterTraining\res\\' + '009A22a.jpg'
#labelTrue = r'.\afterTraining\res\\' + '009A22a.pts'

trueImg = testFaceLabel(file,labelTrue,locCod=False)
trueImg = changeBgr2Rbg(trueImg)
#showimage(trueImg)
imgList,nameList = [],[]
imgList.append(trueImg),nameList.append('trueImg')
plotImagList(imgList,nameList,showticks=False)

def main():
return drawPtsAndNumber()
return testFromDrawpt()

arg = argCmdParse()
Expand Down
Binary file added weights/trainFacialRecognition.h5
Binary file not shown.

0 comments on commit e5d682f

Please sign in to comment.