作者介绍
熊文博,男,西安工程大学电子信息学院,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
文件夹里面文件分布如下图所示:
其中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.
运行结果
下面展示一些图片的运行结果
Original: https://blog.csdn.net/m0_37758063/article/details/117824722
Author: ZHW_AI课题组
Title: YOLO v2实现图像目标检测

git bash运行CycleGAN-TensorFlow问题(个人笔记)

Ubuntu18.04/20.04复现算法RandLa-net 数据集S3DIS

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

【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection

tensorflow-gpu 2.4 import multi_gpu_model 报错

camera2 java 层框架分析和使用案例

音频处理-2 WAV格式

Metalama简介4.使用Fabric操作项目或命名空间

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

Mac M1芯片安装tensorflow和pytorch和spaCy

下载安装配置python、Anaconda,在Anaconda上配置tensorflow和keras,在pycharm上配置tensorflow

Unity调用微软SpeechLib.Dll的C#类实现语音合成功能

开源虚拟形象驱动测试(npm,Tensorflow.js)

超级详细易懂的GhostNet解析
