HAL1993 commited on
Commit
b63ff43
·
verified ·
1 Parent(s): 028ca4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -86
app.py CHANGED
@@ -1,91 +1,60 @@
1
- import insightface
2
  import os
3
- import onnxruntime
4
- import cv2
5
- import gfpgan
6
- import tempfile
7
- import time
8
  import gradio as gr
9
 
10
-
11
- class Predictor:
12
- def __init__(self):
13
- self.setup()
14
-
15
- def setup(self):
16
- os.makedirs('models', exist_ok=True)
17
- os.chdir('models')
18
- if not os.path.exists('GFPGANv1.4.pth'):
19
- os.system(
20
- 'wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth'
21
- )
22
- if not os.path.exists('inswapper_128.onnx'):
23
- os.system(
24
- 'wget https://huggingface.co/ashleykleynhans/inswapper/resolve/main/inswapper_128.onnx'
25
- )
26
- os.chdir('..')
27
-
28
- self.face_swapper = insightface.model_zoo.get_model(
29
- 'models/inswapper_128.onnx',
30
- providers=onnxruntime.get_available_providers()
31
- )
32
- self.face_enhancer = gfpgan.GFPGANer(model_path='models/GFPGANv1.4.pth', upscale=1)
33
- self.face_analyser = insightface.app.FaceAnalysis(name='buffalo_l')
34
- self.face_analyser.prepare(ctx_id=0, det_size=(640, 640))
35
-
36
- def get_face(self, img_data):
37
- analysed = self.face_analyser.get(img_data)
38
- try:
39
- largest = max(analysed, key=lambda x: (x.bbox[2] - x.bbox[0]) * (x.bbox[3] - x.bbox[1]))
40
- return largest
41
- except:
42
- print("Nuk u gjet fytyra")
43
- return None
44
-
45
- def predict(self, input_image, swap_image):
46
- try:
47
- frame = cv2.imread(input_image.name)
48
- face = self.get_face(frame)
49
- source_face = self.get_face(cv2.imread(swap_image.name))
50
- if not face or not source_face:
51
- return None
52
- result = self.face_swapper.get(frame, face, source_face, paste_back=True)
53
-
54
- _, _, result = self.face_enhancer.enhance(result, paste_back=True)
55
-
56
- out_path = tempfile.mkdtemp() + f"/{str(int(time.time()))}.jpg"
57
- cv2.imwrite(out_path, result)
58
- return out_path
59
- except Exception as e:
60
- print(f"{e}")
61
- return None
62
-
63
-
64
- predictor = Predictor()
65
-
66
- with gr.Blocks(title="Face Swap & Enhance") as demo:
67
- gr.Markdown("## Face Swap & Enhancement Tool")
68
- with gr.Row():
69
- input_img = gr.Image(type="file", label="Foto tjeter")
70
- swap_img = gr.Image(type="file", label="Foto e juaj")
71
-
72
- with gr.Row():
73
- output_img = gr.Image(type="filepath", label="Rezultati", visible=False)
74
- download_btn = gr.File(label="Shkarko", visible=False)
75
-
76
- submit_btn = gr.Button("Përpunoni")
77
-
78
- def predict_and_update_ui(img1, img2):
79
- result = predictor.predict(img1, img2)
80
- if result:
81
- return result, result, gr.update(visible=True), gr.update(visible=True)
82
- else:
83
- return None, None, gr.update(visible=False), gr.update(visible=False)
84
-
85
- submit_btn.click(
86
- fn=predict_and_update_ui,
87
- inputs=[input_img, swap_img],
88
- outputs=[output_img, download_btn, output_img, download_btn]
89
  )
 
90
 
91
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ from gradio_client import Client, handle_file
 
 
 
 
3
  import gradio as gr
4
 
5
+ HF_TOKEN = os.getenv("HF_TOKEN")
6
+
7
+ client = Client(
8
+ "franciszzj/Leffa",
9
+ hf_token=HF_TOKEN,
10
+ )
11
+
12
+ def virtual_tryon(person_path, garment_path, garment_type):
13
+ person_file = handle_file(person_path)
14
+ garment_file = handle_file(garment_path)
15
+
16
+ result = client.predict(
17
+ person_file,
18
+ garment_file,
19
+ ref_acceleration=False,
20
+ step=30,
21
+ scale=2.5,
22
+ seed=42,
23
+ vt_model_type="viton_hd",
24
+ vt_garment_type=garment_type,
25
+ vt_repaint=False,
26
+ api_name="/leffa_predict_vt",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  )
28
+ return result[0], result[0] # show image and prepare file for download
29
 
30
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
31
+ gr.Markdown("")
32
+ with gr.Row():
33
+ with gr.Column():
34
+ gr.Markdown("")
35
+ src = gr.Image(sources="upload", type="filepath", label="Foto e personit")
36
+ with gr.Column():
37
+ gr.Markdown("")
38
+ ref = gr.Image(sources="upload", type="filepath", label="Foto e veshjes")
39
+ with gr.Column():
40
+ gr.Markdown("")
41
+ garment_type = gr.Radio(
42
+ choices=[("Siper", "upper_body"),
43
+ ("Posht", "lower_body"),
44
+ ("Komplet", "dresses")],
45
+ value="upper_body",
46
+ label="Lloji i veshjes",
47
+ )
48
+ with gr.Column():
49
+ gr.Markdown("")
50
+ out = gr.Image(type="filepath", label="Rezultati")
51
+ download_btn = gr.File(label="Shkarko", interactive=True)
52
+ btn = gr.Button("VISHU")
53
+
54
+ btn.click(virtual_tryon, [src, ref, garment_type], [out, download_btn])
55
+
56
+ demo.launch(
57
+ share=True,
58
+ show_error=True,
59
+ pwa=True,
60
+ )