前言
基于YOLO进行物体检测、对象识别,在搭建好开发环境后,先和大家进行实践应用中,体验YOLOv3物体/目标检测效果和魅力;同时逐步了解YOLOv3的不足和优化思路。
开发环境参数
系统:Windows 编程语言:Python 3.8
深度学习框架:TensorFlow 2.3 整合开发环境:Anaconda 开发代码IDE:PyCharm
主要使用TensorFlow2.3、opencv-python4.4.0、Pillow、matplotlib 等依赖库。
详情请参考我的另一篇博客:YOLO实践应用之搭建开发环境(Windows系统、Python 3.8、TensorFlow2.3版本)
YOLOv3的物体/目标检测效果:
1)有四只小猫被检测出来:
使用浅蓝色的框框,把小猫的所在位置框出来,并在框框上方注释标签(类别 置信度)。比如第一只小猫检测出的标签是cat ,置信度是0.95,即有95%的把握认为是cat 猫。
2)一只小狗和一只小猫同时被检测出来:
小猫被检测出是cat,1.00;有100%的把握认为是cat 猫;
小狗被检测出是dog,0.97;有97%的把握认为是cat 猫;
3)在复杂的十字路口,有许多行人和车辆被检测出来了:
大家可以看到大部分的行人、小汽车和公交车是被检测出来了,存在小部分没有被检测出来;如果是做特定场景的目标检测,建议大家后续采购特定场景的数据,重新训练网络,生成稳定且高精度的模型,保存权重文件,便于后续使用。
目录
体验YOLOv3物体/目标检测
1)下载代码,打开工程
先到githug下载代码,然后解压工程,然后使用PyCharm工具打开工程;
githug代码下载地址:https://github.com/guo-pu/yolov3-tf2
说明:此仓库代码源于zzh8829/yolov3-tf2 进行修改的,zzh8829/yolov3-tf2代码仓库地址 :https://github.com/zzh8829/yolov3-tf2
使用PyCharm工具打开工程:
打开后的页面是这样的:
【选择开发环境】
文件(file)——>设置(setting)——>项目(Project)——>Project Interpreters 选择搭建的开发环境;
然后先点击Apply,等待加载完成,再点击OK;
2)下载权重文件
方式1:使用wget 来下载
前提:需要支持wget命令;
yolov3.weights、yolov3-tiny.weights都是预先训练好的Darknet网络权重;
yolov3.weights 是默认的权重,支持识别目标的类别更多更精准;
yolov3-tiny.weights 是应用在轻量级设备的权重,对设备的性能要求没这么高,相对yolov3.weights响应速度更快;
进入windows管理员命令窗口:
【下载yolov3.weights权重文件】
进入存放数据的目录,比如e盘的data目录,然后执行如下命令进行下载权重值:
wget https://pjreddie.com/media/files/yolov3.weights -O .\yolov3.weights
然后就会开始下载了;
【下载yolov3-tiny.weights权重文件】
wget https://pjreddie.com/media/files/yolov3.weights -O .\yolov3-tiny.weights
下载好后,来到存放的目录检测是否下载成功和完整;
方式2:带我网盘提取
链接: https://pan.baidu.com/s/1TK4EEWsCHPyunNkJ98Mhjw
提取码: urad
然后把数据复制到下载工程包中,yolov3-tf2-master\data
3)权重文件应用到工程
执行如下命令,把训练好的权重进行转换,并应用到工程中。
在Pycharm的命令终端进入YOLO3-GPU-TensorFlow2开发环境:
conda activate YOLO3-GPU-TensorFlow2
【yolov3.weights】
python convert.py --weights ./data/yolov3.weights --output ./checkpoints/yolov3.tf
执行命令成功后,能看到在checkpoints目录下有三个新增文件
【yolov3-tiny.weights】(可选)
python convert.py --weights ./data/yolov3-tiny.weights --output ./checkpoints/yolov3-tiny.tf --tiny
4)进行目标检测
检测图片中的目标:
python detect.py --image ./data/cat.jpg
有四只小猫被检测出来:使用浅蓝色的框框,把小猫的所在位置框出来,并在框框上方注释标签(类别 置信度)。比如第一只小猫检测出的标签是cat ,置信度是0.95,即有95%的把握认为是cat 猫。
我们可以指定目标检测后生成的图片:
python detect.py --image ./data/cat.jpg
一只小狗和一只小猫同时被检测出来:小猫被检测出是cat,1.00;有100%的把握认为是cat 猫;小狗被检测出是dog,0.97;有97%的把握认为是cat 猫;
我们还可以尝试使用摄像头实时目标检测,或对视频文件进行目标检测,详细参看如下:
目标检测执行命令汇总:
-
# yolov3 检测图片的对象
-
python detect.py --image ./data/cat.jpg
-
-
# yolov3-tiny
-
python detect.py --weights ./checkpoints/yolov3-tiny.tf --tiny --image ./data/street.jpg
-
-
# webcam 摄像头实时检测对象
-
python detect_video.py --video
0
-
-
# video file 检测视频文件的对象
-
python detect_video.py --video path_to_file.mp4 --weights ./checkpoints/yolov3-tiny.tf --tiny
-
-
# video file with output
-
python detect_video.py --video path_to_file.mp4 --output ./output.avi
调用模型的核心代码
detect.py 代码: # yolov3 检测图片的对象
-
import time
-
from absl
import app, flags, logging
-
from absl.flags
import FLAGS
-
import cv2
-
import numpy
as np
-
import tensorflow
as tf
-
from yolov3_tf2.models
import (
-
YoloV3, YoloV3Tiny
-
)
-
from yolov3_tf2.dataset
import transform_images, load_tfrecord_dataset
-
from yolov3_tf2.utils
import draw_outputs
-
-
flags.DEFINE_string(
'classes',
'./data/coco.names',
'path to classes file')
-
flags.DEFINE_string(
'weights',
'./checkpoints/yolov3.tf',
-
'path to weights file')
-
flags.DEFINE_boolean(
'tiny',
False,
'yolov3 or yolov3-tiny')
-
flags.DEFINE_integer(
'size',
416,
'resize images to')
-
flags.DEFINE_string(
'image',
'./data/girl.png',
'path to input image')
-
flags.DEFINE_string(
'tfrecord',
None,
'tfrecord instead of image')
-
flags.DEFINE_string(
'output',
'./output.jpg',
'path to output image')
-
flags.DEFINE_integer(
'num_classes',
80,
'number of classes in the model')
-
-
-
def main(_argv):
-
physical_devices = tf.config.experimental.list_physical_devices(
'GPU')
-
for physical_device
in physical_devices:
-
tf.config.experimental.set_memory_growth(physical_device,
True)
-
-
if FLAGS.tiny:
-
yolo = YoloV3Tiny(classes=FLAGS.num_classes)
-
else:
-
yolo = YoloV3(classes=FLAGS.num_classes)
-
-
yolo.load_weights(FLAGS.weights).expect_partial()
-
logging.info(
'weights loaded')
-
-
class_names = [c.strip()
for c
in open(FLAGS.classes).readlines()]
-
logging.info(
'classes loaded')
-
-
if FLAGS.tfrecord:
-
dataset = load_tfrecord_dataset(
-
FLAGS.tfrecord, FLAGS.classes, FLAGS.size)
-
dataset = dataset.shuffle(
512)
-
img_raw, _label = next(iter(dataset.take(
1)))
-
else:
-
img_raw = tf.image.decode_image(
-
open(FLAGS.image,
'rb').read(), channels=
3)
-
-
img = tf.expand_dims(img_raw,
0)
-
img = transform_images(img, FLAGS.size)
-
-
t1 = time.time()
-
boxes, scores, classes, nums = yolo(img)
-
t2 = time.time()
-
logging.info(
'time: {}'.format(t2 - t1))
-
-
logging.info(
'detections:')
-
for i
in range(nums[
0]):
-
logging.info(
'\t{}, {}, {}'.format(class_names[int(classes[
0][i])],
-
np.array(scores[
0][i]),
-
np.array(boxes[
0][i])))
-
-
img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR)
-
img = draw_outputs(img, (boxes, scores, classes, nums), class_names)
-
cv2.imwrite(FLAGS.output, img)
-
logging.info(
'output saved to: {}'.format(FLAGS.output))
-
-
-
if __name__ ==
'__main__':
-
try:
-
app.run(main)
-
except SystemExit:
-
pass
希望对你有帮助。
转载:https://blog.csdn.net/qq_41204464/article/details/108837246