日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 目标检测 >内容正文

目标检测

opencv+yolov3实现目标检测

發布時間:2024/9/30 目标检测 77 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv+yolov3实现目标检测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果圖

代碼/font>
語言:python

import cv2 as cv import argparse import numpy as np# Initialize the parameters confThreshold = 0.25 # Confidence threshold nmsThreshold = 0.4 # Non-maximum suppression threshold inpWidth = 320 # Width of network's input image inpHeight = 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 classes classesFile = "voc.names" classes = None with 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 layers def 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 box def 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 suppression def 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.’)

所有代碼見個人資源:
https://download.csdn.net/download/KOBEYU652453/13082584


作者:電氣余登武

總結

以上是生活随笔為你收集整理的opencv+yolov3实现目标检测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。