mischeiwiller's picture
Update tutorials link
1247692 verified
raw
history blame contribute delete
No virus
2.27 kB
import gradio as gr
import kornia as K
from kornia.core import Tensor
import numpy as np
def enhance(image, brightness, contrast, saturation, gamma, hue):
# Convert numpy array to tensor
if isinstance(image, np.ndarray):
img = K.image_to_tensor(image).float() / 255.0
else:
# If it's a file path (for examples), load it using kornia
img = K.io.load_image(image, K.io.ImageLoadType.RGB32)
img = img.unsqueeze(0) # 1xCxHxW / fp32 / [0, 1]
# apply tensor image enhancement
x_out: Tensor = K.enhance.adjust_brightness(img, float(brightness))
x_out = K.enhance.adjust_contrast(x_out, float(contrast))
x_out = K.enhance.adjust_saturation(x_out, float(saturation))
x_out = K.enhance.adjust_gamma(x_out, float(gamma))
x_out = K.enhance.adjust_hue(x_out, float(hue))
return K.utils.tensor_to_image(x_out.squeeze())
examples = [
["examples/ninja_turtles.jpg", 0, 1, 1, 1, 0],
["examples/kitty.jpg", 0, 1, 1, 1, 0],
]
title = "Kornia Image Enhancements"
description = "<p style='text-align: center'>This is a Gradio demo for Kornia's Image Enhancements.</p><p style='text-align: center'>To use it, simply upload your image, or click one of the examples to load them, and use the sliders to enhance! Read more at the links at the bottom.</p>"
article = "<p style='text-align: center'><a href='https://kornia.readthedocs.io/en/latest/' target='_blank'>Kornia Docs</a> | <a href='https://github.com/kornia/kornia' target='_blank'>Kornia Github Repo</a> | <a href='https://kornia.github.io/tutorials/#category=kornia.enhance' target='_blank'>Kornia Enhancements Tutorial</a></p>"
iface = gr.Interface(
enhance,
[
gr.Image(type="numpy"),
gr.Slider(minimum=0, maximum=1, step=0.1, value=0, label="Brightness"),
gr.Slider(minimum=0, maximum=4, step=0.1, value=1, label="Contrast"),
gr.Slider(minimum=0, maximum=4, step=0.1, value=1, label="Saturation"),
gr.Slider(minimum=0, maximum=1, step=0.1, value=1, label="Gamma"),
gr.Slider(minimum=-0.5, maximum=0.5, step=0.1, value=0, label="Hue"),
],
"image",
examples=examples,
title=title,
description=description,
article=article,
live=True
)
iface.launch()