Surya152002 commited on
Commit
a3df750
·
verified ·
1 Parent(s): 7cb0447

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -68
app.py CHANGED
@@ -1,83 +1,61 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import cv2
3
  import numpy as np
4
+ import matplotlib.pyplot as plt
5
  from PIL import Image
6
 
7
+ # Function to detect circular pipes
8
+ def detect_pipes(image):
9
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
 
10
 
11
+ # Apply GaussianBlur to reduce image noise and improve circle detection
12
+ blurred = cv2.GaussianBlur(gray, (15, 15), 0)
13
+
14
+ # Detect circles using HoughCircles
15
  circles = cv2.HoughCircles(
16
+ blurred,
17
+ cv2.HOUGH_GRADIENT,
18
+ dp=1.2,
19
+ minDist=30,
20
+ param1=50,
21
+ param2=30,
22
+ minRadius=20,
23
+ maxRadius=150
24
  )
25
 
 
26
  if circles is not None:
27
+ circles = np.uint16(np.around(circles)) # Convert float to integer
28
  for i in circles[0, :]:
29
+ center = (i[0], i[1]) # center of the circle
30
+ radius = i[2] # radius of the circle
31
+ # Draw the circle center
32
+ cv2.circle(image, center, 1, (0, 100, 100), 3)
33
+ # Draw the circle perimeter
34
+ cv2.circle(image, center, radius, (255, 0, 255), 3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ return image, len(circles[0, :]) # Return image with circles drawn and the count
37
+ else:
38
+ return image, 0 # No circles found
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ # Streamlit UI
41
+ st.title('Pipe Detection in Industrial Rack')
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ # Image upload
44
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
 
 
45
 
46
+ if uploaded_file is not None:
47
+ # Read the uploaded image
48
+ image = np.array(Image.open(uploaded_file))
49
+
50
+ # Convert from RGB to BGR (OpenCV uses BGR by default)
51
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
52
+
53
+ # Detect pipes
54
+ result_image, pipe_count = detect_pipes(image_bgr)
55
+
56
+ # Convert the result image back to RGB for displaying in Streamlit
57
+ result_image_rgb = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
58
+
59
+ # Display the result
60
+ st.image(result_image_rgb, channels="RGB", caption="Detected Pipes in Rack")
61
+ st.write(f"Total number of pipes detected: {pipe_count}")