import cv2 import numpy as np # Load YOLOv3 model net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") # Load class names classes = [] with open("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()] # Set input image size input_size = (416, 416) # Load input image image = cv2.imread("input.jpg") # Resize image to input size resized_image = cv2.resize(image, input_size) # Normalize image normalized_image = resized_image / 255.0 # Convert image to blob blob = cv2.dnn.blobFromImage(normalized_image, 1/255.0, input_size, (0,0,0), swapRB=True, crop=False) # Set input blob to the network net.setInput(blob) # Run forward pass to get output of the network output_layers_names = net.getUnconnectedOutLayersNames() outputs = net.forward(output_layers_names) # Extract bounding boxes, class ids and confidence scores boxes = [] confidences = [] class_ids = [] for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * input_size[0]) center_y = int(detection[1] * input_size[1]) width = int(detection[2*續(xù)* * input_size[0]) height = int(detection[3] * input_size[1]) left = int(center_x - width / 2) top = int(center_y - height / 2) boxes.append([left, top, width, height]) confidences.append(float(confidence)) class_ids.append(class_id) # Apply non-maximum suppression to remove overlapping boxes indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) # Draw bounding boxes and labels on the image for i in indices: i = i[0] box = boxes[i] left = box[0] top = box[1] width = box[2] height = box[3] label = classes[class_ids[i]] confidence = confidences[i] color = (0, 255, 0) cv2.rectangle(image, (left, top), (left + width, top + height), color, 2) text = f"{label}: {confidence:.2f}" cv2.putText(image, text, (left, top - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # Show the result cv2.imshow("YOLOv3", image) cv2.waitKey(0) cv2.destroyAllWindows()在上述代碼中,首先使用OpenCV的dnn模塊加載預(yù)訓(xùn)練的YOLOv3模型。然后,讀取類別名稱列表和輸入圖像,并將圖像調(diào)整為網(wǎng)絡(luò)的輸入大小。接下來,將圖像歸一化并轉(zhuǎn)換為blob格式,作為網(wǎng)絡(luò)的輸入。運(yùn)行前向傳遞以獲取網(wǎng)絡(luò)的輸出,并從輸出中提取邊界框、類別ID和置信度分?jǐn)?shù)。使用非最大抑制(Non-Maximum Suppression,NMS)算法去除重疊的邊界框,并在圖像上繪制檢測(cè)結(jié)果。 ## 結(jié)論 本文介紹了YOLO的基本原理和Python編程技術(shù),以及如何使用YOLO進(jìn)行目標(biāo)檢測(cè)。需要注意的是,在使用YOLO進(jìn)行目標(biāo)檢測(cè)時(shí),需要考慮許多參數(shù)和選項(xiàng),例如輸入圖像大小、置信度閾值、非最大抑制的參數(shù)等。通過調(diào)整這些參數(shù),可以獲得更好的檢測(cè)結(jié)果。此外,YOLO還可以進(jìn)行實(shí)時(shí)目標(biāo)檢測(cè)和視頻目標(biāo)檢測(cè),這些應(yīng)用也值得進(jìn)一步研究和探索。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/130683.html
摘要:摘要本文介紹使用和完成視頻流目標(biāo)檢測(cè),代碼解釋詳細(xì),附源碼,上手快。將應(yīng)用于視頻流對(duì)象檢測(cè)首先打開文件并插入以下代碼同樣,首先從導(dǎo)入相關(guān)數(shù)據(jù)包和命令行參數(shù)開始。 摘要:?本文介紹使用opencv和yolo完成視頻流目標(biāo)檢測(cè),代碼解釋詳細(xì),附源碼,上手快。 在上一節(jié)內(nèi)容中,介紹了如何將YOLO應(yīng)用于圖像目標(biāo)檢測(cè)中,那么在學(xué)會(huì)檢測(cè)單張圖像后,我們也可以利用YOLO算法實(shí)現(xiàn)視頻流中的目標(biāo)檢...
摘要:近幾年來,目標(biāo)檢測(cè)算法取得了很大的突破。本文主要講述算法的原理,特別是算法的訓(xùn)練與預(yù)測(cè)中詳細(xì)細(xì)節(jié),最后將給出如何使用實(shí)現(xiàn)算法。但是結(jié)合卷積運(yùn)算的特點(diǎn),我們可以使用實(shí)現(xiàn)更高效的滑動(dòng)窗口方法。這其實(shí)是算法的思路。下面將詳細(xì)介紹算法的設(shè)計(jì)理念。 1、前言當(dāng)我們談起計(jì)算機(jī)視覺時(shí),首先想到的就是圖像分類,沒錯(cuò),圖像分類是計(jì)算機(jī)視覺最基本的任務(wù)之一,但是在圖像分類的基礎(chǔ)上,還有更復(fù)雜和有意思的任務(wù),如目...
摘要:工作原理以前的檢測(cè)系統(tǒng)通過重復(fù)利用分類器和定位器來實(shí)現(xiàn)目標(biāo)識(shí)別。修改檢測(cè)閾值缺省情況下,只顯示信心大于的對(duì)象。用法如下這個(gè),呵呵,不完美把白馬識(shí)別成綿羊了,把黑狗識(shí)別成奶牛了,但確實(shí)很快。 原標(biāo)題:YOLO: Real-Time Object Detection英文原文:https://pjreddie.com/darknet/... 強(qiáng)烈推薦(TED視頻):https://www....
摘要:工作原理以前的檢測(cè)系統(tǒng)通過重復(fù)利用分類器和定位器來實(shí)現(xiàn)目標(biāo)識(shí)別。修改檢測(cè)閾值缺省情況下,只顯示信心大于的對(duì)象。用法如下這個(gè),呵呵,不完美把白馬識(shí)別成綿羊了,把黑狗識(shí)別成奶牛了,但確實(shí)很快。 原標(biāo)題:YOLO: Real-Time Object Detection英文原文:https://pjreddie.com/darknet/... 強(qiáng)烈推薦(TED視頻):https://www....
閱讀 1158·2023-04-25 17:51
閱讀 2938·2021-11-23 09:51
閱讀 1556·2021-11-08 13:21
閱讀 2594·2021-09-22 15:14
閱讀 1584·2019-08-30 12:48
閱讀 1145·2019-08-29 12:44
閱讀 1198·2019-08-26 12:21
閱讀 1454·2019-08-26 10:47