circlecount / app.py
Surya152002's picture
Update app.py
4ce8219 verified
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}")