Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return image, proper_circles
|
| 36 |
-
|
| 37 |
-
# Streamlit App
|
| 38 |
def main():
|
| 39 |
-
st.title("
|
| 40 |
-
st.write("Upload an image to detect and count
|
|
|
|
|
|
|
| 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 |
-
#
|
| 49 |
-
|
| 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
|
| 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()
|