Hi, I am trying to make an example of object detection using opencv an yolo, my opencv is cuda compatible as shown below, but I am getting 23 fps at most, when using jtop we can to see the usage gpu gets unstable:
Here is the code:
import cv2
import numpy as np
import time
class fps_counter:
t0 =0
t1 =0
fps =0
def __init__(self):
self.t0 = time.time()
self.t1 = self.t0
def update(self):
self.t1 = time.time()
self.fps = 1/(self.t1-self.t0)
self.t0 = self.t1
def get_fps(self):
return self.fps
# Load Yolo
net = cv2.dnn.readNetFromDarknet("yolov4-tiny.cfg","yolov4-tiny.weights")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# Set CUDA as backend
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# Get output layers
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))
# Get camera and start fps counter
cam = cv2.VideoCapture('/dev/video0',cv2.CAP_V4L2)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
cam.set(cv2.CAP_PROP_FOURCC, fourcc);
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cam.set(cv2.CAP_PROP_FPS, 60)
fps = fps_counter()
while True:
# Loading image
fps.update()
loaded,img = cam.read()
if not loaded:
print('no video data')
break
img = cv2.resize(img,(416,416))
height, width, channels = img.shape
# Detecting objects
# 0.003921569 = 1/255
blob = cv2.dnn.blobFromImage(img, 0.003921569, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
fps.update()
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.1:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = colors[class_ids[i]]
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
cv2.putText(img, label, (x, y + 30), font, 3, color, 3)
cv2.putText(img, "FPS: %.2f"%fps.get_fps(), (10, 50),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
cv2.imshow("Yolo GPU", img)
if cv2.waitKey(1) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
Do I need to do any additional configuration or modify the code to improve the use of the gpu?
If it helps, here is the use of cpu: