def make_deep_face(args): # Read images src_img = cv2.imread(args.src) dst_img = cv2.imread(args.dst) # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_points, dst_shape, dst_face = select_face(dst_img) if src_points is None or dst_points is None: print('Detect 0 Face !!!') exit(-1) output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args) dir_path = os.path.dirname(args.out) if not os.path.isdir(dir_path): os.makedirs(dir_path) cv2.imwrite(args.out, output) # For debug if not args.no_debug_window: cv2.imshow("From", dst_img) cv2.imshow("To", output) cv2.waitKey(0) cv2.destroyAllWindows()
def faceSwap(): parser = argparse.ArgumentParser(description='FaceSwapApp') parser.add_argument('--warp_2d', default=False, action='store_true', help='2d or 3d warp') parser.add_argument('--correct_color', default=False, action='store_true', help='Correct color') parser.add_argument('--no_debug_window', default=False, action='store_true', help='Don\'t show debug window') args = parser.parse_args() src_img = cv2.imread("photo0.jpg") dst_img = cv2.imread("photo1.jpg") # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_points, dst_shape, dst_face = select_face(dst_img) if src_points is None or dst_points is None: os.remove("photo0.jpg") os.remove("photo1.jpg") return 'Detect 0 Face !!!' exit(-1) output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args) cv2.imwrite("photoOut.jpg", output) os.remove("photo0.jpg") os.remove("photo1.jpg") return 'Кожаный мешок, держи одно фото.'
def swap_face(): try: bin_img = request.data arr_img = np.fromstring(bin_img, np.uint8) src_img = cv2.imdecode(arr_img, cv2.IMREAD_COLOR) gender = request.args["gender"] character = request.args["character"] # Select src face src_points, src_shape, src_face = select_face(src_img, choose=False) dst_name = gender + "-" + character dst = dst_img_info[dst_name] dst_points = dst["points"] dst_shape = dst["shape"] dst_face = dst["face"] dst_img = dst["img"] out_img = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, correct_color=True, warp_2d=False) _, img = cv2.imencode(".jpg", out_img) return jsonify(img=str(base64.b64encode(img), 'utf-8'), error=False) except Exception: return jsonify(error=True)
def main(): try: #Upload images uploaded_file = st.file_uploader("Choose a picture", type=['jpg', 'png']) if uploaded_file is not None: st.image(uploaded_file, width=200) second_uploaded_file = st.file_uploader("Choose another picture", type=['jpg', 'png']) if second_uploaded_file is not None: st.image(second_uploaded_file, width=200) image1 = Image.open(uploaded_file) image2 = Image.open(second_uploaded_file) image1_arr = np.array(image1) image2_arr = np.array(image2) # Select src face src_points, src_shape, src_face = select_face(image1_arr) src_points1, src_shape1, src_face1 = select_face(image2_arr) # Select dst face dst_points, dst_shape, dst_face = select_face(image2_arr) dst_points1, dst_shape1, dst_face1 = select_face(image1_arr) if src_points is None or dst_points is None: print('No Face Detected') exit(-1) output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, image2_arr) output2 = face_swap(src_face1, dst_face1, src_points1, dst_points1, dst_shape1, image1_arr) if st.button('Swap Faces ⬇️'): st.image(output) if st.button('Swap Faces ⬆️'): st.image(output2) except: st.title('upload images')
def swap(src, dst): parser = argparse.ArgumentParser(description='FaceSwapApp') parser.add_argument('--warp_2d', default=True, action='store_true', help='2d or 3d warp') parser.add_argument('--correct_color', default=True, action='store_true', help='Correct color') parser.add_argument('--no_debug_window', default=True, action='store_true', help='Don\'t show debug window') args = parser.parse_args() # Read images src_img = cv2.imread(src) dst_img = cv2.imread(dst) out = 'imgs/result.jpg' # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_points, dst_shape, dst_face = select_face(dst_img) if src_points is None or dst_points is None: print('Detect 0 Face !!!') exit(-1) output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args) dir_path = os.path.dirname(out) if not os.path.isdir(dir_path): os.makedirs(dir_path) cv2.imwrite(out, output) im_rgb = cv2.cvtColor(output, cv2.COLOR_BGR2RGB) image = Image.fromarray(im_rgb) w, h = image.size ratio = (h // w) # resize the image and apply a high-quality down sampling filter try: image = image.resize(((450), (450 * ratio)), Image.ANTIALIAS) except ValueError: ratio = (w // h) image = image.resize(((450 * ratio), (450)), Image.ANTIALIAS) return image
def start(self): while self.video.isOpened(): if cv2.waitKey(1) & 0xFF == ord('q'): break _, dst_img = self.video.read() dst_points, dst_shape, dst_face = select_face(dst_img, choose=False) if dst_points is not None: dst_img = face_swap(self.src_face, dst_face, self.src_points, dst_points, dst_shape, dst_img, self.args, 68) self.writer.write(dst_img) if self.args.show: cv2.imshow("Video", dst_img) self.video.release() self.writer.release() cv2.destroyAllWindows()
def swap_faces(src_url, dst_img): src_img = cv2.imread(src_url) # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_points, dst_shape, dst_face = select_face(dst_img) if src_points is None or dst_points is None: return '' default_settings = SwapperArgs() default_settings.correct_color = False default_settings.warp_2d = False output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, default_settings) print(output.shape) output_file = f'static/output/{str(uuid.uuid4())}.jpg' cv2.imwrite(output_file, output) return '/' + output_file
def swap_faces(face_src, face_dst): # refering from https://github.com/wuhuikai/FaceSwap def get_shape(image, points): r = 0 im_w, im_h = image.shape[:2] left, top = np.min(points, 0) right, bottom = np.max(points, 0) x, y = max(0, left - r), max(0, top - r) w, h = min(right + r, im_h) - x, min(bottom + r, im_w) - y return points, (x, y, w, h) names = ['face', 'eyebrow1', 'eyebrow2'] landmarks_face_src, landmarks_face_dst = detect_landmarks_anime( face_src, True), detect_landmarks_anime(face_dst, True, True) landmarks_face_src, shape_src = get_shape(face_src, landmarks_face_src) landmarks_face_dst, shape_dst = get_shape(face_dst, landmarks_face_dst) output = face_swap(face_src, face_dst, landmarks_face_src, landmarks_face_dst, shape_dst, end=9) return output
print(args) print() # Read images src_img = cv2.imread(args.src) dst_img = cv2.imread(args.dst) # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_points, dst_shape, dst_face = select_face(dst_img) if src_points is None or dst_points is None: print('Detect 0 Face !!!') exit(-1) output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args) dir_path = os.path.dirname(args.out) if not os.path.isdir(dir_path): os.makedirs(dir_path) cv2.imwrite(args.out , output) ##For debug if not args.no_debug_window: # cv2.imwrite("result", output) # cv2.imshow("From", dst_img) # cv2.imshow("To", output) # cv2.waitKey(0)
dst_img = cv2.imread(args.dst) # makeup the src face if args.makeup: src_img = makeup_face(src_img) # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_points, dst_shape, dst_face = select_face(dst_img) if src_points is None or dst_points is None: print('Detect 0 Face !!!') exit(-1) output = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args.correct_color) # beauty the face if args.beauty: src_img = beauty_face(src_img) dir_path = os.path.dirname(args.out) cv2.imwrite(args.out, output) # --------------------------Test code begin----------------------------- # correct_color = True # face_makeup = True # face_beauty = True # # users = range(1, 9)
src_img = cv2.imread(args.src) dst_img = cv2.imread(args.dst) # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_faceBoxes = select_all_faces(dst_img) if dst_faceBoxes is None: print('Detect 0 Face !!!') exit(-1) output = dst_img for k, dst_face in dst_faceBoxes.items(): output = face_swap(src_face, dst_face["face"], src_points, dst_face["points"], dst_face["shape"], output, args) dir_path = os.path.dirname(args.out) if not os.path.isdir(dir_path): os.makedirs(dir_path) cv2.imwrite(args.out, output) ##For debug if not args.no_debug_window: cv2.imshow("From", dst_img) cv2.imshow("To", output) cv2.waitKey(0) cv2.destroyAllWindows()
try: dst_alpha = cv2.imread(args.dst, cv2.IMREAD_UNCHANGED)[:, :, 3] except Exception: dst_alpha = None # Select src face src_points, src_shape, src_face = select_face(src_img) # Select dst face dst_points, dst_shape, dst_face = select_face(dst_img) if src_points is None or dst_points is None: print('Detect 0 Face !!!') exit(-1) swap_result = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args) if dst_alpha is None: output = swap_result else: output_bgr = swap_result[:, :, :3] output = numpy.dstack([output_bgr, dst_alpha]) dir_path = os.path.dirname(args.out) if not os.path.isdir(dir_path): os.makedirs(dir_path) cv2.imwrite(args.out, output) ##For debug if not args.no_debug_window: cv2.imshow("From", dst_img)