700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > opencv+yolov3实现目标检测

opencv+yolov3实现目标检测

时间:2021-03-21 15:50:29

相关推荐

opencv+yolov3实现目标检测

效果图

代码/font>

语言:python

import cv2 as cvimport argparseimport numpy as np# Initialize the parametersconfThreshold = 0.25 # Confidence thresholdnmsThreshold = 0.4 # Non-maximum suppression thresholdinpWidth = 320 # Width of network's input imageinpHeight = 320 # Height of network's input image# Give the configuration and weight files for the model and load the network using them.modelConfiguration = "Yolo-Fastest-voc/yolo-fastest-xl.cfg"modelWeights = "Yolo-Fastest-voc/yolo-fastest-xl.weights"# Load names of classesclassesFile = "voc.names"classes = Nonewith open(classesFile, 'rt') as f:classes = f.read().rstrip('\n').split('\n')colors = [np.random.randint(0, 255, size=3).tolist() for _ in range(len(classes))]# Get the names of the output layersdef getOutputsNames(net):# Get the names of all the layers in the networklayersNames = net.getLayerNames()# print(dir(net))# Get the names of the output layers, i.e. the layers with unconnected outputsreturn [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]# Draw the predicted bounding boxdef drawPred(classId, conf, left, top, right, bottom):# Draw a bounding box.cv.rectangle(frame, (left, top), (right, bottom), (0,0,255), thickness=4)label = '%.2f' % conf# Get the label for the class name and its confidenceif classes:assert (classId < len(classes))label = '%s:%s' % (classes[classId], label)# Display the label at the top of the bounding boxlabelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)top = max(top, labelSize[1])# cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine), (255,255,255), cv.FILLED)cv.putText(frame, label, (left, top-10), cv.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), thickness=2)# Remove the bounding boxes with low confidence using non-maxima suppressiondef postprocess(frame, outs):frameHeight = frame.shape[0]frameWidth = frame.shape[1]classIds = []confidences = []boxes = []# Scan through all the bounding boxes output from the network and keep only the# ones with high confidence scores. Assign the box's class label as the class with the highest score.classIds = []confidences = []boxes = []for out in outs:for detection in out:scores = detection[5:]classId = np.argmax(scores)confidence = scores[classId]if confidence > confThreshold:center_x = int(detection[0] * frameWidth)center_y = int(detection[1] * frameHeight)width = int(detection[2] * frameWidth)height = int(detection[3] * frameHeight)left = int(center_x - width / 2)top = int(center_y - height / 2)classIds.append(classId)confidences.append(float(confidence))boxes.append([left, top, width, height])# Perform non maximum suppression to eliminate redundant overlapping boxes with# lower confidences.indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)for i in indices:i = i[0]box = boxes[i]left = box[0]top = box[1]width = box[2]height = box[3]drawPred(classIds[i], confidences[i], left, top, left + width, top + height)if __name__=='__main__':parser = argparse.ArgumentParser(description='Object Detection using YOLO in OPENCV')parser.add_argument('--image', type=str, default='person.jpg', help='Path to image file.')args = parser.parse_args()net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)# Process inputsframe = cv.imread(args.image)# Create a 4D blob from a frame.blob = cv.dnn.blobFromImage(frame, 1/255.0, (inpWidth, inpHeight), [0, 0, 0], swapRB=False, crop=False)# Sets the input to the networknet.setInput(blob)# Runs the forward pass to get output of the output layersouts = net.forward(getOutputsNames(net))# Remove the bounding boxes with low confidencepostprocess(frame, outs)# Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)t, _ = net.getPerfProfile()label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))winName = 'Deep learning object detection in OpenCV'cv.namedWindow(winName,0)cv.imshow(winName, frame)cv.waitKey(0)cv.destroyAllWindows()

代码的中图 存放位置 是和资源代码一个文件夹。路径为相对路径

parser.add_argument(’–image’, type=str, default=‘person.jpg’, help=‘Path to image file.’)

所有代码见个人资源:

/download/KOBEYU652453/13082584

作者:电气余登武

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。