saiki987's picture
gbrからrgbへの変換忘れを修正
1678125
Raw
History Blame Contribute Delete
2.1 kB
import glob
import cv2 as cv
import gradio as gr
import numpy as np
images = glob.glob("examples/*")
cache_data = {}
def clear_cache(img_path: str):
global cache_data
cache_data = {}
img = cv.imread(img_path)
return img[:, :, ::-1]
def findCircle(img_path: str, center):
if not cache_data:
source = cv.imread(img_path, cv.IMREAD_GRAYSCALE)
img = cv.medianBlur(source,5)
_color_img = cv.imread(img_path)
_circles = cv.HoughCircles(img,cv.HOUGH_GRADIENT,1,minDist=20,
param1=50,param2=30,minRadius=10,maxRadius=28)
_circles = np.uint16(np.around(_circles))
cache_data['img'] = _color_img
cache_data['circles'] = _circles
circles = cache_data['circles']
color_img = cache_data['img']
for i in circles[0,:]:
if abs(center[0] - i[0]) < 20 \
and abs(center[1] - i[1]) < 20:
# draw the outer circle
cv.circle(color_img,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv.circle(color_img,(i[0],i[1]),2,(0,0,255),3)
# bgr to rgb
return color_img[:, :, ::-1]
with gr.Blocks() as demo:
input_img = gr.Image(images[0], type='filepath', label="", interactive=False).style(height=600, width=1000)
# output_img = gr.Image(label="出力")
# キャッシュがある場合は、それを表示する。
def on_image_load():
if cache_data:
return cache_data['img'][:, :, ::-1]
return images[0]
input_img.attach_load_event(on_image_load, every=None)
button = gr.Button(value='リセット').style(size='sm')
def get_select_coords(evt: gr.SelectData):
position = (evt.index[0], evt.index[1])
img_path = images[0]
return findCircle(img_path, position)
def on_reset():
return clear_cache(images[0])
input_img.select(get_select_coords, [], input_img)
button.click(on_reset, [], input_img)
if __name__ == "__main__":
demo.launch()