python
import cv2
# Load YOLOv3 weights and configuration files
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# Load object classes
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# Set input and output layers for the network
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# Load image
img = cv2.imread("image.jpg")
# Resize image to fit the network input size
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
# Convert image to blob format
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
# Set input for the network
net.setInput(blob)
# Run forward pass through the network
outs = net.forward(output_layers)
# Extract bounding boxes, confidence scores and class IDs
boxes = []
confidences = []
class_ids = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
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)
# Apply non-maximum suppression to remove overlapping boxes
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Draw bounding boxes and object labels on the image
font = cv2.FONT_HERSHEY_PLN
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = (0, 255, 0)
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
cv2.putText(img, label, (x, y - 5), font, 1, color, 2)
# Display the image
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
在這個(gè)代碼示例中,我們首先加載了YOLOv3的權(quán)重和配置文件。然后,我們加載了類別標(biāo)簽文件,并設(shè)置了網(wǎng)絡(luò)的輸入和輸出層。接下來,我們加載了要檢測(cè)的圖像,并將其縮放到適合網(wǎng)絡(luò)輸入大小的尺寸。然后,我們將圖像轉(zhuǎn)換為blob格式,并將其設(shè)置為網(wǎng)絡(luò)的輸入。接下來,我們運(yùn)行了網(wǎng)絡(luò)的前向傳遞,并從輸出中提取了邊界框、置信度分?jǐn)?shù)和類別ID。最后,我們應(yīng)用了非最大值抑制來消除重疊的邊界框,并在圖像上繪制了邊界框和對(duì)象標(biāo)簽。
總之,使用Python和OpenCV庫實(shí)現(xiàn)YOLOv3算法并不難。通過使用這個(gè)算法,我們可以快速準(zhǔn)確地檢測(cè)出多個(gè)對(duì)象,并在圖像或視頻中進(jìn)行分類和跟蹤。如果你對(duì)計(jì)算機(jī)視覺和目標(biāo)檢測(cè)感興趣,那么YOLOv3算法是一個(gè)值得學(xué)習(xí)的重要工具。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.hztianpu.com/yun/130913.html
摘要:近日,來自華盛頓大學(xué)的和提出的版本。而那些評(píng)分較高的區(qū)域就可以視為檢測(cè)結(jié)果。此外,相對(duì)于其它目標(biāo)檢測(cè)方法,我們使用了完全不同的方法。從圖中可以看出準(zhǔn)確率高,速度也快。對(duì)于的圖像,可以達(dá)到的檢測(cè)速度,獲得的性能,與的準(zhǔn)確率相當(dāng)?shù)撬俣瓤毂丁?近日,來自華盛頓大學(xué)的 Joseph Redmon 和 Ali Farhadi 提出 YOLO 的版本 YOLOv3。通過在 YOLO 中加入設(shè)計(jì)細(xì)節(jié)的變...
摘要:來自原作者,快如閃電,可稱目標(biāo)檢測(cè)之光。實(shí)現(xiàn)教程去年月就出現(xiàn)了,實(shí)現(xiàn)一直零零星星。這份實(shí)現(xiàn),支持用自己的數(shù)據(jù)訓(xùn)練模型。現(xiàn)在可以跑腳本了來自原作者拿自己的數(shù)據(jù)集訓(xùn)練快速訓(xùn)練這個(gè)就是給大家一個(gè)粗略的感受,感受的訓(xùn)練過程到底是怎樣的。 來自YOLOv3原作者YOLOv3,快如閃電,可稱目標(biāo)檢測(cè)之光。PyTorch實(shí)現(xiàn)教程去年4月就出現(xiàn)了,TensorFlow實(shí)現(xiàn)一直零零星星。現(xiàn)在,有位熱心公益的程...
閱讀 3277·2023-04-26 01:52
閱讀 3803·2021-09-04 16:40
閱讀 3858·2021-08-31 09:41
閱讀 2012·2021-08-09 13:41
閱讀 755·2019-08-30 15:54
閱讀 3173·2019-08-30 11:22
閱讀 1874·2019-08-30 10:52
閱讀 1158·2019-08-29 13:24