HariLogicgo commited on
Commit
ae1d47c
·
1 Parent(s): 713ca36

esrgan added

Browse files
Files changed (2) hide show
  1. app.py +52 -11
  2. requirements.txt +3 -1
app.py CHANGED
@@ -3,24 +3,28 @@ import insightface
3
  from insightface.app import FaceAnalysis
4
  import gradio as gr
5
  from huggingface_hub import hf_hub_download
 
 
 
6
 
7
  # -------------------------------------------------
8
  # Hugging Face repo ID where models are stored
9
  # -------------------------------------------------
10
  REPO_ID = "HariLogicgo/face_swap_models"
 
11
 
12
  # -------------------------------------------------
13
  # Download models from HF Hub
14
  # -------------------------------------------------
15
- # Download inswapper model
16
  inswapper_path = hf_hub_download(
17
  repo_id=REPO_ID,
18
  filename="models/inswapper_128.onnx",
19
  repo_type="model",
20
- local_dir="./models"
21
  )
22
 
23
- # Download buffalo_l ONNX files one by one
24
  buffalo_files = [
25
  "1k3d68.onnx",
26
  "2d106det.onnx",
@@ -34,19 +38,51 @@ for f in buffalo_files:
34
  repo_id=REPO_ID,
35
  filename=f"models/buffalo_l/{f}",
36
  repo_type="model",
37
- local_dir="./models"
38
  )
39
 
 
 
 
 
 
 
 
 
40
  # -------------------------------------------------
41
  # Initialize Face Analysis + Swapper
42
  # -------------------------------------------------
43
- app = FaceAnalysis(name="buffalo_l", root="./models", providers=['CPUExecutionProvider'])
44
  app.prepare(ctx_id=0, det_size=(640, 640))
45
 
46
  swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
47
 
48
  # -------------------------------------------------
49
- # Face swap function (images only)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # -------------------------------------------------
51
  def swap_face_image(source_img, target_img):
52
  src_img = cv2.cvtColor(source_img, cv2.COLOR_RGB2BGR)
@@ -58,22 +94,27 @@ def swap_face_image(source_img, target_img):
58
  if len(src_faces) == 0 or len(tgt_faces) == 0:
59
  return None # No face detected
60
 
61
- result = swapper.get(tgt_img, tgt_faces[0], src_faces[0])
62
- result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
 
 
 
 
 
63
  return result
64
 
65
  # -------------------------------------------------
66
  # Gradio Interface
67
  # -------------------------------------------------
68
  with gr.Blocks() as demo:
69
- gr.Markdown("Face Swap API (Image Only, CPU Deployment)")
70
 
71
  with gr.Row():
72
  src_img = gr.Image(type="numpy", label="Source Image")
73
  tgt_img = gr.Image(type="numpy", label="Target Image")
74
 
75
- output_img = gr.Image(type="numpy", label="Swapped Output")
76
- btn1 = gr.Button("Swap Faces")
77
 
78
  btn1.click(swap_face_image, inputs=[src_img, tgt_img], outputs=output_img)
79
 
 
3
  from insightface.app import FaceAnalysis
4
  import gradio as gr
5
  from huggingface_hub import hf_hub_download
6
+ from basicsr.archs.rrdbnet_arch import RRDBNet
7
+ from realesrgan import RealESRGANer
8
+ import os
9
 
10
  # -------------------------------------------------
11
  # Hugging Face repo ID where models are stored
12
  # -------------------------------------------------
13
  REPO_ID = "HariLogicgo/face_swap_models"
14
+ MODELS_DIR = "./models"
15
 
16
  # -------------------------------------------------
17
  # Download models from HF Hub
18
  # -------------------------------------------------
19
+ # InSwapper model
20
  inswapper_path = hf_hub_download(
21
  repo_id=REPO_ID,
22
  filename="models/inswapper_128.onnx",
23
  repo_type="model",
24
+ local_dir=MODELS_DIR
25
  )
26
 
27
+ # Buffalo_l ONNX files
28
  buffalo_files = [
29
  "1k3d68.onnx",
30
  "2d106det.onnx",
 
38
  repo_id=REPO_ID,
39
  filename=f"models/buffalo_l/{f}",
40
  repo_type="model",
41
+ local_dir=MODELS_DIR
42
  )
43
 
44
+ # RealESRGAN model
45
+ realesrgan_path = hf_hub_download(
46
+ repo_id=REPO_ID,
47
+ filename="models/RealESRGAN_x4plus.pth",
48
+ repo_type="model",
49
+ local_dir=MODELS_DIR
50
+ )
51
+
52
  # -------------------------------------------------
53
  # Initialize Face Analysis + Swapper
54
  # -------------------------------------------------
55
+ app = FaceAnalysis(name="buffalo_l", root=MODELS_DIR, providers=['CPUExecutionProvider'])
56
  app.prepare(ctx_id=0, det_size=(640, 640))
57
 
58
  swapper = insightface.model_zoo.get_model(inswapper_path, providers=['CPUExecutionProvider'])
59
 
60
  # -------------------------------------------------
61
+ # Initialize RealESRGAN Upscaler (CPU mode)
62
+ # -------------------------------------------------
63
+ model = RRDBNet(
64
+ num_in_ch=3,
65
+ num_out_ch=3,
66
+ num_feat=64,
67
+ num_block=23,
68
+ num_grow_ch=32,
69
+ scale=4
70
+ )
71
+
72
+ upscaler = RealESRGANer(
73
+ scale=4,
74
+ model_path=realesrgan_path,
75
+ dni_weight=None,
76
+ model=model,
77
+ tile=0,
78
+ tile_pad=10,
79
+ pre_pad=0,
80
+ half=False, # must be False for CPU
81
+ gpu_id=None # ensures CPU mode
82
+ )
83
+
84
+ # -------------------------------------------------
85
+ # Face swap + upscale function
86
  # -------------------------------------------------
87
  def swap_face_image(source_img, target_img):
88
  src_img = cv2.cvtColor(source_img, cv2.COLOR_RGB2BGR)
 
94
  if len(src_faces) == 0 or len(tgt_faces) == 0:
95
  return None # No face detected
96
 
97
+ swapped = swapper.get(tgt_img, tgt_faces[0], src_faces[0])
98
+
99
+ # Upscale with RealESRGAN
100
+ upscaled, _ = upscaler.enhance(swapped, outscale=4)
101
+
102
+ # Convert back to RGB for Gradio
103
+ result = cv2.cvtColor(upscaled, cv2.COLOR_BGR2RGB)
104
  return result
105
 
106
  # -------------------------------------------------
107
  # Gradio Interface
108
  # -------------------------------------------------
109
  with gr.Blocks() as demo:
110
+ gr.Markdown("### Face Swap + Upscale (CPU, HuggingFace Models)")
111
 
112
  with gr.Row():
113
  src_img = gr.Image(type="numpy", label="Source Image")
114
  tgt_img = gr.Image(type="numpy", label="Target Image")
115
 
116
+ output_img = gr.Image(type="numpy", label="Swapped & Upscaled Output")
117
+ btn1 = gr.Button("Swap + Upscale")
118
 
119
  btn1.click(swap_face_image, inputs=[src_img, tgt_img], outputs=output_img)
120
 
requirements.txt CHANGED
@@ -3,4 +3,6 @@ onnxruntime
3
  opencv-python
4
  numpy
5
  huggingface_hub
6
- gradio
 
 
 
3
  opencv-python
4
  numpy
5
  huggingface_hub
6
+ gradio
7
+ basicsr
8
+ realesrgan