from ultralytics import YOLO
mannequin = YOLO("yolov8n.pt")
We load a pre-trained YOLOv8 mannequin. Right here, we use the YOLOv8 nano mannequin (yolov8n.pt
), which is light-weight and optimized for quick, real-time detection.
When a picture is uploaded, it’s processed as a NumPy array, and the YOLO mannequin is utilized to detect objects:
uploaded_file = st.file_uploader("Select a picture...", sort=["jpg", "jpeg", "png"])
if uploaded_file is just not None:
picture = np.array(Picture.open(uploaded_file))
outcomes = mannequin(picture)
annotated_image = outcomes[0].plot()
st.picture(annotated_image, caption="Processed Picture with Detections")
This a part of the code captures the uploaded picture, applies object detection, and returns the annotated picture.
For video information, the app processes every body individually and runs object detection on every body:
cap = cv2.VideoCapture(tfile.title)
whereas cap.isOpened():
ret, body = cap.learn()
if not ret:
break
outcomes = mannequin(body)
annotated_frame = outcomes[0].plot()
stframe.picture(annotated_frame, channels="BGR")
The frames are learn utilizing OpenCV’s VideoCapture
, handed by way of the YOLO mannequin, and the outcomes are displayed in real-time.
For real-time detection by way of the webcam:
cap = cv2.VideoCapture(0)
whereas True:
ret, body = cap.learn()
if not ret:
break
outcomes = mannequin(body)
annotated_frame = outcomes[0].plot()
stframe.picture(annotated_frame, channels="BGR")
The webcam feed is opened, and the YOLO mannequin detects objects in every body. The outcomes are displayed stay.