Spaces:
Sleeping
Sleeping
File size: 1,814 Bytes
ff4c615 7cb0447 c8594a4 a3df750 4ce8219 5f84ddc 3dc54e0 4ce8219 a3df750 4ce8219 7cb0447 4ce8219 7cb0447 a3df750 4ce8219 a3df750 4ce8219 a3df750 3dc54e0 a3df750 4ce8219 4a53c7b a3df750 ff4c615 a3df750 4ce8219 a3df750 4ce8219 a3df750 4ce8219 a3df750 4ce8219 a3df750 4ce8219 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import streamlit as st
import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
from numpy.lib.polynomial import poly
from PIL import Image
# Function to detect circular objects
def detect_circles(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Use HoughCircles to detect circles
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=1.2, minDist=30, param1=50, param2=30, minRadius=10, maxRadius=100)
# If we found some circles, draw them
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
center = (i[0], i[1]) # center of the circle
radius = i[2] # radius of the circle
# Draw the circle center
cv2.circle(image, center, 1, (0, 100, 100), 3)
# Draw the circle outline
cv2.circle(image, center, radius, (255, 0, 255), 3)
return image, len(circles[0, :]) # Return image with drawn circles and count
else:
return image, 0 # No circles found
# Streamlit UI
st.title('Circular Object Detection')
# Image upload
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
if uploaded_file is not None:
# Read and process the uploaded image
image = np.array(Image.open(uploaded_file))
# Convert RGB to BGR
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Detect circles
result_image, circle_count = detect_circles(image_bgr)
# Convert BGR back to RGB for displaying in Streamlit
result_image_rgb = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
# Display result
st.image(result_image_rgb, channels="RGB", caption="Detected Circles")
st.write(f"Total circular objects detected: {circle_count}")
|