File size: 9,642 Bytes
686f3e2
 
 
 
77b40bf
 
 
 
 
 
686f3e2
 
77b40bf
 
 
 
 
 
 
 
 
 
 
 
 
4251451
 
77b40bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4251451
 
 
77b40bf
 
 
 
 
 
 
 
 
 
 
 
4251451
77b40bf
4251451
77b40bf
 
 
4251451
 
 
 
 
 
 
 
 
 
 
 
77b40bf
 
 
 
4251451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77b40bf
 
 
 
 
 
4251451
77b40bf
4251451
 
 
 
 
 
 
77b40bf
 
 
4251451
 
 
 
 
 
 
 
 
77b40bf
 
 
 
 
 
 
 
 
 
4251451
 
 
 
 
 
 
77b40bf
 
 
 
4251451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77b40bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4251451
77b40bf
 
 
 
 
 
 
 
 
4251451
77b40bf
53d84e5
 
 
 
 
 
c7acffa
 
 
 
 
 
 
53d84e5
4251451
c7acffa
77b40bf
 
 
 
 
 
 
 
 
c7acffa
 
 
53d84e5
4251451
c7acffa
77b40bf
 
 
 
 
 
 
 
 
 
c7acffa
 
686f3e2
 
 
 
 
c7acffa
 
77b40bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4251451
 
 
 
 
686f3e2
c7acffa
 
 
 
 
 
77b40bf
 
 
 
 
 
 
 
 
4251451
77b40bf
c7acffa
 
686f3e2
77b40bf
 
686f3e2
c7acffa
4251451
77b40bf
4251451
77b40bf
 
4251451
 
77b40bf
686f3e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import gradio as gr
import subprocess
from git import Repo
import os
import cv2
import numpy as np
from PIL import Image
import spaces
from diffusers import StableDiffusionImg2ImgPipeline
import torch


device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = None


def load_pipeline():
    global pipe
    if pipe is None:
        pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
            "nroggendorff/epicrealism",
            torch_dtype=torch.float16 if device == "cuda" else torch.float32,
            safety_checker=None,
        )
        pipe = pipe.to(device)
        if hasattr(pipe, "enable_attention_slicing"):
            pipe.enable_attention_slicing()
    return pipe


@spaces.GPU
def refine_with_img2img(image_path, strength=0.3, steps=30, seed=42):
    pipeline = load_pipeline()
    img = Image.open(image_path).convert("RGB")
    generator = torch.Generator(device=device).manual_seed(seed)

    result = pipeline(
        prompt="high quality, detailed, photorealistic, natural texture",
        negative_prompt="blurry, low quality, distorted, deformed, watermark",
        image=img,
        strength=strength,
        num_inference_steps=steps,
        guidance_scale=7.5,
        generator=generator,
    ).images[0]

    result.save(image_path)


@spaces.GPU
def refine_video_with_img2img(
    video_path, strength=0.3, steps=30, seed=42, batch_size=4
):
    pipeline = load_pipeline()
    generator = torch.Generator(device=device).manual_seed(seed)

    cap = cv2.VideoCapture(video_path)
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    temp_output = "temp_refined.mp4"
    out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))

    frames_batch = []
    frame_count = 0

    while True:
        ret, frame = cap.read()
        if not ret:
            if frames_batch:
                for pil_frame in frames_batch:
                    refined = pipeline(
                        prompt="high quality, detailed face, photorealistic, natural skin texture",
                        image=pil_frame,
                        strength=strength,
                        num_inference_steps=steps,
                        guidance_scale=7.5,
                        generator=generator,
                    ).images[0]
                    refined_cv = cv2.cvtColor(np.array(refined), cv2.COLOR_RGB2BGR)
                    out.write(refined_cv)
            break

        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        pil_frame = Image.fromarray(frame_rgb)
        frames_batch.append(pil_frame)

        if len(frames_batch) >= batch_size:
            for pil_frame in frames_batch:
                refined = pipeline(
                    prompt="high quality, detailed face, photorealistic, natural skin texture",
                    image=pil_frame,
                    strength=strength,
                    num_inference_steps=steps,
                    guidance_scale=7.5,
                    generator=generator,
                ).images[0]
                refined_cv = cv2.cvtColor(np.array(refined), cv2.COLOR_RGB2BGR)
                out.write(refined_cv)
                frame_count += 1
                print(f"Processed frame {frame_count}")
            frames_batch = []

    cap.release()
    out.release()
    os.replace(temp_output, video_path)


def denoise_image_gpu(image_path, strength=10):
    img = cv2.imread(image_path)
    img_gpu = cv2.cuda_GpuMat()
    img_gpu.upload(img)

    denoised_gpu = cv2.cuda.fastNlMeansDenoisingColored(
        img_gpu, strength, strength, 7, 21
    )
    denoised = denoised_gpu.download()
    cv2.imwrite(image_path, denoised)


def denoise_image(image_path, strength=10):
    try:
        denoise_image_gpu(image_path, strength)
    except:
        img = cv2.imread(image_path)
        denoised = cv2.fastNlMeansDenoisingColored(img, None, strength, strength, 7, 21)
        cv2.imwrite(image_path, denoised)


