aheedsajid commited on
Commit
dab748b
·
verified ·
1 Parent(s): d711651

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -70
app.py CHANGED
@@ -1,31 +1,24 @@
1
- # -*- coding:UTF-8 -*-
2
  # !/usr/bin/env python
3
- import spaces
4
  import numpy as np
5
  import gradio as gr
6
- import gradio.exceptions
7
  import roop.globals
8
  from roop.core import (
9
  start,
10
  decode_execution_providers,
 
 
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
- import uuid
17
- import onnxruntime as ort
18
- import cv2
19
- from roop.face_analyser import get_one_face
20
 
21
- @spaces.GPU
22
- def swap_face(source_file, target_file, doFaceEnhancer):
23
- session_id = str(uuid.uuid4()) # Tạo một UUID duy nhất cho mỗi phiên làm việc
24
- session_dir = f"temp/{session_id}"
25
- os.makedirs(session_dir, exist_ok=True)
26
 
27
- source_path = os.path.join(session_dir, "input.jpg")
28
- target_path = os.path.join(session_dir, "target.jpg")
 
 
29
 
30
  source_image = Image.fromarray(source_file)
31
  source_image.save(source_path)
@@ -35,68 +28,45 @@ def swap_face(source_file, target_file, doFaceEnhancer):
35
  print("source_path: ", source_path)
36
  print("target_path: ", target_path)
37
 
38
- # Check if a face is detected in the source image
39
- source_face = get_one_face(cv2.imread(source_path))
40
- if source_face is None:
41
- raise gradio.exceptions.Error("No face in source path detected.")
42
-
43
- # Check if a face is detected in the target image
44
- target_face = get_one_face(cv2.imread(target_path))
45
- if target_face is None:
46
- raise gradio.exceptions.Error("No face in target path detected.")
47
-
48
- output_path = os.path.join(session_dir, "output.jpg")
49
- normalized_output_path = normalize_output_path(source_path, target_path, output_path)
50
-
51
- frame_processors = ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
52
- headless = True
53
- keep_fps = True
54
- keep_audio = True
55
- keep_frames = False
56
- many_faces = False
57
- video_encoder = "libx264"
58
- video_quality = 18
59
- max_memory = "12G"
60
- execution_providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] # Ưu tiên GPU, sau đó là CPU
61
- execution_threads = 4
62
- reference_face_position = 0
63
- similar_face_distance = 0.6
64
 
65
- print("Available providers:", ort.get_available_providers()) # Kiểm tra các provider hiện có
66
- print("Configured execution providers:", execution_providers)
 
 
 
 
67
 
68
- for frame_processor in get_frame_processors_modules(frame_processors):
 
 
69
  if not frame_processor.pre_check():
70
- print(f"Pre-check failed for {frame_processor}")
71
- raise gradio.exceptions.Error(f"Pre-check failed for {frame_processor}")
72
-
73
- roop.globals.source_path = source_path
74
- roop.globals.target_path = target_path
75
- roop.globals.output_path = normalized_output_path
76
- roop.globals.frame_processors = frame_processors
77
- roop.globals.headless = headless
78
- roop.globals.keep_fps = keep_fps
79
- roop.globals.keep_audio = keep_audio
80
- roop.globals.keep_frames = keep_frames
81
- roop.globals.many_faces = many_faces
82
- roop.globals.video_encoder = video_encoder
83
- roop.globals.video_quality = video_quality
84
- roop.globals.max_memory = max_memory
85
- roop.globals.execution_providers = execution_providers
86
- roop.globals.execution_threads = execution_threads
87
- roop.globals.reference_face_position = reference_face_position
88
- roop.globals.similar_face_distance = similar_face_distance
89
 
90
  start()
91
- return normalized_output_path
 
92
 
93
  app = gr.Interface(
94
- fn=swap_face,
95
- inputs=[
96
- gr.Image(),
97
- gr.Image(),
98
- gr.Checkbox(label="Face Enhancer?", info="Do face enhancement?")
99
- ],
100
- outputs="image"
101
  )
102
  app.launch()
 
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,doFaceEnhancer):
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)
 
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
+ if doFaceEnhancer == True:
38
+ roop.globals.frame_processors = ["face_swapper","face_enhancer"]
39
+ else:
40
+ roop.globals.frame_processors = ["face_swapper"]
41
+ roop.globals.headless = True
42
+ roop.globals.keep_fps = True
43
+ roop.globals.keep_audio = True
44
+ roop.globals.keep_frames = False
45
+ roop.globals.many_faces = False
46
+ roop.globals.video_encoder = "libx264"
47
+ roop.globals.video_quality = 18
48
+ roop.globals.max_memory = suggest_max_memory()
49
+ roop.globals.execution_providers = decode_execution_providers(["cuda"])
50
+ roop.globals.execution_threads = suggest_execution_threads()
 
 
 
 
 
 
51
 
52
+ print(
53
+ "start process",
54
+ roop.globals.source_path,
55
+ roop.globals.target_path,
56
+ roop.globals.output_path,
57
+ )
58
 
59
+ for frame_processor in get_frame_processors_modules(
60
+ roop.globals.frame_processors
61
+ ):
62
  if not frame_processor.pre_check():
63
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  start()
66
+ return output_path
67
+
68
 
69
  app = gr.Interface(
70
+ fn=swap_face, inputs=[gr.Image(), gr.Image(),gr.Checkbox(label="face_enhancer?", info="do face enhancer?")], outputs="image"
 
 
 
 
 
 
71
  )
72
  app.launch()