gradio_opencv / app.py
emilios's picture
Update app.py
b8e825d verified
raw
history blame
1.11 kB
import gradio
import cv2
import numpy as np
def inference(img):
#my_result = cv2.erode(img,(1,1))
#my_result = cv2.dilate(my_result,(1,1))
# https://scikit-image.org/docs/dev/api/skimage.morphology.html#skimage.morphology.remove_small_objects
# my_result = cv2.remove_small_objects(binarized.astype(bool), min_size=2, connectivity=2).astype(int)
img_bw = 255*(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) > 5).astype('uint8')
se1 = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
se2 = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
mask = cv2.morphologyEx(img_bw, cv2.MORPH_CLOSE, se1)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, se2)
mask = np.dstack([mask, mask, mask]) / 255
out = img * mask
return out
# For information on Interfaces, head to https://gradio.app/docs/
# For user guides, head to https://gradio.app/guides/
# For Spaces usage, head to https://huggingface.co/docs/hub/spaces
iface = gradio.Interface(
fn=inference,
inputs='image',
outputs='image',
title='Hello World',
description='The simplest interface!',
examples=["llama.jpg"])
iface.launch()