Vignesh455 commited on
Commit
bd6214e
·
verified ·
1 Parent(s): d6ade83

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +48 -0
  2. app.py +105 -0
  3. requirements.txt +11 -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"]
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ progress = 0
13
+
14
+ def video_to_frames(video_path, output_folder):
15
+ vidcap = cv2.VideoCapture(video_path)
16
+ fps = vidcap.get(cv2.CAP_PROP_FPS)
17
+ success, image = vidcap.read()
18
+ count = 0
19
+ if not os.path.exists(output_folder):
20
+ os.makedirs(output_folder)
21
+
22
+ while success:
23
+ frame_name = os.path.join(output_folder, f"frame_{count}.jpg")
24
+ cv2.imwrite(frame_name, image)
25
+ success, image = vidcap.read()
26
+ count += 1
27
+ if count > 550:
28
+ break
29
+ print(f"{count} frames extracted from {video_path}.")
30
+ return [count, fps]
31
+
32
+ def frames_to_video(frame_folder, video_path, image_path, frame_count, fps):
33
+ global progress
34
+ frames = [f for f in os.listdir(frame_folder) if f.endswith('.jpg')]
35
+ frames.sort(key=lambda x: int(x.split('_')[1].split('.')[0])) # Sort frames in ascending order
36
+
37
+ frame = cv2.imread(os.path.join(frame_folder, frames[0]))
38
+ height, width, _ = frame.shape
39
+
40
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
41
+ out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
42
+
43
+ providers = ["CUDAExecutionProvider"]
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
+ print("Progress:",progress)
51
+ img1 = cv2.imread(os.path.join(frame_folder, frames[i]))
52
+ faces1 = app.get(img1)
53
+ for _ in range(20):
54
+ faces2 = app.get(image_path)
55
+ if faces2:
56
+ break
57
+ else:
58
+ return
59
+ if faces1:
60
+ face1 = faces1[0]
61
+ face2 = faces2[0]
62
+ result = img1.copy()
63
+ result = swapper.get(result, face1, face2, paste_back=True)
64
+ _, _, result = face_enhancer.enhance(result)
65
+ out.write(result)
66
+ else:
67
+ out.write(img1)
68
+ progress = int((i + 1) / frame_count * 100)
69
+ out.release()
70
+
71
+ print(f"Video saved at {video_path}.")
72
+
73
+ def face_swap(video_path, image_path):
74
+ global progress
75
+ progress = 0
76
+ output_folder = "Out_Frames"
77
+ frame_count = video_to_frames(video_path, output_folder)
78
+ if frame_count[0] > 400:
79
+ frame_count[0] = 400
80
+ output_video_path = "output_video.mp4"
81
+ frames_to_video(output_folder, output_video_path, image_path, frame_count[0], frame_count[1])
82
+ return output_video_path
83
+
84
+ def get_progress():
85
+ global progress
86
+ return progress
87
+
88
+ iface = gr.Interface(
89
+ fn=face_swap,
90
+ inputs=["video", "image"],
91
+ outputs="video",
92
+ title="Profaker's Face Swap",
93
+ description="Upload a video and an image. The faces in the video will be swapped with the face in the image.",
94
+ )
95
+
96
+ progress_iface = gr.Interface(
97
+ fn=get_progress,
98
+ inputs=[],
99
+ outputs="number",
100
+ live=True,
101
+ title="Progress Tracker"
102
+ )
103
+
104
+ iface.launch(share=True)
105
+ progress_iface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu118
2
+ insightface
3
+ aiogram==2.25.1
4
+ onnxruntime-gpu==1.17.1
5
+ opennsfw2
6
+ keras
7
+ gfpgan
8
+ accelerate
9
+ torchvision==0.16.2
10
+ nest_asyncio
11
+ gradio==4.31.0