Spaces:
Sleeping
Sleeping
initial commit
Browse files- app.py +71 -0
- requirements.txt +117 -0
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import insightface
|
| 5 |
+
from insightface.app import FaceAnalysis
|
| 6 |
+
import datetime
|
| 7 |
+
import os
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def faceswapper(user_image, result_image, username="test"):
|
| 12 |
+
output_folder = 'outputs'
|
| 13 |
+
|
| 14 |
+
# Convert PIL images to NumPy arrays for processing
|
| 15 |
+
guest_img = np.array(user_image)
|
| 16 |
+
result_img = np.array(result_image)
|
| 17 |
+
|
| 18 |
+
# Convert RGB (PIL) to BGR (OpenCV)
|
| 19 |
+
guest_img = guest_img[:, :, ::-1]
|
| 20 |
+
result_img = result_img[:, :, ::-1]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Initialize the FaceAnalysis app
|
| 24 |
+
app = FaceAnalysis(name='buffalo_l')
|
| 25 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 26 |
+
|
| 27 |
+
# Initialize the face swapper model
|
| 28 |
+
swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=False, download_zip=False)
|
| 29 |
+
|
| 30 |
+
# Detect face in the guest image
|
| 31 |
+
guest_faces = app.get(guest_img)
|
| 32 |
+
guest_face = guest_faces[0]
|
| 33 |
+
|
| 34 |
+
# Detect faces in the result image
|
| 35 |
+
faces = app.get(result_img)
|
| 36 |
+
|
| 37 |
+
# Perform face swapping
|
| 38 |
+
for face in faces:
|
| 39 |
+
result_img = swapper.get(result_img, face, guest_face, paste_back=True)
|
| 40 |
+
|
| 41 |
+
# Save the result in the specified output folder
|
| 42 |
+
if not os.path.exists(output_folder):
|
| 43 |
+
os.makedirs(output_folder)
|
| 44 |
+
current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 45 |
+
output_path = os.path.join(output_folder, f'{username}_swapped_face_{current_time}.jpg')
|
| 46 |
+
cv2.imwrite(output_path, result_img)
|
| 47 |
+
|
| 48 |
+
# Convert the final image from BGR to RGB before returning
|
| 49 |
+
result_img = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Convert back to PIL image
|
| 53 |
+
result_img_pil = Image.fromarray(result_img)
|
| 54 |
+
original_size = result_image.size
|
| 55 |
+
result_img_pil = result_img_pil.resize(original_size, Image.Resampling.LANCZOS)
|
| 56 |
+
|
| 57 |
+
return result_img_pil
|
| 58 |
+
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
name = gr.Textbox(label="이름(파일저장용)")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
user_image_input = gr.Image(type="pil", label="유저사진(얼굴추출)", width=300, height=300)
|
| 65 |
+
result_image_input = gr.Image(type="pil", label="결과물 사진", width=300, height=300)
|
| 66 |
+
swap_btn = gr.Button("Swap Faces")
|
| 67 |
+
with gr.Column():
|
| 68 |
+
output_image = gr.Image(label="합성 후 사진")
|
| 69 |
+
swap_btn.click(fn=faceswapper, inputs=[user_image_input, result_image_input, name], outputs=output_image)
|
| 70 |
+
|
| 71 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
albumentations==1.3.1
|
| 3 |
+
altair==5.2.0
|
| 4 |
+
annotated-types==0.6.0
|
| 5 |
+
anyio==4.2.0
|
| 6 |
+
appnope @ file:///home/conda/feedstock_root/build_artifacts/appnope_1649077682618/work
|
| 7 |
+
asttokens @ file:///home/conda/feedstock_root/build_artifacts/asttokens_1698341106958/work
|
| 8 |
+
attrs==23.2.0
|
| 9 |
+
certifi==2023.11.17
|
| 10 |
+
charset-normalizer==3.3.2
|
| 11 |
+
click==8.1.7
|
| 12 |
+
colorama==0.4.6
|
| 13 |
+
coloredlogs==15.0.1
|
| 14 |
+
comm @ file:///home/conda/feedstock_root/build_artifacts/comm_1704278392174/work
|
| 15 |
+
contourpy==1.2.0
|
| 16 |
+
cycler==0.12.1
|
| 17 |
+
Cython==3.0.7
|
| 18 |
+
debugpy @ file:///Users/runner/miniforge3/conda-bld/debugpy_1695534448458/work
|
| 19 |
+
decorator @ file:///home/conda/feedstock_root/build_artifacts/decorator_1641555617451/work
|
| 20 |
+
easydict==1.11
|
| 21 |
+
exceptiongroup @ file:///home/conda/feedstock_root/build_artifacts/exceptiongroup_1700579780973/work
|
| 22 |
+
executing @ file:///home/conda/feedstock_root/build_artifacts/executing_1698579936712/work
|
| 23 |
+
fastapi==0.108.0
|
| 24 |
+
ffmpy==0.3.1
|
| 25 |
+
filelock==3.13.1
|
| 26 |
+
flatbuffers==23.5.26
|
| 27 |
+
fonttools==4.47.0
|
| 28 |
+
fsspec==2023.12.2
|
| 29 |
+
gradio==4.13.0
|
| 30 |
+
gradio_client==0.8.0
|
| 31 |
+
h11==0.14.0
|
| 32 |
+
httpcore==1.0.2
|
| 33 |
+
httpx==0.26.0
|
| 34 |
+
huggingface-hub==0.20.2
|
| 35 |
+
humanfriendly==10.0
|
| 36 |
+
idna==3.6
|
| 37 |
+
imageio==2.33.1
|
| 38 |
+
importlib-metadata @ file:///home/conda/feedstock_root/build_artifacts/importlib-metadata_1703269254275/work
|
| 39 |
+
importlib-resources==6.1.1
|
| 40 |
+
insightface==0.7.3
|
| 41 |
+
ipykernel @ file:///Users/runner/miniforge3/conda-bld/ipykernel_1703631823913/work
|
| 42 |
+
ipython @ file:///home/conda/feedstock_root/build_artifacts/ipython_1703263236586/work
|
| 43 |
+
jedi @ file:///home/conda/feedstock_root/build_artifacts/jedi_1696326070614/work
|
| 44 |
+
Jinja2==3.1.2
|
| 45 |
+
joblib==1.3.2
|
| 46 |
+
jsonschema==4.20.0
|
| 47 |
+
jsonschema-specifications==2023.12.1
|
| 48 |
+
jupyter_client @ file:///home/conda/feedstock_root/build_artifacts/jupyter_client_1699283905679/work
|
| 49 |
+
jupyter_core @ file:///Users/runner/miniforge3/conda-bld/jupyter_core_1704321426011/work
|
| 50 |
+
kiwisolver==1.4.5
|
| 51 |
+
lazy_loader==0.3
|
| 52 |
+
markdown-it-py==3.0.0
|
| 53 |
+
MarkupSafe==2.1.3
|
| 54 |
+
matplotlib==3.8.2
|
| 55 |
+
matplotlib-inline @ file:///home/conda/feedstock_root/build_artifacts/matplotlib-inline_1660814786464/work
|
| 56 |
+
mdurl==0.1.2
|
| 57 |
+
mpmath==1.3.0
|
| 58 |
+
nest-asyncio @ file:///home/conda/feedstock_root/build_artifacts/nest-asyncio_1697083700168/work
|
| 59 |
+
networkx==3.2.1
|
| 60 |
+
numpy==1.26.3
|
| 61 |
+
onnx==1.15.0
|
| 62 |
+
onnxruntime==1.16.3
|
| 63 |
+
opencv-python-headless==4.9.0.80
|
| 64 |
+
orjson==3.9.10
|
| 65 |
+
packaging @ file:///home/conda/feedstock_root/build_artifacts/packaging_1696202382185/work
|
| 66 |
+
pandas==2.1.4
|
| 67 |
+
parso @ file:///home/conda/feedstock_root/build_artifacts/parso_1638334955874/work
|
| 68 |
+
pexpect @ file:///home/conda/feedstock_root/build_artifacts/pexpect_1667297516076/work
|
| 69 |
+
pickleshare @ file:///home/conda/feedstock_root/build_artifacts/pickleshare_1602536217715/work
|
| 70 |
+
pillow==10.2.0
|
| 71 |
+
platformdirs @ file:///home/conda/feedstock_root/build_artifacts/platformdirs_1701708255999/work
|
| 72 |
+
prettytable==3.9.0
|
| 73 |
+
prompt-toolkit @ file:///home/conda/feedstock_root/build_artifacts/prompt-toolkit_1702399386289/work
|
| 74 |
+
protobuf==4.25.1
|
| 75 |
+
psutil @ file:///Users/runner/miniforge3/conda-bld/psutil_1702833169958/work
|
| 76 |
+
ptyprocess @ file:///home/conda/feedstock_root/build_artifacts/ptyprocess_1609419310487/work/dist/ptyprocess-0.7.0-py2.py3-none-any.whl
|
| 77 |
+
pure-eval @ file:///home/conda/feedstock_root/build_artifacts/pure_eval_1642875951954/work
|
| 78 |
+
pydantic==2.5.3
|
| 79 |
+
pydantic_core==2.14.6
|
| 80 |
+
pydub==0.25.1
|
| 81 |
+
Pygments @ file:///home/conda/feedstock_root/build_artifacts/pygments_1700607939962/work
|
| 82 |
+
pyparsing==3.1.1
|
| 83 |
+
python-dateutil @ file:///home/conda/feedstock_root/build_artifacts/python-dateutil_1626286286081/work
|
| 84 |
+
python-multipart==0.0.6
|
| 85 |
+
pytz==2023.3.post1
|
| 86 |
+
PyYAML==6.0.1
|
| 87 |
+
pyzmq @ file:///Users/runner/miniforge3/conda-bld/pyzmq_1701783254483/work
|
| 88 |
+
qudida==0.0.4
|
| 89 |
+
referencing==0.32.1
|
| 90 |
+
requests==2.31.0
|
| 91 |
+
rich==13.7.0
|
| 92 |
+
rpds-py==0.16.2
|
| 93 |
+
scikit-image==0.22.0
|
| 94 |
+
scikit-learn==1.3.2
|
| 95 |
+
scipy==1.11.4
|
| 96 |
+
semantic-version==2.10.0
|
| 97 |
+
shellingham==1.5.4
|
| 98 |
+
six @ file:///home/conda/feedstock_root/build_artifacts/six_1620240208055/work
|
| 99 |
+
sniffio==1.3.0
|
| 100 |
+
stack-data @ file:///home/conda/feedstock_root/build_artifacts/stack_data_1669632077133/work
|
| 101 |
+
starlette==0.32.0.post1
|
| 102 |
+
sympy==1.12
|
| 103 |
+
threadpoolctl==3.2.0
|
| 104 |
+
tifffile==2023.12.9
|
| 105 |
+
tomlkit==0.12.0
|
| 106 |
+
toolz==0.12.0
|
| 107 |
+
tornado @ file:///Users/runner/miniforge3/conda-bld/tornado_1695373570914/work
|
| 108 |
+
tqdm==4.66.1
|
| 109 |
+
traitlets @ file:///home/conda/feedstock_root/build_artifacts/traitlets_1704212992681/work
|
| 110 |
+
typer==0.9.0
|
| 111 |
+
typing_extensions @ file:///home/conda/feedstock_root/build_artifacts/typing_extensions_1702176139754/work
|
| 112 |
+
tzdata==2023.4
|
| 113 |
+
urllib3==2.1.0
|
| 114 |
+
uvicorn==0.25.0
|
| 115 |
+
wcwidth @ file:///home/conda/feedstock_root/build_artifacts/wcwidth_1700607916581/work
|
| 116 |
+
websockets==11.0.3
|
| 117 |
+
zipp @ file:///home/conda/feedstock_root/build_artifacts/zipp_1695255097490/work
|