Surya152002 commited on
Commit
8d036e1
·
verified ·
1 Parent(s): 4a05a02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -37
app.py CHANGED
@@ -3,49 +3,81 @@ import cv2
3
  import numpy as np
4
  from PIL import Image
5
 
6
- # Function to detect and count circles
7
- def detect_circles(image):
8
- # Convert to grayscale
9
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
10
- # Apply GaussianBlur to reduce noise and improve circle detection
11
- blurred = cv2.GaussianBlur(gray, (9, 9), 2)
12
- # Hough Circle Transform for detecting circles
13
- circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=1.2, minDist=30,
14
- param1=50, param2=30, minRadius=10, maxRadius=100)
15
-
 
 
16
  circle_count = 0
17
  if circles is not None:
18
- circles = np.uint16(np.around(circles)) # Convert to integer
19
  for i in circles[0, :]:
20
- # Draw the detected circles
21
- cv2.circle(image, (i[0], i[1]), i[2], (0, 255, 0), 3)
22
  circle_count += 1
 
 
23
 
24
- return image, circle_count
25
-
26
- # Streamlit App
27
  def main():
28
- st.title("Log and Pipe Counter Using Computer Vision")
29
- st.write("Upload an image to detect and count wooden logs or PVC pipes in a truck.")
30
-
31
- # File upload
32
- uploaded_file = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
33
- if uploaded_file is not None:
34
- # Load the image
35
- image = Image.open(uploaded_file)
36
- st.image(image, caption="Uploaded Image", use_column_width=True)
37
-
38
- # Convert to OpenCV format
39
- image_cv = np.array(image)
40
- image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
41
-
42
- # Detect and count circles
43
- processed_image, count = detect_circles(image_cv)
44
-
45
- # Display the result
46
- st.image(cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB),
47
- caption=f"Processed Image - Total Circles Detected: {count}", use_column_width=True)
48
- st.success(f"Number of circular objects detected: {count}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  if __name__ == "__main__":
51
  main()
 
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()