Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenHuang2020 committed Jun 22, 2020
1 parent 87f431d commit c2800d1
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 31 deletions.
94 changes: 75 additions & 19 deletions CascadeClassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,87 @@ def detecvFace(self,img):
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
minSize=(20, 20)
)
return faces

def detecvFaceImgOne(self,img): #return one
def getNewLocFromDetection(self,centerX,centerY,img,reSize=None):
newW = reSize[0]
newH = reSize[1]
H,W = img.shape[0],img.shape[1]
if newW>W:
newW = W
if newH>H:
newH = H

newX = int(centerX-newW/2)
newY = int(centerY-newH/2)
if newX<0:
newX = 0
if newY<0:
newY = 0
return newX,newY,newW,newH
'''
def getNewLocFromDetection(self,x,y,w,h,img,reSize=None):
if reSize is None:
return x,y,w,h
centerX = int(x + w/2)
centerY = int(y + h/2)
newW = reSize[0]
newH = reSize[1]
H,W = img.shape[0],img.shape[1]
if newW>W:
newW = W
if newH>H:
newH = H
newX = int(centerX-newW/2)
newY = int(centerY-newH/2)
if newX<0:
newX = 0
if newY<0:
newY = 0
return newX,newY,newW,newH
'''

def detecvFaceImgOne(self,img,reSize=None): #return one
faces = self.detecvFace(img)
if len(faces)>0:
(x, y, w, h) = faces[-1]
for (x, y, w, h) in faces:
#return img[x:x+w,y:y+h]
return img[y:y+h,x:x+w]
#print(x, y, w, h)
if reSize is None:
return img[y:y+h,x:x+w]
else:
centerX = int(x + w/2)
centerY = int(y + h/2)
#print('center=',centerX,centerY)
newX,newY,newW,newH = self.getNewLocFromDetection(centerX,centerY,img,reSize)
#print('changesize=',newW,newH,reSize[0],reSize[1])
return img[newY:newY+newH, newX:newX+newW]

print('Warning!,No detection of face!')
return None
# for (x, y, w, h) in faces:
# print(x,y,w,h)
# #return img[x:x+w,y:y+h].copy()
# return img[x:x+w,y:y+h]

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

centerX = int(x + w/2)
centerY = int(y + h/2)
print('center=',centerX,centerY)
x,y,w,h = self.getNewLocFromDetection(centerX,centerY,newImg,reSize)
cv2.rectangle(newImg, (x, y), (x+w, y+h), (0, 255, 0), 2)
break
return newImg

def detecvFaceImgs(self,img): #return one
faces = self.detecvFace(img)
Expand All @@ -36,16 +102,6 @@ def detecvFaceImgs(self,img): #return one
imgList.append(img[x:x+w,y:y+h].copy())
return imgList

def getDetectImg(self,image):
faces = self.detecvFace(image)
#print("Found {0} faces!".format(len(faces)))
newImg = image.copy()
for (x, y, w, h) in faces:
#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)))
Expand Down
1 change: 1 addition & 0 deletions commonModule/imagePlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def plotImagList(imgList,nameList,gray=False,showTitle=True,showticks=True):
ax.set_yticks([])
ax.set_xticks([])

ax.set_aspect('equal')
#ax.margins(0, 0)
#ax.xaxis.set_major_locator(plt.NullLocator())
#ax.yaxis.set_major_locator(plt.NullLocator())
Expand Down
15 changes: 8 additions & 7 deletions faceIdentification.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def CascadeDetect(cascPath=r'./res/haarcascade_frontalface_default.xml'):

def main():
cv2.useOptimized()
file = r'./res/trump.jpg'#r'./res/Lenna.png' #obama.jpg
file = r'./res/trump.jpg'##obama.jpg #r'./res/Lenna.png' #

print('Number of parameter:', len(sys.argv))
print('Parameters:', str(sys.argv))
Expand All @@ -32,25 +32,26 @@ def main():
img = loadImg(file,mode=cv2.IMREAD_COLOR) # IMREAD_GRAYSCALE IMREAD_COLOR

faceROI = CascadeDetect()
faceR=faceROI.getDetectImg(img)
face = faceROI.detecvFaceImgOne(img)
faceR=faceROI.getDetectImg(img) #reSize=(newW,newH)
#print('faceR.shape=',faceR.shape)
face = faceROI.detecvFaceImgOne(img.copy()) #reSize=(newW,newH)
faceGray = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

img = changeBgr2Rbg(img)
faceR = changeBgr2Rbg(faceR)
#face = changeBgr2Rbg(face)
#faceGray = changeBgr2Rbg(faceGray)
face = changeBgr2Rbg(face)
faceGray = changeBgr2Rbg(faceGray)

ls,names = [],[]
ls.append(img),names.append('orignal')
ls.append(faceR),names.append('faceR')
ls.append(face),names.append('face')
ls.append(faceGray),names.append('faceGray')

plotImagList(ls,names)
plotImagList(ls,names,showTitle=False,showticks=True)

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

if __name__=="__main__":
Expand Down
Binary file added images/Figure.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_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions makeDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ def UpdatePtsToLocation(pts,H,W):
pts[i][1] = pts[i][1]*H
return pts

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

#print(pts)

writeAnotationFile(file,pts)
#writeAnotationFile(file,pts)

pts = np.array(pts)
#print(pts.shape)

Expand Down
2 changes: 1 addition & 1 deletion predictPerson.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def main():

showimage(testFaceLabelPts(img,pts,locCod=False))
if 1:
feature = np.array(calculateFeature(pts,H,W,'predict.pts'))
feature = np.array(calculateFeature(pts,H,W,r'./res/predict.pts'))
else:
feature = pts.reshape(1,136)
print('feature=',len(feature),feature)
Expand Down
4 changes: 2 additions & 2 deletions testLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def showPredictImage():
plotImagList(imgList,nameList,showticks=False,showTitle=False)

def showPredictOutdatasetImage():
file1 = r'.\res\\' + 'Lenna.png'
file2 = r'.\res\\' + 'obama.png' #obama.png
file1 = r'.\res\\' + 'anna.png'
file2 = r'.\res\\' + 'trump.png' #obama.png

predictImg1 = testFaceLabelPredict(file1)
predictImg2 = testFaceLabelPredict(file2)
Expand Down

0 comments on commit c2800d1

Please sign in to comment.