基於谷歌開源的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中輸入如下程式碼:
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實現了視訊物體識別。