doridoridoriand’s diary

主に技術的なことを書いていく予定(たぶん)

動画を分析してみよう with Tensorflow

どちらかと言うとTensorflowを使ってみたかった側面が大きい。 同様のことを行いたいのならば、音声を使ったり、画像の色彩の変化を利用したりともっと別の方法があるはず。

TensorFlow結構前から話題になっていたのにほとんど関わりが無かったので触る機会を作ってみた。 ニューラルネットワークの構築を容易にするライブラリなので、多方面に応用が効く(だからこれだけ広く使われているっていうのも理由の1つだと思うけれども)

基本的にGPU Optimizedなので、GPUを利用出来る環境で使用すべきであるが気軽に触ってみたかっただけなので、一旦Mac上のUbuntuにインストールして使用。

Tensorflowはチュートリアルが充実しており、サンプルコードも満載。以下リポジトリがとても参考になる。 https://github.com/tensorflow/models

今回はこの内のImage Recognitionを利用して動画の中に何が写っているかを結果として出力させ、その結果から場面転換点等を検知出来るようにする。 考えた処理フローは以下の通り。

  1. OpenCVを利用して動画をフレームごとに分割&保存
  2. TensofrowのInceptionV3を利用して、物体認識。結果をフレームごとに保存
  3. 結果の差異から転換点と思われるところを出力

実際に実証用のコードを書いてみた。コードは以下リポジトリに存在している。 https://github.com/doridoridoriand/train-movie-data

/bin 配下にコードを格納それぞれの役割は次の通り。

  1. divide.py
    OpenCVを利用して動画をフレーム単位に分割&保存

  2. classify_image.py
    Tensorflowを用いて画像ごとに写っている物体の名前一覧をjsonで出力。出力名はフレーム番号と同様。

  3. parse_json.py
    2で作成したJSONから変化を大きいフレームを検出

  4. convert_frame_to_second.py
    3で抽出下したフレームを時間に変換する突貫コード()

使用した映像はUltra Japan2016のアフタームービー。理由は行きたかったけど行けなかったから。 真面目にやるのであれば、モデルデーターの関係から自然や動物が写っているデーターのほうが良いのだけれども。。

実際に検出できた場面転換点は以下のとおり。

https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=100s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=101s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=102s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=104s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=105s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=106s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=107s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=109s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=111s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=112s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=117s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=118s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=121s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=122s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=123s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=126s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=128s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=129s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=130s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=133s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=139s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=140s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=141s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=143s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=145s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=150s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=152s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=156s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=159s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=163s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=167s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=170s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=172s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=175s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=176s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=178s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=184s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=186s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=188s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=189s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=192s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=193s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=195s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=196s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=197s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=198s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=199s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=201s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=203s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=204s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=205s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=207s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=208s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=209s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=210s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=212s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=213s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=215s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=217s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=219s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=220s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=221s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=222s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=223s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=225s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=226s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=227s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=228s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=231s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=232s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=233s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=234s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=236s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=237s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=238s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=239s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=240s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=241s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=242s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=243s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=244s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=245s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=247s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=248s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=250s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=251s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=252s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=253s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=255s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=258s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=260s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=261s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=265s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=266s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=267s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=268s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=269s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=270s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=276s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=278s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=281s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=282s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=287s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=288s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=291s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=293s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=294s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=296s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=298s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=299s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=300s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=301s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=303s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=305s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=306s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=308s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=311s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=312s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=313s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=316s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=319s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=320s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=323s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=325s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=326s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=328s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=32s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=330s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=331s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=332s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=333s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=334s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=335s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=337s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=338s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=339s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=340s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=341s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=342s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=344s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=345s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=347s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=348s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=349s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=34s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=351s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=352s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=359s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=360s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=361s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=362s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=363s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=364s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=365s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=366s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=367s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=368s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=370s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=372s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=373s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=374s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=375s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=379s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=381s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=382s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=383s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=385s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=389s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=392s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=396s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=397s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=398s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=399s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=400s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=402s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=403s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=405s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=412s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=413s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=416s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=428s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=431s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=438s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=43s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=443s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=446s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=447s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=452s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=455s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=456s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=458s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=462s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=469s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=46s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=470s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=473s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=483s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=484s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=485s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=486s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=488s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=489s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=490s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=496s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=497s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=498s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=499s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=500s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=503s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=504s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=507s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=511s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=512s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=513s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=515s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=516s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=518s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=519s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=522s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=525s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=53s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=54s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=57s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=58s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=62s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=66s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=68s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=69s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=71s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=75s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=77s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=79s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=82s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=83s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=87s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=90s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=91s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=96s
https://www.youtube.com/watch?v=fYB1Ovpd0nc&t=99s

。。。多いわww

Youtubeの仕様で1秒以下の指定ができなかったので重複とみなされてしまうところはカットした。 ちょっと敏感に反応している感も否めないが、結構精度良く抽出出来ているのではないかと思う。

問題は映像を分割する際の時間とファイルサイズだと思われる。 今回画像に分割する際にPNGを選んでしまったことも有り、9分の動画を画像に分割しただけで10GBにもなってしまった。 またTensorflowの処理時間も2時間程度掛かった。

GPUを利用すればもっと高速化出来ると思われるので、改善の余地は沢山ありそう。 ひとまずチュートリアルをアレンジしても結構遊べることが分かった。