def inferenceHtptsVideo(frame,interpreter,input_tensor,session): input_size = [int(v.strip()) for v in args.input_size.split(",")] priors = define_img_size(input_size) interpreter.runSession(session) image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = cv2.resize(image, tuple(input_size)) image = image.astype(float) image = (image - image_mean) / image_std image = np.asarray(image, dtype=np.float32) image = image.transpose((2, 0, 1)) tmp_input = MNN.Tensor((1, 3, input_size[1], input_size[0]), MNN.Halide_Type_Float, image, MNN.Tensor_DimensionType_Caffe) input_tensor.copyFrom(tmp_input) scores = interpreter.getSessionOutput(session, "scores").getData() boxes = interpreter.getSessionOutput(session, "boxes").getData() boxes = np.expand_dims(np.reshape(boxes, (-1, 4)), axis=0) scores = np.expand_dims(np.reshape(scores, (-1, 2)), axis=0) boxes = box_utils.convert_locations_to_boxes(boxes, priors, center_variance, size_variance) boxes = box_utils.center_form_to_corner_form(boxes) boxes, labels, probs = predict(frame.shape[1], frame.shape[0], scores, boxes, args.threshold) tmp_face_array = [] for i in range(boxes.shape[0]): box = boxes[i, :] # remove to small face min_size = 5 if ((box[3] - box[1]) < min_size and (box[2] - box[0]) > min_size)\ or ((box[3] - box[1]) > min_size and (box[2] - box[0]) < min_size)\ or ((box[3] - box[1]) < min_size and (box[2] - box[0]) < min_size): continue cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) tmp_face_array.append(frame[max(0,box[1] - 100):box[3]+100, max(0,box[0]-100):box[2]+100]) cv2.imshow("DD", cv2.resize(frame,(720,480))) cv2.waitKey(1) return tmp_face_array
def inferenceHtptsVideo(window_name,frame,dataset_emb,names_list,interpreter,input_tensor,session,face_net): input_size = [int(v.strip()) for v in args.input_size.split(",")] priors = define_img_size(input_size) interpreter.runSession(session) image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = cv2.resize(image, tuple(input_size)) image = image.astype(float) image = (image - image_mean) / image_std image = np.asarray(image, dtype=np.float32) image = image.transpose((2, 0, 1)) tmp_input = MNN.Tensor((1, 3, input_size[1], input_size[0]), MNN.Halide_Type_Float, image, MNN.Tensor_DimensionType_Caffe) input_tensor.copyFrom(tmp_input) time_time = time.time() scores = interpreter.getSessionOutput(session, "scores").getData() boxes = interpreter.getSessionOutput(session, "boxes").getData() boxes = np.expand_dims(np.reshape(boxes, (-1, 4)), axis=0) scores = np.expand_dims(np.reshape(scores, (-1, 2)), axis=0) boxes = box_utils.convert_locations_to_boxes(boxes, priors, center_variance, size_variance) boxes = box_utils.center_form_to_corner_form(boxes) boxes, labels, probs = predict(frame.shape[1], frame.shape[0], scores, boxes, args.threshold) files = [] all_face_results = [] for i in range(boxes.shape[0]): box = boxes[i, :] # remove to small face min_size = 5 if ((box[3] - box[1]) < min_size and (box[2] - box[0]) > min_size)\ or ((box[3] - box[1]) > min_size and (box[2] - box[0]) < min_size)\ or ((box[3] - box[1]) < min_size and (box[2] - box[0]) < min_size): continue # up image ============ tmp_cut_face = frame[max(0,box[1] - 100):box[3]+100, max(0,box[0]-100):box[2]+100] tmp_cut_face = cv2.resize(tmp_cut_face, (400, 500)) # cut face from image cut_face = frame[max(0,box[1]):box[3], max(0,box[0]):box[2]] cut_face = cv2.resize(cut_face, (160, 160)) cut_face = cut_face.reshape((1,160,160,3)) # FaceNet chrack ================================================ people / 20ms # pred_emb = face_net.get_embedding(cut_face) # pred_name, pred_score = compare_embadding(pred_emb, dataset_emb, names_list,0.7) # # 在图像上绘制人脸边框和识别的结果 # show_info = [n + ':' + str(s)[:5] for n, s in zip(pred_name, pred_score)] # all_face_results.append(pred_name[0]) # files.append(("face", np.array(cv2.imencode('.png', tmp_cut_face)[1]).tobytes())) # # cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) # cv2.putText(frame, show_info[0], (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2) # # 保存或者上传数据 # if len(files) != 0: # # 拼接参数 # headers = {} # payload["detectionResult"] = ",".join(all_face_results) # print(payload) # res = requests.request("POST", url, headers=headers, data=payload, files=files) # print(res.text) print("inference time: {} s".format(round(time.time() - time_time, 4)))
def inference(): input_size = [int(v.strip()) for v in args.input_size.split(",")] priors = define_img_size(input_size) result_path = args.results_path imgs_path = args.imgs_path if not os.path.exists(result_path): os.makedirs(result_path) listdir = os.listdir(imgs_path) for file_path in listdir: img_path = os.path.join(imgs_path, file_path) image_ori = cv2.imread(img_path) interpreter = MNN.Interpreter(args.model_path) session = interpreter.createSession() input_tensor = interpreter.getSessionInput(session) image = cv2.cvtColor(image_ori, cv2.COLOR_BGR2RGB) image = cv2.resize(image, tuple(input_size)) image = image.astype(float) image = (image - image_mean) / image_std image = np.asarray(image, dtype=np.float32) image = image.transpose((2, 0, 1)) tmp_input = MNN.Tensor((1, 3, input_size[1], input_size[0]), MNN.Halide_Type_Float, image, MNN.Tensor_DimensionType_Caffe) input_tensor.copyFrom(tmp_input) time_time = time.time() interpreter.runSession(session) scores = interpreter.getSessionOutput(session, "scores").getData() boxes = interpreter.getSessionOutput(session, "boxes").getData() boxes = np.expand_dims(np.reshape(boxes, (-1, 4)), axis=0) scores = np.expand_dims(np.reshape(scores, (-1, 2)), axis=0) print("inference time: {} s".format(round(time.time() - time_time, 4))) boxes = box_utils.convert_locations_to_boxes(boxes, priors, center_variance, size_variance) boxes = box_utils.center_form_to_corner_form(boxes) boxes, labels, probs = predict(image_ori.shape[1], image_ori.shape[0], scores, boxes, args.threshold) for i in range(boxes.shape[0]): box = boxes[i, :] cv2.rectangle(image_ori, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) cv2.imwrite(os.path.join(result_path, file_path), image_ori) print("result_pic is written to {}".format( os.path.join(result_path, file_path))) cv2.imshow("UltraFace_mnn_py", image_ori) cv2.waitKey(-1) cv2.destroyAllWindows()
def inferenceHtptsVideo(): input_size = [int(v.strip()) for v in args.input_size.split(",")] priors = define_img_size(input_size) result_path = args.results_path videos_path = args.videos_path if not os.path.exists(result_path): os.makedirs(result_path) if not os.path.exists(result_path + "/face"): os.makedirs(result_path + "/face") frame_number = 0 # url = 'rtsp://*****:*****@192.168.101.107:554' url = 'rtsp://*****:*****@192.168.2.5/Streaming/Channels/1' cap = cv2.VideoCapture(url) if not cap.isOpened(): print("Cannot open camera") exit() else: ret, frame = cap.read() frame_number = 0 while ret: interpreter.runSession(session) ret, frame = cap.read() if not ret: break image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = cv2.resize(image, tuple(input_size)) image = image.astype(float) image = (image - image_mean) / image_std image = np.asarray(image, dtype=np.float32) image = image.transpose((2, 0, 1)) tmp_input = MNN.Tensor((1, 3, input_size[1], input_size[0]), MNN.Halide_Type_Float, image, MNN.Tensor_DimensionType_Caffe) input_tensor.copyFrom(tmp_input) time_time = time.time() scores = interpreter.getSessionOutput(session, "scores").getData() boxes = interpreter.getSessionOutput(session, "boxes").getData() boxes = np.expand_dims(np.reshape(boxes, (-1, 4)), axis=0) scores = np.expand_dims(np.reshape(scores, (-1, 2)), axis=0) boxes = box_utils.convert_locations_to_boxes(boxes, priors, center_variance, size_variance) boxes = box_utils.center_form_to_corner_form(boxes) boxes, labels, probs = predict(frame.shape[1], frame.shape[0], scores, boxes, args.threshold) for i in range(boxes.shape[0]): box = boxes[i, :] # remove to small face min_size = 30 if ((box[3] - box[1]) < min_size and (box[2] - box[0]) > min_size)\ or ((box[3] - box[1]) > min_size and (box[2] - box[0]) < min_size)\ or ((box[3] - box[1]) < min_size and (box[2] - box[0]) < min_size): continue # cut face from image cut_face = frame[box[1]:box[3], box[0]:box[2]] tmp_cut_face = frame[box[1] - 100:box[3] + 100, box[0] - 100:box[2] + 100] tmp_cut_face = cv2.resize(tmp_cut_face, (400, 500)) # save people face # cv2.imwrite(os.path.join(result_path + "/face", str(frame_number) + ".g"), tmp_cut_face) cut_face = cv2.resize(cut_face, (160, 160)) cut_face = cut_face.reshape((1, 160, 160, 3)) # FaceNet chrack ================================================ pred_emb = face_net.get_embedding(cut_face) pred_name, pred_score = compare_embadding(pred_emb, dataset_emb, names_list, 1.0) # 在图像上绘制人脸边框和识别的结果 show_info = [ n + ':' + str(s)[:5] for n, s in zip(pred_name, pred_score) ] # upLoad Image and Data upDate["timestape"] = time.time() upDate["detection_result"] = show_info[0] upDate["image"] = np.array(cv2.imencode( '.png', tmp_cut_face)[1]).tobytes() res = requests.post(url=url, data=json.dumps(upDate)) print(res.text) cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) cv2.putText(frame, show_info[0], (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2) # remove no face frame frame_number += 1 print("inference time: {} s".format(round(time.time() - time_time, 4))) # cv2.imwrite(os.path.join(result_path, str(frame_number) + ".jpg"), frame) # print("result_pic is written to {}".format(os.path.join(result_path, file_path))) cv2.imshow("UltraFace_mnn", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()