main.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #! /usr/bin/env python
  2. import os
  3. import cv2
  4. import argparse
  5. import numpy as np
  6. from face_detection import select_face, mask_mouth
  7. from face_swap import face_swap
  8. if __name__ == '__main__':
  9. parser = argparse.ArgumentParser(description='FaceSwapApp')
  10. parser.add_argument('--src', required=True, help='Path for source image')
  11. parser.add_argument('--dst', required=True, help='Path for target image')
  12. parser.add_argument('--out', required=True, help='Path for storing output images')
  13. parser.add_argument('--point_plot', default=False, help='facial landmark visualizations')
  14. parser.add_argument('--warp_2d', default=False, action='store_true', help='2d or 3d warp')
  15. parser.add_argument('--correct_color', default=False, action='store_true', help='Correct color')
  16. parser.add_argument('--no_debug_window', default=False, action='store_true', help='Don\'t show debug window')
  17. args = parser.parse_args()
  18. # Read images
  19. src_img = cv2.imread(args.src)
  20. src_img_mask = mask_mouth(args.src)
  21. dst_img = cv2.imread(args.dst)
  22. # Select src face
  23. src_points, src_shape, src_face = select_face(src_img_mask)
  24. # Select dst face
  25. dst_points, dst_shape, dst_face = select_face(dst_img)
  26. if src_points is None or dst_points is None:
  27. print('Detect 0 Face !!!')
  28. exit(-1)
  29. output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args)
  30. dir_path = os.path.dirname(args.out)
  31. if not os.path.isdir(dir_path):
  32. os.makedirs(dir_path)
  33. cv2.imwrite(args.out, output)
  34. ##For debug
  35. if not args.no_debug_window:
  36. if args.point_plot:
  37. tmp_img=src_img.copy()
  38. x, y, w, h = src_shape
  39. for point in src_points:
  40. x_, y_ = point.tolist()
  41. cv2.circle(tmp_img, [x_+x,y_+y], 6, (0,0,255), -1)
  42. cv2.circle(tmp_img, [x_+x,y_+y], 3, (255,255,255), -1)
  43. tmp_img2=dst_img.copy()
  44. x, y, w, h = dst_shape
  45. for point in dst_points:
  46. x_, y_ = point.tolist()
  47. cv2.circle(tmp_img2, [x_+x,y_+y], 6, (0,0,255), -1)
  48. cv2.circle(tmp_img2, [x_+x,y_+y], 3, (255,255,255), -1)
  49. result=np.hstack((tmp_img, tmp_img2, output))
  50. else:
  51. result=np.hstack((src_img, dst_img, output))
  52. cv2.namedWindow('result', 0)
  53. cv2.imshow("result", result)
  54. cv2.waitKey(0)
  55. cv2.destroyAllWindows()