Skip to content

附录

智能相机配置文件

有参配置文件的内容、位置及文件名由客户自行定义配置,但格式及参数需按照以下要求:

  • 配置文件为json格式。示例设置:/home/aidlux/camera_config.json
  • 标注为必选参数或因为开关参数打开而成为必选参数,则必须配置。
  • 相应参数的名字(大小写敏感)及类型必须符合下面表格要求。
参数名类型必填说明
autoExposurebool自动曝光设置。true:开启曝光,false:关闭曝光。参数示例 "autoExposure": true
autoFocusbool自动对焦,ture:开启自动对焦,false:关闭自动对焦。参数示例 "autoFocus": false
autoWhiteBalanceModeint自动白平衡模式,自定义白平衡关闭时生效且必填,0:关闭白平衡模式,1:开启白平衡模式。参数示例 "autoWhiteBalanceMode": 1
autoWhiteBalanceLockbool自动白平衡当前是否锁定为最新计算值,自动白平衡时生效,true:开启锁定,false:关闭锁定。参数示例 "autoWhiteBalanceLock": false
aidluxFIFOint采集数据保存地址。参数示例 "aidluxFIFO": "/tmp/static_metadata_fifo"
customWhiteBalanceModeint自定义白平衡模式,自定义白平衡开启时生效且必填,0:关闭,1:开启。参数示例 "customWhiteBalanceMode": 1
customWhiteBalancebool自定义白平衡当前是否锁定为最新计算值,自定义白平衡时生效,true:开启锁定,false:关闭锁定。参数示例 "customWhiteBalance": false
colorCorrectionGainsarray色彩矫正增益(4位float数组), RGB增益时生效且必填。参数示例 [1.0,1.0,1.0,1.0]
colorCorrectionTransformarray色彩矫正变换(18位int数组)。 参数示例 [1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1]
colorTemperatureint色温。参数示例 "colorTemperature": 5500
contrastint对比度。 取值范围(0 - 100) 参数示例 "contrast": 0
debounceint防抖间隔(ms),触发方式为IO触发时必填。参数示例 "debounce": 0
exposureMeteringModeint曝光测光模式,0表示关闭,1表示开启。参数示例 "exposureMeteringMode": 1
exposureCompensationfloat曝光补偿设置,自动曝光开启时生效且必填 0:开启,1:关闭。参数示例 "exposureCompensation": 0
exposureTimelong相机的曝光时长(us)。自动曝光关闭时必填。参数示例 "exposureTime": 20000000
fpsint相机的帧率(fps)。自动曝光关闭时必填。参数示例 "fps": 60
formatint相机编码目前仅支持35,后续再增加其他编码格式。参数示例 "format": 35
widthint图像的宽度设置。参数示例 "width": 1920 或者 "width": 3840
heightint图像的高度设置。参数示例 "height" : 1080 或者 "height" : 2160
inputChannelintIO输入类型。参数示例 "inputChannel": 1
rimTypeint边沿设置。0:上升沿,1:下降沿。(当为IO触发即"triggerWay": 0 时必须进行配置)
sensitivityint相机的感光度(iso)。自动曝光关闭时必填。参数示例 "sensitivity": 500
sharpnessint锐度。 取值范围(0 - 100) 参数示例 "sharpness": 2
saturationint饱和度。 取值范围(0 - 100) 参数示例 "saturation": 5
triggerWayint触发方式。0:IO触发,1:软触发即连续触发。参数示例 "triggerWay": 0
triggerDelayint最小触发间隔(ms)。"triggerDelay": 0 时表示无延迟触发。参数示例 "triggerDelay": 0

有参配置文件参考内容

tip

在使用相关配置文件的时候请确保以下八个参数的存在:

triggerWay, width, height, fps

autoExposure, autoFocus, customWhiteBalance, autoWhiteBalanceMode

json配置文件路径自定义 例如<"/home/aidlux/aidlux_smart_vision/data/param.json">

json
{
    "triggerWay" : 1,
    "rimType" : 1,
    "triggerDelay" : 0,
    "width" : 1920, 
    "height" : 1080,
    "fps": 30,
    "format" : 35,
    "autoExposure" : true,
    "exposureCompensation" : 0,
    "exposureTime" : 20000,
    "sensitivity" : 500,
    "autoFocus": true,
    "customWhiteBalance": false,
    "autoWhiteBalanceMode" : 1,
    "autoWhiteBalanceLock" : false,
    "customWhiteBalanceMode" : 1,
    "colorCorrectionGains": [1.0,1.0,1.0,1.0],
    "contrast" : 0, 
    "sharpness" : 0, 
    "saturation" : 0
}

