我在opencv和yolov3上遇到了一个小问题。我用CUDA编译了opencv。一切都很好,我在几个进程上运行推断,一切都在图形卡上运行。 我的电脑里有4张显卡。 如果我使用一个进程,那么选择脚本应该使用的图形卡是没有问题的
cv2.cuda.setDevice (0-3)
很好。但在一个依赖于多个进程的脚本中,我加入了图形卡选择,得到了一个错误
(-217: Gpu API call) initialization error in function 'ManagedPtr'
import cv2
import numpy as np
from datetime import datetime
import os
class YoloEval(object):
def __init__(self, requestedProbability):
print("LOADING YOLO")
cv2.cuda.setDevice(1)
self.net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
self.net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16)
self.layer_names = self.net.getLayerNames()
self.output_layers = [self.layer_names[i[0]-1] for i in self.net.getUnconnectedOutLayers()]
self.requestedProbability = requestedProbability
print("YOLO LOADED")
self.saveImg = True
def predict(self, imageToPredict, locationName, cameraId):
height, width, channels = imageToPredict.shape
blob = cv2.dnn.blobFromImage(imageToPredict, 1/255.0, (416,416), swapRB=True, crop=False)
self.net.setInput(blob)
outs = self.net.forward(self.output_layers)
class_ids = []
confidences = []
boxes = []
detected = False
for out_index, out in enumerate(outs):
for detection_index, detection in enumerate(out):
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > self.requestedProbability and class_id == 0:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
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, self.requestedProbability, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(1, 3))
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = "Osoba"
color = colors[class_ids[i]]
cv2.rectangle(imageToPredict, (x, y), (x + w, y + h), color, 2)
cv2.putText(imageToPredict, label, (x, y + 30), font, 2, color, 3)
detected = True
if detected:
return 1
else: return 0
目前没有回答
相关问题 更多 >
编程相关推荐