async def detection_loop(preload): # =================== FD MODEL ==================== detector = face_detector.DetectorModel(preload) embedding_threshold = preload.embedding_threshold rate = preload.max_frame_rate loop = asyncio.get_running_loop() def retina_deal(pair): address, frame = pair res = [] for img, box in detector.get_all_boxes(frame, save_img=False): res.append(box.tolist()) if box[4] > embedding_threshold: try: suspicion_face_queue.put_nowait((address, img)) except: pass return (address, frame) if res == [] else (address, res, frame) while True: start_time = loop.time() frame_list = [retina_deal(pair) for pair in frame_queue.get()] print(colored(f'Detection cost: {loop.time() - start_time}', 'red'), flush=True) upstream_queue.put(frame_list) for _ in range(int((loop.time() - start_time) * rate)): upstream_queue.put(frame_queue.get())
async def detection_loop(preload): # =================== FD MODEL ==================== detector = face_detector.DetectorModel(preload) rate = preload.max_frame_rate loop = asyncio.get_running_loop() while True: start_time = loop.time() head_frame_list = frame_queue.get() for (ip_address, head_frame) in head_frame_list: for img, box in detector.get_all_boxes(head_frame, save_img=False): try: suspicion_face_queue.put_nowait((ip_address, img)) except Exception as e: pass box = box.astype(int) cv2.rectangle(head_frame, (box[0], box[1]), (box[2], box[3]), [255, 255, 0], 2) upstream_queue.put((ip_address, head_frame)) print(colored(loop.time() - start_time, 'red'), flush=True) for i in range(int((loop.time() - start_time) * rate + 1)): for item in frame_queue.get(): upstream_queue.put(item)
async def detection_loop(preload, address): # =================== FD MODEL ==================== detector = face_detector.DetectorModel(preload) rate_time = 1 / preload.max_frame_rate embedding_threshold = preload.embedding_threshold loop = asyncio.get_running_loop() camera = ipc.XMIPCamera(address.encode('UTF-8'), 34567, b"admin", b"") camera.start() while True: start_time = loop.time() frame = camera.frame(rows=540, cols=960) for img, box in detector.get_all_boxes(frame, save_img=False): if box[4] > embedding_threshold: try: suspicion_face_queue.put_nowait((address, img)) except Exception as _: pass # print(box[4]) box = box.astype(np.int) cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), [255, 255, 0], 2) upstream_queue.put((address, frame)) # print(colored(loop.time() - start_time, 'red'), flush=True) restime = rate_time - loop.time() + start_time if restime > 0: await asyncio.sleep(restime)
async def detection_loop(preload, frame_queue): # =================== FD MODEL ==================== detector = face_detector.DetectorModel(preload) ip_address = preload.ip_address loop = asyncio.get_running_loop() while True: start_time = loop.time() head_frame = frame_queue.get() # tracker = cv2.MultiTracker_create() # t_box = [] for img, box in detector.get_all_boxes(head_frame, save_img=False): try: if box[4] > 0.98: suspicion_face_queue.put_nowait(img) except Exception as e: pass box = box.astype(int) cv2.rectangle( head_frame, (box[0], box[1]), (box[2], box[3]), [255, 255, 0], 2) # t_box.append(box[:4]/2) print(colored(loop.time()-start_time, 'blue')) # head_frame = cv2.resize(head_frame, (960, 540), cv2.INTER_AREA) # for item in t_box: # tracker.add(cv2.TrackerMedianFlow_create(), head_frame, tuple(item)) upstream_frame_queue.put((ip_address, head_frame)) for i in range(int((loop.time() - start_time) * 25 + 1)): body_frame = frame_queue.get() # ok, tricker_boxes = tracker.update(body_frame) # if ok: # for box in tricker_boxes: # box = box.astype(int) # cv2.rectangle(body_frame, (box[0], box[1]), # (box[2], box[3]), [255, 255, 0], 2) upstream_frame_queue.put((ip_address, body_frame)) # await sio.emit('frame_data', encode_image(body_frame), namespace='/remilia') # end_time = loop.time() # print(colored(loop.time()-track_time, 'red')) sys.stdout.flush()
async def detection_loop(preload): reciprocal_of_max_frame_rate = 1/preload.max_frame_rate address = preload.address xmcp = ipc.XMIPCamera(address.encode('UTF-8'), 34567, b"admin", b"") xmcp.start() # =================== FD MODEL ==================== detector = face_detector.DetectorModel(preload) rate = preload.max_frame_rate loop = asyncio.get_running_loop() predictor = dlib.shape_predictor(preload.face_landmark_path) (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # =================== ETERNAL LOOP ==================== while True: start_time = loop.time() try: head_frame = xmcp.frame(rows=540, cols=960) for img, box, points in detector.get_boxes_and_landmarks(head_frame): box = box.astype(int) # nimg = cv2.resize( # head_frame[box[1]:box[3], box[0]:box[2]], (112, 112)) shape = predictor(img, dlib.rectangle(0, 0, 112, 112)) # shape = predictor(head_frame, dlib.rectangle( # box[0], box[1], box[2], box[3])) # shape = face_utils.shape_to_np(shape) + 50 shape = face_utils.shape_to_np(shape) reprojectdst, euler_angle = get_head_pose(shape) # for (x, y) in shape: # cv2.circle(head_frame, (x, y), 2, (0, 0, 255), -1) # for start, end in line_pairs: # cv2.line( # head_frame, reprojectdst[start], reprojectdst[end], (0, 0, 255)) # =================== EYES ==================== shape = shape * 10 leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # head_frame = np.zeros([540, 960, 3]) result_queue.put((euler_angle, shape, leftEAR, rightEAR)) except Exception as e: print(e) pass print(colored(loop.time()-start_time, 'red'), flush=True) restime = reciprocal_of_max_frame_rate - loop.time() + start_time if restime > 0: await asyncio.sleep(restime)
from sklearn.preprocessing import label_binarize import imageGenerator from sklearn import metrics from helper import read_pkl_model, start_up_init, get_dataset, get_image_paths_and_labels import face_embedding import face_detector # =================== ARGS ==================== os.environ["MXNET_CUDNN_AUTOTUNE_DEFAULT"] = "0" args = start_up_init() args.retina_model = './model/M25' args.scales = [0.5] # =================== MODEL CLASS ==================== detector = face_detector.DetectorModel(args) arcface = face_embedding.EmbeddingModel(args) # =================== LOAD DATASET ====================. dir_train = './Temp/train.npy' data_train = './Temp/train_data' dataset_train = get_dataset(data_train) paths_train, labels_train = get_image_paths_and_labels(dataset_train) try: train_emb_array = np.load(dir_train) train_emb_array = imageGenerator.generator() except OSError: if not os.path.exists('./Temp/raw/'): os.makedirs('./Temp/raw/') detector.get_all_boxes_from_path(paths_train, save_img=True) dataset_train = get_dataset(data_train)