Spaces:
Running
Running
gdTharusha
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image
|
|
|
3 |
import torch
|
4 |
import torchvision.transforms as transforms
|
5 |
-
from torchvision.models import
|
6 |
-
import torch.nn.functional as F
|
7 |
-
import numpy as np
|
8 |
|
9 |
-
# Load a pre-trained ResNet model
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
-
#
|
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 |
-
|
26 |
-
|
27 |
-
|
28 |
-
saturation=color_range,
|
29 |
-
hue=0
|
30 |
-
)
|
31 |
-
image = enhancer(image)
|
32 |
|
33 |
# Adjust sharpness
|
34 |
-
|
|
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
|
|
|
|
41 |
|
42 |
-
|
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,16 +63,15 @@ with gr.Blocks() as demo:
|
|
65 |
with gr.Group():
|
66 |
gr.Markdown("### Upscaling Options")
|
67 |
upscale_checkbox = gr.Checkbox(label="Apply Upscaling")
|
68 |
-
upscale_factor = gr.Slider(
|
69 |
-
|
70 |
-
|
71 |
-
|
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")
|
@@ -83,8 +80,8 @@ with gr.Blocks() as demo:
|
|
83 |
|
84 |
process_button.click(
|
85 |
process_image,
|
86 |
-
inputs=[image_input, upscale_checkbox, upscale_factor,
|
87 |
-
remaster_checkbox, color_range,
|
88 |
outputs=image_output
|
89 |
)
|
90 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageEnhance
|
3 |
+
import numpy as np
|
4 |
import torch
|
5 |
import torchvision.transforms as transforms
|
6 |
+
from torchvision.models import resnet34
|
|
|
|
|
7 |
|
8 |
+
# Load a pre-trained ResNet model
|
9 |
+
model = resnet34(pretrained=True)
|
10 |
+
|
11 |
+
# Define the upscaling function
|
12 |
+
def upscale_image(image, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0):
|
13 |
+
# Resize the image
|
14 |
+
width, height = image.size
|
15 |
+
new_size = (int(width * upscale_factor), int(height * upscale_factor))
|
16 |
+
upscaled_image = image.resize(new_size, Image.BICUBIC)
|
17 |
+
|
18 |
+
# Apply sharpness, contrast, and brightness adjustments
|
19 |
+
upscaled_image = ImageEnhance.Sharpness(upscaled_image).enhance(sharpness)
|
20 |
+
upscaled_image = ImageEnhance.Contrast(upscaled_image).enhance(contrast)
|
21 |
+
upscaled_image = ImageEnhance.Brightness(upscaled_image).enhance(brightness)
|
22 |
+
|
23 |
+
return upscaled_image
|
24 |
|
25 |
+
# Define the remastering function
|
26 |
def remaster_image(image, color_range=1.0, sharpness=1.0, hdr_intensity=1.0, tone_mapping=1.0, color_grading=1.0):
|
27 |
+
# Adjust color range
|
28 |
+
enhancer = ImageEnhance.Color(image)
|
29 |
+
image = enhancer.enhance(color_range)
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Adjust sharpness
|
32 |
+
enhancer = ImageEnhance.Sharpness(image)
|
33 |
+
image = enhancer.enhance(sharpness)
|
34 |
|
35 |
+
# For HDR simulation and tone mapping, we're using simple brightness adjustments
|
36 |
+
enhancer = ImageEnhance.Brightness(image)
|
37 |
+
image = enhancer.enhance(hdr_intensity)
|
38 |
|
39 |
+
# Simulate color grading by adjusting contrast
|
40 |
+
enhancer = ImageEnhance.Contrast(image)
|
41 |
+
image = enhancer.enhance(color_grading)
|
42 |
|
43 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Process function for Gradio
|
46 |
+
def process_image(image, upscale=False, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0,
|
47 |
+
remaster=False, color_range=1.0, hdr_intensity=1.0, tone_mapping=1.0, color_grading=1.0):
|
48 |
+
if upscale:
|
49 |
+
image = upscale_image(image, upscale_factor, sharpness, contrast, brightness)
|
50 |
+
|
51 |
if remaster:
|
52 |
image = remaster_image(image, color_range, sharpness, hdr_intensity, tone_mapping, color_grading)
|
53 |
|
|
|
54 |
return image
|
55 |
|
56 |
# Gradio UI
|
|
|
63 |
with gr.Group():
|
64 |
gr.Markdown("### Upscaling Options")
|
65 |
upscale_checkbox = gr.Checkbox(label="Apply Upscaling")
|
66 |
+
upscale_factor = gr.Slider(1, 8, value=2, label="Upscale Factor")
|
67 |
+
sharpness = gr.Slider(0.5, 2.0, value=1.0, label="Sharpness")
|
68 |
+
contrast = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
|
69 |
+
brightness = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
|
70 |
|
71 |
with gr.Group():
|
72 |
gr.Markdown("### Remastering Options")
|
73 |
remaster_checkbox = gr.Checkbox(label="Apply Remastering")
|
74 |
color_range = gr.Slider(0.5, 2.0, value=1.0, label="Dynamic Color Range")
|
|
|
75 |
hdr_intensity = gr.Slider(0.5, 2.0, value=1.0, label="HDR Intensity")
|
76 |
tone_mapping = gr.Slider(0.5, 2.0, value=1.0, label="Tone Mapping")
|
77 |
color_grading = gr.Slider(0.5, 2.0, value=1.0, label="Color Grading")
|
|
|
80 |
|
81 |
process_button.click(
|
82 |
process_image,
|
83 |
+
inputs=[image_input, upscale_checkbox, upscale_factor, sharpness, contrast, brightness,
|
84 |
+
remaster_checkbox, color_range, hdr_intensity, tone_mapping, color_grading],
|
85 |
outputs=image_output
|
86 |
)
|
87 |
|