import numpy as np import cvlib as cv from cvlib.object_detection import draw_bbox import streamlit as st from PIL import Image from collections import Counter import os import requests # Streamlit app title st.set_page_config(page_title="Object Detection App", page_icon="🖼️", layout="centered") st.title("Object Detection with cvlib 🖼️") # Custom header and instructions st.markdown(""" ### Detect Objects in Your Images Upload an image to automatically detect common objects and get a count of each detected object. The application will display the image with bounding boxes around the objects. """) # Upload image st.markdown("### Step 1: Upload an Image for Object Detection") uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"]) # Function to download a file without using the progress bar def download_file_no_progress(url, file_name, dest_dir): file_path = os.path.join(dest_dir, file_name) if not os.path.exists(file_path): print(f"Downloading {file_name}...") # Disable the progress bar by using requests response = requests.get(url, stream=True) with open(file_path, 'wb') as f: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk) return file_path # Function to perform object detection with manual model file paths def detect_common_objects_skip_progress(image, model='yolov3', skip_progress=False): if skip_progress: # Manually set paths for config and weights if progress bar should be disabled model_config = 'path_to_config.cfg' # Provide the path to the .cfg file model_weights = 'path_to_weights.weights' # Provide the path to the .weights file else: # Download the config and weights files if not already present model_config = download_file_no_progress( url="https://github.com/opencv/opencv_contrib/blob/master/modules/dnn/data/yolov3.cfg", file_name="yolov3.cfg", dest_dir="." ) model_weights = download_file_no_progress( url="https://github.com/opencv/opencv_contrib/blob/master/modules/dnn/data/yolov3.weights", file_name="yolov3.weights", dest_dir="." ) # Run the object detection box, label, count = cv.detect_common_objects(image, model=model) return box, label, count if uploaded_file is not None: # Convert image file to OpenCV format image = np.array(Image.open(uploaded_file)) # Perform object detection with progress bar disabled box, label, count = detect_common_objects_skip_progress(image, skip_progress=True) # Draw bounding boxes on the image output_image = draw_bbox(image, box, label, count) # Step 2: Display image with bounding boxes st.markdown("### Step 2: Detected Objects in the Image") st.image(output_image, channels="BGR", use_column_width=True) # Step 3: Display the count of detected objects dynamically st.markdown("### Step 3: Detected Objects Count") # Count each label in the image using Counter label_counts = Counter(label) # Display counts in a well-formatted table for obj, count in label_counts.items(): st.markdown(f"**{obj.capitalize()}s**: {count}") st.markdown(""" --- ### Tips: - You can upload different images to see how the object detection model works. - Supported formats: PNG, JPG, JPEG. - The app uses cvlib to detect common objects in the images, such as people, cars, trucks, and more. """) # Display a footer st.markdown(""" --- Made with ❤️ by [SenasuDemir](https://github.com/SenasuDemir). """)