def main(args): frozen_model = args.frozen_model svc_model = args.svc_model mtcnn_model = args.mtcnn_model with open(svc_model, 'rb') as infile: (model, class_names) = pickle.load(infile) class_names_print = ['Lai Ngoc Thang Long', 'Luong Trong Tri', \ 'Ngo Song Viet Hoang', 'Nguyen Ba Đuc', 'Nguyen Hoang Son',\ 'Nguyen Dinh Dung', 'Nguyen Dinh Hung', 'Tran Quang Ha', 'Vu Minh Thanh'] detector = MTCNN(mtcnn_model) cap = cv2.VideoCapture(0) while (True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) faces, bboxs = cut_face(gray, detector) if faces: for i in range(len(faces)): face = faces[i] bounding_box = bboxs[i] index, score = facenet_svc(frozen_model, model, class_names, face) if score >= 0.5: cv2.rectangle(frame, (bounding_box[0], bounding_box[1]), (bounding_box[2], bounding_box[3]), (0, 155, 255), 2) cv2.putText(frame, class_names_print[index] + " : " + str(score), (bounding_box[0], bounding_box[1]), cv2.FONT_ITALIC, 0.8, (0, 255, 0), 1, cv2.LINE_AA) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
def main(): meta_file = './models/model8/model.meta' ckpt_file = './models/model8/model.ckpt-63234' image_size = 112 with tf.Graph().as_default(): with tf.Session() as sess: print('Loading feature extraction model.') saver = tf.train.import_meta_graph(meta_file) saver.restore(tf.get_default_session(), ckpt_file) graph = tf.get_default_graph() images_placeholder = graph.get_tensor_by_name('image_batch:0') phase_train_placeholder = graph.get_tensor_by_name('phase_train:0') landmark_L1 = graph.get_tensor_by_name('landmark_L1:0') landmark_L2 = graph.get_tensor_by_name('landmark_L2:0') landmark_L3 = graph.get_tensor_by_name('landmark_L3:0') landmark_L4 = graph.get_tensor_by_name('landmark_L4:0') landmark_L5 = graph.get_tensor_by_name('landmark_L5:0') landmark_total = [ landmark_L1, landmark_L2, landmark_L3, landmark_L4, landmark_L5 ] cap = cv2.VideoCapture(0) mtcnn = MTCNN() while True: ret, image = cap.read() height, width, _ = image.shape if not ret: break boxes = mtcnn.predict(image) for box in boxes: score = box[4] x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(max([w, h]) * 1.1) cx = x1 + w // 2 cy = y1 + h // 2 x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1) edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) cropped = image[y1:y2, x1:x2] if (dx > 0 or dy > 0 or edx > 0 or edy > 0): cropped = cv2.copyMakeBorder(cropped, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0) cropped = cv2.resize(cropped, (image_size, image_size)) input = cv2.resize(cropped, (image_size, image_size)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = input.astype(np.float32) / 256.0 input = np.expand_dims(input, 0) feed_dict = { images_placeholder: input, phase_train_placeholder: False } cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0)) pre_landmarks = sess.run(landmark_total, feed_dict=feed_dict) pre_landmark = pre_landmarks[0] pre_landmark = pre_landmark.reshape(-1, 2) * [size, size] for (x, y) in pre_landmark.astype(np.int32): cv2.circle(image, (x1 + x, y1 + y), 1, (0, 0, 255)) cv2.imshow('0', image) if cv2.waitKey(10) == 27: break
def getLandMarks(img_path): image_size = 112 lite_filename = 'deploy/landmarks68/pfld_landmarks.tflite' # load TFLite model and allocate tensors interpreter = tf.lite.Interpreter(model_path=lite_filename) interpreter.allocate_tensors() # get input and output tensors. input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() with tf.Graph().as_default(): with tf.Session() as sess: mtcnn = MTCNN() image = cv2.imread(img_path) height, width, _ = image.shape boxes, landmarks_mtcnn = mtcnn.predict(image) if len(landmarks_mtcnn) > 0: landmarks_mtcnn = np.asarray(landmarks_mtcnn).reshape(len(boxes), 10) for box in boxes: score = box[4] x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) # mtcnn boundding box fine adjustment dta_y = (min(landmarks_mtcnn[0][5], landmarks_mtcnn[0][6]) - y1) / 2.6 y1 = int(y1 + dta_y) y2 = int(y2 + dta_y / 2) w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(max([w, h]) * 1.05) cx = x1 + w // 2 cy = y1 + h // 2 x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1) edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) cropped = image[y1:y2, x1:x2] if (dx > 0 or dy > 0 or edx > 0 or edy > 0): cropped = cv2.copyMakeBorder(cropped, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0) cropped = cv2.resize(cropped, (image_size, image_size)) input = cv2.resize(cropped, (image_size, image_size)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = input.astype(np.float32) / 256.0 input = np.expand_dims(input, 0) interpreter.set_tensor(input_details[0]['index'], input) interpreter.invoke() pre_landmarks = interpreter.get_tensor(output_details[0]['index']) pre_landmark = pre_landmarks[0] pre_landmark = pre_landmark.reshape(-1, 2) * [size, size] return pre_landmark, x1, y1
def main(): #meta_file = './models2/model4/model.meta' #ckpt_file = './models2/model4/model.ckpt-61' meta_file = './models1/model_test/model.meta' ckpt_file = './models1/model_test/model.ckpt-13' image_size = 112 with tf.Graph().as_default(): with tf.compat.v1.Session() as sess: print('Loading feature extraction model.') saver = tf.compat.v1.train.import_meta_graph(meta_file) saver.restore(tf.compat.v1.get_default_session(), ckpt_file) graph = tf.compat.v1.get_default_graph() images_placeholder = graph.get_tensor_by_name('image_batch:0') phase_train_placeholder = graph.get_tensor_by_name('phase_train:0') landmarks = graph.get_tensor_by_name('pfld_inference/fc/BiasAdd:0') # landmark_L1 = graph.get_tensor_by_name('landmark_L1:0') # landmark_L2 = graph.get_tensor_by_name('landmark_L2:0') # landmark_L3 = graph.get_tensor_by_name('landmark_L3:0') # landmark_L4 = graph.get_tensor_by_name('landmark_L4:0') # landmark_L5 = graph.get_tensor_by_name('landmark_L5:0') # landmark_total = [landmark_L1, landmark_L2, landmark_L3, landmark_L4, landmark_L5] cap = cv2.VideoCapture(0) cap_width = 640 cap_height = 480 cap.set(3, cap_width) cap.set(4, cap_height) mtcnn = MTCNN() while True: ret, image = cap.read() height, width, _ = image.shape if not ret: break boxes = mtcnn.predict(image) for box in boxes: # score = box[4] x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(max([w, h]) * 1.1) # center point cx = x1 + w // 2 cy = y1 + h // 2 # square x1 = cx - size // 2 #可能为负值 y1 = cy - size // 2 #可能为负值 x2 = x1 + size # 可能大于宽度 y2 = y1 + size # 可能大于宽度 dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1) edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) # print('box->', 'x1:', x1, 'y1:', y1, 'x2:', x2, 'y2:', y2, # 'in', 'w:', width, 'h:', height) # print('box->', 'top:', dy, 'bottom:', edy, 'left:', dx, # 'right:', edx, 'in', 'w:', width, 'h:', height) cropped = image[y1:y2, x1:x2] if (dx > 0 or dy > 0 or edx > 0 or edy > 0): # 当裁区域越出原图时,进行必要的黑边填充 # print('padding', dy, edy, dx, edx) cropped = cv2.copyMakeBorder(cropped, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0) # 缩放到112 size cropped = cv2.resize(cropped, (image_size, image_size)) cv2.imshow('cropped', cropped) input = cv2.resize(cropped, (image_size, image_size)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = input.astype(np.float32) / 256.0 input = np.expand_dims(input, 0) feed_dict = { images_placeholder: input, phase_train_placeholder: False } cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0)) pre_landmarks = sess.run(landmarks, feed_dict=feed_dict) pre_landmark = pre_landmarks[0] pre_landmark = pre_landmark.reshape(-1, 2) * [size, size] for (x, y) in pre_landmark.astype(np.int32): cv2.circle(image, (x1 + x, y1 + y), 1, (0, 0, 255)) cv2.imshow('0', image) if cv2.waitKey(10) == 27: break
def read_images(): meta_file = './test_models2/model.meta' ckpt_file = './test_models2/model.ckpt-59' main_dir = './test_models2/DSM_lina/Lina15' landmark_file = '%s/landmarks1.txt' % main_dir result_image_path = '%s/result_image' % main_dir if not os.path.exists(result_image_path): os.mkdir(result_image_path) image_size = 112 with tf.Graph().as_default(): with tf.Session(config=tf.ConfigProto( log_device_placement=True)) as sess: print('Loading feature extraction model.') saver = tf.train.import_meta_graph(meta_file) saver.restore(tf.get_default_session(), ckpt_file) graph = tf.get_default_graph() images_placeholder = graph.get_tensor_by_name('image_batch:0') phase_train_placeholder = graph.get_tensor_by_name('phase_train:0') landmark_total = graph.get_tensor_by_name( 'pfld_inference/fc/BiasAdd:0') mtcnn = MTCNN() sum_time = 0. num_face_box = 0 with open(landmark_file, 'r') as fr: lines = fr.readlines() for index, line in enumerate(lines): line_split = line.strip('\t\n').split(' ', 1) image_path = '%s/%s' % (main_dir, line_split[0]) image = cv2.imread(image_path) if image is None: continue height, width, _ = image.shape boxes = mtcnn.predict(image) # boxes, _ = predict(image) for box in boxes: start_time = time.time() x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) w = x2 - x1 + 1 h = y2 - y1 + 1 size = int(max([w, h]) * 1.1) cx = x1 + w // 2 cy = y1 + h // 2 x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size dx = max(0, -x1) dy = max(0, -y1) x1 = max(0, x1) y1 = max(0, y1) edx = max(0, x2 - width) edy = max(0, y2 - height) x2 = min(width, x2) y2 = min(height, y2) cropped = image[y1:y2, x1:x2] if dx > 0 or dy > 0 or edx > 0 or edy > 0: cropped = cv2.copyMakeBorder( cropped, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0) cropped = cv2.resize(cropped, (image_size, image_size)) input = cv2.resize(cropped, (image_size, image_size)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = input.astype(np.float32) / 256.0 input = np.expand_dims(input, 0) feed_dict = { images_placeholder: input, phase_train_placeholder: False } pre_landmarks = sess.run(landmark_total, feed_dict=feed_dict) pre_landmark = pre_landmarks[0] pre_landmark = pre_landmark.reshape(-1, 2) * [size, size] sum_time += (time.time() - start_time) num_face_box += 1 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0)) for (x, y) in pre_landmark.astype(np.int32): cv2.circle(image, (x1 + x, y1 + y), 1, (0, 0, 255), 2) # points = line_split[1].split(',') # # for i in range(68): # cv2.circle(image, (int(float(points[i*2])), int(float(points[i*2+1]))), 1, (255, 0, 0), 2) # for i in range(68): # cv2.circle(image, (int(float(points[i*2]) * width), int(float(points[i*2+1])*height)), # 1, (255, 0, 0), 2) # cv2.imwrite('%s/%s' % (result_image_path, line_split[0].rsplit('/', 1)[1]), image) cv2.imshow('0', image) if cv2.waitKey(10) == 27: break # img_path = './test_models2/DSM_lina/01.bmp' # # image_size = 112 # with tf.Graph().as_default(): # with tf.Session() as sess: # print('Loading feature extraction model.') # saver = tf.train.import_meta_graph(meta_file) # saver.restore(tf.get_default_session(), ckpt_file) # # graph = tf.get_default_graph() # images_placeholder = graph.get_tensor_by_name('image_batch:0') # phase_train_placeholder = graph.get_tensor_by_name('phase_train:0') # landmark_total = graph.get_tensor_by_name('pfld_inference/fc/BiasAdd:0') # # # image = cv2.imread(img_path) # if image is None: # print('Error:image is None!') # height, width, _ = image.shape # boxes, _ = predict(image) # for box in boxes: # x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) # w = x2 - x1 + 1 # h = y2 - y1 + 1 # # size = int(max([w, h]) * 1.1) # cx = x1 + w // 2 # cy = y1 + h // 2 # x1 = cx - size // 2 # x2 = x1 + size # y1 = cy - size // 2 # y2 = y1 + size # # dx = max(0, -x1) # dy = max(0, -y1) # x1 = max(0, x1) # y1 = max(0, y1) # # edx = max(0, x2 - width) # edy = max(0, y2 - height) # x2 = min(width, x2) # y2 = min(height, y2) # # cropped = image[y1:y2, x1:x2] # if dx > 0 or dy > 0 or edx > 0 or edy > 0: # cropped = cv2.copyMakeBorder(cropped, dy, edy, dx, edx, cv2.BORDER_CONSTANT, 0) # cropped = cv2.resize(cropped, (image_size, image_size)) # # input = cv2.resize(cropped, (image_size, image_size)) # input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) # input = input.astype(np.float32) / 256.0 # input = np.expand_dims(input, 0) # # feed_dict = { # images_placeholder: input, # phase_train_placeholder: False # } # cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0)) # pre_landmarks = sess.run(landmark_total, feed_dict=feed_dict) # pre_landmark = pre_landmarks[0] # # pre_landmark = pre_landmark.reshape(-1, 2) * [size, size] # for (x, y) in pre_landmark.astype(np.int32): # cv2.circle(image, (x1 + x, y1 + y), 1, (0, 0, 255), 2) # # cv2.imshow('0', image) # cv2.waitKey(0) average_time = sum_time * 1000.0 / num_face_box print("nums:{}, sum time:{:.3f}s, avg time:{:.3f}ms\n".format( num_face_box, sum_time, average_time))