Aadhithya commited on
Commit
8249541
1 Parent(s): 8ffbc92

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -* coding:UTF-8 -*
2
+ # !/usr/bin/env python
3
+ import numpy as np
4
+ import gradio as gr
5
+ import roop.globals
6
+ from roop.core import (
7
+ start,
8
+ decode_execution_providers,
9
+ suggest_max_memory,
10
+ suggest_execution_threads,
11
+ )
12
+ from roop.processors.frame.core import get_frame_processors_modules
13
+ from roop.utilities import normalize_output_path
14
+ import os
15
+ from PIL import Image
16
+
17
+
18
+ def swap_face(source_file, target_file):
19
+
20
+ source_path = "input.jpg"
21
+ target_path = "target.jpg"
22
+
23
+ source_image = Image.fromarray(source_file)
24
+ source_image.save(source_path)
25
+ target_image = Image.fromarray(target_file)
26
+ target_image.save(target_path)
27
+
28
+ print("source_path: ", source_path)
29
+ print("target_path: ", target_path)
30
+
31
+ roop.globals.source_path = source_path
32
+ roop.globals.target_path = target_path
33
+ output_path = "output.jpg"
34
+ roop.globals.output_path = normalize_output_path(
35
+ roop.globals.source_path, roop.globals.target_path, output_path
36
+ )
37
+ roop.globals.frame_processors = ["face_swapper"]
38
+ roop.globals.headless = True
39
+ roop.globals.keep_fps = True
40
+ roop.globals.keep_audio = True
41
+ roop.globals.keep_frames = False
42
+ roop.globals.many_faces = False
43
+ roop.globals.video_encoder = "libx264"
44
+ roop.globals.video_quality = 18
45
+ roop.globals.max_memory = suggest_max_memory()
46
+ roop.globals.execution_providers = decode_execution_providers(["cpu"])
47
+ roop.globals.execution_threads = suggest_execution_threads()
48
+
49
+ print(
50
+ "start process",
51
+ roop.globals.source_path,
52
+ roop.globals.target_path,
53
+ roop.globals.output_path,
54
+ )
55
+
56
+ for frame_processor in get_frame_processors_modules(
57
+ roop.globals.frame_processors
58
+ ):
59
+ if not frame_processor.pre_check():
60
+ return
61
+
62
+ start()
63
+ return output_path
64
+
65
+
66
+ app = gr.Interface(
67
+ fn=swap_face, inputs=[gr.Image(), gr.Image()], outputs="image"
68
+ )
69
+ app.launch()