|
沙发
楼主 |
发表于 2020-3-24 17:21:56
|
只看该作者
成功跑起来的例子:
- img_data = cv2.imread('/home/dfy888/DataSets/64_CASIA-FaceV5/images_test/000/000_0.png')
- # current word dict
- print(os.getcwd())
- # 定义人脸检测器
- # detector = dlib.get_frontal_face_detector()
- cnn_face_detector = dlib.cnn_face_detection_model_v1('./models/mmod_human_face_detector.dat')
- predictor_path = './models/shape_predictor_5_face_landmarks.dat'
- # 定义人脸关键点检测器
- sp = dlib.shape_predictor(predictor_path)
- # OpenCV + Dlib对人脸位置进行校准 查找脸部位置
- fo = dlib.full_object_detections()
- # img_path = '/home/dfy888/DataSets/CelebA/img_align_celeba/100205.jpg'
- # img_path = './testimgs/img01.png'
- # # img_path = '/home/dfy888/DataSets/VOCdevkit_widerface_dfy/VOC2020/JPEGImages/9--Press_Conference_9_Press_Conference_Press_Conference_9_91.jpg'
- # img_data = cv2.imread(img_path)
- print(img_data.shape)
- img = img_data[:, :, ::-1]
- # 1、人脸检测:使用 Dlib 中预先训练的模型检测面部
- dets = cnn_face_detector(img, 0)
- # dets = detector(img, 1)
- num_faces = len(dets)
- print('检测出%d个人脸' % num_faces)
- if num_faces == 1:
- # 获取人脸的坐标 注意img与img_data的区别
- x1 = dets[0].rect.left()
- y1 = dets[0].rect.top()
- x2 = dets[0].rect.right()
- y2 = dets[0].rect.bottom()
- cv2.rectangle(img_data, (x1, y1), (x2, y2), (0, 0, 255), 2)
- landmarks = np.matrix([[p.x, p.y] for p in sp(img, dets[0].rect).parts()])
- print('每个人脸的关键点个数:', len(landmarks))
- for point in landmarks:
- pos = (point[0, 0], point[0, 1])
- print('pos-', pos)
- cv2.circle(img_data, pos, 2, color=(0, 255, 255), thickness=3)
- # 下面开始clip 查找脸部位置
- fo.append(sp(img, dets[0].rect))
- image = dlib.get_face_chip(img, fo[0], size=139)
- fo.pop()
- image = image[:, :, ::-1]
- cv2.imshow('chip shape:%s' % str(image.shape), image)
- cv2.waitKey(0)
- pass
- cv2.imshow('origianl+face', img_data)
- cv2.waitKey(0)
复制代码 |
|