YOLO v2实现图像目标检测

目录

1、作者介绍

熊文博,男,西安工程大学电子信息学院,2020级硕士研究生,张宏伟人工智能课题组。
研究方向:机器视觉与人工智能
电子邮件:996270714@qq.com
师兄的CSDN主页:欢迎关注和相互交流学习.

YOLO系列学习-主要熟悉各个模型的改进

2、算法简介

YOLO英文名字为Yolo Only Look Once,意为你只看一次,也就是说你只看一次,就可以把图像中的目标检测出来。

YOLO是一种目标检测的算法,其于2015年首次提出,目前最新的已经到YOLO v8了。

现在用YOLO v2加载训练好的 COCO数据集权重模型进行图片目标预测,有关细节部分就不赘述了。

这里放上YOLO v2的论文地址:
YOLO v2论文地址:https://arxiv.org/pdf/1612.08242.pdf

3、环境配置

首先需要配置好相关镜像,这里需要安装上OpenCV和TensorFlow这两个安装包。
清华镜像安装OpenCV:

pip install opencv-python==3.4.9.31 -i https://pypi.tuna.tsinghua.edu.cn/simple

豆瓣镜像安装TensorFlow:(这里强烈建议大家安装CPU版本的TensorFlow,GPU版本需要提前配置好cuda和cudnn(具体步骤见: Windows系统GPU版本PyTorch安装教程),CPU版本的用于图片目标检测足够了)

安装CPU版本:

pip install tensorflow==1.13.1 -i https://pypi.douban.com/simple

安装GPU版本的TensorFlow:

pip install tensorflow-gpu==1.13.1 -i https://pypi.douban.com/simple

4、代码实现

4.1 数据准备

这里给出YOLO v2算法用TensorFlow框架编写好的程序的百度网盘链接:
链接: https://pan.baidu.com/s/1ZDTfuDHh1jXgc271KDnrsw 提取码: rtbx

文件夹里面文件分布如下图所示:

YOLO v2实现图像目标检测
其中Main.py就是用于图像目标检测的程序,在该程序中,需要修改相应的读取图片路径、输出路径和模型路径。
30行修改读入图片的路径,这里要注意最好用图片的绝对路径
image_file = '1.jpg'

55行修改图片的输出保存路径

cv2.imwrite("detection_result.jpg", img_detection)

44行是读取模型权重文件的路径

model_path = "./yolo2_model/yolo2_coco.ckpt"

4.2 完整代码

下面是完整的 Main.py中的代码:


import numpy as np
import tensorflow as tf
import cv2,os
from PIL import Image
import matplotlib.pyplot as plt

from model_darknet19 import darknet
from decode import decode
from utils import preprocess_image, postprocess, draw_detection
from config import anchors, class_names

def main():
    input_size = (416,416)
    image_file = '1.jpg'
    image = cv2.imread(image_file)
    image_shape = image.shape[:2]

    image_cp = preprocess_image(image,input_size)

    tf_image = tf.placeholder(tf.float32,[1,input_size[0],input_size[1],3])
    model_output = darknet(tf_image)
    output_sizes = input_size[0]//32, input_size[1]//32
    output_decoded = decode(model_output=model_output,output_sizes=output_sizes,
                               num_class=len(class_names),anchors=anchors)

    model_path = "./yolo2_model/yolo2_coco.ckpt"
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess,model_path)
        bboxes,obj_probs,class_probs = sess.run(output_decoded,feed_dict={tf_image:image_cp})

    bboxes,scores,class_max_index = postprocess(bboxes,obj_probs,class_probs,image_shape=image_shape)

    img_detection = draw_detection(image, bboxes, scores, class_max_index, class_names)
    cv2.imwrite("detection_result.jpg", img_detection)
    img_detection = cv2.cvtColor(img_detection, cv2.COLOR_RGB2BGR)
    plt.figure(figsize=(10,10))
    plt.imshow(img_detection)

    print('YOLO_v2 检测完成!')

    plt.show()
if __name__ == '__main__':
    main()

4.3 运行结果

运行程序,就可以对图片进行目标检测了。更换图片路径,可以对不同的图片进行目标检测了。
下面展示一些图片的运行结果:

YOLO v2实现图像目标检测

; 常见问题总结

1、 问题1:AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’

YOLO v2实现图像目标检测
报错原因:加载的虚拟环境中Tensorflow版本太高,Tensorflow 1.x 版本提供placeholder,而Tensorflow 2.x 版本暂时没有这个模块。
解决办法:降低Tensorflow版本。

注意:Python解释器版本太高也无法安装成功,建议安装3.7.x版本解释器。

pip install tensorflow==1.13.1 -i https://pypi.douban.com/simple

2、 问题2:TypeError: Descriptors cannot not be created directly. If this call came from a pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible work
YOLO v2实现图像目标检测
_报错原因
:这是由于我们使用pip安装tensorflow的时候,安装依赖包protobuf的版本过高造成的。

解决办法:只需要卸掉原来的,按照编译器提示的信息下载3.19.0对应的版本即可解决问题。


pip uninstall protobuf

pip install protobuf==3.19.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

3、 问题3:使用pip指令进行安装时,若出现以下提示信息,则只需对pip进行更新即可。

YOLO v2实现图像目标检测

pip升级指令

python -m pip install --upgrade pip

Original: https://blog.csdn.net/m0_37758063/article/details/117824722
Author: ZHW_AI课题组
Title: YOLO v2实现图像目标检测

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/629618/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球