Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenHuang2020 committed Jun 21, 2020
1 parent e5d682f commit d232e73
Show file tree
Hide file tree
Showing 19 changed files with 1,258 additions and 1,062 deletions.
6 changes: 3 additions & 3 deletions CascadeClassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ def detecvFaceImgs(self,img): #return one

def getDetectImg(self,image):
faces = self.detecvFace(image)
print("Found {0} faces!".format(len(faces)))
#print("Found {0} faces!".format(len(faces)))
newImg = image.copy()
for (x, y, w, h) in faces:
print(x, y, w, h)
#print(x, y, w, h)
cv2.rectangle(newImg, (x, y), (x+w, y+h), (0, 255, 0), 2)

return newImg

def showDetectImg(self,image):
faces = self.detecvFace(image)
print("Found {0} faces!".format(len(faces)))
#print("Found {0} faces!".format(len(faces)))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
![Tensorflow Version](https://img.shields.io/badge/Tensorflow-V2.2.0-brightgreen)

<br/>
Facial key-points detection by using CNN model.
Facial key-points detection by using CNN model. Dataset:FG-Net

#### Keypoints regresiion
<br/>
<img src="images/Figure_3.png" width="320" height="240">
<img src="images/Figure_4.png" width="320" height="240">
<img src="images/Figure_5.png" width="320" height="240">
<img src="images/Figure_2.png" width="320" height="240">
4 changes: 4 additions & 0 deletions commonModule/ImageBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def getImagChannel(img):
return 3
return 1 #only one channel

def resizeImg(img,NewW,NewH):
rimg = cv2.resize(img, (NewW,NewH), interpolation=cv2.INTER_CUBIC) #INTER_CUBIC INTER_NEAREST INTER_LINEAR INTER_AREA
return rimg

def showimage(img,str='image',autoSize=False):
flag = cv2.WINDOW_NORMAL
if autoSize:
Expand Down
51 changes: 49 additions & 2 deletions commonModule/imagePlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,57 @@
#Steven 11/03/2020 image plot modoule

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from commonModule.common import getRowAndColumn

def plotImagList(imgList,nameList,gray=False,showticks=True):
def plotImagList2(imgList,nameList,gray=False,showTitle=True,showticks=True):
nImg = len(imgList)
nRow,nColumn = getRowAndColumn(nImg)

#f, axarr = plt.subplots(nRow, nColumn,gridspec_kw = {'wspace':0, 'hspace':0}) #
f, axarr = plt.subplots(nRow, nColumn, constrained_layout=True) #

gs1 = gridspec.GridSpec(nRow, nColumn)
gs1.update(wspace=0, hspace=0)

print(len(f.axes),nRow,nColumn)

for n in range(nImg):
ax = plt.subplot(gs1[n])
if 1:
img = imgList[n]

if showTitle:
ax.title.set_text(nameList[n])
if gray:
ax.imshow(img,cmap="gray")
else:
ax.imshow(img)

if not showticks:
ax.set_yticks([])
ax.set_xticks([])

ax.margins(0, 0)
#ax.xaxis.set_major_locator(plt.NullLocator())
#ax.yaxis.set_major_locator(plt.NullLocator())


#plt.grid(True)
plt.tight_layout(pad=0)
plt.subplots_adjust(wspace=0, hspace=0)
#plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
plt.show()

def plotImagList(imgList,nameList,gray=False,showTitle=True,showticks=True):
nImg = len(imgList)
nRow,nColumn = getRowAndColumn(nImg)

for n in range(nImg):
img = imgList[n]
ax = plt.subplot(nRow, nColumn, n + 1)
ax.title.set_text(nameList[n])
if showTitle:
ax.title.set_text(nameList[n])
if gray:
plt.imshow(img,cmap="gray")
else:
Expand All @@ -20,8 +61,14 @@ def plotImagList(imgList,nameList,gray=False,showticks=True):
if not showticks:
ax.set_yticks([])
ax.set_xticks([])

#ax.margins(0, 0)
#ax.xaxis.set_major_locator(plt.NullLocator())
#ax.yaxis.set_major_locator(plt.NullLocator())
#plt.grid(True)
plt.tight_layout()
#plt.subplots_adjust(wspace=0, hspace=0)
#plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
plt.show()

def main():
Expand Down
2,006 changes: 1,003 additions & 1,003 deletions db/facial.csv

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions faceIdentification.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def resizeImg(img,NewW,NewH):
#return cv2.resize(img, (int(h*ratio), int(w*ratio)), interpolation=cv2.INTER_CUBIC) #INTER_LANCZOS4
return cv2.resize(img, (NewW,NewH), interpolation=cv2.INTER_CUBIC) #INTER_CUBIC INTER_NEAREST INTER_LINEAR INTER_AREA

def CascadeDetect(cascPath=r'./res/haarcascade_frontalface_default.xml'):
return CascadeClassifier(cascPath)

def main():
cv2.useOptimized()
file = r'./res/obama.jpg'#r'./res/Lenna.png' #
Expand All @@ -25,11 +28,10 @@ def main():
if len(sys.argv)>1:
file = sys.argv[1]

cascPath=r'./res/haarcascade_frontalface_default.xml'

img = loadImg(file,mode=cv2.IMREAD_COLOR) # IMREAD_GRAYSCALE IMREAD_COLOR

faceROI = CascadeClassifier(cascPath)
faceROI = CascadeDetect()

faceR=faceROI.getDetectImg(img)
face = faceROI.detecvFaceImgOne(img)
Expand All @@ -43,9 +45,9 @@ def main():

plotImagList(ls,names)

faceGray = resizeImg(faceGray,newW,newH)
#faceGray = resizeImg(faceGray,newW,newH)
#img.writeImg(face,'./res/myface_.png')
writeImg(faceGray,'./res/myface_gray.png')
#writeImg(faceGray,'./res/myface_gray.png')

if __name__=="__main__":
main()
Expand Down
6 changes: 4 additions & 2 deletions genLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ def resizeImg(img,NewW,NewH,pts):
for i in pts:
x,y = i[0],i[1]
#newPts.append((round(x*NewW/w,4),round(y*NewH/h,4)))
#newPts.append((x*NewW/w, y*NewH/h)) #use location coordinates
newPts.append((x/w, y/h)) #use location/size ratio
if 0:
newPts.append((x*NewW/w, y*NewH/h)) #use location coordinates
else:
newPts.append((x/w, y/h)) #use location/size ratio
return rimg,newPts

newW=364
Expand Down
Binary file added images/Figure_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 44 additions & 23 deletions makeDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import cv2
import numpy as np
import pandas as pd
from genLabel import loadImg,getImgHW,getFileName,getLabelFileLabels,pathsFiles
from genLabel import loadImg,getImgHW,getFileName,getLabelFileLabels,pathsFiles,writeAnotationFile

def distanceAB(a,b):
#print('a.shape=',a.shape)
#print('b.shape=',b.shape)
return np.sqrt(np.sum((a-b)**2))
#return np.sqrt(np.sum(np.abs(a-b)))

def calculateRatio(pts,id1,id2,id3,id4):
assert(id1<len(pts))
Expand All @@ -14,12 +17,24 @@ def calculateRatio(pts,id1,id2,id3,id4):
assert(id4<len(pts))
return distanceAB(pts[id1], pts[id2])/distanceAB(pts[id3],pts[id4])

def calculateFeature(pts,H,W):
def UpdatePtsToLocation(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
return pts

def calculateFeature(pts,H,W,file=r'test.pts'):
pts = UpdatePtsToLocation(pts,H,W)

if 0:
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)

writeAnotationFile(file,pts)
pts = np.array(pts)
#print(pts.shape)

Expand All @@ -43,22 +58,23 @@ def calculateFeature(pts,H,W):
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)
# 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]
#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]
return [F_Facial_Index,F_Mandibular_Index,F_Intercanthal,F_OrbitalWidth,F_EyeFissure,F_VermilionHeight,F_MouthFaceWidth]

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
# 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():
Expand All @@ -67,11 +83,10 @@ def makeDb():
imgPath = base + r'\images'
LabelPath = base + r'\labels'