def denoise_video(video_path, strength=10):
    cap = cv2.VideoCapture(video_path)
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    temp_output = "temp_denoised.mp4"
    out = cv2.VideoWriter(temp_output, fourcc, fps, (width, height))

    use_gpu = False
    try:
        test_gpu = cv2.cuda_GpuMat()
        use_gpu = True
    except:
        pass

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        if use_gpu:
            try:
                frame_gpu = cv2.cuda_GpuMat()
                frame_gpu.upload(frame)
                denoised_gpu = cv2.cuda.fastNlMeansDenoisingColored(
                    frame_gpu, strength, strength, 7, 21
                )
                denoised_frame = denoised_gpu.download()
            except:
                denoised_frame = cv2.fastNlMeansDenoisingColored(
                    frame, None, strength, strength, 7, 21
                )
        else:
            denoised_frame = cv2.fastNlMeansDenoisingColored(
                frame, None, strength, strength, 7, 21
            )

        out.write(denoised_frame)

    cap.release()
    out.release()
    os.replace(temp_output, video_path)


def enhance_image(image_path):
    img = cv2.imread(image_path)
    lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(lab)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    l = clahe.apply(l)
    enhanced = cv2.merge([l, a, b])
    enhanced = cv2.cvtColor(enhanced, cv2.COLOR_LAB2BGR)
    cv2.imwrite(image_path, enhanced)


@spaces.GPU
def process_media(
    image,
    image_or_video,
    denoise_strength,
    enhance,
    use_img2img,
    img2img_strength,
    img2img_steps,
    seed,
    execution_provider,
):
    if os.path.exists("output_video.mp4"):
        os.remove("output_video.mp4")
    if os.path.exists("output_image.png"):
        os.remove("output_image.png")
    if os.path.exists("source.png"):
        os.remove("source.png")

    image_output = None
    video_output = None

    if isinstance(image_or_video, str) and image_or_video.endswith(
        (".mp4", ".avi", ".mov")
    ):
        image.save("source.png")
        cmd = f"python3 roop/run.py -s source.png -t '{image_or_video}' -o output_video.mp4 --execution-provider {execution_provider}"
        subprocess.run(cmd, shell=True)

        if os.path.exists("output_video.mp4"):
            if use_img2img:
                refine_video_with_img2img(
                    "output_video.mp4", img2img_strength, img2img_steps, seed
                )
            denoise_video("output_video.mp4", denoise_strength)
            video_output = gr.Video(value="output_video.mp4", visible=True)

    elif isinstance(image_or_video, str) and image_or_video.endswith(
        (".png", ".jpg", ".jpeg")
    ):
        image.save("source.png")
        cmd = f"python3 roop/run.py -s source.png -t '{image_or_video}' -o output_image.png --execution-provider {execution_provider}"
        subprocess.run(cmd, shell=True)

        if os.path.exists("output_image.png"):
            if use_img2img:
                refine_with_img2img(
                    "output_image.png", img2img_strength, img2img_steps, seed
                )
            denoise_image("output_image.png", denoise_strength)
            if enhance:
                enhance_image("output_image.png")
            image_output = gr.Image(value="output_image.png", visible=True)

    return image_output, video_output


with gr.Blocks() as demo:
    with gr.Row():
        image = gr.Image(label="Image", type="pil")
        image_or_video = gr.File(label="Image or Video", type="filepath")

    with gr.Row():
        denoise_strength = gr.Slider(
            minimum=0, maximum=30, value=1, step=0.5, label="Denoise Strength"
        )
        enhance = gr.Checkbox(label="Enhance Contrast (Images Only)", value=True)

    with gr.Row():
        use_img2img = gr.Checkbox(
            label="Use EpicRealism Img2Img Refinement", value=False
        )
        img2img_strength = gr.Slider(
            minimum=0.1, maximum=0.8, value=0.15, step=0.05, label="Img2Img Strength"
        )
        img2img_steps = gr.Slider(
            minimum=10, maximum=50, value=20, step=5, label="Img2Img Steps"
        )
        seed = gr.Number(label="Seed", value=42, precision=0)

    with gr.Row():
        execution_provider = gr.Radio(
            choices=["cuda", "tensorrt"], value="cuda", label="Roop Execution Provider"
        )

    process_btn = gr.Button("Process")

    image_output = gr.Image(label="Output Image", visible=False)
    video_output = gr.Video(label="Output Video", visible=False)

    process_btn.click(
        fn=process_media,
        inputs=[
            image,
            image_or_video,
            denoise_strength,
            enhance,
            use_img2img,
            img2img_strength,
            img2img_steps,
            seed,
            execution_provider,
        ],
        outputs=[image_output, video_output],
    )

demo.queue()

if __name__ == "__main__":
    if not os.path.exists("roop"):
        Repo.clone_from("https://github.com/nroggendorff/roop.git", "roop")
        subprocess.run("pip install -r roop/requirements.txt", shell=True)

    os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
    os.environ["TF_USE_LEGACY_KERAS"] = "1"
    os.environ["OMP_NUM_THREADS"] = "8"
    os.environ["MKL_NUM_THREADS"] = "8"

    demo.launch()