.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "build/examples_action_recognition/demo_tsn_ucf101.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_build_examples_action_recognition_demo_tsn_ucf101.py: 1. Getting Started with Pre-trained TSN Models on UCF101 =========================================================== `UCF101 `_ is an action recognition dataset of realistic action videos, collected from YouTube. With 13,320 short trimmed videos from 101 action categories, it is one of the most widely used dataset in the research community for benchmarking state-of-the-art video action recognition models. `TSN `_ (Temporal Segment Network) is a widely adopted video classification method. It is proposed to incorporate temporal information from an entire video. The idea is straightforward: we can evenly divide the video into several segments, process each segment individually, obtain segmental consensus from each segment, and perform final prediction. TSN is more like a general algorithm, rather than a specific network architecture. It can work with both 2D and 3D neural networks. In this tutorial, we will demonstrate how to load a pre-trained TSN model from :ref:`gluoncv-model-zoo` and classify video frames from the Internet or your local disk into one of the 101 action classes. Step by Step ------------ We will show two exmaples here. For simplicity, we first try out a pre-trained UCF101 model on a single video frame. This is actually an image action recognition problem. First, please follow the `installation guide <../../index.html#installation>`__ to install ``MXNet`` and ``GluonCV`` if you haven't done so yet. .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. code-block:: default import matplotlib.pyplot as plt import numpy as np import mxnet as mx from mxnet import gluon, nd, image from mxnet.gluon.data.vision import transforms from gluoncv.data.transforms import video from gluoncv import utils from gluoncv.model_zoo import get_model .. GENERATED FROM PYTHON SOURCE LINES 39-40 Then, we download and show the example image: .. GENERATED FROM PYTHON SOURCE LINES 41-50 .. code-block:: default url = 'https://github.com/bryanyzhu/tiny-ucf101/raw/master/ThrowDiscus.png' im_fname = utils.download(url) img = image.imread(im_fname) plt.imshow(img.asnumpy()) plt.show() .. image-sg:: /build/examples_action_recognition/images/sphx_glr_demo_tsn_ucf101_001.png :alt: demo tsn ucf101 :srcset: /build/examples_action_recognition/images/sphx_glr_demo_tsn_ucf101_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Downloading ThrowDiscus.png from https://github.com/bryanyzhu/tiny-ucf101/raw/master/ThrowDiscus.png... 0%| | 0/147 [00:00
.. GENERATED FROM PYTHON SOURCE LINES 118-138 .. code-block:: default from gluoncv.utils import try_import_cv2 cv2 = try_import_cv2() url = 'https://github.com/bryanyzhu/tiny-ucf101/raw/master/v_Basketball_g01_c01.avi' video_fname = utils.download(url) cap = cv2.VideoCapture(video_fname) cnt = 0 video_frames = [] while(cap.isOpened()): ret, frame = cap.read() cnt += 1 if ret and cnt % 25 == 0: video_frames.append(frame) if not ret: break cap.release() print('We evenly extract %d frames from the video %s.' % (len(video_frames), video_fname)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Downloading v_Basketball_g01_c01.avi from https://github.com/bryanyzhu/tiny-ucf101/raw/master/v_Basketball_g01_c01.avi... 0%| | 0/281 [00:00`__. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 30.899 seconds) .. _sphx_glr_download_build_examples_action_recognition_demo_tsn_ucf101.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_tsn_ucf101.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_tsn_ucf101.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_