detección de objetos en tiempo real

1. YOLOV5 detecta Youtube

import torch
import numpy as np
import cv2
import pafy
from time import time

class ObjectDetection:
    Class implements Yolo5 model to make inferences on a youtube video using Opencv2.

    def __init__(self, url, out_file="Labeled_Video.avi"):
        Initializes the class with youtube url and output file.
        :param url: Has to be as youtube URL,on which prediction is made.
        :param out_file: A valid output file name.
        self._URL = url
        self.model = self.load_model()
        self.classes = self.model.names
        self.out_file = out_file
        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'

    def get_video_from_url(self):
        Creates a new video streaming object to extract video frame by frame to make prediction on.
        :return: opencv2 video capture object, with lowest quality frame available for video.
        play =[-1]
        assert play is not None
        return cv2.VideoCapture(play.url)

    def load_model(self):
        Loads Yolo5 model from pytorch hub.
        :return: Trained Pytorch model.
        model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
        return model

    def score_frame(self, frame):
        Takes a single frame as input, and scores the frame using yolo5 model.
        :param frame: input frame in numpy/list/tuple format.
        :return: Labels and Coordinates of objects detected by model in the frame.
        frame = [frame]
        results = self.model(frame)
        labels, cord = results.xyxyn[0][:, -1].numpy(), results.xyxyn[0][:, :-1].numpy()
        return labels, cord

    def class_to_label(self, x):
        For a given label value, return corresponding string label.
        :param x: numeric label
        :return: corresponding string label
        return self.classes[int(x)]

    def plot_boxes(self, results, frame):

Supongo que te gusta