0

So basically I am using YOLOv8 for object detection. I have passed my RTSP URL of CCTV as my video path. So it takes the feed from the CCTV and detects objects in real time. Now what I want to do is create an imaginary line using OpenCV and detect objects only below that line. So the bounding boxes should come below the line only. All the objects that are above the line shouldn't be detected and filtered out

Below is the code

import cv2
import torch
import numpy as np
from ultralytics import YOLO

video_path ="rtsp://192.168.1.83/live/0/MAIN"


cap = cv2.VideoCapture(video_path)

model = YOLO('yolov8n.pt')

x_line=600

while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

    if success:
        
        # Run YOLOv8 inference on the frame
        resized_frame = cv2.resize(frame, (1280, 720), interpolation=cv2.INTER_LINEAR)
        cv2.line(resized_frame, (0, x_line), (width, x_line), (255, 0, 0), 10)

        # Visualize the results on the frame
        results = model(resized_frame, conf=0.6,classes=0)
        
        annotated_frame = results[0].plot()

        # Display the annotated frame
        cv2.imshow("YOLOv8 Inference", annotated_frame)

        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    else:
        # Break the loop if the end of the video is reached
        break

cap.release()
cv2.destroyAllWindows()
Arnav86
  • 3
  • 3
  • Why don't you just crop the image to the region of interest? – Markus May 10 '23 at 06:50
  • Thats also a feasible option. but i want the whole view of camera Aswell. whole view should be there, but object should only be detected below the line. – Arnav86 May 10 '23 at 07:00
  • You could do the detection on the cropped part and display the results on the full image. – Markus May 10 '23 at 07:10
  • wait really? how? – Arnav86 May 10 '23 at 07:14
  • Helpful link if you want to work with the bounding box coordinates: https://stackoverflow.com/a/75332799/18667225 – Markus May 10 '23 at 07:15
  • Or you can use [vstack](https://numpy.org/doc/stable/reference/generated/numpy.vstack.html) to recombine the upper and lower part of the image after annotation. Not that flexible but less code. – Markus May 10 '23 at 07:21
  • Hey @Markus. I also want one more help – Arnav86 May 12 '23 at 07:41
  • Do you like to create a new question and drop a link here? – Markus May 12 '23 at 12:27
  • here you go @Markus https://stackoverflow.com/questions/76236145/detect-objects-in-yellow-zone-and-red-zone-with-yolov8-and-opencv – Arnav86 May 12 '23 at 12:30

1 Answers1

1

Based on the discussion above you can simply filter the result set according to your region of interest:

import cv2
from ultralytics import YOLO
from ultralytics.yolo.utils.plotting import Annotator


model = YOLO('yolov8n.pt')
x_line = 100

img = cv2.imread('zidane.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = model.predict(img, conf=0.5, classes=0)
annotator = Annotator(img)

for r in results:
    for box in r.boxes:
        b = box.xyxy[0]
        if b[1] > x_line:
            c = box.cls
            annotator.box_label(b, f"{r.names[int(c)]} {float(box.conf):.2}")

img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.line(img, (0, x_line), (img.shape[1] - 1, x_line), (255, 0, 0), 2)

cv2.imshow("YOLO", img)
cv2.waitKey(0)

cv2.destroyAllWindows()

Result:

enter image description here

Markus
  • 5,976
  • 5
  • 6
  • 21