Yolov5 VisionArtificial

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import torch

import cv2
import numpy as np

#### -----------------------------------------------------

model = torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True);

#### -----------------------------------------------------

def draw_boxes(image, boxes):


image_h, _, _ = image.shape

for box in boxes.xyxy[0]:


xmin = int(box[0])
ymin = int(box[1])
xmax = int(box[2])
ymax = int(box[3])
pred = "{:.2f}".format(box[4])

cv2.rectangle(image, (xmin,ymin), (xmax,ymax), (0,255,0), 3)


cv2.putText(image,
boxes.names[int(box[5])] + ' ' + pred,
(xmin, ymin - 13),
cv2.FONT_HERSHEY_SIMPLEX,
1e-3 * image_h,
(0,255,0), 2)

return image

#### -----------------------------------------------------

camera = cv2.VideoCapture(0)

while True:
_, image = camera.read()

boxes = model(image)
image = draw_boxes(image, boxes)

cv2.imshow('Deteccion', image)

key = cv2.waitKey(1) & 0xFF


if key == ord('q'):
break

cv2.destroyAllWindows()
camera.release()

You might also like