Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,58 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
def remaster_image(image,
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
if
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
|
|
|
| 41 |
return image
|
| 42 |
|
| 43 |
# Gradio UI
|
|
@@ -50,24 +65,26 @@ with gr.Blocks() as demo:
|
|
| 50 |
with gr.Group():
|
| 51 |
gr.Markdown("### Upscaling Options")
|
| 52 |
upscale_checkbox = gr.Checkbox(label="Apply Upscaling")
|
| 53 |
-
upscale_factor = gr.Slider(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
with gr.Group():
|
| 59 |
gr.Markdown("### Remastering Options")
|
| 60 |
remaster_checkbox = gr.Checkbox(label="Apply Remastering")
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
process_button = gr.Button("Process Image")
|
| 66 |
|
| 67 |
process_button.click(
|
| 68 |
process_image,
|
| 69 |
-
inputs=[image_input, upscale_checkbox, upscale_factor,
|
| 70 |
-
remaster_checkbox,
|
| 71 |
outputs=image_output
|
| 72 |
)
|
| 73 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
import torch
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from torchvision.models import resnet50
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
import numpy as np
|
| 8 |
|
| 9 |
+
# Load a pre-trained ResNet model and modify it for upscaling
|
| 10 |
+
class Upscaler(torch.nn.Module):
|
| 11 |
+
def __init__(self, upscale_factor):
|
| 12 |
+
super(Upscaler, self).__init__()
|
| 13 |
+
self.model = resnet50(pretrained=True)
|
| 14 |
+
self.upscale_factor = upscale_factor
|
| 15 |
+
self.conv1x1 = torch.nn.Conv2d(1000, 3, kernel_size=1)
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
x = F.interpolate(x, scale_factor=self.upscale_factor, mode='bilinear', align_corners=True)
|
| 19 |
+
x = self.model(x)
|
| 20 |
+
x = self.conv1x1(x)
|
| 21 |
+
return x
|
| 22 |
|
| 23 |
+
# Custom remastering function with multiple options
|
| 24 |
+
def remaster_image(image, color_range=1.0, sharpness=1.0, hdr_intensity=1.0, tone_mapping=1.0, color_grading=1.0):
|
| 25 |
+
enhancer = transforms.ColorJitter(
|
| 26 |
+
brightness=hdr_intensity,
|
| 27 |
+
contrast=contrast,
|
| 28 |
+
saturation=color_range,
|
| 29 |
+
hue=0
|
| 30 |
+
)
|
| 31 |
+
image = enhancer(image)
|
| 32 |
+
|
| 33 |
+
# Adjust sharpness
|
| 34 |
+
image = transforms.functional.adjust_sharpness(image, sharpness_factor=sharpness)
|
| 35 |
+
|
| 36 |
+
# Apply tone mapping and color grading
|
| 37 |
+
tone_map = lambda x: x * tone_mapping
|
| 38 |
+
graded_image = transforms.functional.lerp(image, tone_map(image), color_grading)
|
| 39 |
|
| 40 |
+
return graded_image
|
| 41 |
+
|
| 42 |
+
# Function to process image with the selected options
|
| 43 |
+
def process_image(image, upscale=False, upscale_factor=2, noise_reduction=0, edge_enhancement=1.0,
|
| 44 |
+
detail_preservation=1.0, remaster=False, color_range=1.0, sharpness=1.0,
|
| 45 |
+
hdr_intensity=1.0, tone_mapping=1.0, color_grading=1.0):
|
| 46 |
+
image = transforms.ToTensor()(image).unsqueeze(0)
|
| 47 |
|
| 48 |
+
if upscale:
|
| 49 |
+
upscaler = Upscaler(upscale_factor)
|
| 50 |
+
image = upscaler(image)
|
| 51 |
+
|
| 52 |
+
if remaster:
|
| 53 |
+
image = remaster_image(image, color_range, sharpness, hdr_intensity, tone_mapping, color_grading)
|
| 54 |
|
| 55 |
+
image = transforms.ToPILImage()(image.squeeze(0))
|
| 56 |
return image
|
| 57 |
|
| 58 |
# Gradio UI
|
|
|
|
| 65 |
with gr.Group():
|
| 66 |
gr.Markdown("### Upscaling Options")
|
| 67 |
upscale_checkbox = gr.Checkbox(label="Apply Upscaling")
|
| 68 |
+
upscale_factor = gr.Slider(2, 8, value=2, label="Upscale Factor")
|
| 69 |
+
noise_reduction = gr.Slider(0, 100, value=0, label="Noise Reduction")
|
| 70 |
+
edge_enhancement = gr.Slider(0.5, 2.0, value=1.0, label="Edge Enhancement")
|
| 71 |
+
detail_preservation = gr.Slider(0.5, 2.0, value=1.0, label="Detail Preservation")
|
| 72 |
|
| 73 |
with gr.Group():
|
| 74 |
gr.Markdown("### Remastering Options")
|
| 75 |
remaster_checkbox = gr.Checkbox(label="Apply Remastering")
|
| 76 |
+
color_range = gr.Slider(0.5, 2.0, value=1.0, label="Dynamic Color Range")
|
| 77 |
+
sharpness = gr.Slider(0.5, 2.0, value=1.0, label="Advanced Sharpness Control")
|
| 78 |
+
hdr_intensity = gr.Slider(0.5, 2.0, value=1.0, label="HDR Intensity")
|
| 79 |
+
tone_mapping = gr.Slider(0.5, 2.0, value=1.0, label="Tone Mapping")
|
| 80 |
+
color_grading = gr.Slider(0.5, 2.0, value=1.0, label="Color Grading")
|
| 81 |
|
| 82 |
process_button = gr.Button("Process Image")
|
| 83 |
|
| 84 |
process_button.click(
|
| 85 |
process_image,
|
| 86 |
+
inputs=[image_input, upscale_checkbox, upscale_factor, noise_reduction, edge_enhancement, detail_preservation,
|
| 87 |
+
remaster_checkbox, color_range, sharpness, hdr_intensity, tone_mapping, color_grading],
|
| 88 |
outputs=image_output
|
| 89 |
)
|
| 90 |
|