def demo_using_images(): img = Image.open('images/0_fp_1_aligned.jpg') bounding_boxes, landmarks = detect_faces(img) img = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR) show_bboxes(img, bounding_boxes, landmarks) img = Image.open('images/0_fp_0_aligned.jpg') bounding_boxes, landmarks = detect_faces(img) img = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR) show_bboxes(img, bounding_boxes, landmarks) img = Image.open('images/0_fn_0_aligned.jpg') bounding_boxes, landmarks = detect_faces(img) img = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR) show_bboxes(img, bounding_boxes, landmarks) img = Image.open('images/5_fn_0_aligned.jpg') bounding_boxes, landmarks = detect_faces(img) img = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR) show_bboxes(img, bounding_boxes, landmarks) img = Image.open('images/6_fn_1_aligned.jpg') bounding_boxes, landmarks = detect_faces(img) img = cv.cvtColor(np.array(img), cv.COLOR_RGB2BGR) show_bboxes(img, bounding_boxes, landmarks)
def image_loader(image_path, out_size, device, transform=None): global dst_img raw = cv.imread(image_path) img = Image.open(image_path).convert('RGB') _, facial5points = detect_faces(img) if len(facial5points) != 1: raise Exception("No face or multi faces...") facial5points = np.reshape(facial5points[0], (2, 5)) # Default set crop_size = 224 inner_padding_factor = 0.1 outer_padding = 0 output_size = 224 # get the reference 5 landmarks position in the crop settings reference_5pts = get_reference_facial_points( (output_size, output_size), inner_padding_factor, (outer_padding, outer_padding), True) dst_img = warp_and_crop_face(raw, facial5points, reference_pts=reference_5pts, crop_size=(crop_size, crop_size)) dst_img = cv.resize(dst_img, (out_size, out_size))[:, :, ::-1] dst_img_ = Image.fromarray(dst_img) im = transform(dst_img_).float() return im.unsqueeze(0).to(device)
def camera(plfd_backbone, video_path, is_video): transform = transforms.Compose([transforms.ToTensor()]) if is_video: cap = cv2.VideoCapture(video_path) else: cap = cv2.VideoCapture(0) while True: ret, img = cap.read() if not ret: break height, width = img.shape[:2] bounding_boxes, landmarks = detect_faces(img) for box in bounding_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 = img[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, (112, 112)) input = cv2.resize(cropped, (112, 112)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = transform(input).unsqueeze(0).cuda(0) with torch.no_grad(): landmarks, _ = plfd_backbone(input) pre_landmark = landmarks[0] pre_landmark = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [size, size] for (x, y) in pre_landmark.astype(np.int32): cv2.circle(img, (x1 + x, y1 + y), 0, (0, 0, 255), 2) cv2.rectangle(img, tuple(box[:2].astype(np.int)), tuple(box[2:4].astype(np.int)), (0, 0, 255), 1) cv2.imshow('0', img) if cv2.waitKey(10) == 27: break
def auto_anno(dir_path): for f in os.listdir(dir_path): p = os.path.join(dir_path, f) print p if ".xml" in p: continue image = ip.image_to_3channels(p, True) if image is None: continue start = time.clock() bounding_boxes, landmarks = det.detect_faces(image) end = time.clock() print "take %sms" % (1000 * end - 1000 * start), "num faces:", len(bounding_boxes) if len(bounding_boxes) > 0: bounding_boxes = [[bbox[0], bbox[1], bbox[2], bbox[3]] for bbox in bounding_boxes] bounding_boxes = np.round(bounding_boxes) bounding_boxes = np.asarray(bounding_boxes, np.int) ap.save_anno_to_xml(p, image.size[0], image.size[1], 3, annos=bounding_boxes)
def detection(self, imgpath): img = cv2.imread(imgpath) bounding_boxes, _ = detect_faces(img) ul = np.array(bounding_boxes[0, 0:2]) br = np.array(bounding_boxes[0, 2:4]) w = br[0] - ul[0] h = br[1] - ul[1] uls = (ul - np.array([w * 0.1, h * 0.1])).astype(np.int) brs = (br + np.array([w * 0.1, h * 0.1])).astype(np.int) w, h = brs - uls if h > w: diff = h - w uls[0] = uls[0] - math.floor(diff / 2) brs[0] = brs[0] + math.ceil(diff / 2) else: diff = w - h uls[1] = uls[1] - math.floor(diff / 2) brs[1] = brs[1] + math.floor(diff / 2) self.uls = uls self.brs = brs self.box = [*uls, *brs] self.factor = (self.brs[0] - self.uls[0]) / 112 img112 = cv2.resize(deepcopy(img[uls[1]:brs[1], uls[0]:brs[0], :]), (112, 112), interpolation=cv2.INTER_CUBIC) return img112
def main(args): checkpoint = torch.load(args.model_path, map_location=device) plfd_backbone = PFLDInference().to(device) plfd_backbone.load_state_dict(checkpoint['plfd_backbone']) plfd_backbone.eval() plfd_backbone = plfd_backbone.to(device) transform = transforms.Compose([transforms.ToTensor()]) cap = cv2.VideoCapture(0) while True: ret, img = cap.read() if not ret: break height, width = img.shape[:2] bounding_boxes, landmarks = detect_faces(img) for box in bounding_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 = img[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, (112, 112)) input = cv2.resize(cropped, (112, 112)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = transform(input).unsqueeze(0).to(device) _, landmarks = plfd_backbone(input) pre_landmark = landmarks[0] pre_landmark = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [size, size] for (x, y) in pre_landmark.astype(np.int32): cv2.circle(img, (x1 + x, y1 + y), 1, (0, 0, 255)) cv2.imshow('0', img) if cv2.waitKey(10) == 27: break
def get_faces_mtcnn(self, img_bgr): """ 基于MTCNN检测人脸 """ from mtcnn.detector import detect_faces bbox_mtcnn, landmarks_mtcnn = detect_faces(img_bgr, self.mtcnn_pro, min_face_size=50) bbox_list, lms_list, size_list = [], [], [] for bbox, lms in zip(bbox_mtcnn, landmarks_mtcnn): box_prob = bbox[4] # 置信度 if box_prob < 0.9: # 小于0.9直接pass continue box = bbox[0:4].astype(np.int32) bbox_list.append(box) lms_tmp = lms.astype(np.int32) lms_points = [] for x, y in zip(lms_tmp[0:5], lms_tmp[5:10]): lms_points.append([x, y]) lms_list.append(lms_points) # draw_box(img_bgr, box, is_new=False) # draw_points(img_bgr, lms_points, is_new=False) # from root_dir import IMGS_DIR # img_path = os.path.join(IMGS_DIR, "mtcnn_res.jpg") # cv2.imwrite(img_path, img_bgr) return bbox_list, lms_list
def main(args): checkpoint = torch.load(args.model_path, map_location=device) pfld_backbone = PFLDInference().to(device) pfld_backbone.load_state_dict(checkpoint['plfd_backbone']) pfld_backbone.eval() pfld_backbone = pfld_backbone.to(device) transform = transforms.Compose([transforms.ToTensor()]) im = Image.open(args.image_path) img = np.array(im) height, width = img.shape[:2] draw = ImageDraw.Draw(im) bounding_boxes, landmarks = detect_faces(img) print(bounding_boxes) for box in bounding_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 = img[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, (112, 112)) input = cv2.resize(cropped, (112, 112)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = transform(input).unsqueeze(0).to(device) landmarks = pfld_backbone(input) pre_landmark = landmarks[0] pre_landmark = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [size, size] print(pre_landmark) for (x, y) in pre_landmark.astype(np.int32): # cv2.circle(img, (x1 + x, y1 + y), 1, (0, 0, 255)) draw.ellipse((x1 + x - 1, y1 + y - 1, x1 + x + 1, y1 + y + 1), fill=(0, 0, 255)) im.show()
def get_central_face_attributes(full_path): img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) if len(landmarks) > 0: i = select_central_face(img.size, bounding_boxes) return True, [bounding_boxes[i]], [landmarks[i]] return False, None, None
def get_faces_v2(self, face_dict): """ 基于MTCNN检测人脸 :param face_dict: 数据字典 :return: 人脸检测框 """ img_op = face_dict['img'] bounding_boxes, landmarks = detect_faces(img_op) return bounding_boxes
def get_all_face_attributes(full_path): try: img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) if len(landmarks) > 0: return True, bounding_boxes, landmarks except KeyboardInterrupt: raise except: pass return False, None, None
def extract_keypoints(img_path, model_path): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") checkpoint = torch.load(model_path, map_location=device) pfld_backbone = PFLDInference().to(device) pfld_backbone.load_state_dict(checkpoint['pfld_backbone']) pfld_backbone.eval() pfld_backbone = pfld_backbone.to(device) transform = torchvision.transforms.Compose( [torchvision.transforms.ToTensor()]) img = cv2.imread(img_path, 1) height, width = img.shape[:2] bounding_boxes, landmarks = detect_faces(img) for box in bounding_boxes: x1, y1, x2, y2 = (box[:4] + 0.5).astype(np.int32) w = x2 - x1 + 1 # 宽度 h = y2 - y1 + 1 # 高度 cx = x1 + w // 2 # 中心宽度 cy = y1 + h // 2 # 中心高度 size = int(max([w, h]) * 1.1) x1 = cx - size // 2 x2 = x1 + size y1 = cy - size // 2 y2 = y1 + size x1 = max(0, x1) y1 = max(0, y1) x2 = min(width, x2) y2 = min(height, y2) edx1 = max(0, -x1) edy1 = max(0, -y1) edx2 = max(0, x2 - width) edy2 = max(0, y2 - height) cropped = img[y1:y2, x1:x2] if edx1 > 0 or edy1 > 0 or edx2 > 0 or edy2 > 0: cropped = cv2.copyMakeBorder(cropped, edy1, edy2, edx1, edx2, cv2.BORDER_CONSTANT, 0) input = cv2.resize(cropped, (112, 112)) cv2.imwrite(img_path.replace(".png", "_1.png"), input) input = transform(input).unsqueeze(0).to(device) _, landmarks = pfld_backbone(input) pre_landmark = landmarks[0] key_points = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [size, size] - [edx1, edy1] keypoints = [] for (x, y) in key_points: cv2.circle(img, (x1 + int(x), y1 + int(y)), 2, (0, 255, 0), -1) keypoints.append([x1 + int(x), y1 + int(y)]) cv2.imshow('face_landmark_68', img) cv2.waitKey(1000) return keypoints
def get_face_all_attributes(full_path): try: img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) if len(landmarks) > 0: i = select_central_face(img.size, bounding_boxes) return True, [bounding_boxes[i]], [landmarks[i]] except KeyboardInterrupt: raise except: pass return False, None, None
def predict_landmarks(self, img_op): height, width = img_op.shape[:2] bounding_boxes, landmarks = detect_faces(img_op) # 检测人脸 res_list = [] for box in bounding_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 = img_op[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, (112, 112)) input = cv2.resize(cropped, (112, 112)) input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB) input = self.transform(input).unsqueeze(0).to(self.device) _, landmarks = self.plfd_backbone(input) pre_landmark = landmarks[0] pre_landmark = pre_landmark.cpu().detach().numpy().reshape( -1, 2) * [size, size] res_list.append([pre_landmark, x1, y1]) # 添加 return res_list
def main(args): checkpoint = torch.load(args.model_path) plfd_backbone = PFLDInference().cuda() plfd_backbone.load_state_dict(checkpoint['plfd_backbone']) plfd_backbone.eval() plfd_backbone = plfd_backbone.cuda() transform = transforms.Compose([transforms.ToTensor()]) cap = cv2.VideoCapture(0) while True: ret, img = cap.read() if not ret: break bounding_boxes, landmarks = detect_faces(img) maxval_index = 0 if (len(bounding_boxes) > 0): maxval_index = np.argmax(bounding_boxes[:, 4]) if (len(bounding_boxes) > maxval_index - 1 and len(bounding_boxes) > 0): face = img[int(bounding_boxes[maxval_index][1] ):int(bounding_boxes[maxval_index][3]), int(bounding_boxes[maxval_index][0] ):int(bounding_boxes[maxval_index][2])] if face.size == 0: continue face_resized = cv2.resize(face, dsize=(112, 112), interpolation=cv2.INTER_LINEAR) face_resized = cv2.cvtColor(face_resized, cv2.COLOR_BGR2RGB) face_resized = transform(face_resized).unsqueeze(0).cuda() _, landmarks = plfd_backbone(face_resized) pre_landmark = landmarks.cpu().detach().numpy()[0].reshape( -1, 2) * [ int(bounding_boxes[maxval_index][2]) - int(bounding_boxes[maxval_index][0]), int(bounding_boxes[maxval_index][3]) - int(bounding_boxes[maxval_index][1]) ] for (x, y) in pre_landmark.astype(np.int32): cv2.circle(face, (x, y), 1, (255, 0, 0), -1) cv2.imshow("xxxx", face) if cv2.waitKey(10) == 27: break
def setUp(self): img_fn = 'images/0_img.jpg' self.image = cv.imread(img_fn, True) img = Image.open(img_fn).convert('RGB') _, facial5points = detect_faces(img) facial5points = np.reshape(facial5points[0], (2, 5)) self.facial5points = facial5points default_square = True inner_padding_factor = 0 outer_padding = (0, 0) output_size = (112, 112) self.reference_5pts = get_reference_facial_points( output_size, inner_padding_factor, outer_padding, default_square) self.output_size = output_size
def detect_test(path): for f in os.listdir(path): p = os.path.join(path, f) image = ip.image_to_3channels(p, True) if image is None: continue start = time.clock() bounding_boxes, landmarks = det.detect_faces(image) end = time.clock() print "take %sms" % (1000 * end - 1000 * start), "num faces:", len(bounding_boxes) if len(bounding_boxes) > 0: draw = ImageDraw.Draw(image) for bbox in bounding_boxes: bbox = (bbox[0], bbox[1], bbox[2], bbox[3]) draw.rectangle(bbox, outline="red") plt.imshow(image) plt.show()
def demo_using_webcam(): cam = VideoCapture(0) # set the port of the camera as before cam.set(cv2.CAP_PROP_FPS, 15) cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) cv2.namedWindow("image", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("image", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) while True: ret_val, img = cam.read( ) # return a True boolean and and the image if all go right if ret_val: bounding_boxes, landmarks = detect_faces(Image.fromarray(img)) show_bboxes(img, bounding_boxes, landmarks, wait=False) else: break cam.release() # Closes video file or capturing device.
def get_face_attributes(full_path): try: img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) width, height = img.size if len(bounding_boxes) == 1: x1, y1, x2, y2 = bounding_boxes[0][0], bounding_boxes[0][ 1], bounding_boxes[0][2], bounding_boxes[0][3] if x1 < 0 or x1 >= width or x2 < 0 or x2 >= width or y1 < 0 or y1 >= height or y2 < 0 or y2 >= height or x1 >= x2 or y1 >= y2: return False, None, None landmarks = [int(round(x)) for x in landmarks[0]] is_valid = (x2 - x1) > width / 10 and (y2 - y1) > height / 10 return is_valid, (int(round(x1)), int(round(y1)), int(round(x2)), int(round(y2))), landmarks except KeyboardInterrupt: raise except: pass return False, None, None
def get_your_faceImg_feature(name, model, features_dict): faceImg_path = './video/your_face_img' num_img_useful = 0 for idx, img_name in enumerate(os.listdir(faceImg_path)): # detect face in your img img = Image.open(faceImg_path + '/' + img_name).convert('RGB') try: bounding_boxes, landmarks = detect_faces(img, pnet=pnet, rnet=rnet, onet=onet, device=device) except: continue if len(bounding_boxes) == 0: continue # crop the face from the img (112, 112) face_img, box = crop_face(np.array(img), bounding_boxes, landmarks) feature = get_face_feature(model, face_img) features_dict[name + '_' + str(idx)] = feature num_img_useful += 1 print('Successfully collected your face img for %ds ' % num_img_useful) return features_dict
def face_detect(): start = time.time() ensure_folder(STATIC_DIR) ensure_folder(UPLOAD_DIR) file = request.files['file'] fn = secure_filename(file.filename) full_path = os.path.join(UPLOAD_DIR, fn) file.save(full_path) # resize(full_path) print('full_path: ' + full_path) img = Image.open(full_path).convert('RGB') bboxes, landmarks = detect_faces(img) num_faces = len(bboxes) if num_faces > 0: img = cv.imread(full_path) draw_bboxes(img, bboxes, landmarks) cv.imwrite(full_path, img) elapsed = time.time() - start return num_faces, float(elapsed), str(fn), bboxes, landmarks
def show_img(self): global temp_t success, self.img = self.camera.read() if success: self.Image_num += 1 if self.Image_num % 10 == 9: frame_rate = 10 / (time.clock() - self.timelb) self.FmRateLCD.display(frame_rate) self.timelb = time.clock() if self.case == 0: showImg = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB) showImg = qimage2ndarray.array2qimage(showImg) self.Camera_2.setPixmap(QPixmap(showImg)) # 展示图片 self.Camera_2.show() if self.case == 1: bounding_boxes, landmarks = detect_faces(self.img) self.img = show_bboxes(self.img, bounding_boxes, landmarks) showImg = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB) showImg = qimage2ndarray.array2qimage(showImg) self.Camera_2.setPixmap(QPixmap(showImg)) # 展示图片 self.Camera_2.show() if self.case == 2: img_copy = self.img.copy() frag_gray = False self.time_ing = time.time() # point=[100,0,540,480] if self.frag_cap: bounding_boxes, landmarks = detect_faces(self.img) print('正在定位······') if len(bounding_boxes) == 1: self.point.clear() for b in bounding_boxes: b = [int(round(value)) for value in b] for i in b: self.point.append(i) self.frag_cap = False # print(point) # cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 2) # 裁剪坐标为[y0:y1, x0:x1] if not self.frag_cap: if self.point[0] < 540: self.img = self.img[self.point[1] - 10:479, self.point[0] - 100:self.point[2] + 100] else: self.img = self.img[self.point[1] - 10:479, self.point[0] - 100:639] else: self.img = self.img[1:479, 1:640] if int(self.time_ing - self.time_first) % 60 == 0: self.frag_cap = True else: self.frag_cap = False bounding_boxes, landmarks = detect_faces(self.img) #通过MTCNN人脸框判断,当检测不到人脸时判断低头or瞌睡 if len(bounding_boxes) == 0: self.nod_fps += 1 if self.nod_fps >= 3: self.Head_state.setText('点头') self.nod_count += 1 if len(bounding_boxes) > 0: self.nod_fps = 0 #通过头部姿态欧拉角角度变化判断是否摇头 if len(bounding_boxes) > 0: Head_Y_X_Z = get_head_pose(landmarks) print('pitch:{}, yaw:{}, roll:{}'.format( Head_Y_X_Z[1], Head_Y_X_Z[2], Head_Y_X_Z[3])) if (Head_Y_X_Z[2] < -0.75): self.shake_fps_l += 1 if (Head_Y_X_Z[2] >= -0.75): self.shake_fps_l = 0 if self.shake_fps_l >= 5: self.shake_count += 1 self.Head_state.setText('摇头') if Head_Y_X_Z[3] >= 0.30: self.shake_fps_r += 1 if self.shake_fps_r >= 5: self.shake_count += 1 self.Head_state.setText('摇头') if Head_Y_X_Z[3] < 0.30: self.shake_fps_r = 0 # print(Head_Y_X_Z[1]) # print(Head_Y_X_Z[2]) # print(Head_Y_X_Z[3]) if time.time() - self.nod_start > 3: self.Head_state.setText('') if time.time() - self.shake_start > 3: self.Head_state.setText('') # 计算低头频率 每10s计算一次 if time.time() - self.nod_start > 10: times = time.time() - self.nod_start self.nod_freq = self.nod_count / times self.nod_start = time.time() self.Nod_LCD.display(self.nod_freq) # 计算摇头频率 if time.time() - self.shake_start > 10: times = time.time() - self.shake_start self.shake_freq = self.shake_count / times self.shake_start = time.time() self.shake_LCD.display(self.shake_freq) if len(bounding_boxes) > 0: Emotions = get_emotion( get_face_expression(self.img, bounding_boxes)) self.Emotion.setText(Emotions[1]) self.Emotion_pred.display(float(Emotions[0])) # print(Emotions) canvas = cv2.imread('img_resource/label_pred.jpg', flags=cv2.IMREAD_UNCHANGED) for (i, (emotion, prob)) in enumerate(zip(self.EMOTIONS, Emotions[2])): # text = "{}: {:.2f}%".format(emotion, prob * 100) text = "{:.2f}%".format(prob * 100) # 绘制表情类和对应概率的条形图 w = int(prob * 180) # print(text) # canvas = 255 * np.ones((250, 300, 3), dtype="uint8") cv2.rectangle(canvas, (0, (i * 44) + 25), (w, (i * 43) + 40), (100, 200, 130), -1) cv2.putText(canvas, text, (170, (i * 43) + 40), cv2.FONT_HERSHEY_DUPLEX, 0.6, (0, 0, 0), 1) show = cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB) showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) # cv2.imshow('test', showImage) # showImg=QPixmap(showImage) self.label_pred_img.setPixmap( QtGui.QPixmap.fromImage(showImage)) # # print('test') # print('Head_Y_X_Z') # print(Head_Y_X_Z) x = cv2.resize(self.img, (300, 300)).astype(np.float32) flag_B = True # 是否闭眼的flag flag_Y = False num_rec = 0 # 检测到的眼睛的数量 # 分界线 x -= self.img_mean x = x.astype(np.float32) x = x[:, :, ::-1].copy() x = torch.from_numpy(x).permute(2, 0, 1) xx = Variable(x.unsqueeze(0)) # if torch.cuda.is_available(): # xx = xx.cuda() xx = xx.cuda() y = self.net(xx) softmax = nn.Softmax(dim=-1) detect = Detect(config.class_num, 0, 200, 0.01, 0.45) priors = utils.default_prior_box() loc, conf = y loc = torch.cat([o.view(o.size(0), -1) for o in loc], 1) conf = torch.cat([o.view(o.size(0), -1) for o in conf], 1) detections = detect( loc.view(loc.size(0), -1, 4), softmax(conf.view(conf.size(0), -1, config.class_num)), torch.cat([o.view(-1, 4) for o in priors], 0)).data labels = VOC_CLASSES # 将检测结果放置于图片上 scale = torch.Tensor(self.img.shape[1::-1]).repeat(2) self.img = show_bboxes(self.img, bounding_boxes, landmarks) for i in range(detections.size(1)): j = 0 while detections[0, i, j, 0] >= 0.4: score = detections[0, i, j, 0] label_name = labels[i - 1] if label_name == 'calling' and score > 0.8: self.Danger_state.setText('打电话') self.danger_count += 1 frag_gray = True if label_name == 'smoke' and score > 0.8: self.Danger_state.setText('吸烟') self.danger_count += 1 frag_gray = True if label_name != 'smoke' and label_name != 'calling': self.danger_t += 1 if self.danger_t >= 20: self.Danger_state.setText('') self.danger_t = 0 if label_name == 'open_eye': self.open_t += 1 if self.open_t >= 20: self.Eyes_state.setText('') self.open_t = 0 if label_name == 'closed_mouth': self.Mouth_state.setText(' ') if label_name == 'closed_eye': flag_B = False frag_gray = True if label_name == 'open_mouth': flag_Y = True display_txt = '%s:%.2f' % (label_name, score) pt = (detections[0, i, j, 1:] * scale).cpu().numpy() self.coords = ( pt[0], pt[1]), pt[2] - pt[0] + 1, pt[3] - pt[1] + 1 color = self.colors_tableau[i] cv2.rectangle(self.img, (pt[0], pt[1]), (pt[2], pt[3]), color, 2) cv2.putText(self.img, display_txt, (int(pt[0]), int(pt[1]) + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, 8) j += 1 num_rec += 1 # cv2.imshow('test', self.img) if num_rec > 0: if flag_B: # print(' 1:eye-open') self.list_B = np.append(self.list_B, 1) # 睁眼为‘1’ self.list_blink = np.append(self.list_blink, 1) else: # print(' 0:eye-closed') self.list_B = np.append(self.list_B, 0) # 闭眼为‘0’ self.list_blink = np.append(self.list_blink, 0) self.list_blink = np.delete(self.list_blink, 0) self.list_B = np.delete(self.list_B, 0) if flag_Y: self.list_Y = np.append(self.list_Y, 1) else: self.list_Y = np.append(self.list_Y, 0) self.list_Y = np.delete(self.list_Y, 0) else: self.Msg.clear() self.Msg.setPlainText('Nothing detected.') # print(list) # 实时计算PERCLOS self.perclos = 1 - np.average(self.list_blink) # print('perclos={:f}'.format(perclos)) self.PERCLOS.display(self.perclos) if self.list_B[8] == 1 and self.list_B[9] == 0: # 如果上一帧为’1‘,此帧为’0‘则判定为眨眼 self.Eyes_state.setText('眨眼') self.blink_count += 1 frag_gray = True str = datetime.datetime.now().strftime("%H:%M:%S") self.State_record.append(str + ':眨眼') # img_copy=cv2.cvtColor(img_copy,cv2.COLOR_RGB2GRAY) blink_T = time.time() - self.blink_start if blink_T > 30: # 每30秒计算一次眨眼频率 blink_freq = self.blink_count / blink_T self.blink_start = time.time() self.blink_count = 0 print('blink_freq={:f}'.format(blink_freq)) self.Blink_freq.display(blink_freq * 2) # 检测打哈欠 # if Yawn(list_Y,list_Y1): if (self.list_Y[len(self.list_Y) - len(self.list_Y1):] == self.list_Y1).all(): # print('----------------------打哈欠----------------------') self.Mouth_state.setText('打哈欠') self.yawn_count += 1 frag_gray = True str = datetime.datetime.now().strftime("%H:%M:%S") self.State_record.append(str + ':打哈欠') self.list_Y = np.zeros(50) # 计算打哈欠频率 yawn_T = time.time() - self.yawn_start if yawn_T > 60: yawn_freq = self.yawn_count / yawn_T self.yawn_start = time.time() self.yawn_count = 0 print('yawn_freq={:f}'.format(yawn_freq)) self.Yawn_freq.display(yawn_freq) # 计算危险行为频率 DangerAct_T = time.time() - self.danger_start if DangerAct_T > 60: danger_freq = self.danger_count / DangerAct_T self.danger_start = time.time() self.danger_count = 0 print('danger_freq={:f}'.format(danger_freq)) self.Danger_LCD.display(danger_freq) if (self.perclos > 0.4): # print('疲劳') self.State.setText('疲劳') elif (self.blink_freq > 0.3): # print('疲劳') self.State.setText('疲劳') self.blink_freq = 0 # 如果因为眨眼频率判断疲劳,则初始化眨眼频率 elif (self.yawn_freq > 5.0 / 60): # print("疲劳") self.State.setText('疲劳') self.yawn_freq = 0 # 初始化,同上 else: self.State.setText('清醒') if not frag_gray: showImg = cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB) else: if self.isRecordImg: str = datetime.datetime.now().strftime( "%Y_%m_%d_%H_%M_%S") temp = 'ImgRecord/' + str + '.jpg' cv2.imwrite(temp, img_copy) showImg = cv2.cvtColor(img_copy, cv2.COLOR_RGB2GRAY) showImg = qimage2ndarray.array2qimage(showImg) self.Camera_2.setPixmap(QPixmap(showImg)) # 展示图片 self.Camera_2.show() if self.case == 3: img_copy = self.img.copy() frag_gray = False self.time_ing = time.time() # point=[100,0,540,480] if self.frag_cap: bounding_boxes, landmarks = detect_faces(self.img) print('正在定位······') if len(bounding_boxes) == 1: self.point.clear() for b in bounding_boxes: b = [int(round(value)) for value in b] for i in b: self.point.append(i) self.frag_cap = False # print(point) # cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 2) # 裁剪坐标为[y0:y1, x0:x1] if not self.frag_cap: if self.point[0] < 540: self.img = self.img[self.point[1] - 10:479, self.point[0] - 100:self.point[2] + 100] else: self.img = self.img[self.point[1] - 10:479, self.point[0] - 100:639] else: self.img = self.img[1:479, 1:640] if int(self.time_ing - self.time_first) % 60 == 0: self.frag_cap = True else: self.frag_cap = False bounding_boxes, landmarks = detect_faces(self.img) # 通过MTCNN人脸框判断,当检测不到人脸时判断低头or瞌睡 if len(bounding_boxes) == 0: self.nod_fps += 1 if self.nod_fps >= 3: self.Head_state.setText('点头') self.nod_count += 1 if len(bounding_boxes) > 0: self.nod_fps = 0 # 通过头部姿态欧拉角角度变化判断是否摇头 if len(bounding_boxes) > 0: Head_Y_X_Z = get_head_pose(landmarks) print('pitch:{}, yaw:{}, roll:{}'.format( Head_Y_X_Z[1], Head_Y_X_Z[2], Head_Y_X_Z[3])) if (Head_Y_X_Z[2] < -0.75): self.shake_fps_l += 1 if (Head_Y_X_Z[2] >= -0.75): self.shake_fps_l = 0 if self.shake_fps_l >= 5: self.shake_count += 1 self.Head_state.setText('摇头') if Head_Y_X_Z[3] >= 0.30: self.shake_fps_r += 1 if self.shake_fps_r >= 5: self.shake_count += 1 self.Head_state.setText('摇头') if Head_Y_X_Z[3] < 0.30: self.shake_fps_r = 0 # print(Head_Y_X_Z[1]) # print(Head_Y_X_Z[2]) # print(Head_Y_X_Z[3]) if time.time() - self.nod_start > 3: self.Head_state.setText('') if time.time() - self.shake_start > 3: self.Head_state.setText('') # 计算低头频率 每10s计算一次 if time.time() - self.nod_start > 10: times = time.time() - self.nod_start self.nod_freq = self.nod_count / times self.nod_start = time.time() self.Nod_LCD.display(self.nod_freq) # 计算摇头频率 if time.time() - self.shake_start > 10: times = time.time() - self.shake_start self.shake_freq = self.shake_count / times self.shake_start = time.time() self.shake_LCD.display(self.shake_freq) if len(bounding_boxes) > 0: Emotions = get_emotion( get_face_expression(self.img, bounding_boxes)) self.Emotion.setText(Emotions[1]) self.Emotion_pred.display(float(Emotions[0])) # print(Emotions) canvas = cv2.imread('img_resource/label_pred.jpg', flags=cv2.IMREAD_UNCHANGED) for (i, (emotion, prob)) in enumerate(zip(self.EMOTIONS, Emotions[2])): # text = "{}: {:.2f}%".format(emotion, prob * 100) text = "{:.2f}%".format(prob * 100) # 绘制表情类和对应概率的条形图 w = int(prob * 180) # print(text) # canvas = 255 * np.ones((250, 300, 3), dtype="uint8") cv2.rectangle(canvas, (0, (i * 44) + 25), (w, (i * 43) + 40), (100, 200, 130), -1) cv2.putText(canvas, text, (170, (i * 43) + 40), cv2.FONT_HERSHEY_DUPLEX, 0.6, (0, 0, 0), 1) show = cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB) showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) # cv2.imshow('test', showImage) # showImg=QPixmap(showImage) self.label_pred_img.setPixmap( QtGui.QPixmap.fromImage(showImage)) # # print('test') # print('Head_Y_X_Z') # print(Head_Y_X_Z) x = cv2.resize(self.img, (300, 300)).astype(np.float32) flag_B = True # 是否闭眼的flag flag_Y = False num_rec = 0 # 检测到的眼睛的数量 # 分界线 x -= self.img_mean x = x.astype(np.float32) x = x[:, :, ::-1].copy() x = torch.from_numpy(x).permute(2, 0, 1) xx = Variable(x.unsqueeze(0)) # if torch.cuda.is_available(): # xx = xx.cuda() xx = xx.cuda() y = self.net(xx) softmax = nn.Softmax(dim=-1) detect = Detect(config.class_num, 0, 200, 0.01, 0.45) priors = utils.default_prior_box() loc, conf = y loc = torch.cat([o.view(o.size(0), -1) for o in loc], 1) conf = torch.cat([o.view(o.size(0), -1) for o in conf], 1) detections = detect( loc.view(loc.size(0), -1, 4), softmax(conf.view(conf.size(0), -1, config.class_num)), torch.cat([o.view(-1, 4) for o in priors], 0)).data labels = VOC_CLASSES # 将检测结果放置于图片上 scale = torch.Tensor(self.img.shape[1::-1]).repeat(2) self.img = show_bboxes(self.img, bounding_boxes, landmarks) for i in range(detections.size(1)): j = 0 while detections[0, i, j, 0] >= 0.4: score = detections[0, i, j, 0] label_name = labels[i - 1] if label_name == 'calling' and score > 0.8: self.Danger_state.setText('打电话') self.danger_count += 1 frag_gray = True if label_name == 'smoke' and score > 0.8: self.Danger_state.setText('吸烟') self.danger_count += 1 frag_gray = True if label_name != 'smoke' and label_name != 'calling': self.danger_t += 1 if self.danger_t >= 20: self.Danger_state.setText('') self.danger_t = 0 if label_name == 'open_eye': self.open_t += 1 if self.open_t >= 20: self.Eyes_state.setText('') self.open_t = 0 if label_name == 'closed_mouth': self.Mouth_state.setText(' ') if label_name == 'closed_eye': flag_B = False frag_gray = True if label_name == 'open_mouth': flag_Y = True display_txt = '%s:%.2f' % (label_name, score) pt = (detections[0, i, j, 1:] * scale).cpu().numpy() self.coords = ( pt[0], pt[1]), pt[2] - pt[0] + 1, pt[3] - pt[1] + 1 color = self.colors_tableau[i] cv2.rectangle(self.img, (pt[0], pt[1]), (pt[2], pt[3]), color, 2) cv2.putText(self.img, display_txt, (int(pt[0]), int(pt[1]) + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, 8) j += 1 num_rec += 1 # cv2.imshow('test', self.img) if num_rec > 0: if flag_B: # print(' 1:eye-open') self.list_B = np.append(self.list_B, 1) # 睁眼为‘1’ self.list_blink = np.append(self.list_blink, 1) else: # print(' 0:eye-closed') self.list_B = np.append(self.list_B, 0) # 闭眼为‘0’ self.list_blink = np.append(self.list_blink, 0) self.list_blink = np.delete(self.list_blink, 0) self.list_B = np.delete(self.list_B, 0) if flag_Y: self.list_Y = np.append(self.list_Y, 1) else: self.list_Y = np.append(self.list_Y, 0) self.list_Y = np.delete(self.list_Y, 0) else: self.Msg.clear() self.Msg.setPlainText('Nothing detected.') # print(list) # 实时计算PERCLOS self.perclos = 1 - np.average(self.list_blink) # print('perclos={:f}'.format(perclos)) self.PERCLOS.display(self.perclos) if self.list_B[8] == 1 and self.list_B[9] == 0: # 如果上一帧为’1‘,此帧为’0‘则判定为眨眼 self.Eyes_state.setText('眨眼') self.blink_count += 1 frag_gray = True str = datetime.datetime.now().strftime("%H:%M:%S") self.State_record.append(str + ':眨眼') # img_copy=cv2.cvtColor(img_copy,cv2.COLOR_RGB2GRAY) blink_T = time.time() - self.blink_start if blink_T > 30: # 每30秒计算一次眨眼频率 blink_freq = self.blink_count / blink_T self.blink_start = time.time() self.blink_count = 0 print('blink_freq={:f}'.format(blink_freq)) self.Blink_freq.display(blink_freq * 2) # 检测打哈欠 # if Yawn(list_Y,list_Y1): if (self.list_Y[len(self.list_Y) - len(self.list_Y1):] == self.list_Y1).all(): # print('----------------------打哈欠----------------------') self.Mouth_state.setText('打哈欠') self.yawn_count += 1 frag_gray = True str = datetime.datetime.now().strftime("%H:%M:%S") self.State_record.append(str + ':打哈欠') self.list_Y = np.zeros(50) # 计算打哈欠频率 yawn_T = time.time() - self.yawn_start if yawn_T > 60: yawn_freq = self.yawn_count / yawn_T self.yawn_start = time.time() self.yawn_count = 0 print('yawn_freq={:f}'.format(yawn_freq)) self.Yawn_freq.display(yawn_freq) # 计算危险行为频率 DangerAct_T = time.time() - self.danger_start if DangerAct_T > 60: danger_freq = self.danger_count / DangerAct_T self.danger_start = time.time() self.danger_count = 0 print('danger_freq={:f}'.format(danger_freq)) self.Danger_LCD.display(danger_freq) if (self.perclos > 0.4): # print('疲劳') self.State.setText('疲劳') elif (self.blink_freq > 0.3): # print('疲劳') self.State.setText('疲劳') self.blink_freq = 0 # 如果因为眨眼频率判断疲劳,则初始化眨眼频率 elif (self.yawn_freq > 5.0 / 60): # print("疲劳") self.State.setText('疲劳') self.yawn_freq = 0 # 初始化,同上 else: self.State.setText('清醒') if not frag_gray: showImg = cv2.cvtColor(img_copy, cv2.COLOR_BGR2RGB) else: if self.isRecordImg: str = datetime.datetime.now().strftime( "%Y_%m_%d_%H_%M_%S") temp = 'ImgRecord/' + str + '.jpg' cv2.imwrite(temp, img_copy) showImg = cv2.cvtColor(img_copy, cv2.COLOR_RGB2GRAY) self.State_record.moveCursor(QTextCursor.End) showImg = qimage2ndarray.array2qimage(showImg) self.Camera_2.setPixmap(QPixmap(showImg)) # 展示图片 self.Camera_2.show()
cv.putText(img_raw, text, (cx, cy), cv.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255)) # landms landms = landmarks[i] cv.circle(img_raw, (landms[0], landms[5]), 1, (0, 0, 255), 4) cv.circle(img_raw, (landms[1], landms[6]), 1, (0, 255, 255), 4) cv.circle(img_raw, (landms[2], landms[7]), 1, (255, 0, 255), 4) cv.circle(img_raw, (landms[3], landms[8]), 1, (0, 255, 0), 4) cv.circle(img_raw, (landms[4], landms[9]), 1, (255, 0, 0), 4) # save image cv.imwrite('images/result.jpg', img_raw) cv.imshow('image', img_raw) cv.waitKey(0) if __name__ == "__main__": full_path = 'test/Jason Behr_27968.JPG' img = Image.open(full_path).convert('RGB') bboxes, landmarks = mtcnn.detect_faces(img) print(bboxes) print(landmarks) show_bboxes(full_path, bboxes, landmarks) bboxes, landmarks = retinaface.detect_faces(img) print(bboxes) print(landmarks) show_bboxes(full_path, bboxes, landmarks)
def get_all_face_attributes(full_path): img = Image.open(full_path).convert('RGB') bounding_boxes, landmarks = detect_faces(img) return bounding_boxes, landmarks
def face_attributes(): start = time.time() ensure_folder(STATIC_DIR) ensure_folder(UPLOAD_DIR) file = request.files['file'] fn = secure_filename(file.filename) full_path = os.path.join(UPLOAD_DIR, fn) file.save(full_path) # resize(full_path) print('full_path: ' + full_path) img = Image.open(full_path).convert('RGB') bboxes, landmarks = detect_faces(img) result = None if len(bboxes) > 0: i = select_central_face((im_size, im_size), bboxes) bbox = bboxes[i] img = cv.imread(full_path) boxed = draw_bboxes(img, [bbox], [landmarks[i]]) cv.imwrite(full_path, boxed) img = crop_image(img, bbox) img = cv.resize(img, (im_size, im_size)) img = transforms.ToPILImage()(img) img = transformer(img) img = img.to(device) inputs = torch.zeros([1, 3, im_size, im_size], dtype=torch.float) inputs[0] = img with torch.no_grad(): reg_out, expression_out, gender_out, glasses_out, race_out = model( inputs) reg_out = reg_out.cpu().numpy() age_out = reg_out[0, 0] pitch_out = reg_out[0, 1] roll_out = reg_out[0, 2] yaw_out = reg_out[0, 3] beauty_out = reg_out[0, 4] age = int(age_out * 100) pitch = float('{0:.2f}'.format(pitch_out * 360 - 180)) roll = float('{0:.2f}'.format(roll_out * 360 - 180)) yaw = float('{0:.2f}'.format(yaw_out * 360 - 180)) beauty = float('{0:.2f}'.format(beauty_out * 100)) beauty_prob = float('{0:.4f}'.format(get_prob(beauty))) _, expression_out = expression_out.topk(1, 1, True, True) _, gender_out = gender_out.topk(1, 1, True, True) _, glasses_out = glasses_out.topk(1, 1, True, True) _, race_out = race_out.topk(1, 1, True, True) expression_out = expression_out.cpu().numpy() gender_out = gender_out.cpu().numpy() glasses_out = glasses_out.cpu().numpy() race_out = race_out.cpu().numpy() expression = idx2name(int(expression_out[0, 0]), 'expression') gender = idx2name(int(gender_out[0, 0]), 'gender') glasses = idx2name(int(glasses_out[0, 0]), 'glasses') race = idx2name(int(race_out[0, 0]), 'race') result = { 'age': age, 'pitch': pitch, 'roll': roll, 'yaw': yaw, 'beauty': beauty, 'beauty_prob': beauty_prob, 'expression': expression, 'gender': gender, 'glasses': glasses, 'race': race } elapsed = time.time() - start return result, float(elapsed), str(fn)
# # 如果连续3次都小于阈值,则表示瞌睡点头一次 # if hCOUNTER >= NOD_AR_CONSEC_FRAMES: # 阈值:3 # hTOTAL += 1 # print('点头') # # 重置点头帧计数器 # hCOUNTER = 0 ########################################## # # 检测 # time_2=time.time() # if int(time_2-time_)%20==0: # frag=True if frag: point = [100, 0, 540, 480] bounding_boxes, landmarks = detect_faces(img) img = show_bboxes(img, bounding_boxes, landmarks) if len(bounding_boxes) > 0: point.clear() for b in bounding_boxes: b = [int(round(value)) for value in b] for i in b: point.append(i) frag = False print(point) # cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 2) # 裁剪坐标为[y0:y1, x0:x1] if frag == False: img = img[point[1]:480, point[0] - 100:point[2] + 100] bounding_boxes, landmarks = detect_faces(img) if len(bounding_boxes) > 0:
# video_src = 0 # camera device id capture = cv2.VideoCapture(video_src) if not capture.isOpened(): print('Camera is not opened!') else: idx_frame = 0 while True: ret, frame = capture.read() idx_frame += 1 if idx_frame % 2 != 0: continue idx_frame = 0 cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) start = time.time() img = Image.fromarray(frame) try: bounding_boxes, landmarks = detect_faces(img, pnet=pnet, rnet=rnet, onet=onet, device=device) except: continue end = time.time() print(end - start) frame = show_bboxes(frame, bounding_boxes, landmarks) cv2.imshow('Video', frame) cv2.waitKey(1) capture.release()
from PIL import Image from mtcnn.detector import detect_faces import time camera_id = 0 cap = cv2.VideoCapture(camera_id) if not cap.isOpened(): sys.exit() while True: ret, frame = cap.read() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) pilImg = Image.fromarray(numpy.uint8(image)) boxes, landmarks = detect_faces(pilImg) for box in boxes: box = [int(i) for i in box] image = cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 2) for marks in landmarks: for i, j in zip(marks[:5], marks[5:]): image = cv2.circle(image, (int(i), int(j)), 3, color=( 255, 0, ), thickness=2)
import cv2 as cv import numpy as np from PIL import Image from align_faces import warp_and_crop_face, get_reference_facial_points from mtcnn.detector import detect_faces if __name__ == "__main__": for i in range(10): img_fn = 'images/{}_raw.jpg'.format(i) print('Loading image {}'.format(img_fn)) raw = cv.imread(img_fn, True) img = Image.open(img_fn).convert('RGB') _, facial5points = detect_faces(img) facial5points = np.reshape(facial5points[0], (2, 5)) crop_size = (224, 224) default_square = True inner_padding_factor = 0.25 outer_padding = (0, 0) output_size = (224, 224) # get the reference 5 landmarks position in the crop settings reference_5pts = get_reference_facial_points( output_size, inner_padding_factor, outer_padding, default_square) # dst_img = warp_and_crop_face(raw, facial5points, reference_5pts, crop_size) dst_img = warp_and_crop_face(raw, facial5points, reference_pts=reference_5pts, crop_size=crop_size) cv.imwrite('images/{}_warped.jpg'.format(i), dst_img) img = cv.resize(raw, (224, 224)) cv.imwrite('images/{}_img.jpg'.format(i), img)
gCategory = ['M', 'W'] if not os.path.exists("results"): os.mkdir("results") # (2) Image processing and cropping for image_path in tqdm.tqdm(sorted( paths.list_images("./demo"))): # Based on the path of the data _, name = os.path.split(image_path) raw = cv.imread(image_path) # BGR raw = imutils.resize(raw, width=256) # img = Image.fromarray(raw[:, :, ::-1]) # RGB t0 = time.time() boxes, facial5points = detect_faces(raw) # whether show face keypoints or not # for p in facial5points: # for i in range(5): # cv.circle(raw, (int(p[i]), int(p[i + 5])), 4, (255, 0, 255), -1) # if len(facial5points) != 1: # continue # facial5points = np.reshape(facial5points[0], (2, 5)) # box = boxes[0].astype(int) if boxes is not None: for i in range(boxes.shape[0]): box = boxes[i].astype(np.int) if facial5points is not None: facial5points = facial5points[i].astype(np.int) for l in range(facial5points.shape[0]):