main.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #! /usr/bin/env python
  2. import os
  3. import cv2
  4. import argparse
  5. from face_detection import select_face
  6. from face_swap import face_swap
  7. if __name__ == '__main__':
  8. parser = argparse.ArgumentParser(description='FaceSwapApp')
  9. parser.add_argument('--src', required=True, help='Path for source image')
  10. parser.add_argument('--dst', required=True, help='Path for target image')
  11. parser.add_argument('--out', required=True, help='Path for storing output images')
  12. parser.add_argument('--warp_2d', default=False, action='store_true', help='2d or 3d warp')
  13. parser.add_argument('--correct_color', default=False, action='store_true', help='Correct color')
  14. parser.add_argument('--no_debug_window', default=False, action='store_true', help='Don\'t show debug window')
  15. args = parser.parse_args()
  16. # Read images
  17. src_img = cv2.imread(args.src)
  18. dst_img = cv2.imread(args.dst)
  19. # Select src face
  20. src_points, src_shape, src_face = select_face(src_img)
  21. # Select dst face
  22. dst_points, dst_shape, dst_face = select_face(dst_img)
  23. if src_points is None or dst_points is None:
  24. print('Detect 0 Face !!!')
  25. exit(-1)
  26. output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args)
  27. dir_path = os.path.dirname(args.out)
  28. if not os.path.isdir(dir_path):
  29. os.makedirs(dir_path)
  30. cv2.imwrite(args.out, output)
  31. ##For debug
  32. if not args.no_debug_window:
  33. cv2.imshow("From", dst_img)
  34. cv2.imshow("To", output)
  35. cv2.waitKey(0)
  36. cv2.destroyAllWindows()