Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import yt_dlp
- import cv2
- from ultralytics import YOLO
- url = "https://www.youtube.com/watch?v=ApFoXMqbCxU"
- ydl_opts = {'quiet': True, 'format': 'best'}
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
- info = ydl.extract_info(url, download=False)
- stream_url = info['url']
- # Load a model
- model = YOLO("yolo11m.pt") # load an official model
- #model = YOLO("best.pt") # load an official model
- # Ahora intentar abrirla con OpenCV (puede o no funcionar)
- cap = cv2.VideoCapture(stream_url)
- while cap.isOpened():
- ret, frame = cap.read()
- if not ret:
- break
- # Predict with the model
- results = model(frame) # predict on an image
- # Access the results
- for result in results:
- boxes = result.boxes
- for box in boxes:
- x1, y1, x2, y2 = map(int, box.xyxy[0]) # Coordenadas del bounding box
- conf = box.conf[0].item() # Confianza
- cls = int(box.cls[0]) # Clase detectada
- # Dibujar rectángulo y texto en la imagen
- cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
- cv2.putText(frame, f"Clase {cls} ({conf:.2f})", (x1, y1 - 10),
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
- cv2.imshow("Imagen", frame)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement