1+ import numpy as np
2+ import cv2
3+ import tensorflow .keras as ks
4+ from testLabel import testFaceLabelPts
5+ from faceIdentification import CascadeDetect
6+
7+ newW = 364
8+ newH = 440
9+
10+ def getImgHW (img ):
11+ return img .shape [0 ],img .shape [1 ]
12+
13+ def resizeImg (img ,NewW ,NewH ):
14+ rimg = cv2 .resize (img , (NewW ,NewH ), interpolation = cv2 .INTER_CUBIC ) #INTER_CUBIC INTER_NEAREST INTER_LINEAR INTER_AREA
15+ return rimg
16+
17+ def preditImgKeyPoints (img , model ):
18+ global newW
19+ global newH
20+ img = resizeImg (img ,newW ,newH )
21+ pts = preditImgPts (img ,model )
22+ return testFaceLabelPts (img ,pts )
23+
24+ def preditImgPts (img , model ):
25+ x = img [:,:,0 ]
26+ #print(x.shape)
27+ x = x .reshape ((1 ,x .shape [0 ],x .shape [1 ],1 ))
28+ #print(x.shape)
29+ pts = model .predict (x )
30+ pts = pts .reshape ((68 ,2 ))
31+ return pts
32+
33+ def InitNet ():
34+ weights = r'./weights/trainFacialRecognition.h5'
35+ model = ks .models .load_model (weights )
36+ return model
37+
38+ def showCamera ():
39+ model = InitNet ()
40+
41+ saveVideo = False
42+
43+ #fourcc = cv2.VideoWriter.fourcc('X','2','6','4')
44+ #fourcc = cv2.VideoWriter.fourcc('v','p','8','0')
45+ fourcc = cv2 .VideoWriter .fourcc ('M' ,'J' ,'P' ,'G' )
46+ out = cv2 .VideoWriter ('output2.mp4' , fourcc , 25.0 , (640 ,480 ))
47+ cap = cv2 .VideoCapture (0 )
48+ fps = cap .get (cv2 .CAP_PROP_FPS )
49+ print ('fps=' ,fps )
50+
51+ while (True ):
52+ ret , frame = cap .read ()
53+ #Our operations on the frame come here
54+ #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
55+ #Display the resulting frame
56+
57+ #frame=CascadeDetect().getDetectImg(frame)
58+ face = CascadeDetect ().detecvFaceImgOne (frame )
59+ if face is None :
60+ continue
61+
62+ frame = preditImgKeyPoints (face ,model )
63+ if saveVideo :
64+ out .write (frame )
65+
66+ cv2 .imshow ('frame' ,frame )
67+ if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
68+ break
69+
70+ cap .release ()
71+ out .release ()
72+ cv2 .destroyAllWindows ()
73+
74+ def main ():
75+ showCamera ()
76+
77+ if __name__ == '__main__' :
78+ main ()
79+
0 commit comments