QPyCam Python API
The QPyCam Python API provides a way to integrate camera functionalities into Python applications. It supports live streaming, recording, RTSP, and AI-based inference with MQTT integration.
Import QPyCam
To use QPyCam in Python, import the class VideoCapture from the qpycam module:
The VideoCapture class serves as the main entry point for the QPyCam API. It provides a flexible and configurable interface for all the powerful features of QPyCam. The workflow is as follows:
- Create a
VideoCaptureobject. Most of the features are configured here using the constructor parameters. - Optionally, enable the AI inference feature by calling the
detectmethod. - Start the camera and all the configured features with
startmethod. - Stop the camera with
stopmethod.
Configure Camera (Video Source)
QPyCam supports three types of cameras: CSI, USB, and RTSP. The camera should be configured in the VideoCapture constructor. Examples for different camera types are shown below.
The CSI camera is the default camera type (camera_type=0).
To capture video from a USB camera, specify camera_type=1 and set the device ID:
Configure Video Output
Display Video Stream
To display the live video feed on the screen, enable the display parameter:
Record Video
To record the video and save it to a file, specify the output_file and timeout parameters:
camera = VideoCapture(
camera_type=0,
source_width=1920,
source_height=1080,
fps=30,
output_file="output.mp4",
timeout = 5000 # milliseconds
)
With the above configuration, the video will be recorded for 5 seconds and saved as output.mp4.
Stream Video via RTSP
To start an RTSP server for remote access, specify the output_ip and output_port parameters:
camera = VideoCapture(
camera_type=0,
source_width=1920,
source_height=1080,
fps=30,
output_ip="192.168.1.100",
output_port=8900
)
Configure Advanced Features
Enable and Configure AI Inference
QPyCam supports various AI inference features such as:
- Object detection
- Classification
- Segmentation
- Pose estimation
Typically, QPyCam would also visualize the inference results on the video stream. The following examples show how to enable and configure the AI inference features.
Note
In the examples above, all the files are assumed to be in the /opt directory. In practice, you will have to download the models by yourself due to the license restrictions. For how to download the models, please refer to How to Download Qualcomm AI Hub Models.
MQTT Integration
QPyCam supports sending inference results via MQTT. This feature has to be configured in the VideoCapture constructor. An example is shown below:
Activate the Camera
Once all the configurations are done, you can activate the camera to perform all the configured operations:
Close the Camera
To stop the camera and end the capture, you can simply close it with the stop method. Alternatively, you can press Ctrl+C to terminate the process.
Full Example
The following example shows how to:
- Use a USB camera as the video source.
- Perform object detection with YOLOv8.
- Display the video on the device screen.
- Stream the video via RTSP.
- Send the inference results via MQTT.
from qpycam import VideoCapture
if __name__ == "__main__":
try:
# Configure the video input/output and MQTT in the constructor
camera = VideoCapture(
camera_type=1, # USB camera
device_id=2,
source_width=1920,
source_height=1080,
fps=30,
display=True,
output_ip="192.168.1.100",
output_port=8900,
mqtt_ip="10.1.9.164",
mqtt_port=1883,
mqtt_topic="ai/detections"
)
# Enable the AI inference feature
camera.detect(
detect_type="detection",
module="yolov8",
model="/opt/yolov8_quantized.tflite",
labels="/opt/coco_labels.labels",
threshold=70,
results=10,
constants="YOLOv8,q-offsets=<21.0, 0.0, 0.0>,q-scales=<3.093529462814331, 0.00390625, 1.0>;"
)
# Start the video capture and all the configured features
camera.start()
finally:
# No matter if the program is terminated by the user or by the system,
# stop the camera and all the tasks.
if 'camera' in locals():
camera.stop()