其他形式使用相机-有参形式使用相机

:::bash 有参模式

打开相机请使用 有参打开相机 start_camera(const char* json_param, int8_t idx) 接口。

获取图片数据请使用 获取图片数据 cv::Mat image_read(int8_t idx) 接口。

具体内容参考 点击跳转->有参形式-使用相机C++示例点击跳转->有参形式-使用相机Python示例。 :::

有参形式-示例

tip

下列示例中的camera_idx参数说明:

  • camera_idx = 0:默认相机索引,即主设备相机0
  • camera_idx = 1:指定相机索引,即分设备相机1
  • camera_idx = 2:指定相机索引,即分设备相机2

编译指令

本实例以example.cpp为例编译生成可执行文件 example

g++ -std=c++17 -o example example.cpp \
-L /usr/local/lib/ \
-I /usr/local/include/aidlux/smartvision/ \
-I /usr/local/include/aidlux/aidlux_opencv/ \
-lsmartvision -lopencv_core

C++

#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <nlohmann/json.hpp>
#include <chrono>                                                                                                   
#include <ctime>
#include <vector>
#include <sys/mman.h>
#include <atomic>

#include "smart_vision.hpp"
#include "crow.h"
#include "crow/json.h"

using namespace std;
using namespace Aidlux::SmartVision;

using json = nlohmann::json;

std::atomic<bool> g_stop_signal{false}; //原子操作,用于控制程序退出

void signal_handler(int signal)     //信号处理函数,用ctrl+c退出程序
{
    if (signal == SIGINT)
    {
        g_stop_signal = true;
    }
}

std::string read_file_to_string(const std::string& path) 
{
    std::ifstream file(path);
    if (!file.is_open()) 
    {
        std::cerr << "Failed to open config file: " << path << std::endl;
        return "";
    }
    std::ostringstream ss;
    ss << file.rdbuf();
    return ss.str();
}

void camera_example(int camera_idx) 
{
    const std::string config_path = <自定义配置文件路径>;   //例如 "/home/aidlux/aidlux_smart_vision/data/param.json"

    // 读取配置文件
    std::string config_str = read_file_to_string(config_path);
    if (config_str.empty()) 
    {
        printf("Failed to load camera config.");
        return;
    }

    // 启动摄像头
    if (start_camera(config_str.c_str(), camera_idx) != 0) 
    {
        printf("Start camera failed.");
        return;
    }
    printf("Camera %d started successfully with config.\n", camera_idx);

    usleep(500 * 1000); // 等待初始化

    while (!g_stop_signal) 
    {
        cv::Mat frame = Aidlux::SmartVision::image_read(camera_idx);
        
        if (frame.empty()) 
        {
            printf("Got empty frame from camera %d", camera_idx);
            usleep(10000);
            continue;
        }
        printf("Got frame: %dx%d, size=%ld\n", 
          frame.cols, frame.rows, frame.total());

        // 在获取帧之后,用户可以在这里添加自定义的操作。 
        // 1:将获取到的帧转换为灰度图像
        // 2:保存图像到文件
        // 3: 进行图像处理
        // 4:进行物体识别

        // 控制采集/处理频率:
        // - 使用 usleep() 可精确控制微秒级间隔
        // - 或替换为 std::this_thread::sleep_for(...)
        //   示例:std::this_thread::sleep_for(std::chrono::milliseconds(33)); // ≈30 FPS
        //
        // 注意:若自定义处理逻辑本身耗时较长,此处 sleep 时间应相应缩短,
        //         否则会导致实际帧率低于预期。
        usleep(/* 请根据目标帧率设置延迟,单位:微秒 */);
    }

    close_camera(camera_idx);
    printf("Camera %d stopped gracefully.\n", camera_idx);
}

int main() {
    signal(SIGINT, signal_handler); // 注册信号处理函数

    const int camera_index = 0;
    camera_example(camera_index);

    return 0;
}

有参形式-示例精讲

必须优先调用start_camera(const char* json_param, int8_t idx)启动摄像头。开启成功后可以看到相机启动成功的信息。

