YOLO v2实现图像目标检测

人工智能28

作者介绍

熊文博,男,西安工程大学电子信息学院,2020级硕士研究生,张宏伟人工智能课题组。

研究方向:机器视觉与人工智能

电子邮件:996270714@qq.com

师兄的CSDN主页:
欢迎关注和相互交流学习.

1.算法简介

现在用YOLO v2加载训练好的 COCO数据集权重模型进行图片目标预测,有关细节部分就不赘述了。
这里放上YOLO v2的论文地址:
YOLO v2论文地址:https://arxiv.org/pdf/1612.08242.pdf

2.环境配置

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

清华镜像安装TensorFlow:(这里强烈建议大家安装CPU版本的TensorFlow,GPU版本需要提前配置好cuda和cudnn,CPU版本的用于图片目标检测足够了)
安装CPU版本:

pip install tensorflow==1.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装GPU版本的TensorFlow:

pip install tensorflow-gpu==1.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

3.程序实现

这里给出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"

下面是完整的 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()

运行该程序,就可以检测到图片的目标。改变图像路径,可以针对不同的图片进行目标检测。

[En]

Run the program, you can detect the target of the picture. Change the image path, you can carry out target detection for different pictures.

运行结果

下面展示一些图片的运行结果

YOLO v2实现图像目标检测YOLO v2实现图像目标检测

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

相关文章
CMOS图像传感器的参数和评价标准 人工智能

CMOS图像传感器的参数和评价标准

作者:Aili-Light | 公众号:艾利光科技 图1 图像传感器CMOS 摄像头的种类和应用场景非常之多,例如手机摄像头、安防摄像头、车载摄像头、工业相机等等。针对每种应用,摄像头的CMOS(图像...
tensorflow-gpu 2.4 import multi_gpu_model 报错 人工智能

tensorflow-gpu 2.4 import multi_gpu_model 报错

总论(这是我的总结可以不看直接看下面的安装) 1、 不用看那些版本对应表啊!!!! 记录那些年自己掉过的坑,自己也是看了其他人好多博客,总结下来一个核心问题是要求版本对应,发现大家都是这样教的,以这样...
音频处理-2 WAV格式 人工智能

音频处理-2 WAV格式

后续要将流量中的音频数据转为WAV格式文件,所以本节重点说下WAV格式。 WAV文件是在PC机平台上很常见的、最经典的多媒体音频文件,最早于1991年8月出现在Windows 3.1操作系统上,文件扩...
《古诗词里的快意人生》读后感 人工智能

《古诗词里的快意人生》读后感

《古诗词里的快意人生》通过讲解诗人的一生让我了解每一首诗的来源背景。一生都在追求极致潇洒的天才诗人李白;一生遭遇很不幸,却心怀天下做狂歌的杜甫。为了功名来到边塞,从悔恨到渐渐爱上边塞的岑参。追求"不平...