gdTharusha commited on
Commit
772383d
1 Parent(s): 95dcecd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -28
app.py CHANGED
@@ -1,24 +1,31 @@
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
 
@@ -32,21 +39,33 @@ def remaster_image(image, color_range=1.0, sharpness=1.0, hdr_intensity=1.0, ton
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)
@@ -64,14 +83,12 @@ with gr.Blocks() as demo:
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,8 +97,7 @@ with gr.Blocks() as demo:
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
 
 
1
  import gradio as gr
2
  from PIL import Image, ImageEnhance
 
3
  import torch
4
+ import torch.nn.functional as F
5
+ from torchvision import transforms
6
  from torchvision.models import resnet34
7
+ from torchvision.models.segmentation import deeplabv3_resnet50
8
+ import numpy as np
9
+ import cv2
10
 
11
+ # Load a pre-trained ResNet model for remastering
12
+ resnet_model = resnet34(pretrained=True)
13
+ resnet_model.eval()
14
 
15
+ # Load a pre-trained DeepLab model for segmentation (optional for advanced remastering)
16
+ deeplab_model = deeplabv3_resnet50(pretrained=True)
17
+ deeplab_model.eval()
18
+
19
+ # Define the upscaling function using super-resolution techniques
20
+ def upscale_image(image, upscale_factor=2):
21
+ # Convert the image to a tensor and upscale it using a neural network
22
+ preprocess = transforms.Compose([
23
+ transforms.ToTensor(),
24
+ transforms.Lambda(lambda x: x.unsqueeze(0))
25
+ ])
26
+ img_tensor = preprocess(image)
27
+ upscaled_tensor = F.interpolate(img_tensor, scale_factor=upscale_factor, mode='bicubic', align_corners=False)
28
+ upscaled_image = transforms.ToPILImage()(upscaled_tensor.squeeze())
29
 
30
  return upscaled_image
31
 
 
39
  enhancer = ImageEnhance.Sharpness(image)
40
  image = enhancer.enhance(sharpness)
41
 
42
+ # Apply a simulated HDR effect using tone mapping
43
  enhancer = ImageEnhance.Brightness(image)
44
  image = enhancer.enhance(hdr_intensity)
45
+
 
46
  enhancer = ImageEnhance.Contrast(image)
47
  image = enhancer.enhance(color_grading)
48
+
49
+ # Optional: Use segmentation to remaster specific regions
50
+ input_tensor = transforms.ToTensor()(image).unsqueeze(0)
51
+ with torch.no_grad():
52
+ output = deeplab_model(input_tensor)['out'][0]
53
+ output_predictions = output.argmax(0)
54
+
55
+ # Process each segmented region (e.g., sky, water) differently (optional)
56
+ # Example: Apply a slight blur to the sky region to create a dreamy effect
57
+ mask = output_predictions.byte().cpu().numpy()
58
+ segmented_image = np.array(image)
59
+ segmented_image[mask == 15] = cv2.GaussianBlur(segmented_image[mask == 15], (5, 5), 0)
60
+
61
+ final_image = Image.fromarray(segmented_image)
62
+
63
+ return final_image
64
 
65
  # Process function for Gradio
66
+ def process_image(image, upscale=False, upscale_factor=2, remaster=False, color_range=1.0, sharpness=1.0, hdr_intensity=1.0, tone_mapping=1.0, color_grading=1.0):
 
67
  if upscale:
68
+ image = upscale_image(image, upscale_factor)
69
 
70
  if remaster:
71
  image = remaster_image(image, color_range, sharpness, hdr_intensity, tone_mapping, color_grading)
 
83
  gr.Markdown("### Upscaling Options")
84
  upscale_checkbox = gr.Checkbox(label="Apply Upscaling")
85
  upscale_factor = gr.Slider(1, 8, value=2, label="Upscale Factor")
 
 
 
86
 
87
  with gr.Group():
88
  gr.Markdown("### Remastering Options")
89
  remaster_checkbox = gr.Checkbox(label="Apply Remastering")
90
  color_range = gr.Slider(0.5, 2.0, value=1.0, label="Dynamic Color Range")
91
+ sharpness = gr.Slider(0.5, 2.0, value=1.0, label="Sharpness")
92
  hdr_intensity = gr.Slider(0.5, 2.0, value=1.0, label="HDR Intensity")
93
  tone_mapping = gr.Slider(0.5, 2.0, value=1.0, label="Tone Mapping")
94
  color_grading = gr.Slider(0.5, 2.0, value=1.0, label="Color Grading")
 
97
 
98
  process_button.click(
99
  process_image,
100
+ inputs=[image_input, upscale_checkbox, upscale_factor, remaster_checkbox, color_range, sharpness, hdr_intensity, tone_mapping, color_grading],
 
101
  outputs=image_output
102
  )
103