Surya152002 commited on
Commit
c8594a4
·
verified ·
1 Parent(s): 4a53c7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -43
app.py CHANGED
@@ -1,43 +1,43 @@
1
  import streamlit as st
2
- import cv2
3
- import numpy as np
4
  from PIL import Image
 
 
 
5
 
6
- def detect_proper_circles(image):
7
- # Convert to grayscale
8
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
-
10
- # Apply adaptive thresholding to detect edges clearly
11
- thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
12
- cv2.THRESH_BINARY_INV, 11, 2)
13
-
14
- # Find contours in the thresholded image
15
- contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
16
- proper_circles = 0
17
-
18
- for contour in contours:
19
- # Fit an enclosing circle
20
- center, radius = cv2.minEnclosingCircle(contour)
21
- center = (int(center[0]), int(center[1]))
22
- radius = int(radius)
23
-
24
- # Calculate circularity: Area of contour vs. area of enclosing circle
25
- area = cv2.contourArea(contour)
26
- enclosing_area = np.pi * (radius ** 2)
27
- circularity = area / enclosing_area if enclosing_area > 0 else 0
28
-
29
- # Set circularity and radius thresholds for proper circles
30
- if circularity > 0.75 and radius > 10: # 0.75 ensures near-perfect circles
31
- # Draw the detected circle
32
- cv2.circle(image, center, radius, (0, 255, 0), 2)
33
- proper_circles += 1
34
-
35
- return image, proper_circles
36
-
37
- # Streamlit App
38
  def main():
39
- st.title("Proper Circle Detector and Counter")
40
- st.write("Upload an image to detect and count proper circular shapes like pipes or log ends.")
 
 
41
 
42
  uploaded_file = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
43
  if uploaded_file is not None:
@@ -45,17 +45,13 @@ def main():
45
  image = Image.open(uploaded_file)
46
  st.image(image, caption="Uploaded Image", use_column_width=True)
47
 
48
- # Convert to OpenCV format
49
- image_cv = np.array(image)
50
- image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
51
-
52
- # Detect proper circles
53
- processed_image, circle_count = detect_proper_circles(image_cv)
54
 
55
  # Display results
56
  st.image(cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB),
57
  caption=f"Processed Image - Circles Counted: {circle_count}", use_column_width=True)
58
- st.success(f"Number of proper circles detected: {circle_count}")
59
 
60
  if __name__ == "__main__":
61
  main()
 
1
  import streamlit as st
 
 
2
  from PIL import Image
3
+ import torch
4
+ import numpy as np
5
+ import cv2
6
 
7
+ # Load the trained YOLOv5 model
8
+ @st.cache_resource
9
+ def load_model():
10
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
11
+ return model
12
+
13
+ # Circle detection function
14
+ def detect_circles(model, image):
15
+ # Convert PIL Image to OpenCV format
16
+ image_cv = np.array(image)
17
+ image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
18
+
19
+ # Run inference
20
+ results = model(image_cv)
21
+
22
+ # Extract detections and count
23
+ detections = results.pandas().xyxy[0]
24
+ circle_count = len(detections)
25
+
26
+ # Draw bounding boxes
27
+ for _, row in detections.iterrows():
28
+ x1, y1, x2, y2, conf = int(row['xmin']), int(row['ymin']), int(row['xmax']), int(row['ymax']), row['confidence']
29
+ cv2.rectangle(image_cv, (x1, y1), (x2, y2), (0, 255, 0), 2)
30
+ cv2.putText(image_cv, f"Circle {conf:.2f}", (x1, y1 - 10),
31
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
32
+
33
+ return image_cv, circle_count
34
+
35
+ # Streamlit app
 
 
 
36
  def main():
37
+ st.title("Circle Detector and Counter")
38
+ st.write("Upload an image to detect and count circular objects like logs or pipes.")
39
+
40
+ model = load_model() # Load YOLOv5 model
41
 
42
  uploaded_file = st.file_uploader("Upload Image", type=["jpg", "png", "jpeg"])
43
  if uploaded_file is not None:
 
45
  image = Image.open(uploaded_file)
46
  st.image(image, caption="Uploaded Image", use_column_width=True)
47
 
48
+ # Detect circles
49
+ processed_image, circle_count = detect_circles(model, image)
 
 
 
 
50
 
51
  # Display results
52
  st.image(cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB),
53
  caption=f"Processed Image - Circles Counted: {circle_count}", use_column_width=True)
54
+ st.success(f"Number of circles detected: {circle_count}")
55
 
56
  if __name__ == "__main__":
57
  main()