YOLO Implementation

import cv2
import matplotlib.pyplot as plt

from utils import *
from darknet import Darknet
# Set the location and name of the cfg file
cfg_file = './cfg/yolov3.cfg'

# Set the location and name of the pre-trained weights file
weight_file = './weights/yolov3.weights'

# Set the location and name of the COCO object classes file
namesfile = 'data/coco.names'

# Load the network architecture
m = Darknet(cfg_file)

# Load the pre-trained weights
m.load_weights(weight_file)

# Load the COCO object classes
class_names = load_class_names(namesfile)
# Print the neural network used in YOLOv3
m.print_network()
Loading and Resizing Our Images

使用OpenCV的cv2.imread()函數加載我們的圖像。 因爲,此函數將圖像加載爲BGR,我們將圖像轉換爲RGB,以便我們可以使用正確的顏色顯示它們 網絡第一層的輸入大小爲416 x 416 x 3.由於圖像大小不同,我們必須調整圖像大小以與第一層的輸入大小兼容。 在下面的代碼中,我們使用OpenCV的cv2.resize()函數調整圖像大小。

# Set the default figure size
plt.rcParams['figure.figsize'] = [24.0, 14.0]

# Load the image
img = cv2.imread('./images/surf.jpg')

# Convert the image to RGB
original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# We resize the image to the input width and height of the first layer of the network.    
resized_image = cv2.resize(original_image, (m.width, m.height))

# Display the images
plt.subplot(121)
plt.title('Original Image')
plt.imshow(original_image)
plt.subplot(122)
plt.title('Resized Image')
plt.imshow(resized_image)
plt.show()`

一旦圖像已經被加載和調整大小,並且您已經爲nms_thresh和iou_thresh選擇了參數,我們就可以使用YOLO算法來檢測圖像中的對象。 我們使用utils模塊中的detect_objects(m,resized_image,iou_thresh,nms_thresh)函數檢測對象。 此函數接收Darknet返回的模型m、調整大小後的圖像以及NMS和IOU閾值,並返回找到的對象的邊界框。 每個邊界框包含7個參數:邊界框中心的座標(x,y),邊界框的寬度 w 和高度 h,置信度檢測級別,對象類概率和 對象類ID。 detect_objects()函數還打印出YOLO算法檢測圖像中對象和檢測到的對象數所花費的時間。 由於我們在CPU上運行算法,因此檢測圖像中的對象大約需要2秒鐘,但是,如果我們使用GPU,它將運行更快。 一旦我們得到YOLO找到的對象的邊界框,我們就可以打印找到的對象的類及其對應的對象類概率。 爲此,我們在utils模塊中使用print_objects()函數。

最後,我們使用plot_boxes()函數繪製YOLO在我們的圖像中找到的邊界框和相應的對象類標籤。 如果將plot_labels標誌設置爲False,您將顯示沒有標籤的邊界框。 如果你的nms_thresh太低,這樣可以更容易地查看邊界框。 plot_boxes()函數使用相同的顏色繪製同一對象類的邊界框。 但是,如果您希望所有邊界框都是相同的顏色,則可以使用color關鍵字來設置所需的顏色。 例如,如果您希望所有邊界框都是紅色,則可以使用: plot_boxes(original_image, boxes, class_names, plot_labels = True, color = (1,0,0))

# Set the default figure size
plt.rcParams['figure.figsize'] = [24.0, 14.0]

# Load the image
img = cv2.imread('./images/1.jpg')

# Convert the image to RGB
original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# We resize the image to the input width and height of the first layer of the network.    
resized_image = cv2.resize(original_image, (m.width, m.height))

# Set the IOU threshold. Default value is 0.4
iou_thresh = 0.4

# Set the NMS threshold. Default value is 0.6
nms_thresh = 0.6

# Detect objects in the image
boxes = detect_objects(m, resized_image, iou_thresh, nms_thresh)

# Print the objects found and the confidence level
print_objects(boxes, class_names)

#Plot the image with bounding boxes and corresponding object class labels
plot_boxes(original_image, boxes, class_names, plot_labels = True)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章