mischeiwiller commited on
Commit
6edb6be
·
verified ·
1 Parent(s): d628205

fix: update app.py to work with new gradio sdk

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -1,23 +1,26 @@
1
  import gradio as gr
2
-
3
  import kornia as K
4
  from kornia.core import Tensor
5
-
6
-
7
- def enhance(file, brightness, contrast, saturation, gamma, hue):
8
- # load the image using the rust backend
9
- img: Tensor = K.io.load_image(file.name, K.io.ImageLoadType.RGB32)
10
- img = img[None] # 1xCxHxW / fp32 / [0, 1]
11
-
 
 
 
 
 
12
  # apply tensor image enhancement
13
- x_out: Tensor = K.enhance.adjust_brightness(img, float(brightness))
14
- x_out = K.enhance.adjust_contrast(x_out, float(contrast))
15
- x_out = K.enhance.adjust_saturation(x_out, float(saturation))
16
- x_out = K.enhance.adjust_gamma(x_out, float(gamma))
17
- x_out = K.enhance.adjust_hue(x_out, float(hue))
18
-
19
- return K.utils.tensor_to_image(x_out)
20
-
21
 
22
  examples = [
23
  ["examples/ninja_turtles.jpg", 0, 1, 1, 1, 0],
@@ -31,18 +34,18 @@ article = "<p style='text-align: center'><a href='https://kornia.readthedocs.io/
31
  iface = gr.Interface(
32
  enhance,
33
  [
34
- gr.inputs.Image(type="file"),
35
- gr.inputs.Slider(minimum=0, maximum=1, step=0.1, default=0, label="Brightness"),
36
- gr.inputs.Slider(minimum=0, maximum=4, step=0.1, default=1, label="Contrast"),
37
- gr.inputs.Slider(minimum=0, maximum=4, step=0.1, default=1, label="Saturation"),
38
- gr.inputs.Slider(minimum=0, maximum=1, step=0.1, default=1, label="Gamma"),
39
- gr.inputs.Slider(minimum=0, maximum=4, step=0.1, default=0, label="Hue"),
40
  ],
41
  "image",
42
  examples=examples,
43
- # title=title,
44
- # description=description,
45
- # article=article,
46
  live=True
47
  )
48
 
 
1
  import gradio as gr
 
2
  import kornia as K
3
  from kornia.core import Tensor
4
+ import numpy as np
5
+
6
+ def enhance(image, brightness, contrast, saturation, gamma, hue):
7
+ # Convert numpy array to tensor
8
+ if isinstance(image, np.ndarray):
9
+ img = K.image_to_tensor(image).float() / 255.0
10
+ else:
11
+ # If it's a file path (for examples), load it using kornia
12
+ img = K.io.load_image(image, K.io.ImageLoadType.RGB32)
13
+
14
+ img = img.unsqueeze(0) # 1xCxHxW / fp32 / [0, 1]
15
+
16
  # apply tensor image enhancement
17
+ x_out: Tensor = K.enhance.adjust_brightness(img, brightness)
18
+ x_out = K.enhance.adjust_contrast(x_out, contrast)
19
+ x_out = K.enhance.adjust_saturation(x_out, saturation)
20
+ x_out = K.enhance.adjust_gamma(x_out, gamma)
21
+ x_out = K.enhance.adjust_hue(x_out, hue)
22
+
23
+ return K.utils.tensor_to_image(x_out.squeeze())
 
24
 
25
  examples = [
26
  ["examples/ninja_turtles.jpg", 0, 1, 1, 1, 0],
 
34
  iface = gr.Interface(
35
  enhance,
36
  [
37
+ gr.Image(type="numpy"),
38
+ gr.Slider(minimum=0, maximum=1, step=0.1, value=0, label="Brightness"),
39
+ gr.Slider(minimum=0, maximum=4, step=0.1, value=1, label="Contrast"),
40
+ gr.Slider(minimum=0, maximum=4, step=0.1, value=1, label="Saturation"),
41
+ gr.Slider(minimum=0, maximum=1, step=0.1, value=1, label="Gamma"),
42
+ gr.Slider(minimum=-0.5, maximum=0.5, step=0.1, value=0, label="Hue"),
43
  ],
44
  "image",
45
  examples=examples,
46
+ title=title,
47
+ description=description,
48
+ article=article,
49
  live=True
50
  )
51