profaker commited on
Commit
24a5a1b
1 Parent(s): e3bbad3

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +48 -0
  2. GFPGANv1.4.pth +3 -0
  3. app.py +93 -0
  4. inswapper_128.onnx +3 -0
  5. requirements.txt +9 -0
Dockerfile ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04
2
+
3
+ ARG DEBIAN_FRONTEND=noninteractive
4
+
5
+ ENV PYTHONUNBUFFERED=1 \
6
+ ORT_CUDA_PROVIDER_OPTIONS=1
7
+
8
+ #RUN apt-get install libcudnn8=${8.9.0}-1+${11.8.0} && apt-get install libcudnn8-dev=${8.9.0}-1+${11.8.0}
9
+
10
+ RUN apt-get update && apt-get install --no-install-recommends -y \
11
+ build-essential \
12
+ python3.10-dev \
13
+ python3-pip \
14
+ git \
15
+ ffmpeg \
16
+ && apt-get clean && rm -rf /var/lib/apt/lists/*
17
+
18
+ # Install NVIDIA CUDA Toolkit
19
+ RUN apt-get update --yes && apt install nvidia-cuda-toolkit --yes && apt-get install zlib1g
20
+
21
+ WORKDIR /code
22
+
23
+ COPY ./requirements.txt /code/requirements.txt
24
+
25
+ # Set up a new user named "user" with user ID 1000
26
+ RUN useradd -m -u 1000 user
27
+ # Switch to the "user" user
28
+ USER user
29
+ # Set home to the user's home directory
30
+ ENV HOME=/home/user \
31
+ PATH=/home/user/.local/bin:$PATH \
32
+ PYTHONPATH=$HOME/app \
33
+ PYTHONUNBUFFERED=1 \
34
+ GRADIO_ALLOW_FLAGGING=never \
35
+ GRADIO_NUM_PORTS=1 \
36
+ GRADIO_SERVER_NAME=0.0.0.0 \
37
+ GRADIO_THEME=huggingface \
38
+ SYSTEM=spaces
39
+
40
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
41
+
42
+ # Set the working directory to the user's home directory
43
+ WORKDIR $HOME/app
44
+
45
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
46
+ COPY --chown=user . $HOME/app
47
+
48
+ CMD ["python3", "app.py"]
GFPGANv1.4.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e2cd4703ab14f4d01fd1383a8a8b266f9a5833dacee8e6a79d3bf21a1b6be5ad
3
+ size 348632874
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import insightface
4
+ import onnxruntime
5
+ from tqdm import tqdm
6
+ import shutil
7
+ import gfpgan
8
+ import gradio as gr
9
+ import subprocess
10
+ from PIL import Image
11
+
12
+
13
+ def video_to_frames(video_path, output_folder):
14
+ vidcap = cv2.VideoCapture(video_path)
15
+ fps = vidcap.get(cv2.CAP_PROP_FPS)
16
+ success, image = vidcap.read()
17
+ count = 0
18
+ if not os.path.exists(output_folder):
19
+ os.makedirs(output_folder)
20
+
21
+ while success:
22
+ frame_name = os.path.join(output_folder, f"frame_{count}.jpg")
23
+ cv2.imwrite(frame_name, image)
24
+ success, image = vidcap.read()
25
+ count += 1
26
+ if count>500:
27
+ break
28
+ print(f"{count} frames extracted from {video_path}.")
29
+ return [count,fps]
30
+
31
+ def frames_to_video(frame_folder, video_path, image_path, frame_count,fps):
32
+ print("ImagePath",image_path)
33
+ frames = [f for f in os.listdir(frame_folder) if f.endswith('.jpg')]
34
+ frames.sort(key=lambda x: int(x.split('_')[1].split('.')[0])) # Sort frames in ascending order
35
+
36
+ frame = cv2.imread(os.path.join(frame_folder, frames[0]))
37
+ height, width, _ = frame.shape
38
+
39
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
40
+ out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
41
+
42
+ providers = ["CUDAExecutionProvider"]
43
+ print("Available Providers==",providers)
44
+ app = insightface.app.FaceAnalysis(name='buffalo_l', providers=providers)
45
+ app.prepare(ctx_id=0, det_size=(640, 640))
46
+ swapper = insightface.model_zoo.get_model("inswapper_128.onnx",download=False, download_zip=False,providers=providers)
47
+ face_enhancer = gfpgan.GFPGANer(model_path="GFPGANv1.4.pth", upscale=1, device='cuda')
48
+
49
+ for i in tqdm(range(frame_count), desc="Converting frames to video"):
50
+ img1 = cv2.imread(os.path.join(frame_folder, frames[i]))
51
+ #img2_pil = Image.open(image_path)
52
+ #img2_cv2 = cv2.cvtColor(np.array(img2_pil), cv2.COLOR_RGB2BGR)
53
+
54
+ faces1 = app.get(img1)
55
+ for _ in range(20):
56
+ faces2 = app.get(image_path)
57
+ if faces2:
58
+ break
59
+ else:
60
+ return
61
+ if faces1:
62
+ face1 = faces1[0]
63
+ face2 = faces2[0]
64
+ result = img1.copy()
65
+ result = swapper.get(result, face1, face2, paste_back=True)
66
+ _, _, result = face_enhancer.enhance(result)
67
+ out.write(result)
68
+ else:
69
+ out.write(img1)
70
+ progress = int((i + 1) / frame_count * 100)
71
+ print(progress)
72
+ out.release()
73
+
74
+ print(f"Video saved at {video_path}.")
75
+
76
+ def face_swap(video_path, image_path):
77
+ output_folder = "Out_Frames"
78
+ frame_count = video_to_frames(video_path, output_folder)
79
+ if frame_count[0] > 150:
80
+ frame_count[0] = 150
81
+ output_video_path = "output_video.mp4"
82
+ frames_to_video(output_folder, output_video_path, image_path, frame_count[0],frame_count[1])
83
+ return output_video_path
84
+
85
+
86
+ iface = gr.Interface(
87
+ fn=face_swap,
88
+ inputs=["video", "image"],
89
+ outputs="video",
90
+ title="Face Swap",
91
+ description="Upload a video and an image. The faces in the video will be swapped with the face in the image.",
92
+ )
93
+ iface.launch(share=True)
inswapper_128.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e4a3f08c753cb72d04e10aa0f7dbe3deebbf39567d4ead6dce08e98aa49e16af
3
+ size 554253681
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu118
2
+ insightface
3
+ onnxruntime-gpu==1.17.1
4
+ opennsfw2
5
+ keras
6
+ gfpgan
7
+ accelerate
8
+ torchvision==0.16.2
9
+ gradio