def register(): global faces_db upload_file = request.files['image'] user_name = request.values.get("name") if upload_file: try: image = cv2.imdecode(np.frombuffer(upload_file.read(), np.uint8), cv2.IMREAD_UNCHANGED) faces, landmarks = mtcnn_detector.detect(image) if faces.shape[0] is not 0: faces_sum = 0 bbox = [] points = [] for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: bbox = faces[i, 0:4] points = landmarks[i, :].reshape((5, 2)) faces_sum += 1 if faces_sum == 1: nimg = face_preprocess.preprocess(image, bbox, points, image_size='112,112') cv2.imencode('.png', nimg)[1].tofile('face_db/%s.png' % user_name) # 更新人脸库 faces_db = load_faces(face_sess, inputs_placeholder, embeddings) return str({"code": 0, "msg": "success"}) return str({"code": 3, "msg": "image not or much face"}) except: return str({"code": 2, "msg": "this file is not image or not face"}) else: return str({"code": 1, "msg": "file is None"})
def load_faces(faces_dir, mtcnn_detector): face_db = [] with tf.Graph().as_default(): with tf.Session() as sess: load_mobilefacenet( "./models/mobilefacenet_model/MobileFaceNet_9925_9680.pb") inputs_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") for root, dirs, files in os.walk(faces_dir): for file in files: input_image = cv2.imread(os.path.join(root, file)) faces, landmarks = mtcnn_detector.detect(input_image) bbox = faces[0, :4] points = landmarks[0, :].reshape((5, 2)) nimg = face_preprocess.preprocess(input_image, bbox, points, image_size='112,112') nimg = nimg - 127.5 nimg = nimg * 0.0078125 name = file.split(".")[0] input_image = np.expand_dims(nimg, axis=0) feed_dict = {inputs_placeholder: input_image} emb_array = sess.run(embeddings, feed_dict=feed_dict) embedding = sklearn.preprocessing.normalize( emb_array).flatten() face_db.append({"name": name, "feature": embedding}) return face_db
def face_register(ifile, IMAGE_SIZE): frame = cv2.imread(ifile) #cv2.imshow('image', frame) faces, landmarks = mtcnn_detector.detect(frame) if faces.shape[0] is not 0: faces_sum = 0 bbox = [] points = [] for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: bbox = faces[i, 0:4] points = landmarks[i, :].reshape((5, 2)) faces_sum += 1 if faces_sum == 1: nimg = face_preprocess.preprocess(frame, bbox, points, image_size=IMAGE_SIZE) #user_name = input("请输入注册名:") user_name = os.path.splitext(os.path.basename(ifile))[0] cv2.imencode('.png', nimg)[1].tofile('face_db/%s.png' % user_name) print("注册成功!") else: print('注册图片有错,图片中有且只有一个人脸') else: print('注册图片有错,图片中有且只有一个人脸')
def get_faces(self, face_img): ret = self.detector.detect_face(face_img) if ret is None: return None bbox, points = ret if bbox.shape[0] == 0: return [], [], [] bboxes = [] pointses = [] faces = [] for i in range(len(bbox)): b = bbox[i, 0:4] bboxes.append(b) p = points[i, :].reshape((2, 5)).T pointses.append(p) nimg = face_preprocess.preprocess(face_img, b, p, image_size='112,112') nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB) aligned = np.transpose(nimg, (2, 0, 1)) input_blob = np.expand_dims(aligned, axis=0) data = mx.nd.array(input_blob) db = mx.io.DataBatch(data=(data, )) faces.append(db) return faces, bboxes, pointses
def face_register(): print("点击y确认拍照!") cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if ret: cv2.imshow('image', frame) if cv2.waitKey(1) & 0xFF == ord('y'): faces, landmarks = mtcnn_detector.detect(frame) if faces.shape[0] is not 0: faces_sum = 0 bbox = [] points = [] for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: bbox = faces[i, 0:4] points = landmarks[i, :].reshape((5, 2)) faces_sum += 1 if faces_sum == 1: nimg = face_preprocess.preprocess(frame, bbox, points, image_size='112,112') user_name = input("请输入注册名:") cv2.imencode('.png', nimg)[1].tofile('face_db/%s.png' % user_name) print("注册成功!") else: print('注册图片有错,图片中有且只有一个人脸') else: print('注册图片有错,图片中有且只有一个人脸') break
def recognition_face(frame): try: faces, landmarks = mtcnn_detector.detect(frame) if faces.shape[0] is not 0: faces_sum = 0 for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: faces_sum += 1 if faces_sum == 0: return # 人脸信息 info_bbox = np.zeros((faces_sum, 4)) info_landmarks = np.zeros((faces_sum, 10)) info_name = [] probs = [] img_i = 0 # 提取图像中的人脸 input_images = np.zeros((faces.shape[0], 112, 112, 3)) for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: bbox = faces[i, 0:4] points = landmarks[i, :].reshape((5, 2)) nimg = face_preprocess.preprocess(frame, bbox, points, image_size='112,112') # 图像预处理 nimg = nimg - 127.5 nimg = nimg * 0.0078125 input_images[i, :] = nimg # 关键点和人脸框 info_bbox[img_i] = bbox info_landmarks[img_i] = landmarks[i, :] img_i += 1 # 进行人脸识别 feed_dict = {inputs_placeholder: input_images} emb_arrays = face_sess.run(embeddings, feed_dict=feed_dict) emb_arrays = sklearn.preprocessing.normalize(emb_arrays) for i, embedding in enumerate(emb_arrays): embedding = embedding.flatten() temp_dict = {} # 比较已经存在的人脸数据库 for com_face in faces_db: ret, sim = feature_compare(embedding, com_face["feature"], 0.70) temp_dict[com_face["name"]] = sim dict = sorted(temp_dict.items(), key=lambda d: d[1], reverse=True) if dict[0][1] > VERIFICATION_THRESHOLD: name = dict[0][0] probs.append(dict[0][1]) info_name.append(name) else: probs.append(dict[0][1]) info_name.append("unknown") # 如果识别就跳出检测 return info_name, probs, info_bbox, info_landmarks else: raise Exception("infer result is None!") except Exception as e: print(e) return None, None, None, None, None, None
def add_faces(mtcnn_detector): face_db_path = config.FACE_DB_PATH faces_name = os.listdir(face_db_path) temp_face_path = config.TEMP_FACE_PATH for root, dirs, files in os.walk(temp_face_path): for file in files: if file not in faces_name: input_image = cv2.imdecode(np.fromfile(os.path.join(root, file), dtype=np.uint8), 1) faces, landmarks = mtcnn_detector.detect(input_image) bbox = faces[0, :4] points = landmarks[0, :].reshape((5, 2)) nimg = face_preprocess.preprocess(input_image, bbox, points, image_size='112,112') cv2.imwrite(os.path.join(face_db_path, os.path.basename(file)), nimg)
def get_input(self, face_img): # ret = self.detector.detect_face(face_img, det_type = self.args.det) # if ret is None: # return None # bbox, points = ret # if bbox.shape[0]==0: # return None # bbox = bbox[0,0:4] # points = points[0,:].reshape((2,5)).T # #print(bbox) # #print(points) # nimg = face_preprocess.preprocess(face_img, bbox, points, image_size='112,112') nimg = preprocess(face_img, image_size='112,112') nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB) aligned = np.transpose(nimg, (2, 0, 1)) return aligned
def get_align(self, face_img): ret = self.detector.detect_face(face_img, det_type=self.args.det) if ret is None: return None bbox, points = ret if bbox.shape[0] == 0: return None box = bbox[0, 0:4] point = points[0, :].reshape((2, 5)).T nimg = face_preprocess.preprocess(face_img, box, point, image_size='112,112') nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB) aligned = np.transpose(nimg, (2, 0, 1)) return aligned
def get_face(self, face_img, random_bg=False): ret = self.detector.detect_face(face_img) if ret is None: return None bbox, points = ret if bbox.shape[0] == 0: return None bbox = bbox[0, 0:4] points = points[0, :].reshape((2, 5)).T if random_bg: b = random.randint(0, 255) g = random.randint(0, 255) r = random.randint(0, 255) borderValue = (b, g, r) else: borderValue = 0.0 nimg = face_preprocess.preprocess(face_img, bbox, points, image_size='112,112', borderValue=borderValue) return nimg
def get_input(self, face_img): ret = self.detector.detect_face(face_img, det_type=self.args.det) if ret is None: return None bbox, points = ret if bbox.shape[0] == 0: return None bbox_total = [] aliged_total = [] for i in range(min(self.args.FaceMaxNum, len(bbox))): box = bbox[i, 0:4] point = points[i, :].reshape((2, 5)).T nimg = face_preprocess.preprocess(face_img, box, point, image_size='112,112') nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB) aligned = np.transpose(nimg, (2, 0, 1)) bbox_total.append(box) aliged_total.append(aligned) return aliged_total, bbox_total
def face_register(img_path, name): image = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1) faces, landmarks = mtcnn_detector.detect(image) if faces.shape[0] is not 0: faces_sum = 0 bbox = [] points = [] for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: bbox = faces[i, 0:4] points = landmarks[i, :].reshape((5, 2)) faces_sum += 1 if faces_sum == 1: nimg = face_preprocess.preprocess(image, bbox, points, image_size='112,112') cv2.imencode('.png', nimg)[1].tofile('face_db/%s.png' % name) print("注册成功!") else: print('注册图片有错,图片中有且只有一个人脸') else: print('注册图片有错,图片中有且只有一个人脸')
def face_recognition(img_path): image = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1) faces, landmarks = mtcnn_detector.detect(image) if faces.shape[0] is not 0: faces_sum = 0 for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: faces_sum += 1 if faces_sum > 0: # 人脸信息 info_location = np.zeros(faces_sum) info_location[0] = 1 info_name = [] probs = [] # 提取图像中的人脸 input_images = np.zeros((faces.shape[0], 112, 112, 3)) for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: bbox = faces[i, 0:4] points = landmarks[i, :].reshape((5, 2)) nimg = face_preprocess.preprocess(image, bbox, points, image_size='112,112') nimg = nimg - 127.5 nimg = nimg * 0.0078125 input_images[i, :] = nimg # 进行人脸识别 feed_dict = {inputs_placeholder: input_images} emb_arrays = face_sess.run(embeddings, feed_dict=feed_dict) print(emb_arrays.shape) emb_arrays = sklearn.preprocessing.normalize(emb_arrays) print(emb_arrays.shape) for i, embedding in enumerate(emb_arrays): embedding = embedding.flatten() temp_dict = {} # 比较已经存在的人脸数据库 for com_face in faces_db: ret, sim = feature_compare(embedding, com_face["feature"], 0.70) temp_dict[com_face["name"]] = sim dict = sorted(temp_dict.items(), key=lambda d: d[1], reverse=True) if dict[0][1] > VERIFICATION_THRESHOLD: name = dict[0][0] probs.append(dict[0][1]) info_name.append(name) else: probs.append(dict[0][1]) info_name.append("unknown") for k in range(faces_sum): # 写上人脸信息 x1, y1, x2, y2 = faces[k][0], faces[k][1], faces[k][2], faces[ k][3] x1 = max(int(x1), 0) y1 = max(int(y1), 0) x2 = min(int(x2), image.shape[1]) y2 = min(int(y2), image.shape[0]) prob = '%.2f' % probs[k] label = "{}, {}".format(info_name[k], prob) cv2img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) pilimg = Image.fromarray(cv2img) draw = ImageDraw.Draw(pilimg) font = ImageFont.truetype('font/simfang.ttf', 18, encoding="utf-8") draw.text((x1, y1 - 18), label, (255, 0, 0), font=font) image = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR) cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2) cv2.imshow('image', image) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): cap = cv2.VideoCapture(0) conf = configparser.ConfigParser() conf.read("config/main.cfg") mtcnn_detector = load_mtcnn(conf) MODEL_PATH = conf.get("MOBILEFACENET", "MODEL_PATH") VERIFICATION_THRESHOLD = float( conf.get("MOBILEFACENET", "VERIFICATION_THRESHOLD")) FACE_DB_PATH = conf.get("MOBILEFACENET", "FACE_DB_PATH") faces_db = load_faces(FACE_DB_PATH, mtcnn_detector) with tf.Graph().as_default(): with tf.Session() as sess: load_mobilefacenet(MODEL_PATH) inputs_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") while True: ret, frame = cap.read() if ret: faces, landmarks = mtcnn_detector.detect(frame) if faces.shape[0] is not 0: input_images = np.zeros((faces.shape[0], 112, 112, 3)) for i, face in enumerate(faces): if round(faces[i, 4], 6) > 0.95: bbox = faces[i, 0:4] points = landmarks[i, :].reshape((5, 2)) nimg = face_preprocess.preprocess( frame, bbox, points, image_size='112,112') cv2.imshow("face", nimg) nimg = nimg - 127.5 nimg = nimg * 0.0078125 # input_image = np.expand_dims(nimg, axis=0) input_images[i, :] = nimg feed_dict = {inputs_placeholder: input_images} emb_arrays = sess.run(embeddings, feed_dict=feed_dict) emb_arrays = sklearn.preprocessing.normalize( emb_arrays) names = [] sims = [] for i, embedding in enumerate(emb_arrays): embedding = embedding.flatten() temp_dict = {} for com_face in faces_db: ret, sim = feature_compare( embedding, com_face["feature"], 0.65) temp_dict[com_face["name"]] = sim dict = sorted(temp_dict.items(), key=lambda d: d[1], reverse=True) if dict[0][1] > VERIFICATION_THRESHOLD: name = dict[0][0] sim = dict[0][1] else: name = "unknown" sim = 0 names.append(name) sims.append(sim) x1, y1, x2, y2 = faces[i][0], faces[i][1], faces[ i][2], faces[i][3] x1 = max(int(x1), 0) y1 = max(int(y1), 0) x2 = min(int(x2), frame.shape[1]) y2 = min(int(y2), frame.shape[0]) cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) draw_rect(faces, names, sims, frame) cv2.imshow("frame", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
def main(strargument): shutil.rmtree("./test") os.mkdir("./test") os.remove("result.txt") f = open("result.txt", "a") cap = cv2.VideoCapture(strargument) # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) #cap = cv2.VideoCapture("NTQ.mkv") #cap = cv2.VideoCapture("/home/fitmta/Real-Time-Face-Detection-OpenCV-GPU/videos/video/out1.1.avi") #cap = cv2.VideoCapture("http://*****:*****@#[email protected]:8932/mjpg/video.mjpg") # cap = cv2.VideoCapture("http://*****:*****@[email protected]:8933/Streaming/channels/102/preview") success, frame = cap.read() startId = countIdFolder("./face_db/") # quit if unable to read the video file if not success: print('Failed to read video') sys.exit(1) #The color of the rectangle we draw around the face rectangleColor = (0, 165, 255) #variables holding the current frame number and the current faceid frameCounter = 0 currentFaceID = 0 #Variables holding the correlation trackers and the name per faceid conf = configparser.ConfigParser() conf.read("config/main.cfg") mtcnn_detector = load_mtcnn(conf) MODEL_PATH = conf.get("MOBILEFACENET", "MODEL_PATH") VERIFICATION_THRESHOLD = float( conf.get("MOBILEFACENET", "VERIFICATION_THRESHOLD")) FACE_DB_PATH = conf.get("MOBILEFACENET", "FACE_DB_PATH") BLUR_THRESH = int(conf.get("CUSTOM", "BLUR_THRESH")) MIN_FACE_SIZE = int(conf.get("MTCNN", "MIN_FACE_SIZE")) MAX_BLACK_PIXEL = int(conf.get("CUSTOM", "MAX_BLACK_PIXEL")) YAWL = int(conf.get("CUSTOM", "YAWL")) YAWR = int(conf.get("CUSTOM", "YAWR")) PITCHL = int(conf.get("CUSTOM", "PITCHL")) PITCHR = int(conf.get("CUSTOM", "PITCHR")) ROLLL = int(conf.get("CUSTOM", "ROLLL")) ROLLR = int(conf.get("CUSTOM", "ROLLR")) MAXDISAPPEARED = int(conf.get("CUSTOM", "MAXDISAPPEARED")) IS_FACE_THRESH = float(conf.get("CUSTOM", "IS_FACE_THRESH")) EXTEND_Y = int(conf.get("CUSTOM", "EXTEND_Y")) EXTEND_X = int(conf.get("CUSTOM", "EXTEND_X")) SIMILAR_THRESH = float(conf.get("CUSTOM", "SIMILAR_THRESH")) MAX_LIST_LEN = int(conf.get("CUSTOM", "MAX_LIST_LEN")) MIN_FACE_FOR_SAVE = int(conf.get("CUSTOM", "MIN_FACE_FOR_SAVE")) LIVE_TIME = int(conf.get("CUSTOM", "LIVE_TIME")) ROIXL = int(conf.get("CUSTOM", "ROIXL")) ROIXR = int(conf.get("CUSTOM", "ROIXR")) ROIYB = int(conf.get("CUSTOM", "ROIYB")) ROIYA = int(conf.get("CUSTOM", "ROIYA")) maxDisappeared = MAXDISAPPEARED ## khong xuat hien toi da 100 frame faces_db = load_faces(FACE_DB_PATH, mtcnn_detector) # load_face_db = ThreadingUpdatefacedb(FACE_DB_PATH,mtcnn_detector) time.sleep(10) for item in faces_db: print(item["name"]) listTrackedFace = [] mark_detector = MarkDetector() tm = cv2.TickMeter() _, sample_frame = cap.read() height, width = sample_frame.shape[:2] pose_estimator = PoseEstimator(img_size=(height, width)) with tf.Graph().as_default(): with tf.Session() as sess: load_mobilefacenet(MODEL_PATH) inputs_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") try: start = time.time() while True: start1 = time.time() retval, frame = cap.read() #Increase the framecounter frameCounter += 1 if retval: _frame = frame[ROIYA:ROIYB, ROIXL:ROIXR] cv2.rectangle(frame, (ROIXL, ROIYA), (ROIXR, ROIYB), (0, 0, 255), 2) good_face_index = [] # faces_db = load_face_db.face_db if (frameCounter % 1) == 0: ### embed and compare name for i, face_db in enumerate(faces_db): if not os.path.isdir( "./face_db/" + face_db["name"].split("_")[0]): faces_db.pop(i) faces, landmarks = mtcnn_detector.detect(_frame) if faces.shape[0] is not 0: input_images = np.zeros( (faces.shape[0], 112, 112, 3)) save_images = np.zeros( (faces.shape[0], 112, 112, 3)) (yaw, pitch, roll) = (0, 0, 0) for i, face in enumerate(faces): if round(faces[i, 4], 6) > IS_FACE_THRESH: bbox = faces[i, 0:4] points = landmarks[i, :].reshape( (5, 2)) nimg = face_preprocess.preprocess( _frame, bbox, points, image_size='112,112') save_images[i, :] = nimg nimg = nimg - 127.5 nimg = nimg * 0.0078125 input_images[i, :] = nimg (x1, y1, x2, y2) = bbox.astype("int") if x1 < 0 or y1 < 0 or x2 < 0 or y2 < 0 or x1 >= x2 or y1 >= y2: continue temp = int((y2 - y1) / EXTEND_Y) y1 = y1 + temp y2 = y2 + temp temp = int((x2 - x1) / EXTEND_X) if x1 > temp: x1 = x1 - temp x2 = x2 + temp # cv2.imshow("mainframe",frame) # cv2.imwrite("temp2.jpg",frame[y1:y2,x1:x2]) face_img = cv2.resize( _frame[y1:y2, x1:x2], (128, 128)) # cv2.imshow("ok",face_img) face_img = cv2.cvtColor( face_img, cv2.COLOR_BGR2RGB) tm.start() marks = mark_detector.detect_marks( [face_img]) tm.stop() marks *= (x2 - x1) marks[:, 0] += x1 marks[:, 1] += y1 # mark_detector.draw_marks( # frame, marks, color=(0, 255, 0)) pose, ( yaw, pitch, roll ) = pose_estimator.solve_pose_by_68_points( marks) # temp = frame # cv2.putText(temp,"yaw: "+str(yaw),(x2,y1),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), thickness=2) # cv2.putText(temp,"pitch: "+str(pitch),(x2,y1+25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), thickness=2) # cv2.putText(temp,"roll: "+str(roll),(x2,y1+50),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), thickness=2) # cv2.imshow("frame",temp) # if measureGoodFace(MIN_FACE_SIZE,MAX_BLACK_PIXEL,frame[y1:y2,x1:x2],yaw,pitch,roll,BLUR_THRESH,YAWL,YAWR,PITCHL,PITCHR,ROLLL,ROLLR): # good_face_index.append(i) # cv2.waitKey(0) # print(good_face_index) feed_dict = {inputs_placeholder: input_images} emb_arrays = sess.run(embeddings, feed_dict=feed_dict) emb_arrays = sklearn.preprocessing.normalize( emb_arrays) names = [] sims = [] for i, embedding in enumerate(emb_arrays): # if len(listTrackedFace)>i and RepresentsInt(listTrackedFace[i].name)==False: # names.append(listTrackedFace[i].name) # continue embedding = embedding.flatten() temp_dict = {} for com_face in faces_db: ret, sim = feature_compare( embedding, com_face["feature"], 0.65) temp_dict[com_face["name"]] = sim # print(temp_dict) dictResult = sorted(temp_dict.items(), key=lambda d: d[1], reverse=True) # print(dictResult[:5]) name = "" if len(dictResult) > 0 and dictResult[0][ 1] > VERIFICATION_THRESHOLD: name = dictResult[0][ 0] #.split("_")[0] sim = dictResult[0][1] ## wite log t = time.time() f.write(name + "___" + str((t - start) // 60) + ":" + str(int(t - start) % 60) + "\n") else: name = "unknown" sim = 0 names.append(name) sims.append(sim) # cv2.imwrite("./test/"+name+"_"+str(frameCounter//60)+":"+str(frameCounter%60)+".jpg",save_images[i,:]) # if len(dictResult)>0 : # cv2.imwrite("./test/"+names[i]+"_"+str(frameCounter//60)+":"+str(frameCounter%60)+"_"+str(dictResult[0][1])+".jpg",save_images[i,:]) ################################ tracker for i, embedding in enumerate(emb_arrays): embedding = embedding.flatten() ResultDict = {} for objectTrackFace in listTrackedFace: tempList = [] (x1, y1, x2, y2) = objectTrackFace.latestBox for com_face in objectTrackFace.listEmbedding: ret, sim = feature_compare( embedding, com_face, 0.65) tempList.append(sim) tempList.sort(reverse=True) if len(tempList) > 0: if tempList[0] > 0.9 or ( similarIOU( faces[i, :4].astype( "int"), objectTrackFace. latestBox) and (frameCounter - objectTrackFace .latestFrameCounter) < 3): ResultDict[objectTrackFace. name] = tempList[0] dictResult = sorted(ResultDict.items(), key=lambda d: d[1], reverse=True) if True: if len( ResultDict ) > 0 and dictResult[0][ 1] > SIMILAR_THRESH: ## neu khop -- 0.5 # for ik in range(len(dict)): # if dict[ik][1]>SIMILAR_THRESH: nameTrackCurrent = dictResult[0][0] for index, tempFaceTrack in enumerate( listTrackedFace): if tempFaceTrack.name == nameTrackCurrent: if len(tempFaceTrack. listImage ) > MAX_LIST_LEN: tempFaceTrack.listImage.pop( 0) tempFaceTrack.listEmbedding.pop( 0) if measureGoodFace( MIN_FACE_SIZE, MAX_BLACK_PIXEL, save_images[ i, :], yaw, pitch, roll, BLUR_THRESH, YAWL, YAWR, PITCHL, PITCHR, ROLLL, ROLLR): tempFaceTrack.listImage.append( save_images[ i, :]) tempFaceTrack.listEmbedding.append( emb_arrays[i]) else: if measureGoodFace( MIN_FACE_SIZE, MAX_BLACK_PIXEL, save_images[ i, :], yaw, pitch, roll, BLUR_THRESH, YAWL, YAWR, PITCHL, PITCHR, ROLLL, ROLLR): tempFaceTrack.listImage.append( save_images[ i, :]) tempFaceTrack.listEmbedding.append( emb_arrays[i]) if names[i] != "unknown": if RepresentsInt( nameTrackCurrent ): tempFaceTrack.name = names[ i] # else: ################# # names[i] = nameTrackCurrent else: if not RepresentsInt( nameTrackCurrent ): names[ i] = nameTrackCurrent tempFaceTrack.countDisappeared = 0 tempFaceTrack.latestBox = faces[ i, 0:4].astype("int") tempFaceTrack.latestFrameCounter = frameCounter tempFaceTrack.liveTime = 0 tempFaceTrack.justAdded = True ## but we still action with it break else: ## neu khong khop thi tao moi nhung chi them anh khi mat du tot if len(ResultDict) > 0: print(dictResult[0][1]) if names[i] != "unknown": newTrackFace = trackedFace( names[i]) else: newTrackFace = trackedFace( str(currentFaceID)) currentFaceID = currentFaceID + 1 if measureGoodFace( MIN_FACE_SIZE, MAX_BLACK_PIXEL, save_images[i, :], yaw, pitch, roll, BLUR_THRESH, YAWL, YAWR, PITCHL, PITCHR, ROLLL, ROLLR): newTrackFace.listImage.append( save_images[i, :]) newTrackFace.listEmbedding.append( emb_arrays[i]) newTrackFace.latestBox = faces[ i, 0:4].astype("int") newTrackFace.latestFrameCounter = frameCounter # print(newTrackFace.latestBox) newTrackFace.justAdded = True listTrackedFace.append( newTrackFace) ## add list ### disappeared for index, trackFace in enumerate( listTrackedFace): if trackFace.justAdded == False: trackFace.countDisappeared = trackFace.countDisappeared + 1 trackFace.liveTime = trackFace.liveTime + 1 else: trackFace.justAdded = False if trackFace.liveTime > LIVE_TIME: t = listTrackedFace.pop(index) del t if trackFace.countDisappeared > maxDisappeared: if len( trackFace.listImage ) < MIN_FACE_FOR_SAVE: ## neu chua duoc it nhat 5 mat thi xoa luon trackedFace.countDisappeared = 0 continue if trackFace.saveTrackedFace( "./temp/", startId): startId = startId + 1 t = listTrackedFace.pop(index) del t for i, face in enumerate(faces): x1, y1, x2, y2 = faces[i][0], faces[i][ 1], faces[i][2], faces[i][3] x1 = max(int(x1), 0) y1 = max(int(y1), 0) x2 = min(int(x2), _frame.shape[1]) y2 = min(int(y2), _frame.shape[0]) cv2.rectangle(frame, (x1 + ROIXL, y1 + ROIYA), (x2 + ROIXL, y2 + ROIYA), (0, 255, 0), 2) # if i in good_face_index: # if not RepresentsInt(names[i]): cv2.putText(frame, names[i].split("_")[0], (int(x1 / 2 + x2 / 2 + ROIXL), int(y1 + ROIYA)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) else: for index, trackFace in enumerate( listTrackedFace): trackFace.countDisappeared = trackFace.countDisappeared + 1 trackFace.liveTime = trackFace.liveTime + 1 if trackFace.liveTime > LIVE_TIME: t = listTrackedFace.pop(index) del t continue if trackFace.countDisappeared > maxDisappeared: if len( trackFace.listImage ) < MIN_FACE_FOR_SAVE: ## neu chua duoc it nhat 5 mat thi xoa luon trackedFace.countDisappeared = 0 continue if trackFace.saveTrackedFace( "./temp/", startId): startId = startId + 1 t = listTrackedFace.pop(index) del t end = time.time() cv2.putText(frame, "FPS: " + str(1 // (end - start1)), (400, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3) cv2.putText( frame, "Time: " + str((end - start) // 60) + ":" + str(int(end - start) % 60), (200, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 3) cv2.imshow("frame", frame) key = cv2.waitKey(30) if key & 0xFF == ord('q'): break if key == 32: cv2.waitKey(0) else: break except KeyboardInterrupt as e: pass gc.collect() cv2.destroyAllWindows() exit(0)