objectdetect / app.py
meraj12's picture
Update app.py
67adfcb verified
import streamlit as st
from PIL import Image
from ultralytics import YOLO
import numpy as np
import cv2
# --- Load YOLOv8 Model ---
model = YOLO('yolov8n.pt') # Nano model, fast and small
# --- Streamlit UI ---
st.title("πŸ“Έ Object Counter App (YOLOv8)")
uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
image = Image.open(uploaded_file).convert('RGB')
st.image(image, caption="Uploaded Image", use_column_width=True)
st.write("πŸ”Ž Detecting objects...")
# Convert PIL image to NumPy array
img_array = np.array(image)
# Inference
results = model.predict(img_array)
# Get number of detected objects
num_objects = len(results[0].boxes)
st.success(f"βœ… Total objects detected: {num_objects}")
# List detected object classes
class_names = [model.names[int(cls)] for cls in results[0].boxes.cls]
st.write(f"Detected Items: {class_names}")
# Draw boxes on image
result_img = results[0].plot()
st.image(result_img, caption='Detected Objects', use_column_width=True)
st.write("Meraj Graphics ❀️ ")