前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >YOLO Implementation

YOLO Implementation

作者头像
小飞侠xp
发布2018-10-10 11:37:30
6030
发布2018-10-10 11:37:30
举报
代码语言:javascript
复制
import cv2
import matplotlib.pyplot as plt

from utils import *
from darknet import Darknet
代码语言:javascript
复制
# 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)
代码语言:javascript
复制
# 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()函数调整图像大小。

代码语言:javascript
复制
# 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))

代码语言:javascript
复制
# 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)
本文参与?腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.09.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Loading and Resizing Our Images
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com