imgPath = r'E:\opencv\project\facialRecognition\db\recognitionDb'
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']
#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']
columns = ['F_Facial_Index', 'F_Mandibular_Index', 'F_Intercanthal','F_OrbitalWidth', 'F_EyeFissure', 'F_VermilionHeight','F_MouthFaceWidth']

for i in pathsFiles(imgPath,'jpg'):
#print(i)
Expand All @@ -83,10 +98,16 @@ def makeDb():
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)
pts = np.reshape(pts,(68,2))
print(fileName,'label=', label,'pts=', len(pts),'H,W=',H,W)
if 1:
data = calculateFeature(pts,H,W)
data = np.array(data).reshape(-1,len(data))
line = pd.DataFrame(data,columns=columns)
else:
pts = pts.reshape(1,136)
line = pd.DataFrame(pts)

line.insert(0, "Id", fileName, True)

df = df.append(line)
Expand Down
78 changes: 78 additions & 0 deletions plotLoss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#Steven 24/04/2020
import argparse
import sys
import matplotlib.pyplot as plt

#----------------------------------------------
#usgae: python plotloss.py .\facialRecognition.log
#----------------------------------------------

def getLoss(log_file,startIter=0,stopIter=None):
numbers = {'1','2','3','4','5','6','7','8','9'}
with open(log_file, 'r') as f:
lines = [line.rstrip("\n") for line in f.readlines()]

