Spaces:
Build error
Build error
Commit ·
d16b2ef
1
Parent(s): 48aceac
Upload 3 files
Browse files- .gitignore +5 -0
- app.py +69 -0
- requirements.txt +6 -0
.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flagged/
|
| 2 |
+
*.pt
|
| 3 |
+
*.png
|
| 4 |
+
*.jpg
|
| 5 |
+
gradio_cached_examples/
|
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import insightface
|
| 4 |
+
from insightface.app import FaceAnalysis
|
| 5 |
+
import insightface.model_zoo
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import requests
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
file_urls = [
|
| 11 |
+
'https://drive.google.com/file/d/18O9t37O0PUEzIF6yFEIIvoO0ouSyo2a1/view?usp=sharing'
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
def download_file(url, save_name):
|
| 15 |
+
url = url
|
| 16 |
+
if not os.path.exists(save_name):
|
| 17 |
+
file = requests.get(url)
|
| 18 |
+
open(save_name, 'wb').write(file.content)
|
| 19 |
+
|
| 20 |
+
for i, url in enumerate(file_urls):
|
| 21 |
+
download_file(
|
| 22 |
+
file_urls[i],
|
| 23 |
+
f'inswapper.onnx'
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
app = FaceAnalysis(name= "buffalo_l")
|
| 27 |
+
app.prepare(ctx_id=0, det_size=(640,640))
|
| 28 |
+
swapper = insightface.model_zoo.get_model('inswapper.onnx', download=True)
|
| 29 |
+
|
| 30 |
+
def show_preds_image(image_path1, image_path2):
|
| 31 |
+
poster = cv2.imread(image_path1)
|
| 32 |
+
facial = cv2.imread(image_path2)
|
| 33 |
+
|
| 34 |
+
poster_faces = app.get(poster)
|
| 35 |
+
facial_faces = app.get(facial)
|
| 36 |
+
|
| 37 |
+
facial_face = facial_faces[0]
|
| 38 |
+
bbox = facial_face['bbox']
|
| 39 |
+
bbox = [int(b) for b in bbox]
|
| 40 |
+
|
| 41 |
+
faces = app.get(poster)
|
| 42 |
+
|
| 43 |
+
res = poster.copy()
|
| 44 |
+
for face in faces:
|
| 45 |
+
poster = swapper.get(poster, face, facial_face, paste_back=True)
|
| 46 |
+
|
| 47 |
+
result_path = 'path_to_save_result_image.jpg'
|
| 48 |
+
cv2.imwrite(result_path, poster)
|
| 49 |
+
|
| 50 |
+
return result_path
|
| 51 |
+
|
| 52 |
+
inputs_image = [
|
| 53 |
+
gr.components.Image(type='filepath', label="Input Image 1"),
|
| 54 |
+
gr.components.Image(type='filepath', label="Input Image 2")
|
| 55 |
+
]
|
| 56 |
+
output_image = [
|
| 57 |
+
gr.components.Image(type='numpy', label='Output Image')
|
| 58 |
+
]
|
| 59 |
+
interface_image = gr.Interface(
|
| 60 |
+
fn= show_preds_image,
|
| 61 |
+
inputs= inputs_image,
|
| 62 |
+
outputs= output_image,
|
| 63 |
+
title= "Face Changer",
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
gr.TabbedInterface(
|
| 67 |
+
[interface_image],
|
| 68 |
+
tab_names=['Image interface']
|
| 69 |
+
).queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
cv2
|
| 3 |
+
insightface
|
| 4 |
+
matplotlib
|
| 5 |
+
os
|
| 6 |
+
requests
|