openvino人脸35个关键点识别

发布时间 2023-09-27 15:26:54作者: FeiYull

还是先检测人脸,然后检测人脸特征点,一共35个特征点,网络最后输出特征图为1*70,表示35个点,存储格式为:(x y x y x y......)

prob_landmarks = em_res[em_out_blob] # (1, 70)

 

 1 import time
 2 
 3 import cv2 as cv
 4 import numpy as np
 5 import numpy as py
 6 from openvino.inference_engine import IECore
 7 
 8 gengers = ["femal", "male"]
 9 
10 def face_landmarks35():
11     ie = IECore()
12     for device in ie.available_devices:
13         print(device)
14     model_xml = "face-detection-0102.xml"
15     model_bin = "face-detection-0102.bin"
16     net = ie.read_network(model=model_xml, weights=model_bin)
17     input_blob = next(iter(net.input_info))
18     out_blob = next(iter(net.outputs))
19     n, c, h, w = net.input_info[input_blob].input_data.shape
20     print(n, c, h, w)  # 1 3 384 384
21 
22     cap = cv.VideoCapture("1.mp4")
23     exec_net = ie.load_network(network=net, device_name="CPU")
24 
25     # 加载人脸特征点识别模型
26     em_xml = "facial-landmarks-35-adas-0002.xml"
27     em_bin = "facial-landmarks-35-adas-0002.bin"
28 
29     em_net = ie.read_network(model=em_xml, weights=em_bin)
30     em_input_blob = next(iter(em_net.input_info))  # 类似于一个空的tensor
31     em_it = iter(em_net.outputs)
32     em_out_blob = next(em_it)
33     #em_out_blob2 = next(em_it)
34     en, ec, eh, ew = em_net.input_info[em_input_blob].input_data.shape
35     print(en, ec, eh, ew)  # 1 3 62 62
36     em_exec_net = ie.load_network(network=em_net, device_name="CPU")
37 
38     while True:
39         ret, frame = cap.read()
40         if ret is not True:
41             break
42         image = cv.resize(frame, (w, h))
43         image = image.transpose(2, 0, 1)
44         inf_start = time.time()
45         # 人脸检测
46         res = exec_net.infer(inputs={input_blob: [image]})
47         inf_end = time.time() - inf_start
48         ih, iw, ic = frame.shape
49         res = res[out_blob] # (1, 1, 200, 7)
50         for obj in res[0][0]:
51             if obj[2] > 0.75:
52                 xmin = int(obj[3] * iw)
53                 ymin = int(obj[4] * ih)
54                 xmax = int(obj[5] * iw)
55                 ymax = int(obj[6] * ih)
56                 if xmin < 0:
57                     xmin = 0
58                 if ymin < 0:
59                     ymin = 0
60                 if xmax >= iw:
61                     xmax = iw - 1
62                 if ymax >= ih:
63                     ymax = ih - 1
64                 roi = frame[ymin:ymax, xmin:xmax, :]
65                 rh, rw, rc = roi.shape
66                 roi_img = cv.resize(roi, (ew, eh))
67                 roi_img = roi_img.transpose(2, 0, 1) # (3, 62, 62)
68                 em_res = em_exec_net.infer(inputs={em_input_blob: [roi_img]})
69                 # 人脸特征点预测
70                 prob_landmarks = em_res[em_out_blob]
71                 for index in range(0, len(prob_landmarks[0]), 2):
72                     x = np.int(prob_landmarks[0][index] * rw)
73                     y = np.int(prob_landmarks[0][index + 1] * rh)
74                     cv.circle(roi, (x, y), 3, (255, 0, 0), -1, 8, 0)
75         cv.imshow("Face+landMarks35", frame)
76         cv.waitKey(1)
77 
78 
79 if __name__ == "__main__":
80     face_landmarks35()
View Code