atlury commited on
Commit
21f63e5
1 Parent(s): 1009662

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -1,21 +1,22 @@
1
  import os
2
-
3
  import torch
4
- import torch.nn.functional as F
5
  from torchvision.transforms import Compose, ToTensor, Resize, Normalize, ConvertImageDtype
6
-
7
  import numpy as np
8
- import cv2
9
 
10
  import gradio as gr
11
- from huggingface_hub import hf_hub_download
12
-
13
  from model import IAT
14
 
15
-
16
  def set_example_image(example: list) -> dict:
17
  return gr.Image.update(value=example[0])
18
 
 
 
 
 
 
 
19
 
20
  def dark_inference(img):
21
  model = IAT()
@@ -34,9 +35,11 @@ def dark_inference(img):
34
  input_img = transform(img)
35
  print(f'Image shape: {input_img.shape}')
36
 
37
- enhanced_img = model(input_img.unsqueeze(0))
38
- return enhanced_img[0].permute(1, 2, 0).detach().numpy()
39
-
 
 
40
 
41
  def exposure_inference(img):
42
  model = IAT()
@@ -49,14 +52,17 @@ def exposure_inference(img):
49
  transform = Compose([
50
  ToTensor(),
51
  Resize(384),
 
52
  ConvertImageDtype(torch.float)
53
  ])
54
  input_img = transform(img)
55
  print(f'Image shape: {input_img.shape}')
56
 
57
- enhanced_img = model(input_img.unsqueeze(0))
58
- return enhanced_img[0].permute(1, 2, 0).detach().numpy()
59
-
 
 
60
 
61
  demo = gr.Blocks()
62
  with demo:
@@ -77,7 +83,7 @@ with demo:
77
  with gr.Row():
78
  exposure_button = gr.Button('Exposure Correction')
79
  with gr.Column():
80
- res_image = gr.Image(type='numpy', label='Resutls')
81
  with gr.Row():
82
  dark_example_images = gr.Dataset(
83
  components=[input_image],
@@ -100,4 +106,4 @@ with demo:
100
  dark_example_images.click(fn=set_example_image, inputs=dark_example_images, outputs=dark_example_images.components)
101
  exposure_example_images.click(fn=set_example_image, inputs=exposure_example_images, outputs=exposure_example_images.components)
102
 
103
- demo.launch(enable_queue=True)
 
1
  import os
 
2
  import torch
3
+ import cv2
4
  from torchvision.transforms import Compose, ToTensor, Resize, Normalize, ConvertImageDtype
5
+ from PIL import Image
6
  import numpy as np
 
7
 
8
  import gradio as gr
 
 
9
  from model import IAT
10
 
 
11
  def set_example_image(example: list) -> dict:
12
  return gr.Image.update(value=example[0])
13
 
14
+ def tensor_to_numpy(tensor):
15
+ tensor = tensor.detach().cpu().numpy()
16
+ if tensor.ndim == 3 and tensor.shape[0] == 3: # Convert CHW to HWC
17
+ tensor = tensor.transpose(1, 2, 0)
18
+ tensor = np.clip(tensor * 255, 0, 255).astype(np.uint8) # Ensure the output is uint8
19
+ return tensor
20
 
21
  def dark_inference(img):
22
  model = IAT()
 
35
  input_img = transform(img)
36
  print(f'Image shape: {input_img.shape}')
37
 
38
+ with torch.no_grad():
39
+ enhanced_img = model(input_img.unsqueeze(0))
40
+
41
+ result_img = tensor_to_numpy(enhanced_img[0])
42
+ return result_img
43
 
44
  def exposure_inference(img):
45
  model = IAT()
 
52
  transform = Compose([
53
  ToTensor(),
54
  Resize(384),
55
+ Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
56
  ConvertImageDtype(torch.float)
57
  ])
58
  input_img = transform(img)
59
  print(f'Image shape: {input_img.shape}')
60
 
61
+ with torch.no_grad():
62
+ enhanced_img = model(input_img.unsqueeze(0))
63
+
64
+ result_img = tensor_to_numpy(enhanced_img[0])
65
+ return result_img
66
 
67
  demo = gr.Blocks()
68
  with demo:
 
83
  with gr.Row():
84
  exposure_button = gr.Button('Exposure Correction')
85
  with gr.Column():
86
+ res_image = gr.Image(type='numpy', label='Results')
87
  with gr.Row():
88
  dark_example_images = gr.Dataset(
89
  components=[input_image],
 
106
  dark_example_images.click(fn=set_example_image, inputs=dark_example_images, outputs=dark_example_images.components)
107
  exposure_example_images.click(fn=set_example_image, inputs=exposure_example_images, outputs=exposure_example_images.components)
108
 
109
+ demo.launch(enable_queue=True)