File size: 2,653 Bytes
010b8bf
 
 
 
 
 
ef5521f
010b8bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35c5617
 
 
 
ef5521f
35c5617
 
 
 
ef5521f
 
010b8bf
 
 
 
35c5617
 
010b8bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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

# 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 skip download and use local files for the model
def detect_common_objects_skip_progress(image, model='yolov3'):
    model_config = 'yolov3.cfg'  # Path to your downloaded .cfg file
    model_weights = 'yolov3.weights'  # Path to your downloaded .weights file
    
    # Perform object detection using the provided model files
    box, label, count = cv.detect_common_objects(image, model=model, 
                                                 model_config=model_config, 
                                                 model_weights=model_weights)
    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 manually provided paths
    box, label, count = detect_common_objects_skip_progress(image)
    
    # 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).
""")