Surya152002 commited on
Commit
ff4c615
·
verified ·
1 Parent(s): 28fe950

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ # Function to count circles in an image frame
7
+ def count_circles(frame):
8
+ # Convert to grayscale and blur to remove noise
9
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
10
+ gray = cv2.medianBlur(gray, 5)
11
+
12
+ # Hough Circle Transform to detect circles
13
+ circles = cv2.HoughCircles(
14
+ gray, cv2.HOUGH_GRADIENT, dp=1.2, minDist=50,
15
+ param1=50, param2=30, minRadius=10, maxRadius=100
16
+ )
17
+
18
+ circle_count = 0
19
+ if circles is not None:
20
+ circles = np.uint16(np.around(circles))
21
+ for i in circles[0, :]:
22
+ # Draw the circle
23
+ cv2.circle(frame, (i[0], i[1]), i[2], (0, 255, 0), 3)
24
+ circle_count += 1
25
+
26
+ return frame, circle_count
27
+
28
+ # Streamlit UI
29
+ def main():
30
+ st.title("Circle Counter with Camera Input")
31
+ st.write("Use your device camera to count and highlight circles in real-time or upload an image.")
32
+
33
+ # Option to choose between Camera and Image upload
34
+ option = st.radio("Choose Input Method:", ["Camera", "Upload Image"])
35
+
36
+ # If user selects Camera
37
+ if option == "Camera":
38
+ st.write("Starting Camera Stream...")
39
+ camera_stream = st.empty() # Stream placeholder
40
+ stop_button = st.button("Stop Camera")
41
+
42
+ # Access camera using OpenCV
43
+ cap = cv2.VideoCapture(0) # 0 is the default camera
44
+
45
+ while cap.isOpened() and not stop_button:
46
+ ret, frame = cap.read()
47
+ if not ret:
48
+ st.error("Failed to capture video!")
49
+ break
50
+
51
+ # Count circles in the frame
52
+ processed_frame, circle_count = count_circles(frame)
53
+
54
+ # Convert the frame for display in Streamlit
55
+ frame_rgb = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
56
+ camera_stream.image(frame_rgb, caption=f"Circles Detected: {circle_count}", use_column_width=True)
57
+
58
+ cap.release()
59
+ st.write("Camera Stopped.")
60
+
61
+ # If user selects Upload Image
62
+ elif option == "Upload Image":
63
+ uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
64
+
65
+ if uploaded_file is not None:
66
+ # Display uploaded image
67
+ image = Image.open(uploaded_file)
68
+ st.image(image, caption="Uploaded Image", use_column_width=True)
69
+
70
+ # Convert to OpenCV format
71
+ frame = np.array(image)
72
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
73
+
74
+ # Count circles
75
+ processed_image, circle_count = count_circles(frame)
76
+
77
+ # Display processed image
78
+ processed_image_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
79
+ st.image(processed_image_rgb, caption=f"Circles Detected: {circle_count}", use_column_width=True)
80
+ st.success(f"Number of circles detected: {circle_count}")
81
+
82
+ if __name__ == "__main__":
83
+ main()