generate_train_data.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import cv2
  2. import dlib
  3. import time
  4. import argparse
  5. import numpy as np
  6. from imutils import video
  7. DOWNSAMPLE_RATIO = 4
  8. def reshape_for_polyline(array):
  9. return np.array(array, np.int32).reshape((-1, 1, 2))
  10. def main():
  11. cap = cv2.VideoCapture(args.filename)
  12. fps = video.FPS().start()
  13. count = 0
  14. while cap.isOpened():
  15. ret, frame = cap.read()
  16. frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO)
  17. gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY)
  18. faces = detector(gray, 1)
  19. black_image = np.zeros(frame.shape, np.uint8)
  20. t = time.time()
  21. # Perform if there is a face detected
  22. if len(faces) == 1:
  23. for face in faces:
  24. detected_landmarks = predictor(gray, face).parts()
  25. landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks]
  26. jaw = reshape_for_polyline(landmarks[0:17])
  27. left_eyebrow = reshape_for_polyline(landmarks[22:27])
  28. right_eyebrow = reshape_for_polyline(landmarks[17:22])
  29. nose_bridge = reshape_for_polyline(landmarks[27:31])
  30. lower_nose = reshape_for_polyline(landmarks[30:35])
  31. left_eye = reshape_for_polyline(landmarks[42:48])
  32. right_eye = reshape_for_polyline(landmarks[36:42])
  33. outer_lip = reshape_for_polyline(landmarks[48:60])
  34. inner_lip = reshape_for_polyline(landmarks[60:68])
  35. color = (255, 255, 255)
  36. thickness = 3
  37. cv2.polylines(black_image, [jaw], False, color, thickness)
  38. cv2.polylines(black_image, [left_eyebrow], False, color, thickness)
  39. cv2.polylines(black_image, [right_eyebrow], False, color, thickness)
  40. cv2.polylines(black_image, [nose_bridge], False, color, thickness)
  41. cv2.polylines(black_image, [lower_nose], True, color, thickness)
  42. cv2.polylines(black_image, [left_eye], True, color, thickness)
  43. cv2.polylines(black_image, [right_eye], True, color, thickness)
  44. cv2.polylines(black_image, [outer_lip], True, color, thickness)
  45. cv2.polylines(black_image, [inner_lip], True, color, thickness)
  46. # Display the resulting frame
  47. count += 1
  48. print(count)
  49. cv2.imwrite("original/{}.png".format(count), frame)
  50. cv2.imwrite("landmarks/{}.png".format(count), black_image)
  51. fps.update()
  52. print('[INFO] elapsed time: {:.2f}'.format(time.time() - t))
  53. if count == args.number: # only take 400 photos
  54. break
  55. elif cv2.waitKey(1) & 0xFF == ord('q'):
  56. break
  57. else:
  58. print("No face detected")
  59. fps.stop()
  60. print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed()))
  61. print('[INFO] approx. FPS: {:.2f}'.format(fps.fps()))
  62. cap.release()
  63. cv2.destroyAllWindows()
  64. if __name__ == '__main__':
  65. parser = argparse.ArgumentParser()
  66. parser.add_argument('--file', dest='filename', type=str, help='Name of the video file.')
  67. parser.add_argument('--num', dest='number', type=int, help='Number of train data to be created.')
  68. parser.add_argument('--landmark-model', dest='face_landmark_shape_file', type=str, help='Face landmark model file.')
  69. args = parser.parse_args()
  70. # Create the face predictor and landmark predictor
  71. detector = dlib.get_frontal_face_detector()
  72. predictor = dlib.shape_predictor(args.face_landmark_shape_file)
  73. main()