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://www.ezyhdfw.cn/yun/130683.html