gdTharusha commited on
Commit
9980bbc
1 Parent(s): dd74f7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -42
app.py CHANGED
@@ -1,43 +1,58 @@
1
  import gradio as gr
 
2
  import torch
3
- from torchvision import transforms
4
- from PIL import Image, ImageEnhance
5
- import rembg
 
6
 
7
- # Upscaling function using a basic upscaling approach
8
- def upscale_image(image, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0):
9
- # Resize the image
10
- width, height = image.size
11
- new_size = (int(width * upscale_factor), int(height * upscale_factor))
12
- upscaled_image = image.resize(new_size, Image.BICUBIC)
13
-
14
- # Apply sharpness, contrast, and brightness adjustments
15
- upscaled_image = ImageEnhance.Sharpness(upscaled_image).enhance(sharpness)
16
- upscaled_image = ImageEnhance.Contrast(upscaled_image).enhance(contrast)
17
- upscaled_image = ImageEnhance.Brightness(upscaled_image).enhance(brightness)
18
-
19
- return upscaled_image
20
 
21
- # Remastering function with background removal and enhancements
22
- def remaster_image(image, remove_background=False, color_balance=1.0, detail_enhancement=1.0):
23
- if remove_background:
24
- image = rembg.remove(image)
25
-
26
- # Apply color balance and detail enhancement
27
- image = ImageEnhance.Color(image).enhance(color_balance)
28
- image = ImageEnhance.Sharpness(image).enhance(detail_enhancement)
29
-
30
- return image
 
 
 
 
 
 
31
 
32
- # Main function combining upscaling and remastering
33
- def process_image(image, apply_upscale=False, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0,
34
- apply_remaster=False, remove_background=False, color_balance=1.0, detail_enhancement=1.0):
35
- if apply_upscale:
36
- image = upscale_image(image, upscale_factor, sharpness, contrast, brightness)
 
 
37
 
38
- if apply_remaster:
39
- image = remaster_image(image, remove_background, color_balance, detail_enhancement)
 
 
 
 
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(1, 4, value=2, label="Upscale Factor")
54
- sharpness = gr.Slider(0.5, 2.0, value=1.0, label="Sharpness")
55
- contrast = gr.Slider(0.5, 2.0, value=1.0, label="Contrast")
56
- brightness = gr.Slider(0.5, 2.0, value=1.0, label="Brightness")
57
 
58
  with gr.Group():
59
  gr.Markdown("### Remastering Options")
60
  remaster_checkbox = gr.Checkbox(label="Apply Remastering")
61
- remove_background = gr.Checkbox(label="Remove Background")
62
- color_balance = gr.Slider(0.5, 2.0, value=1.0, label="Color Balance")
63
- detail_enhancement = gr.Slider(0.5, 2.0, value=1.0, label="Detail Enhancement")
 
 
64
 
65
  process_button = gr.Button("Process Image")
66
 
67
  process_button.click(
68
  process_image,
69
- inputs=[image_input, upscale_checkbox, upscale_factor, sharpness, contrast, brightness,
70
- remaster_checkbox, remove_background, color_balance, detail_enhancement],
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