作者介紹
熊文博,男,西安工程大學電子資訊學院,2020級碩士研究生,張宏偉人工智能課題組,
研究方向:機器視覺與人工智能
電子郵件:996270714@qq.com
1.演算法簡介
YOLO英文名字為Yolo Only Look Once,意為你只看一次,也就是說你只看一次,就可以把影像中的目標檢測出來,
YOLO是一種目標檢測的演算法,其于2015年首次提出,目前最新的已經到YOLO v5了,
現在用YOLO v2加載訓練好的COCO資料集權重模型進行圖片目標預測,有關細節部分就不贅述了,
這里放上YOLO v2的論文地址:
YOLO v2論文地址:https://arxiv.org/pdf/1612.08242.pdf
2.環境配置
首先需要配置好相關鏡像,這里需要安裝上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,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中的代碼:
# %load Main.py
# --------------------------------------
# @Time : 2018/5/16$ 17:17$
# @Author : KOD Chen
# @Email : 821237536@qq.com
# @File : Main$.py
# Description :YOLO_v2主函式.
# --------------------------------------
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
#%matplotlib inline
def main():
input_size = (416,416)
image_file = '1.jpg' #讀取輸入圖片
image = cv2.imread(image_file)
image_shape = image.shape[:2] #只取wh,channel=3不取
# copy、resize416*416、歸一化、在第0維增加存放batchsize維度
image_cp = preprocess_image(image,input_size)
# 【1】輸入圖片進入darknet19網路得到特征圖,并進行解碼得到:xmin xmax表示的邊界框、置信度、類別概率
tf_image = tf.placeholder(tf.float32,[1,input_size[0],input_size[1],3])
model_output = darknet(tf_image) # darknet19網路輸出的特征圖
output_sizes = input_size[0]//32, input_size[1]//32 # 特征圖尺寸是圖片下采樣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})
# 【2】篩選解碼后的回歸邊界框——NMS(post process后期處理)
bboxes,scores,class_max_index = postprocess(bboxes,obj_probs,class_probs,image_shape=image_shape)
# 【3】繪制篩選后的邊界框
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 detection has done!')
print('YOLO_v2 檢測完成!')
#cv2.imshow("detection_results", img_detection)
#cv2.waitKey(0)
plt.show()
if __name__ == '__main__':
main()
運行程式,就可以對圖片進行目標檢測了,更換圖片路徑,可以對不同的圖片進行目標檢測了,
運行結果
下面展示一些圖片的運行結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/287832.html
標籤:其他
