Rashmitha1234/objectdetection
0
1import os2os.environ["XDG_CONFIG_HOME"] = "/tmp" # Fix permission error on Hugging Face3 4import streamlit as st5import cv26from ultralytics import YOLO7import numpy as np8 9# Set Streamlit page config10st.set_page_config(page_title="YOLOv8 Object Detection", layout="centered")11 12# Title and description13st.title("🎯 YOLOv8 Object Detection with Streamlit")14st.markdown("Upload an image to detect common objects like person, phone, bottle, etc.")15 16# Load YOLOv8 model17model = YOLO("yolov8m.pt") # Auto-downloads from Ultralytics18 19# Image input (upload only)20image_input = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])21 22if image_input:23 file_bytes = np.asarray(bytearray(image_input.read()), dtype=np.uint8)24 image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)25 26 # Run detection27 results = model(image)[0]28 29 # Draw results30 for box in results.boxes:31 cls_id = int(box.cls[0])32 conf = float(box.conf[0])33 label = model.names[cls_id]34 x1, y1, x2, y2 = map(int, box.xyxy[0])35 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)36 cv2.putText(image, f"{label} {conf:.2f}", (x1, y1 - 10),37 cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)38 39 # Show the result40 st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="🧠 Detection Result", use_column_width=True)