Spaces:
Sleeping
Sleeping
File size: 1,011 Bytes
e73dc89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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()
|