在这篇博文中,将针对具有 80 个标签的 coco 数据集逐行解释 Yolov3 预训练对象检测的代码说明。
我们可以从 yolo 官网获取 weights 文件和 cfg 文件:https://pjreddie.com/darknet/yolo/
image = cv2.imread('./testing images/crosswalk-featured.jpg')
#cv2.imshow('image',image)
#cv2.waitKey()
#cv2.destroyAllWindows()
original_with , original_height = image.shape[1] , image.shape[0]
Neural_Network = cv2.dnn.readNetFromDarknet('./Files/yolov3.cfg','./Files/yolov3.weights')
classes_names = []
k = open('./Files/class_names','r')
for i in k.readlines():
classes_names.append(i.strip())
#print(classes_names)
blob = cv2.dnn.blobFromImage(image , 1/255 , (320,320) , True , crop = False)
#print(blob.shape)
Neural_Network.setInput(blob)
cfg_data = Neural_Network.getLayerNames()
#print(cfg_data)
layer_names = Neural_Network.getUnconnectedOutLayers()
outputs = [cfg_data[i-1] for i in layer_names]
#print(outputs)
output_data = Neural_Network.forward(outputs)
prediction_box , bounding_box , confidence , class_labels = bounding_box_prediction(output_data)
final_prediction(prediction_box , bounding_box , confidence , class_labels , original_with / 320 , original_height / 320 )
第一行从
testing images
目录中读取图像文件crosswalk-featured.jpg
并将其作为数组存储在变量image
中。接下来的两行注释:使用 OpenCV 显示图像
下一行检索图像的尺寸(宽度和高度)并将它们分别存储在
original_with
和original_height
变量中。cv2.dnn.readNetFromDarknet('./Files/yolov3.cfg','./Files/yolov3.weights')
:从 Darknet 框架加载预训练的 YOLOv3 模型。这两个参数分别是配置文件和权重文件的路径。接下来的几行从文件中读取 C