Smart Vision Python 接口文档
Smart Vision Python API
tip
To use the Smart Vision SDK, first import the smartvisionsdk package:
from pysmartvision import smartvisionsdk
Python API Overview
| Class | Public Member Functions | Type | Description |
|---|---|---|---|
| Camera | open(GetImageCB) | function | Open camera in parameterless mode |
| open(DeviceParam, GetImageCB) | function | Open camera with parameters | |
| start_capture() | function | Start streaming | |
| stop_capture() | function | Stop streaming | |
| close() | function | Close camera | |
| set_camera_type(CameraType camera_type) | function | Set camera type. Supported types: MIPI_CAMERA_ENUM1 / USB_CAMERA_ENUM2 / USB_CAMERA_ENUM3. Default is MIPI. | |
| DeviceParam | cfg_name | str | Path to parameter configuration file |
| Image | data | str | Pointer to buffer containing image data |
| w | int | Target image width | |
| h | int | Target image height | |
| c | int | Number of image channels (typically 3) | |
| length | int | Memory length occupied by image data | |
| IOConfig | do1_keeptime | int | Signal 1 duration |
| do2_keeptime | int | Signal 2 duration | |
| do1 | SignalType | Pin 1 | |
| do2 | SignalType | Pin 2 | |
| TcpConfig | ip | str | IP address |
| port | int | Port number | |
| SVSdkMemfdInfo | srcYPlaneStride | int | Width of YUV data |
| srcSliceHeight | int | Height of YUV data | |
| memCopyStartIndex | int | Start index value (Note: Start index equals End index) | |
| memCopyEndIndex | int | End index value | |
| planeOffset | int | YUV offset value |
| Public Function | Description |
|---|---|
set_log_level(SVLogLevel log_level) | Set log level |
set_log_destination(str destinationPath, bool also_to_stderr) | Set log file name and path |
log_to_stderr() | Output logs to terminal. Once set, log files will not be saved. Generally not recommended. |
GetImageCB(const Image, const SVSdkMemfdInfo) | Used as input parameter for open() function to acquire image resources. This function is user-defined. The interface only provides type definition to standardize the callback function definition. |
get_signal_sending_mode(IOConfig, TcpConfig) | Get signal output configuration. Determine whether IO or TCP configuration is returned based on the return value. If return value is 1, IO configuration is returned from iosetting object. |
set_io_signal_send_mode(SignalType, SignalType, int, int) | Set IO signal output configuration. For example, setting do1 to NG_SIGNAL means pin 1 can only send NG signals. Note that pin 1 and pin 2 cannot be set to the same signal type simultaneously. |
set_tcp_signal_send_mode(str, int) | Set TCP signal output configuration. The IP address and port are for the server side. |
set_off_mode() | Set OFF mode to disable signal output. |
io_send_ng() | Send NG signal via IO. |
io_send_alarm_raising() | Send alarm signal via IO. |
io_send_alarm_clear() | Send alarm clear signal via IO. |
tcp_send_ng() | Send NG signal via TCP. |
tcp_send_alarm() | Send alarm signal via TCP. |
tcp_send_alarmclear() | Send alarm clear signal via TCP. |
| Enum | Member Variable | Type | Description |
|---|---|---|---|
| SVLogLevel | SINFO | int | Log level SINFO |
| SWARNING | int | Log level SWARNING | |
| SERROR | int | Log level SERROR | |
| SDEBUG | int | Log level SDEBUG | |
| SOFF | int | Log level SOFF (log disabled) | |
| SignalType | OFF | int | Disable signal output |
| NG_SIGNAL | int | NG signal output mode | |
| ALARM_SIGNAL | int | Alarm signal output mode | |
| OK_SIGNAL | int | OK signal output mode | |
| CameraType | MIPI_CAMERA_ENUM1 | int | MIPI camera |
| USB_CAMERA_ENUM2 | int | Generic USB camera | |
| USB_CAMERA_ENUM3 | int | Hikvision USB camera |
Camera Class
Operations related to instantiating Camera objects. Camera operations such as opening and capturing images are all based on instantiated Camera objects.
warning
Unlike C++, Python code does not require namespace import. Instead, import the smartvisionsdk module using:
from pysmartvision import smartvisionsdk
See the following interfaces for specific usage.
Open Camera (Parameterless Mode)
| API | open(cb) |
|---|---|
| Description | Open camera in parameterless mode. |
| Parameters | Input parameter is a callback function. Callback function is user-defined. See Callback Function for details. |
| Return Value | 0: Success. -1: Failure. |
Example (camera_instance is an instance of Camera class, same below):
# Open camera in parameterless mode
camera_instance = smartvisionsdk.Camera()
res = camera_instance.open(get_img_cb)Open Camera (With Parameters)
| API | open(dev_param, cb) |
|---|---|
| Description | Open camera with parameters. |
| Parameters | dev_param: Instance of DeviceParam class. cb: Callback function. Callback function is user-defined. See Callback Function for details. |
| Return Value | 0: Success. -1: Failure. |
Example:
# Open camera with parameters
camera_instance = smartvisionsdk.Camera()
res = camera_instance.open(device_param, get_img_cb)Start Streaming
| API | start_capture() |
|---|---|
| Description | Start streaming |
| Parameters | None |
| Return Value | None |
Example:
# Start streaming
camera_instance.start_capture()Stop Streaming
| API | stop_capture() |
|---|---|
| Description | Stop streaming |
| Parameters | None |
| Return Value | None |
Example:
# Stop streaming
camera_instance.stop_capture()Close Camera (Function close)
| API | close() |
|---|---|
| Description | Close camera |
| Parameters | None |
| Return Value | 0: Success. -1: Failure. |
Example:
# Close camera
res = camera_instance.close()Set Camera Type (Function set_camera_type)
| API | set_camera_type(CameraType camera_type) |
|---|---|
| Description | Set camera type |
| Parameters | camera_type: CameraType enum value. Supported types: MIPI_CAMERA_ENUM1 / USB_CAMERA_ENUM2 / USB_CAMERA_ENUM3 |
| Return Value | None |
Example:
# Set camera type to Hikvision camera
camera_instance.set_camera_type(smartvisionsdk.USB_CAMERA_ENUM3)DeviceParam Class
Defines the path to parameter configuration file. This configuration file is used to configure camera parameters. Therefore, it is only used when opening camera with parameters.
| Member Variable | Type | Default | Description |
|---|---|---|---|
| cfg_name | str | Path to parameter configuration file |
Set DeviceParam Configuration File
| API | set_cfgname(config_file) |
|---|---|
| Description | Set path to parameter configuration file. This interface sets the cfg_name variable. |
| Parameters | config_file: Camera parameter configuration file |
| Return Value | None |
Example:
# Set configuration file
device_param = smartvisionsdk.DeviceParam()
device_param.set_cfgname("./config.json")Image Class
Defines image data related information, including image configuration and pointer variables.
| Member Variable | Type | Default | Description |
|---|---|---|---|
| data | str | String pointer to buffer containing data | |
| w | int | Target image width | |
| h | int | Target image height | |
| c | int | Number of image channels (typically 3) | |
| length | int | Memory length occupied by image data |
IOConfig Class
Defines IO signal output configuration.
| Member Variable | Type | Default | Description |
|---|---|---|---|
| do1_keeptime | int | Signal 1 duration | |
| do2_keeptime | int | Signal 2 duration | |
| do1 | SignalType | Pin 1 | |
| do2 | SignalType | Pin 2 |
TcpConfig Class
Defines TCP signal output configuration.
| Member Variable | Type | Default | Description |
|---|---|---|---|
| ip | str | IP address | |
| port | int | Port number |
SVSdkMemfdInfo Class
Defines data buffer information.
| Member Variable | Type | Default | Description |
|---|---|---|---|
| srcYPlaneStride | int | Width of YUV data | |
| srcSliceHeight | int | Height of YUV data | |
| memCopyStartIndex | int | Start index value (Note: Start index equals End index) | |
| memCopyEndIndex | int | End index value | |
| planeOffset | int | YUV offset value |
Set Log Level (Function set_log_level)
| API | set_log_level(log_level) |
|---|---|
| Description | Set log level. |
| Parameters | log_level: SVLogLevel enum. See SVLogLevel Enum for details. |
| Return Value | None |
Example:
# Set log level
smartvisionsdk.set_log_level(smartvisionsdk.SVLogLevel.SINFO)Set Log File Name and Path (Function set_log_destination)
| API | set_log_destination(destinationPath, also_to_stderr) |
|---|---|
| Description | Set log file name and path |
| Parameters | destinationPath: Log file path, default is "./aidclog_smart_vision_". also_to_stderr: Whether to output to terminal, default is False |
| Return Value | None |
Example:
# Set log file name and path
log_path = "./aidclog_smart_vision_setting_"
smartvisionsdk.set_log_destination(log_path, False)Output Logs to Terminal (Function log_to_stderr)
| API | log_to_stderr() |
|---|---|
| Description | Output logs to terminal. Once set, log files will not be saved. Generally not recommended. |
| Parameters | None |
| Return Value | None |
Example:
# Output logs to terminal
smartvisionsdk.log_to_stderr()Log Level (Enum SVLogLevel)
| Member Variable | Type | Default | Description |
|---|---|---|---|
| SINFO | int | 0 | Log level SINFO |
| SWARNING | int | Log level SWARNING | |
| SERROR | int | Log level SERROR | |
| SDEBUG | int | Log level SDEBUG | |
| SOFF | int | Log level SOFF (log disabled) |
Callback Function GetImageCB
| API | GetImageCB(image, mem_info) |
|---|---|
| Description | Used as input parameter for open() function to acquire image resources. This function is user-defined. The interface only provides type definition to standardize the callback function definition. |
| Parameters | First parameter is an Image class instance through which data is passed. Second parameter is an SVSdkMemfdInfo class instance containing buffer index, YUV data width/height, etc. |
| Return Value | User-defined |
Example:
# Callback function definition
def get_img_cb(cap_img, mem_info):
......
return 0Get Signal Output Configuration (Function get_signal_sending_mode)
| API | get_signal_sending_mode(iosetting, tcpsetting) |
|---|---|
| Description | Get signal output configuration. Determine whether IO or TCP configuration is returned based on the return value. If return value is 1, IO configuration is returned from iosetting object. |
| Parameters | iosetting: IOConfig instance containing IO output information. tcpsetting: TcpConfig instance containing TCP output information. |
| Return Value | -1: Failed to get configuration. 0: Configuration is OFF (not set). 1: IO configuration returned from iosetting. 2: TCP configuration returned from tcpsetting. |
Example:
# Get signal output configuration
iosetting = smartvisionsdk.IOConfig()
tcpsetting = smartvisionsdk.TcpConfig()
res = smartvisionsdk.get_signal_sending_mode(iosetting, tcpsetting)Set IO Signal Output Configuration (Function set_io_signal_send_mode)
| API | set_io_signal_send_mode(do1, do2, do1_keeptime, do2_keeptime) |
|---|---|
| Description | Set IO signal output configuration. For example, setting do1 to NG_SIGNAL means pin 1 can only send NG signals. Note that pin 1 and pin 2 cannot be set to the same signal type simultaneously. |
| Parameters | do1: Pin 1. do2: Pin 2. do1_keeptime: Signal 1 duration. do2_keeptime: Signal 2 duration |
| Return Value | -1: Failure. 0: Success |
Example:
# Set IO signal output mode
res = smartvisionsdk.set_io_signal_send_mode(smartvisionsdk.SignalType.NG_SIGNAL, smartvisionsdk.SignalType.ALARM_SIGNAL, 6, 0)Set TCP Signal Output Configuration (Function set_tcp_signal_send_mode)
| API | set_tcp_signal_send_mode(ip, port) |
|---|---|
| Description | Set TCP signal output configuration. The IP address and port are for the server side. |
| Parameters | ip: Server IP address. port: Server port number |
| Return Value | -1: Failure. 0: Success |
Example:
# Set TCP signal output mode
res = smartvisionsdk.set_tcp_signal_send_mode(ip, port)Set OFF Mode (Function set_off_mode)
| API | set_off_mode() |
|---|---|
| Description | Set OFF mode to disable signal output. |
| Parameters | None |
| Return Value | -1: Failure. 0: Success |
Example:
# Set OFF mode to disable signal output
res = smartvisionsdk.set_off_mode()Send NG Signal via IO (Function io_send_ng)
| API | io_send_ng() |
|---|---|
| Description | Send NG signal via IO. |
| Parameters | None |
| Return Value | -1: Failure. 0: Success |
Example:
# Send NG signal via IO
res = smartvisionsdk.io_send_ng()Send Alarm Signal via IO (Function io_send_alarm_raising)
| API | io_send_alarm_raising() |
|---|---|
| Description | Send alarm signal via IO. |
| Parameters | None |
| Return Value | -1: Failure. 0: Success |
Example:
# Send alarm signal via IO
res = smartvisionsdk.io_send_alarm_raising()Send Alarm Clear Signal via IO (Function io_send_alarm_clear)
| API | io_send_alarm_clear() |
|---|---|
| Description | Send alarm clear signal via IO. |
| Parameters | None |
| Return Value | -1: Failure. 0: Success |
Example:
# Send alarm clear signal via IO
res = smartvisionsdk.io_send_alarm_clear()Send NG Signal via TCP (Function tcp_send_ng)
| API | tcp_send_ng() |
|---|---|
| Description | Send NG signal via TCP. |
| Parameters | None |
| Return Value | -1: Failure. 0: Success |
Example:
# Send NG signal via TCP
res = smartvisionsdk.tcp_send_ng()Send Alarm Signal via TCP (Function tcp_send_alarm)
| API | tcp_send_alarm() |
|---|---|
| Description | Send alarm signal via TCP. |
| Parameters | None |
| Return Value | -1: Failure. 0: Success |
Example:
# Send alarm signal via TCP
res = smartvisionsdk.tcp_send_alarm()Send Alarm Clear Signal via TCP (Function tcp_send_alarmclear)
| API | tcp_send_alarmclear() |
|---|---|
| Description | Send alarm clear signal via TCP. |
| Parameters | None |
| Return Value | -1: Failure. 0: Success |
Example:
# Send alarm clear signal via TCP
res = smartvisionsdk.tcp_send_alarmclear()Signal Type Definition (Enum SignalType)
| Member Variable | Type | Default | Description |
|---|---|---|---|
| OFF | int | 0 | Disable signal output |
| NG_SIGNAL | int | NG signal output mode | |
| ALARM_SIGNAL | int | Alarm signal output mode | |
| OK_SIGNAL | int | OK signal output mode |
Get SDK Version (Function get_build_version)
| API | get_build_version() |
|---|---|
| Description | Get SDK version information. |
| Parameters | None |
| Return Value | SDK version string |
Example:
# Get SDK version information
res = smartvisionsdk.get_build_version()Get Mat Object Data
| API | get_cvimage(image) |
|---|---|
| Description | Get Mat object data. Only available in Python version. This interface can directly return converted cv Mat object data. |
| Parameters | image: Input image object |
| Return Value | Mat object data |
Example:
# Get Mat object data, where cap_img is the input Mat object containing YUV data
im = smartvisionsdk.get_cvimage(cap_img)Get Latest Log (Function log_last_msg)
| API | log_last_msg(log_level) |
|---|---|
| Description | Get latest log message |
| Parameters | SVLogLevel log level |
| Return Value | Latest log record |
Example:
# Get latest log message
res = smartvisionsdk.log_last_msg(log_level)Get FPS (Function get_fps)
| API | get_fps(idx) |
|---|---|
| Description | Get real-time FPS from callback function, which represents the camera capture frame rate. |
| Parameters | Camera index idx. MIPI camera: 0~2, Generic USB camera: 7, Hikvision USB camera: 8. Default is 0. |
| Return Value | FPS value |
Example:
# Get real-time camera capture FPS
fps_v = smartvisionsdk.get_fps()Configure MIPI Camera (Function configure_camera)
| API | configure_camera(file_path, set_property, validation) |
|---|---|
| Description | Set camera parameters. |
| Parameters | file_path: Configuration file. set_property: Apply immediately (True: yes, False: no). validation: Validate parameters (True: yes, False: no) |
| Return Value | 0: Success. -1: Failure |
Example:
# Configure camera parameters
res = smartvisionsdk.configure_camera("./param.json", True, True)Enable MIPI Camera Full Size (Function fullsize_enable)
| API | fullsize_enable(enable) |
|---|---|
| Description | Enable MIPI camera full size configuration. |
| Parameters | enable: True to enable, False to disable. |
| Return Value | 0: Success. -1: Failure |
Example:
# Enable MIPI camera fullsize configuration
res = smartvisionsdk.fullsize_enable(True)Set MIPI Camera Binning Mode (Function set_binning_mode)
| API | set_binning_mode(mode) |
|---|---|
| Description | Set binning mode. |
| Parameters | mode: 1: Select nearest binning mode, 2: Select 2x2 binning mode. MIPI camera default is 0, equivalent to mode 1. |
| Return Value | 0: Success. -1: Failure |
Example:
# Set camera binning mode
res = smartvisionsdk.set_binning_mode(1)Query MIPI Camera Fullsize Parameter (Function query_fullsize)
| API | query_fullsize() |
|---|---|
| Description | Query fullsize value. |
| Parameters | None |
| Return Value | Fullsize parameter as string |
Example:
# Get camera fullsize configuration
res = smartvisionsdk.query_fullsize()Query MIPI Camera Binning Mode (Function query_binning_mode)
| API | query_binning_mode() |
|---|---|
| Description | Query binning mode. |
| Parameters | None |
| Return Value | Binning mode as string |
Example:
# Get camera binning mode configuration
res = smartvisionsdk.query_binning_mode()