実際のところ
import streamlit as st from skimage import io, color, feature, draw from skimage.transform import hough_circle, hough_circle_peaks import numpy as np def find_circles(image, min_radius, max_radius): gray_image = color.rgb2gray(image) edges = feature.canny(gray_image) hough_radii = np.arange(min_radius, max_radius,2) hough_res = hough_circle(edges, hough_radii) for _, x, y, radius in zip(*hough_circle_peaks(hough_res, hough_radii,total_num_peaks=3)): rr, cc = draw.circle_perimeter(int(y), int(x), int(radius), shape=image.shape) image[rr, cc] = (255, 0, 0) return image st.title("Circle Detection using Streamlit and scikit-image") uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) if uploaded_file is not None: image = io.imread(uploaded_file) st.image(image, caption="Uploaded Image", use_column_width=True) st.sidebar.header("Hough Radii Parameters") # Added a header in the sidebar min_radius = st.sidebar.slider("Min Radius", 1, 100, 40) max_radius = st.sidebar.slider("Max Radius", 1, 1000, 90) result = find_circles(image, min_radius, max_radius) st.image(result, caption="Result with Circles Detected", use_column_width=True)
使ってみる
$ streamlit run app.py
読み込む画像はWikimedia Commonsから借りてきました
五度圏という音楽用語を解説した図のようです(無知
https://upload.wikimedia.org/wikipedia/commons/3/3b/Circle_OF_Fifths.jpg
デフォのパラメータだとこんな検知ですが
スライダーを動かしてやる事で画像の再判定と描画ができます。
処理中は色合いが処理中っポイ感じで変化し、上の方には処理中のアイコンが動いてくれます。
……自分で作ると面倒なので助かりますね、これは。
参考もと
- ChatGPT-4大明神の神託
- st.file_uploader - Streamlit Docs