import gradio as gr
import cv2
import json
import numpy as np
import glob2 as glob
from PIL import Image
from pyAAMED import pyAAMED
title = """
Arc Adjacency Matrix based Fast Ellipse Detection
Gitub
"""
def detect_ellipses(img_path):
imgC = cv2.imread(img_path)
imgG = cv2.cvtColor(imgC, cv2.COLOR_BGR2GRAY)
ammed_size = 600
iheight, iwidth = imgG.shape
imax = max(iheight, iwidth)
iscale = (ammed_size - 1) / imax
is_landscape = iwidth >= iheight
if is_landscape:
iw = imax * iscale
ih = iheight * iscale
else:
iw = iwidth * iscale
ih = imax * iscale
imgG = cv2.resize(imgG, (int(iw), int(ih)))
aamed = pyAAMED(ammed_size, ammed_size)
aamed.setParameters(3.1415926/3, 3.4, 0.77)
print(ammed_size, iw, ih, imgG.shape)
result = aamed.run_AAMED(imgG)
return [Image.fromarray(imgG), json.dumps(result.tolist())]
examples = []
test_files = glob.glob('./examples/*.jpg') + glob.glob('./examples/*.png')
for f in test_files:
examples = examples + [[f]]
gr.Interface(
fn=detect_ellipses,
inputs=gr.Image(label="Upload image with ellipses", type="filepath"),
outputs=[
gr.Image(type="pil", label="Detected ellipses"),
gr.Textbox(label="Detected ellipses")
],
title=title,
examples=examples,
allow_flagging='never'
).launch(
debug=True,
server_name="0.0.0.0",
server_port=7860
)