107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
"""
|
|
示例 1: 基础用法 - 单传感器同步处理
|
|
|
|
本示例演示 orisys 的基本使用流程:
|
|
1. 初始化传感器(视频文件或摄像头)
|
|
2. 循环获取图像并计算形变和触碰点
|
|
3. 读取各类传感器数据
|
|
4. 可视化显示
|
|
|
|
|
|
"""
|
|
import os
|
|
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
|
|
import cv2
|
|
import orisys
|
|
import numpy as np
|
|
import time
|
|
def main():
|
|
|
|
# 步骤1:创建传感器对象 (必要步骤),config_name为配置文件名(内置配置文件名)或路径(自定义配置文件路径)
|
|
# 方式1: 从视频文件读取
|
|
sensor1 = orisys.Sensor("s1.mp4", config_name="./config/prototype_of1.json", cal_path= "./config/cal.npy", isstitch=True, cuda=True, verbose=True)
|
|
sensor2 = orisys.Sensor("s1.mp4", config_name="./config/prototype_of2.json", cal_path= "./config/cal.npy", isstitch=True, cuda=True, verbose=True)
|
|
|
|
|
|
# 方式2: 从摄像头读取(取消注释使用)
|
|
# sensor = orisys.Sensor(0, config_name="./config/lens_test.json", cal_path= "./config/cal.npy", isstitch=True, cuda=True, verbose=True)
|
|
|
|
print("\n按 'q' 退出程序,按 'r' 重置光流追踪器")
|
|
|
|
|
|
while True:
|
|
# 步骤2:信息提取
|
|
# 步骤 2.1: 获取并拼接图像 (必要步骤)
|
|
sensor1.get_img()
|
|
sensor2.get_img()
|
|
# if sensor.detect_motion(threshold=2):
|
|
# print("motion detected")
|
|
# 步骤 2.2: 计算形变场 (必要步骤)
|
|
sensor1.compute_deformation()
|
|
sensor2.compute_deformation()
|
|
|
|
|
|
#步骤 2.4: 读取信息
|
|
fps, img, flow = sensor1.read_info(
|
|
sensor1.info.FPS,
|
|
sensor1.info.IMG,
|
|
sensor1.info.VRAW,
|
|
)
|
|
fps2, img2, flow2 = sensor2.read_info(
|
|
sensor2.info.FPS,
|
|
sensor2.info.IMG,
|
|
sensor2.info.VRAW,
|
|
)
|
|
|
|
# flow_mag = np.linalg.norm(flow, axis=2)
|
|
# flow_mag2 = np.linalg.norm(flow2, axis=2)
|
|
# flow_diff = np.linalg.norm(flow - flow2, axis=2)
|
|
|
|
# print(f"flow_mag: {flow_mag.mean()}, {flow_mag.min()}, flow_mag2: {flow_mag2.mean()}, {flow_mag2.min()}, flow_diff: {flow_diff.mean()}, {flow_diff.min()}")
|
|
# flow_diff_norm = cv2.applyColorMap((flow_diff*255).astype(np.uint8), cv2.COLORMAP_JET)
|
|
# cv2.imshow("flow_diff", flow_diff_norm)
|
|
|
|
#步骤 3: 可视化显示
|
|
# 步骤 3.1: 绘制形变场
|
|
arrows = orisys.util.draw_arrows(
|
|
img,
|
|
flow,
|
|
threshold=5,
|
|
grid_spacing=10,
|
|
arrow_scale=1.0
|
|
)
|
|
arrows2 = orisys.util.draw_arrows(
|
|
img2,
|
|
flow2,
|
|
threshold=5,
|
|
grid_spacing=10,
|
|
arrow_scale=1.0
|
|
)
|
|
cv2.imshow("形变场 Deformation", arrows)
|
|
cv2.imshow("形变场 Deformation2", arrows2)
|
|
|
|
|
|
#步骤 3.4: 打印资料
|
|
print(f"fps: {fps}, fps2: {fps2}")
|
|
|
|
#步骤 3.5: 键盘控制
|
|
key = cv2.waitKey(1) & 0xFF
|
|
if key == ord("q"):
|
|
print("\n退出程序...")
|
|
break
|
|
if key == ord("r"):
|
|
# 重置光流追踪器(当追踪漂移时使用)
|
|
sensor1.reset()
|
|
sensor2.reset()
|
|
print("传感器已重置")
|
|
|
|
# =========================================================================
|
|
# 4. 清理资源
|
|
# =========================================================================
|
|
sensor1.disconnect() # 释放视频源和相关资源
|
|
sensor2.disconnect() # 释放视频源和相关资源
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|