fjenett's picture
Update app.py
85df948
raw
history blame
1.64 kB
import gradio as gr
import cv2
import json
import numpy as np
from pyAAMED import pyAAMED
title = """
<h1>Arc Adjacency Matrix based Fast Ellipse Detection</h1>
<a href="https://github.com/Li-Zhaoxi/AAMED">Gitub</a>
"""
def detect_ellipses(img_path):
imgC = cv2.imread(img_path)
imgG = cv2.cvtColor(imgC, cv2.COLOR_BGR2GRAY)
ammed_size = 1200
iheight, iwidth = np.array(imgG.shape, type='int')
imax = max(iheight, iwidth)
iscale = ammed_size / imax
is_landscape = iwidth >= iheight
if is_landscape:
iw = imax * iscale
ih = iheight * iscale
else:
iw = iwidth * iscale
ih = imax * iscale
imgG = imgG.resize(iw, ih)
if is_landscape:
ipad = (imax - iheight) / 2 * iscale
imgG = cv2.copyMakeBorder(imgG, ipad, ipad, 0, 0, cv2.BORDER_REPLICATE)
else:
ipad = (imax - iwidth) / 2 * iscale
imgG = cv2.copyMakeBorder(imgG, 0, 0, ipad, ipad, cv2.BORDER_REPLICATE)
aamed = pyAAMED(ammed_size, ammed_size)
aamed.setParameters(3.1415926/3, 3.4, 0.77)
result = aamed.run_AAMED(imgG)
result = result.replace(' ', ', ')
result = json.loads(result)
print(result)
return [img_path, json.dumps(result)]
examples = [
["./AAMED/python/002_0038.jpg"]
]
gr.Interface(
fn=detect_ellipses,
inputs=gr.Image(label="Upload image with ellipses", type="filepath"),
outputs=[
gr.Image(type="filepath", label="Detected ellipses"),
gr.Text(label="Detected data")
],
title=title,
examples=examples,
allow_flagging='never'
).launch(server_name="0.0.0.0", server_port=7860)