行人属性
from openvino.inference_engine import IECore
import numpy as np
import time
import cv2 as cv
attris = "is_male:F,has_bag:F,has_backpack:F,has_hat:F,has_longsleeves:F," \
"has_longpants:F,has_longhair:F,has_coat_jacket:F"
# 行人检测模型
model_xml = "/home/bhc/BHC/model/intel/pedestrian-detection-adas-0002/FP16/pedestrian-detection-adas-0002.xml"
model_bin = "/home/bhc/BHC/model/intel/pedestrian-detection-adas-0002/FP16/pedestrian-detection-adas-0002.bin"
# 行人属性识别
head_xml = "/home/bhc/BHC/model/intel/person-attributes-recognition-crossroad-0230/FP16/person-attributes-recognition-crossroad-0230.xml"
head_bin = "/home/bhc/BHC/model/intel/person-attributes-recognition-crossroad-0230/FP16/person-attributes-recognition-crossroad-0230.bin"
def person_attributes_demo(attris):
ie = IECore()
for device in ie.available_devices:
print(device)
net = ie.read_network(model=model_xml, weights=model_bin)
input_blob = next(iter(net.input_info))
out_blob = next(iter(net.outputs))
n, c, h, w = net.input_info[input_blob].input_data.shape
print(n, c, h, w)
cap = cv.VideoCapture("1.mp4")
# cap = cv.VideoCapture(0)
exec_net = ie.load_network(network=net, device_name="CPU")
head_net = ie.read_network(model=head_xml, weights=head_bin)
em_input_blob = next(iter(head_net.input_info))
head_it = iter(head_net.outputs)
head_out_blob1 = next(head_it) # angle_p_fc
head_out_blob2 = next(head_it) # angle_r_fc
head_out_blob3 = next(head_it) # angle_y_fc
print(head_out_blob1, head_out_blob2, head_out_blob3)
en, ec, eh, ew = head_net.input_info[em_input_blob].input_data.shape
print(en, ec, eh, ew)
em_exec_net = ie.load_network(network=head_net, device_name="CPU")
while True:
ret, frame = cap.read()
if ret is not True:
break
image = cv.resize(frame, (w, h))
image = image.transpose(2, 0, 1)
inf_start = time.time()
res = exec_net.infer(inputs={input_blob: [image]})
inf_end = time.time() - inf_start
# print("infer time(ms):%.3f"%(inf_end*1000))
ih, iw, ic = frame.shape #(1, 1, N, 7)
res = res[out_blob] #[image_id, label, conf, x_min, y_min, x_max, y_max]
for obj in res[0][0]:
if obj[2] > 0.75:
xmin = int(obj[3] * iw)
ymin = int(obj[4] * ih)
xmax = int(obj[5] * iw)
ymax = int(obj[6] * ih)
if xmin < 0:
xmin = 0
if ymin < 0:
ymin = 0
if xmax >= iw:
xmax = iw - 1
if ymax >= ih:
ymax = ih - 1
roi = frame[ymin:ymax, xmin:xmax, :]
roi_img = cv.resize(roi, (ew, eh))
roi_img = roi_img.transpose(2, 0, 1)
head_res = em_exec_net.infer(inputs={em_input_blob: [roi_img]})
c_453 = head_res[head_out_blob1].reshape(1, 8) #(1, 8, 1, 1 )人的属性:[is_male, has_bag, has_backpack, has_hat, has_longsleeves, has_longpants, has_longhair, has_coat_jacket]
c_456 = head_res[head_out_blob2].reshape(1, 2) #(1, 2, 1, 1 )(上颜色)
c_459 = head_res[head_out_blob3].reshape(1, 2) #(1, 2, 1, 1 )(下颜色)
if c_453[0][0] > 0.5:
attris = attris.replace("is_male:F","is_male:T")
if c_453[0][0] > 0.5:
attris = attris.replace("has_bag:F","has_bag:T")
if c_453[0][0] > 0.5:
attris = attris.replace("has_backpack:F","has_backpack:T")
if c_453[0][0] > 0.5:
attris = attris.replace("has_hat:F","has_hat:T")
if c_453[0][0] > 0.5:
attris = attris.replace("has_longsleeves:F","has_longsleeves:T")
if c_453[0][0] > 0.5:
attris = attris.replace("has_longpants:F","has_longpants:T")
if c_453[0][0] > 0.5:
attris = attris.replace("has_longhair:F","has_longhair:T")
if c_453[0][0] > 0.5:
attris = attris.replace("has_coat_jacket:F","has_coat_jacket:T")
cv.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 255), 2, 8)
cv.putText(frame, attris, (xmin, ymin), cv.FONT_HERSHEY_PLAIN, 1.0, (255, 0, 255), 2, 8)
cv.putText(frame, "infer time(ms): %.3f, FPS: %.2f" % (inf_end * 1000, 1/inf_end), (50, 50),
cv.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 255), 2, 8)
cv.imshow("Face+emotion Detection", frame)
c = cv.waitKey(1)
if c == 27:
break
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
person_attributes_demo(attris)
天道酬勤 循序渐进 技压群雄
浙公网安备 33010602011771号