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 __init__(self, video_path=0, img_path=None, args=None): self.src_points, self.src_shape, self.src_face = select_face(cv2.imread(img_path)) if self.src_points is None: print('No face detected in the source image !!!') exit(-1) self.args = args self.video = cv2.VideoCapture(video_path) self.writer = cv2.VideoWriter(args.save_path, cv2.VideoWriter_fourcc(*'MJPG'), self.video.get(cv2.CAP_PROP_FPS), (int(self.video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(self.video.get(cv2.CAP_PROP_FRAME_HEIGHT))))
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 generate_dst_img_info(): info = dict() dst_img_dir = Path("imgs/src") for img_path in dst_img_dir.glob("*.jpg"): path = os.path.abspath(img_path) img = cv2.imread(path) points, shape, face = select_face(img) img_name = os.path.basename(img_path).split(".")[0] img_info = dict(points=points, shape=shape, face=face, img=img) info[img_name] = img_info return info
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 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()
name_list.append(name) args_src = "./data/" + name_list[count_name] args_dst = "./unknown/unknown.jpg" args_out = "./output/result" Y = int((face_locations[count_name][2]-face_locations[count_name][0])/2) X = int((face_locations[count_name][1]-face_locations[count_name][3])/2) # Read images src_img = cv2.imread(args_src) # dst_img = cv2.imread(args.dst) dst_img = img[face_locations[count_name][0]-Y:face_locations[count_name][2]+Y,face_locations[count_name][3]-X:face_locations[count_name][1]+X] # 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) os.environ['CUDA_VISIBLE_DEVICES'] = "0" # GPU number, -1 for CPU prn = PRN(is_dlib = True) demo_texture.texture_editing(prn, "./unknown/unknown.jpg", src_img, "./unknown/unknown.jpg", mode = 1) # img[face_locations[count_name][0]-Y:face_locations[count_name][2]+Y,face_locations[count_name][3]-X:face_locations[count_name][1]+X] = face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args) cv2.imwrite("asdf.jpg" , img) count_name += 1
# 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() args.src = out_two args.dst = out_one args.out = "output/test.jpg" args.warp_2d = False args.correct_color = False args.no_debug_window = False # 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_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):