Spaces:
Sleeping
Sleeping
import gradio as gr | |
import numpy as np | |
import cv2 | |
def run(img: np.ndarray, thres: int) -> tuple[np.ndarray, int | str]: | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
if thres > 0: | |
_, img = cv2.threshold(img, thres, 255, cv2.THRESH_BINARY) | |
threshold = thres | |
elif thres < 0: | |
img = cv2.adaptiveThreshold( | |
img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, -thres | |
) | |
threshold = "adaptive" | |
else: | |
threshold, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) | |
return img, threshold | |
app = gr.Interface( | |
fn=run, | |
inputs=[ | |
gr.Image(label="image"), | |
gr.Slider( | |
-30, | |
255, | |
-2, | |
label="threshold", | |
info="0 for Otsu's method, negative for adaptive thresholding", | |
), | |
], | |
outputs=[ | |
gr.Image(label="processed"), | |
gr.Label(label="threshold"), | |
], | |
allow_flagging="never", | |
) | |
app.queue().launch() | |