基於谷歌開源的TensorFlow Object Detection API視頻物體識別系統實現教程

安裝Python
進入Python3.6.2下載頁,選擇 Files 中Windows平臺的Python安裝包,下載並安裝(本人安裝的是3.6.2版本的python,可根據實際情況下載不同版本的python)
安裝TensorFlow
進入TensorFlow on Windows下載頁,
打開cmd,輸入以下指令即進行TensorFlow的下載安裝,下載位置爲python3.6.2\Lib\site-packages\tensorflow:
在這裏插入圖片描述
打開 IDLE,輸入以下指令:
在這裏插入圖片描述
如果出現如下結果則安裝成功:
在這裏插入圖片描述
安裝其餘組件
在cmd內輸入如下指令下載並安裝相關API運行支持組件:
pillow 、lxml 、jupyter、matplotlib、imageio、requests等
下載代碼並編譯
在cmd中輸入如下代碼:
在這裏插入圖片描述
從github下載谷歌tensorflow/models的代碼,同樣在cmd進入到models文件夾,編譯Object Detection API的代碼:
在這裏插入圖片描述
運行notebook demo
繼續在models文件夾下運行如下命令:
在這裏插入圖片描述
進入object_detection文件夾中的object_detection_tutorial.ipynb:
在這裏插入圖片描述
點擊Cell內的Run All,等待一會兒,即可顯示如下結果:
在這裏插入圖片描述
在這裏插入圖片描述
視頻物體識別
谷歌在github上公佈了此項目的完整代碼,接下來我們將在現有代碼基礎上添加相應模塊實現對於視頻中物體的識別。
https://github.com/priya-dwivedi/Deep-Learning/blob/master/Object_Detection_Tensorflow_API.ipynb
第一步:下載opencv的cv2包
在Python官網即可下載opencv相關庫,點擊此處直接進入。
在這裏插入圖片描述
載完成後,在cmd中執行安裝命令
pip install opencv_python-3.4.3.18-cp36-cp36m-win_amd64.whl
安裝完成後,進入IDLE輸入命令
import cv2
若未報錯,則opencv-python庫成功導入,環境搭配成功。
第二步:在原代碼中引入cv2包
在這裏插入圖片描述
第三步:添加視頻識別代碼
主要步驟如下:
1.使用 VideoFileClip 函數從視頻中抓取圖片。
2.用fl_image函數將原圖片替換爲修改後的圖片,用於傳遞物體識別的每張抓取圖片。
3.所有修改的剪輯圖像被組合成爲一個新的視頻。
在原版代碼基礎上,在最後面依次添加如下代碼(可從完整代碼 處複製,但需要作出一些改變,當然也可以直接從下文複製修改後的代碼):
在這裏插入圖片描述

Import everything needed to edit/save/watch video clips

import imageio
imageio.plugins.ffmpeg.download()

from moviepy.editor import VideoFileClip
from IPython.display import HTML
注意:直接下載即可,有時候會遇到下載過程中斷線的情況,重新下載即可
def detect_objects(image_np, sess, detection_graph):
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name(‘image_tensor:0’)

# Each box represents a part of the image where a particular object was detected.
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
    [boxes, scores, classes, num_detections],
    feed_dict={image_tensor: image_np_expanded})

# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
    image_np,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8)
return image_np
另起一行:
def process_image(image):
# NOTE: The output you return should be a color image (3 channel) for processing video below
# you should return the final output (image with lines are drawn on lanes)
with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        image_process = detect_objects(image, sess, detection_graph)
        return image_process

另起一行:
white_output = ‘video1_out.mp4’
clip1 = VideoFileClip(“video1.mp4”).subclip(25,30)
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!s
%time white_clip.write_videofile(white_output, audio=False)
其中video1.mp4已經從電腦中上傳至object_detection文件夾,subclip(16,26)代表識別視頻中16-26s這一時間段。
HTML("""

""".format(white_output)) 另起一行輸入: from moviepy.editor import * clip1 = VideoFileClip("video1_out.mp4") clip1.write_gif("final.gif") 將識別完畢的視頻導爲gif格式,並保存至object_detection文件夾。 至此,快速教程結束。各位應該都能使用谷歌開放的API實現了視頻物體識別。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章