tip

json配置文件请参考附录。 在使用相关配置文件的时候请确保以下八个参数的存在:

triggerWay, width, height, fps

autoExposure, autoFocus, customWhiteBalance, autoWhiteBalanceMode

json配置文件路径自定义 例如<"/home/aidlux/aidlux_smart_vision/data/param.json">

cpp
if(start_camera(<json配置文件>, camera_idx) != 0)
{
    printf("Start camera failed.");
    return;
}

使用while循环,通过调用image_read

cpp
while (!g_stop_signal) 
{
    cv::Mat frame = Aidlux::SmartVision::image_read(camera_idx);
    
    if (frame.empty()) {
        printf("Got empty frame from camera %d", camera_idx);
        usleep(10000);
        continue;
    }
    printf("Got frame: %dx%d, size=%ld\n", 
        frame.cols, frame.rows, frame.total());
}

最后,一定记得使用使用close_camera关闭摄像头。 完成后即可完成相机操作。

tip

该实例中使用了原子变量g_stop_signal,用于控制程序退出。 退出时可以按下CTRL + C 来退出程序。 主函数中一定要注册signal(SIGINT, signal_handler);

有参形式-Python示例

python
import os
import signal
import time
import cv2
import numpy as np
from pathlib import Path

# 只导入这一行,符合你的要求
from pysmartvision import smartvisionsdk

# 全局停止标志
stop_signal = False

def signal_handler(signum, frame):
    global stop_signal
    if signum == signal.SIGINT:
        print("\n收到中断信号,正在退出...")
        stop_signal = True

def read_file_to_string(path):
    try:
        with open(path, 'r') as f:
            return f.read()
    except:
        print("Failed to open config file:", path)
        return ""

def camera_example(camera_idx=0):
    config_path = <自定义配置文件路径>   #例如 "/home/aidlux/aidlux_smart_vision/data/param.json"

    # 读取配置
    config_str = read_file_to_string(config_path)
    if not config_str:
        print("Failed to load camera config.")
        return

    # 启动摄像头
    if smartvisionsdk.start_camera(config_str, camera_idx) != 0:
        print("Start camera failed.")
        return

    print(f"Camera {camera_idx} started successfully.")
    time.sleep(0.5)  # 等待初始化

    while not stop_signal:
        raw_frame = smartvisionsdk.image_read(camera_idx)
        
        if raw_frame is None:
            continue
        
        frame = np.asarray(raw_frame)

        if frame is None or frame.size == 0:
            print(f"Got empty frame from camera {camera_idx}")
            time.sleep(0.01)
            continue

        # 打印帧信息
        h, w = frame.shape[:2]
        pixel_count = h * w
        ch = frame.shape[2] if len(frame.shape) == 3 else 1
        print(f"Got frame: {w}x{h}, total={pixel_count}, ch={ch}")

    smartvisionsdk.close_camera(camera_idx)
    print(f"Camera {camera_idx} stopped gracefully.")

def main():
    signal.signal(signal.SIGINT, signal_handler)
    camera_example(0)

if __name__ == "__main__":
    main()

有参形式-Python示例精讲

必须优先调用start_camera启动摄像头。开启成功后可以看到相机启动成功的信息。

tip

参数配置文件请参考附录。 在使用相关配置文件的时候请确保以下八个参数的存在:

triggerWay, width, height, fps

autoExposure, autoFocus, customWhiteBalance, autoWhiteBalanceMode

json配置文件路径自定义 例如<"/home/aidlux/aidlux_smart_vision/data/param.json">

然后使用while循环,通过调用image_read

python
while not stop_signal:
    frame = smartvisionsdk.image_read(camera_idx)

    if frame is None or frame.size == 0:
        print(f"Got empty frame from camera {camera_idx}")
        time.sleep(0.01)
        continue

    # 打印帧信息
    h, w = frame.shape[:2]
    pixel_count = h * w
    ch = frame.shape[2] if len(frame.shape) == 3 else 1
    print(f"Got frame: {w}x{h}, total={pixel_count}, ch={ch}")

最后,一定记得使用使用close_camera关闭摄像头。 完成后即可完成相机操作。

tip

该实例中使用了原子变量stop_signal,用于控制程序退出。 退出时可以按下CTRL + C 来退出程序。 主函数中一定要注册signal(SIGINT, signal_handler);