如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體

如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的正鑲白網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

注意: windows用戶名不能出現(xiàn)中文!??!

安裝Python

注意: Windows平臺(tái)的TensorFlow僅支持3.5.X版本的Python 進(jìn)入Python3.5.2下載頁(yè),選擇 Files 中Windows平臺(tái)的Python安裝包,下載并安裝。 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體

安裝TensorFlow

進(jìn)入TensorFlow on Windows下載頁(yè),本教程使用最簡(jiǎn)便的組合 CPU support only + Native pip。

打開(kāi)cmd,輸入以下指令即進(jìn)行TensorFlow的下載安裝,下載位置為python\Lib\site-packages\tensorflow: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 打開(kāi) IDLE,輸入以下指令: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 如果出現(xiàn)如下結(jié)果則安裝成功: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 若出現(xiàn)問(wèn)題,請(qǐng)參考TensorFlow on Windows下載頁(yè)底端的常見(jiàn)問(wèn)題。 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體

安裝Protoc

Protoc用于編譯相關(guān)程序運(yùn)行文件,進(jìn)入Protoc下載頁(yè),下載類似下圖中帶win32的壓縮包。 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 解壓后將bin文件夾內(nèi)的protoc.exe拷貝到c:\windows\system32目錄下(用于將protoc.exe所在的目錄配置到環(huán)境變量當(dāng)中)。

安裝git

進(jìn)入git官網(wǎng)下載Windows平臺(tái)的git,詳細(xì)安裝及配置注意事項(xiàng)可參考此文。 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體

安裝其余組件

在cmd內(nèi)輸入如下指令下載并安裝相關(guān)API運(yùn)行支持組件: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 注意: Native pip會(huì)受電腦中另外Python應(yīng)用的影響,博主因?yàn)橹白龇抡姘惭b了Anaconda,導(dǎo)致下載的jupyter等相關(guān)組件安裝到了Anaconda內(nèi)的site-packages文件夾,后期調(diào)用失敗。

下載代碼并編譯

在cmd中輸入如下代碼: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 從github下載谷歌tensorflow/models的代碼,一般默認(rèn)下載到C盤(pán)。

同樣在cmd進(jìn)入到models文件夾,編譯Object Detection API的代碼: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體

運(yùn)行notebook demo

繼續(xù)在models文件夾下運(yùn)行如下命令: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 瀏覽器自動(dòng)開(kāi)啟,顯示如下界面: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 進(jìn)入object_detection文件夾中的object_detection_tutorial.ipynb: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 點(diǎn)擊Cell內(nèi)的Run All,等待三分鐘左右(博主電腦接近報(bào)廢),即可顯示如下結(jié)果: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 修改文件路徑,即可檢測(cè)自己的圖片: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 注意:要將圖片名稱設(shè)置的和代碼描述相符合,如image1.jpg 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 TensorFlow Object Detection API中提供了五種可直接調(diào)用的識(shí)別模型,默認(rèn)的是最簡(jiǎn)單的ssd + mobilenet模型。 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 可直接將MODEL_NAME修改為如下值調(diào)用其他模型:

MODEL_NAME = 'ssd_inception_v2_coco_11_06_2017'

MODEL_NAME = 'rfcn_resnet101_coco_11_06_2017'

MODEL_NAME = 'faster_rcnn_resnet101_coco_11_06_2017'

MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_coco_11_06_2017'

將模型換為faster_rcnn_inception_resnet,結(jié)果如下: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 準(zhǔn)確率確實(shí)獲得了極大提高,但是速度卻下降了,在博主的老爺機(jī)上需要五分鐘才能跑出結(jié)果。

視頻物體識(shí)別

谷歌在github上公布了此項(xiàng)目的完整代碼,接下來(lái)我們將在現(xiàn)有代碼基礎(chǔ)上添加相應(yīng)模塊實(shí)現(xiàn)對(duì)于視頻中物體的識(shí)別。

第一步:下載opencv的cv2包 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 在Python官網(wǎng)即可下載opencv相關(guān)庫(kù),點(diǎn)擊此處直接進(jìn)入。

博主安裝的版本如下: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 下載完成后,在cmd中執(zhí)行安裝命令

pip install opencv_python-3.2.0.8-cp35-cp35m-win_amd64.whl

安裝完成后,進(jìn)入IDLE輸入命令

import cv2

若未報(bào)錯(cuò),則opencv-python庫(kù)成功導(dǎo)入,環(huán)境搭配成功。

第二步:在原代碼中引入cv2包 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 第三步:添加視頻識(shí)別代碼 主要步驟如下: 1.使用 VideoFileClip 函數(shù)從視頻中抓取圖片。 2.用fl_image函數(shù)將原圖片替換為修改后的圖片,用于傳遞物體識(shí)別的每張抓取圖片。 3.所有修改的剪輯圖像被組合成為一個(gè)新的視頻。

在原版代碼基礎(chǔ)上,在最后面依次添加如下代碼(可從完整代碼 處復(fù)制,但需要作出一些改變,當(dāng)然也可以直接從下文復(fù)制修改后的代碼): 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體

# 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

此處會(huì)下載一個(gè)剪輯必備的程序ffmpeg.win32.exe,內(nèi)網(wǎng)下載過(guò)程中容易斷線,可以使用下載工具下載完然后放入如下路徑:

C:\Users\ 用戶名 \AppData\Local\imageio\ffmpeg\ffmpeg.win32.exe

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已經(jīng)從電腦中上傳至object_detection文件夾,subclip(25,30)代表識(shí)別視頻中25-30s這一時(shí)間段。

原版視頻: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體 展示識(shí)別完畢的視頻: 如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體

from moviepy.editor import *
clip1 = VideoFileClip("video1_out.mp4")
clip1.write_gif("final.gif")

將識(shí)別完畢的視頻導(dǎo)為gif格式,并保存至object_detection文件夾。

看完上述內(nèi)容,你們掌握如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站名稱:如何使用TensorFlow進(jìn)行訓(xùn)練識(shí)別視頻圖像中物體
文章分享:http://muchs.cn/article42/gdejhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管微信小程序、域名注冊(cè)、App設(shè)計(jì)、移動(dòng)網(wǎng)站建設(shè)、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)