
Video transition detection also known as scene cut detection is an important video processing task that aims at finding interjoined scenes to identify visual content. It's a fundamental step for video post-production tasks that may involve the addition of newer scenes to visual content. For example, the best duration to insert advertisements into a video streaming service is between the video scene transitions to reduce the prediction of visual changes. Additionally, a content-based video retrieval process requires efficient access to a huge amount of video frame archives which is solved by selecting a representative picture from each scene.
This article explains how to perform video scene transition detection and split video sections using PySceneDetect on a Vultr Cloud GPU server. You will import a sample video, detect transitions, and identify all scenes for advanced usage in a Jupyter Notebook session.
To start video scene transition detection processes on the server, install the necessary dependency packages, and download a sample video to use as the input file for classification purposes. Follow the steps below to install packages using the Python PIP package manager and download a sample Google Chromecast video on your server.
Switch to your user home directory.
Install all required packages using Pip.
The above command installs the necessary detection libraries including scenedetect, opencv-python, and matplotlib with the following functionalities:
scenedetect: Detects scene changes and cuts by analyzing the input video. opencv-python: Enables a wide range of image processing functionalities.matplotlib: Creates static or interactive visualizations.ipywidgets: Enables interactive HTML widgets for Jupyter Notebooks using IPython kernel.Download a sample video to use as the input file. Replace the Google Chromecast video link with your desired file URL and save it as video.mp4.
Copy the input video file to the /home/jupyter/ directory.
Click Notebook within the JupyterLab interface and create a new Python3 Kernel file
In the new notebook code cell, import the required dependency libraries.
Press Shift + Enter to run the code cell and import the libraries in your session.
Define a new video_local_path variable with your input video file location as the value.
Start a video scene transition detection function.
The above command starts a new scene detection function using the Adaptive Content Detector package. Other supported detectors include:
Get the frame in the middle of the first detected timeline scene. There are two elements for each scene indicating the start, end timestamp, and frame number. Run the following code to view the frame index of the image in the middle of the first scene.
Create the video processing instance using the OpenCV VideoCapture class to seek and extract frames using the specified frame index.
Get the first_scene_frame value based on the calculated frame_index.
Display and view the extracted frame.
Based on the input video, the first video frame displays in your output with a monochrome standard.
.
The openCV package uses a BGR order when processing images while matplotlib and most other tools use RGB order. As a result, to view the image in color mode, you should convert BGR to RGB.
Convert BGR to RGB and view the frame image again.
A well-aligned and colored image is displayed similar to the original input video.
Remove the x and y alignment ticks, then add the title for the image display. Replace representative frame for the first scene with your desired output image title, and fontsize=10 with the text size to display.
The new output image includes a representative title and closely matches the first detected scene.
Follow the steps below to merge all functions and visualize the representative images for all the detected scenes. Then, export the generated images to a data directory for additional usage or modification tasks.
Define a new function to get the representative frame for each scene.
Define the number of rows and columns to use for visualization.
Create an empty array to store the representative images for each scene.
Visualize the representative frames of each scene by iterating the detected scene list, then merge the code implementations. Replace scene with your desired frame title text.
Based on the input video, all the representative images for detected video scenes display with a custom title in your notebook session.
Define a new function to save the generated images to the directory on your server. Replace frames with your desired data directory to auto-create in the Jupyter user home directory.
Save all generated scene images to the target directory.
You can perform multiple downstreaming tasks from detected scenes using functions such as:
Video frame indexing and searching: Locates specific frames inside a video by taking keywords or queries/questions in a natural language. For example, a query input such as show me all films where there's a scene for people watching TV traditionally requires the slicing and storage of all video frames in a database. However, this is inefficient when working with multiple videos with a long video length. The scene detection and cut function could store, and process only the representative frames which reduces the searching space. Based on the example in this article, the total number of frames can reduce from 1440 to 44 indicating a 97% reduction in the processing time.Objectionable video frame detection: Checks if a newly uploaded video contains objectionable scenes that are suitable for some audiences. Similarly, it's inefficient to check all the frames from all the videos on a large scale and scene detection could significantly improve its efficiency.Best places to insert ads: Finds the video transition durations for advertisement slots so that viewer experiences are less interrupted. This task explicitly requires scene detection as a dependency.You have created a video scene transition detection pipeline on a Vultr Cloud GPU server. It consists of multiple steps that include an input video file, conducting the scene transition detection, visualizing the representative frames of each scene, and the storage of frames for further downstreaming tasks. For more detector methods and parameters, visit the scenedetect CLI documentation.
0 Comments
Be the first to comment and share your perspective with the community.