Decoding Camera Devices using Indexes¶
With DeFFcode APIs, we are able to probe and enumerate all Camera Devices names along with their respective "device indexes" or "camera indexes" no matter how many cameras are connected to your system. This makes Camera Devices decoding as simple as OpenCV, where one can effortlessly access a specific Camera Device just by the specifying the matching index of it. These indexes are much easier to read, memorize, and type, and one don't have to remember long Device names or worry about its Demuxer.
We'll discuss the Decoding Camera Devices using Indexes briefly in the following recipes:
DeFFcode APIs requires FFmpeg executable
DeFFcode APIs MUST requires valid FFmpeg executable for all of its core functionality, and any failure in detection will raise RuntimeError
immediately. Follow dedicated FFmpeg Installation doc ➶ for its installation.
Additional Python Dependencies for following recipes
Following recipes requires additional python dependencies which can be installed easily as below:
-
OpenCV: OpenCV is required for previewing video frames. You can easily install it directly via
pip
:OpenCV installation from source
You can also follow online tutorials for building & installing OpenCV on Windows, Linux, MacOS and Raspberry Pi machines manually from its source.
Make sure not to install both pip and source version together. Otherwise installation will fail to work!
Other OpenCV binaries
OpenCV maintainers also provide additional binaries via pip that contains both main modules and contrib/extra modules
opencv-contrib-python
, and for server (headless) environments likeopencv-python-headless
andopencv-contrib-python-headless
. You can also install any one of them in similar manner. More information can be found here.
Always use FFdecoder API's terminate()
method at the end to avoid undesired behavior.
Never name your python script deffcode.py
When trying out these recipes, never name your python script deffcode.py
otherwise it will result in ModuleNotFound
error.
Enumerating all Camera Devices with Indexes¶
In Sourcer API, you can easily use its
enumerate_devices
property object to enumerate all probed Camera Devices (connected to your system) as dictionary object with device indexes as keys and device names as their respective values.
Requirement for Enumerating all Camera Devices in Sourcer API
-
MUST have appropriate FFmpeg binaries, Drivers, and Softwares installed:
Internally, DeFFcode APIs achieves Index based Camera Device Capturing by employing some specific FFmpeg demuxers on different platforms(OSes). These platform specific demuxers are as follows:
Platform(OS) Demuxer Windows OS dshow
(or DirectShow)Linux OS video4linux2
(or its aliasv4l2
)Mac OS avfoundation
Important: Kindly make sure your FFmpeg binaries support these platform specific demuxers as well as system have the appropriate video drivers and related softwares installed.
-
The
source
parameter value MUST be any Camera Device index that can be of either integer (e.g.-1
,0
,1
, etc.) or string of integer (e.g."-1"
,"0"
,"1"
, etc.) type. -
The
source_demuxer
parameter value MUST be eitherNone
(also means empty) or"auto"
.
In this example we will enumerate all probed Camera Devices connected on a Windows machine using enumerate_devices
property object in Sourcer API, both as dictionary object and JSON string.
# import the necessary packages
from deffcode import Sourcer
import json
# initialize and formulate the decoder
sourcer = Sourcer("0").probe_stream()
# enumerate probed devices as Dictionary object(`dict`)
print(sourcer.enumerate_devices)
# enumerate probed devices as JSON string(`json.dump`)
print(json.dumps(sourcer.enumerate_devices,indent=2))
After running above python code, the resultant Terminal Output will look something as following on Windows machine:
Capturing and Previewing frames from a Camera using Indexes¶
After knowing the index of Camera Device with Sourcer API, One can easily Capture desired Camera Device in FFdecoder API by specifying its matching index value either as integer or string of integer type to its
source
parameter.
Requirement for Index based Camera Device Capturing in FFdecoder API
-
MUST have appropriate FFmpeg binaries, Drivers, and Softwares installed:
Internally, DeFFcode APIs achieves Index based Camera Device Capturing by employing some specific FFmpeg demuxers on different platforms(OSes). These platform specific demuxers are as follows:
Platform(OS) Demuxer Windows OS dshow
(or DirectShow)Linux OS video4linux2
(or its aliasv4l2
)Mac OS avfoundation
Important: Kindly make sure your FFmpeg binaries support these platform specific demuxers as well as system have the appropriate video drivers and related softwares installed.
-
The
source
parameter value MUST be exactly the probed Camera Device index (use Sourcer API'senumerate_devices
to list them). - The
source_demuxer
parameter value MUST be eitherNone
(also means empty) or"auto"
.
In this example we will decode BGR24 video frames from Integrated Camera at index 0
on a Windows Machine, and preview them using OpenCV Library's cv2.imshow()
method.
Important Facts related to Camera Device Indexing
- Camera Device indexes are 0-indexed. So the first device is at
0
, second is at1
, so on. So if the there aren
devices, the last device is atn-1
. - Camera Device indexes can be of either integer (e.g.
0
,1
, etc.) or string of integer (e.g."0"
,"1"
, etc.) type. - Camera Device indexes can be negative (e.g.
-1
,-2
, etc.), this means you can also start indexing from the end.- For example, If there are three devices:
-
Then, You can specify Positive Indexes and its Equivalent Negative Indexes as follows:
Positive Indexes Equivalent Negative Indexes FFdecoder("0").formulate()
FFdecoder("-3").formulate()
FFdecoder("1").formulate()
FFdecoder("-2").formulate()
FFdecoder("2").formulate()
FFdecoder("-1").formulate()
Out of Index Camera Device index values will raise ValueError
in FFdecoder API
# import the necessary packages
from deffcode import FFdecoder
import cv2
# initialize and formulate the decoder with "0" index source for BGR24 output
decoder = FFdecoder("0", frame_format="bgr24", verbose=True).formulate()
# grab the BGR24 frames from decoder
for frame in decoder.generateFrame():
# check if frame is None
if frame is None:
break
# {do something with the frame here}
# Show output window
cv2.imshow("Output", frame)
# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# close output window
cv2.destroyAllWindows()
# terminate the decoder
decoder.terminate()