openVINO性别、年龄识别

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

模型下载:

C:\OpenVINO\openvino_2020.4.287\deployment_tools\open_model_zoo\tools\downloader> python downloader.py --name age-gender-recognition-retail-0013

   下面代码中,在循环中,先用人脸检测模型检测出人脸,得到人脸子图 roi_img(大小3, 62, 62)

1 roi = frame[ymin:ymax, xmin:xmax, :]
2 roi_img = cv.resize(roi, (ew, eh))
3 roi_img = roi_img.transpose(2, 0, 1) # (3, 62, 62)                 

  接着,将人脸子图输入到性别、年龄模型中进行识别:

1 em_res = em_exec_net.infer(inputs={em_input_blob: [roi_img]})
2 # 年龄预测
3 age = em_res[em_out_blob1].reshape(1, 1)[0][0] * 100
4 # 性别预测
5 prob_sex = em_res[em_out_blob2].reshape(1, 2)

  上面有对模型输出结果em_res进行解析,其中em_res是一个字典,和open VINO手册中一样,如下图:

 

 

   其余的,没啥好说了。

 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 age_sex_detection():
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 = "age-gender-recognition-retail-0013.xml"
27     em_bin = "age-gender-recognition-retail-0013.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_blob1 = 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                 roi_img = cv.resize(roi, (ew, eh))
66                 roi_img = roi_img.transpose(2, 0, 1) # (3, 62, 62)
67                 em_res = em_exec_net.infer(inputs={em_input_blob: [roi_img]})
68                 # 年龄预测
69                 age = em_res[em_out_blob1].reshape(1, 1)[0][0] * 100
70                 # 性别预测
71                 prob_sex = em_res[em_out_blob2].reshape(1, 2)
72                 label_index = np.int(np.argmax(prob_sex, 1))
73                 print(prob_sex, 1)
74                 cv.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 255), 2, 8)
75                 cv.putText(frame, "infer time(ms): %.3f"%(inf_end*1000), (50, 50), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, 8)
76                 cv.putText(frame, gengers[label_index] + ", " + str(int(age)), (xmin, ymin), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255),1, 8)
77         cv.imshow("Face+age+sex", frame)
78         cv.waitKey(1)
79 
80 
81 if __name__ == "__main__":
82     age_sex_detection()
View Code