sombruxo

yolo

Oct 21st, 2026
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | Source Code | 0 0
  1. import numpy as np
  2. import cv2 as cv
  3. from ultralytics import YOLO
  4.  
  5. cap = cv.VideoCapture("/mnt/c/Users/Bemposta/Videos/madrid.mp4")
  6. if not cap.isOpened():
  7.     print("Cannot open camera")
  8.     exit()
  9.  
  10. model = YOLO("yolo26s.pt")  # pretrained YOLO26n model
  11.  
  12. while True:
  13.     # Capture frame-by-frame
  14.     ret, frame = cap.read()
  15.  
  16.     # if frame is read correctly ret is True
  17.     if not ret:
  18.         print("Can't receive frame (stream end?). Exiting ...")
  19.         break
  20.        
  21.     # Our operations on the frame come here
  22.     #imgrgb = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
  23.    
  24.     # Run batched inference on a list of images
  25.     results = model.predict(source=frame, iou=0.5, verbose=False, imgsz=[736, 1280])  # return a list of Results objects
  26.     #print(model.names)
  27.     #model.info()  # Prints model summary and returns tuple
  28.     #model.info(detailed=True)  # Prints detailed info and returns tuple
  29.     img_result = results[0].plot(
  30.         line_width=1,       # Grosor de las cajas
  31.         font_size=10,       # TamaƱo del texto
  32.         labels=True,        # Mostrar etiquetas
  33.         conf=True,          # Mostrar confianza
  34.         boxes=True,         # Dibujar cajas
  35.     )
  36.     print("results[0].to_json():\n", results[0].to_json(normalize=True, decimals=5))
  37.     # Display the resulting frame
  38.     #img_result = cv.cvtColor(img_result, cv.COLOR_RGB2BGR)
  39.     cv.imshow('frame', img_result)
  40.     if cv.waitKey(10) == ord('q'):
  41.        break
  42.  
  43. # When everything done, release the capture
  44. cap.release()
  45. cv.destroyAllWindows()
  46.  
  47.  
  48.  
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment