OpenCV for Unity 2.6.4
Enox Software / Please refer to OpenCV official document ( http://docs.opencv.org/4.10.0/index.html ) for the details of the argument of the method.
Loading...
Searching...
No Matches
OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper Class Reference

A helper component class for obtaining video frames from a file using Unity's VideoPlayer and converting them to OpenCV Mat format. More...

Public Member Functions

virtual bool DidUpdateThisFrame ()
 Indicate whether the video buffer of the frame has been updated.
 
virtual void Dispose ()
 Releases all resource used by the UnityVideoPlayer2MatHelper object.
 
virtual Source2MatHelperColorFormat GetBaseColorFormat ()
 Return the video base color format.
 
virtual string GetDeviceName ()
 Return the active video device name.
 
virtual float GetFPS ()
 Return the video framerate.
 
virtual int GetFrameCount ()
 Return the number of frames in the current video content.
 
virtual int GetFrameIndex ()
 Return the video frame index.
 
virtual float GetFramePosRatio ()
 Return the relative position of the video file: 0=start of the film, 1=end of the film.
 
virtual int GetHeight ()
 Return the video height.
 
virtual Mat GetMat ()
 Get the mat of the current frame.
 
virtual VideoPlayer GetVideoPlayer ()
 Return the VideoCapture instance.
 
virtual int GetWidth ()
 Return the video width.
 
virtual void Initialize (bool autoPlay=true)
 Initialize this instance.
 
virtual void Initialize (string requestedVideoFilePath, bool autoPlay=true)
 Initialize this instance.
 
virtual bool IsInitialized ()
 Indicate whether this instance has been initialized.
 
virtual bool IsPaused ()
 Indicate whether the video is paused.
 
virtual bool IsPlaying ()
 Indicate whether the video is currently playing.
 
virtual void Pause ()
 Pause the video.
 
virtual void Play ()
 Start the video.
 
virtual void SetFrameIndex (int index)
 Set the video frame index.
 
virtual void SetFramePosRatio (float ratio)
 Set the relative position of the video file: 0=start of the film, 1=end of the film.
 
virtual void Stop ()
 Stop the video.
 
- Public Member Functions inherited from OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper
- Public Member Functions inherited from OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper

Protected Member Functions

virtual IEnumerator _Initialize ()
 Initialize this instance by coroutine.
 
virtual void CancelInitCoroutine ()
 Cancel Init Coroutine.
 
virtual void LateUpdate ()
 
virtual void OnDestroy ()
 Raises the destroy event.
 
virtual void OnValidate ()
 
virtual void ReleaseResources ()
 To release the resources.
 

Protected Attributes

bool _loop = true
 Indicate whether to play this video in a loop.
 
UnityEvent _onDisposed
 UnityEvent that is triggered when this instance is disposed.
 
Source2MatHelperErrorUnityEvent _onErrorOccurred
 UnityEvent that is triggered when this instance is error Occurred.
 
UnityEvent _onInitialized
 UnityEvent that is triggered when this instance is initialized.
 
Source2MatHelperColorFormat _outputColorFormat = Source2MatHelperColorFormat.RGBA
 Select the output color format.
 
string _requestedVideoFilePath = string.Empty
 Set the video file path, relative to the starting point of the "StreamingAssets" folder, or absolute path.
 
int _timeoutFrameCount = 1500
 The number of frames before the initialization process times out.
 
bool autoPlayAfterInitialize
 If set to true play after completion of initialization.
 
Source2MatHelperColorFormat baseColorFormat = Source2MatHelperColorFormat.RGBA
 The base color format.
 
Mat baseMat
 The base mat.
 
bool didUpdateThisFrame = false
 
long frameIndex
 The frame index.
 
Mat frameMat
 The frame mat.
 
IEnumerator getFilePathCoroutine
 The get file path coroutine.
 
bool hasInitDone = false
 Indicates whether this instance has been initialized.
 
IEnumerator initCoroutine
 The initialization coroutine.
 
bool isInitWaiting = false
 Indicates whether this instance is waiting for initialization to complete.
 
Texture2D texture
 The texture.
 
bool useAsyncGPUReadback
 The useAsyncGPUReadback.
 
VideoPlayer videoPlayer
 The videoPlayer.
 

Properties

virtual bool loop [get, set]
 
UnityEvent onDisposed [get, set]
 
Source2MatHelperErrorUnityEvent onErrorOccurred [get, set]
 
UnityEvent onInitialized [get, set]
 
virtual Source2MatHelperColorFormat outputColorFormat [get, set]
 
virtual string requestedVideoFilePath [get, set]
 
virtual int timeoutFrameCount [get, set]
 
- Properties inherited from OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper
- Properties inherited from OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper

Detailed Description

A helper component class for obtaining video frames from a file using Unity's VideoPlayer and converting them to OpenCV Mat format.

The UnityVideoPlayer2MatHelper class uses Unity's VideoPlayer to read frames from a specified video file and converts each frame to an OpenCV Mat object. This component automatically retrieves frames based on the video's playback rate, ensuring smooth frame updates and accurate timing.

To ensure compatibility within Unity, it is recommended to use the MP4 format for video files, as it is widely supported by the VideoPlayer class.

This component is particularly useful for applications requiring frame-by-frame video processing in Unity, allowing seamless integration with OpenCV-based image processing routines while using Unity’s native video playback features.

Note: By setting outputColorFormat to RGBA processing that does not include extra color conversion is performed.

Attach this component to a GameObject and call GetMat() to retrieve the current video frame in Mat format. The helper class manages video file loading, playback control, and frame updates based on the video's playback rate.

Member Function Documentation

◆ _Initialize()

virtual IEnumerator OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._Initialize ( )
protectedvirtual

Initialize this instance by coroutine.

◆ CancelInitCoroutine()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.CancelInitCoroutine ( )
protectedvirtual

Cancel Init Coroutine.

◆ DidUpdateThisFrame()

virtual bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.DidUpdateThisFrame ( )
virtual

Indicate whether the video buffer of the frame has been updated.

Returns
true, if the video buffer has been updated false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ Dispose()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.Dispose ( )
virtual

Releases all resource used by the UnityVideoPlayer2MatHelper object.

Call Dispose when you are finished using the UnityVideoPlayer2MatHelper. The Dispose method leaves the UnityVideoPlayer2MatHelper in an unusable state. After calling Dispose, you must release all references to the UnityVideoPlayer2MatHelper so the garbage collector can reclaim the memory that the UnityVideoPlayer2MatHelper was occupying.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetBaseColorFormat()

virtual Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetBaseColorFormat ( )
virtual

Return the video base color format.

Returns
The video base color format.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetDeviceName()

virtual string OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetDeviceName ( )
virtual

Return the active video device name.

Returns
The active video device name.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetFPS()

virtual float OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetFPS ( )
virtual

Return the video framerate.

Returns
The video framerate.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetFrameCount()

virtual int OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetFrameCount ( )
virtual

Return the number of frames in the current video content.

Returns
The number of frames in the current video content.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetFrameIndex()

virtual int OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetFrameIndex ( )
virtual

Return the video frame index.

Returns
The video frame index.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetFramePosRatio()

virtual float OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetFramePosRatio ( )
virtual

Return the relative position of the video file: 0=start of the film, 1=end of the film.

Returns
The relative position of the video file: 0=start of the film, 1=end of the film.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ GetHeight()

virtual int OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetHeight ( )
virtual

Return the video height.

Returns
The video height.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetMat()

virtual Mat OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetMat ( )
virtual

Get the mat of the current frame.

The Mat object's type is 'CV_8UC4' or 'CV_8UC3' or 'CV_8UC1' (ColorFormat is determined by the outputColorFormat setting). Please do not dispose of the returned mat as it will be reused.

Returns
The mat of the current frame.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ GetVideoPlayer()

virtual VideoPlayer OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetVideoPlayer ( )
virtual

Return the VideoCapture instance.

Returns
The VideoCapture instance.

◆ GetWidth()

virtual int OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.GetWidth ( )
virtual

Return the video width.

Returns
The video width.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ Initialize() [1/2]

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.Initialize ( bool autoPlay = true)
virtual

Initialize this instance.

Parameters
autoPlayIf set to true play after completion of initialization.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ Initialize() [2/2]

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.Initialize ( string requestedVideoFilePath,
bool autoPlay = true )
virtual

Initialize this instance.

Parameters
requestedVideoFilePathRequested video file path.
autoPlayIf set to true play after completion of initialization.

◆ IsInitialized()

virtual bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.IsInitialized ( )
virtual

Indicate whether this instance has been initialized.

Returns
true, if this instance has been initialized, false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ IsPaused()

virtual bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.IsPaused ( )
virtual

Indicate whether the video is paused.

Returns
true, if the video is paused, false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ IsPlaying()

virtual bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.IsPlaying ( )
virtual

Indicate whether the video is currently playing.

Returns
true, if the video is playing, false otherwise.

Implements OpenCVForUnity.UnityUtils.Helper.ISource2MatHelper.

◆ LateUpdate()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.LateUpdate ( )
protectedvirtual

◆ OnDestroy()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.OnDestroy ( )
protectedvirtual

Raises the destroy event.

◆ OnValidate()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.OnValidate ( )
protectedvirtual

◆ Pause()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.Pause ( )
virtual

◆ Play()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.Play ( )
virtual

◆ ReleaseResources()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.ReleaseResources ( )
protectedvirtual

To release the resources.

◆ SetFrameIndex()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.SetFrameIndex ( int index)
virtual

Set the video frame index.

Parameters
indexThe video frame index.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ SetFramePosRatio()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.SetFramePosRatio ( float ratio)
virtual

Set the relative position of the video file: 0=start of the film, 1=end of the film.

Parameters
ratioThe relative position of the video file: 0=start of the film, 1=end of the film.

Implements OpenCVForUnity.UnityUtils.Helper.IVideoSource2MatHelper.

◆ Stop()

virtual void OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.Stop ( )
virtual

Member Data Documentation

◆ _loop

bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._loop = true
protected

Indicate whether to play this video in a loop.

◆ _onDisposed

UnityEvent OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._onDisposed
protected

UnityEvent that is triggered when this instance is disposed.

◆ _onErrorOccurred

Source2MatHelperErrorUnityEvent OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._onErrorOccurred
protected

UnityEvent that is triggered when this instance is error Occurred.

◆ _onInitialized

UnityEvent OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._onInitialized
protected

UnityEvent that is triggered when this instance is initialized.

◆ _outputColorFormat

Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._outputColorFormat = Source2MatHelperColorFormat.RGBA
protected

Select the output color format.

◆ _requestedVideoFilePath

string OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._requestedVideoFilePath = string.Empty
protected

Set the video file path, relative to the starting point of the "StreamingAssets" folder, or absolute path.

◆ _timeoutFrameCount

int OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper._timeoutFrameCount = 1500
protected

The number of frames before the initialization process times out.

◆ autoPlayAfterInitialize

bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.autoPlayAfterInitialize
protected

If set to true play after completion of initialization.

◆ baseColorFormat

Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.baseColorFormat = Source2MatHelperColorFormat.RGBA
protected

The base color format.

◆ baseMat

Mat OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.baseMat
protected

The base mat.

◆ didUpdateThisFrame

bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.didUpdateThisFrame = false
protected

◆ frameIndex

long OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.frameIndex
protected

The frame index.

◆ frameMat

Mat OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.frameMat
protected

The frame mat.

◆ getFilePathCoroutine

IEnumerator OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.getFilePathCoroutine
protected

The get file path coroutine.

◆ hasInitDone

bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.hasInitDone = false
protected

Indicates whether this instance has been initialized.

◆ initCoroutine

IEnumerator OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.initCoroutine
protected

The initialization coroutine.

◆ isInitWaiting

bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.isInitWaiting = false
protected

Indicates whether this instance is waiting for initialization to complete.

◆ texture

Texture2D OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.texture
protected

The texture.

◆ useAsyncGPUReadback

bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.useAsyncGPUReadback
protected

The useAsyncGPUReadback.

◆ videoPlayer

VideoPlayer OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.videoPlayer
protected

The videoPlayer.

Property Documentation

◆ loop

virtual bool OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.loop
getset

◆ onDisposed

UnityEvent OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.onDisposed
getset

◆ onErrorOccurred

Source2MatHelperErrorUnityEvent OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.onErrorOccurred
getset

◆ onInitialized

UnityEvent OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.onInitialized
getset

◆ outputColorFormat

virtual Source2MatHelperColorFormat OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.outputColorFormat
getset

◆ requestedVideoFilePath

virtual string OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.requestedVideoFilePath
getset

◆ timeoutFrameCount

virtual int OpenCVForUnity.UnityUtils.Helper.UnityVideoPlayer2MatHelper.timeoutFrameCount
getset

The documentation for this class was generated from the following file: