1. 程式人生 > >基於谷歌開源的TensorFlow Object Detection API視訊物體識別系統實現教程

基於谷歌開源的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實現了視訊物體識別。