iters = []
loss = []
val_loss=[]
for line in lines:
trainIterRes = line.split(' ')
#print(line)
epoch = 0
if trainIterRes[0] == 'Epoch' and trainIterRes[1][-1:]!=':':
str = trainIterRes[1]
epoch = int(str[:str.find('/')])
#print(trainIterRes[1],epoch)
if(epoch<startIter):
continue
if stopIter and epoch > stopIter:
break

iters.append(epoch)

if trainIterRes[0] == '9/9' and trainIterRes[3] != 'ETA:':
print(line)
print(trainIterRes[7],trainIterRes[10])

loss.append(float(trainIterRes[7]))
val_loss.append(float(trainIterRes[10]))

return iters,loss,val_loss

def plotLoss(ax,iters,loss,label='',name='Training loss'):
#ax.set_title(name)
ax.plot(iters,loss,label=label)
ax.set_xlabel('Epoch')
ax.set_ylabel('Loss')
ax.legend()

def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('-l','--list', nargs='+', help='path to log file', required=True)
parser.add_argument('-s', '--start', help = 'startIter')
parser.add_argument('-t', '--stop', help = 'stopIter')

args = parser.parse_args()
startIter = 0
stopIter = None
if args.start:
startIter = int(args.start)
if args.stop:
stopIter = int(args.stop)

print(args.list,startIter,stopIter)

ax = plt.subplot(1,1,1)
file = args.list[0]
iters,loss,val_loss = getLoss(file,startIter,stopIter)

plotLoss(ax,iters,loss,label='On train set')
plotLoss(ax,iters,val_loss,label='On validation set')
#plt.ylim(0, 4)
plt.yscale("log")
plt.legend()
plt.show()

if __name__ == "__main__":
main(sys.argv)

3 changes: 2 additions & 1 deletion predictKeyPoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def argCmdParse():

return parser.parse_args()

def preditImg(img, modelName = r'./weights/trainFacialRecognition.h5'):
def preditImg(img, modelName = r'./weights/trainFacialRecognition.h5'): # #r'./weightLoc/trainFacialRecognition.h5'
model = ks.models.load_model(modelName)
print(img.shape)
x = img[:,:,0]
Expand All @@ -25,6 +25,7 @@ def preditImg(img, modelName = r'./weights/trainFacialRecognition.h5'):
print(x.shape)
pts = model.predict(x)
pts = pts.reshape((68,2))
print('preditImg pts.shape=',type(pts),pts.shape)
return pts

def main():
Expand Down
Loading

0 comments on commit d232e73

Please sign in to comment.