Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +62 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cvlib as cv
|
| 6 |
+
from cvlib.object_detection import draw_bbox
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from collections import Counter
|
| 10 |
+
|
| 11 |
+
# Streamlit app title
|
| 12 |
+
st.set_page_config(page_title="Object Detection App", page_icon="🖼️", layout="centered")
|
| 13 |
+
st.title("Object Detection with cvlib 🖼️")
|
| 14 |
+
|
| 15 |
+
# Custom header and instructions
|
| 16 |
+
st.markdown("""
|
| 17 |
+
### Detect Objects in Your Images
|
| 18 |
+
Upload an image to automatically detect common objects and get a count of each detected object.
|
| 19 |
+
The application will display the image with bounding boxes around the objects.
|
| 20 |
+
""")
|
| 21 |
+
|
| 22 |
+
# Upload image
|
| 23 |
+
st.markdown("### Step 1: Upload an Image for Object Detection")
|
| 24 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
| 25 |
+
|
| 26 |
+
if uploaded_file is not None:
|
| 27 |
+
# Convert image file to OpenCV format
|
| 28 |
+
image = np.array(Image.open(uploaded_file))
|
| 29 |
+
|
| 30 |
+
# Perform object detection
|
| 31 |
+
box, label, count = cv.detect_common_objects(image)
|
| 32 |
+
|
| 33 |
+
# Draw bounding boxes on the image
|
| 34 |
+
output_image = draw_bbox(image, box, label, count)
|
| 35 |
+
|
| 36 |
+
# Step 2: Display image with bounding boxes
|
| 37 |
+
st.markdown("### Step 2: Detected Objects in the Image")
|
| 38 |
+
st.image(output_image, channels="BGR", use_column_width=True)
|
| 39 |
+
|
| 40 |
+
# Step 3: Display the count of detected objects dynamically
|
| 41 |
+
st.markdown("### Step 3: Detected Objects Count")
|
| 42 |
+
|
| 43 |
+
# Count each label in the image using Counter
|
| 44 |
+
label_counts = Counter(label)
|
| 45 |
+
|
| 46 |
+
# Display counts in a well-formatted table
|
| 47 |
+
for obj, count in label_counts.items():
|
| 48 |
+
st.markdown(f"**{obj.capitalize()}s**: {count}")
|
| 49 |
+
|
| 50 |
+
st.markdown("""
|
| 51 |
+
---
|
| 52 |
+
### Tips:
|
| 53 |
+
- You can upload different images to see how the object detection model works.
|
| 54 |
+
- Supported formats: PNG, JPG, JPEG.
|
| 55 |
+
- The app uses cvlib to detect common objects in the images, such as people, cars, trucks, and more.
|
| 56 |
+
""")
|
| 57 |
+
|
| 58 |
+
# Display a footer
|
| 59 |
+
st.markdown("""
|
| 60 |
+
---
|
| 61 |
+
Made with ❤️ by [SenasuDemir](https://github.com/SenasuDemir).
|
| 62 |
+
""")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python-headless=
|
| 2 |
+
cvlib
|
| 3 |
+
streamlit
|
| 4 |
+
Pillow
|
| 5 |
+
collections
|