|
|
|
|
@ -7,7 +7,7 @@ import os
|
|
|
|
|
import struct
|
|
|
|
|
from ctypes import *
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
import Point as point
|
|
|
|
|
from Mv3dRgbdImport.Mv3dRgbdDefine import *
|
|
|
|
|
from Mv3dRgbdImport.Mv3dRgbdApi import *
|
|
|
|
|
from Mv3dRgbdImport.Mv3dRgbdDefine import DeviceType_Ethernet, DeviceType_USB, DeviceType_Ethernet_Vir, DeviceType_USB_Vir, MV3D_RGBD_FLOAT_EXPOSURETIME, \
|
|
|
|
|
@ -15,7 +15,6 @@ from Mv3dRgbdImport.Mv3dRgbdDefine import DeviceType_Ethernet, DeviceType_USB, D
|
|
|
|
|
FileType_BMP,FileType_TIFF,ImageType_Depth, ImageType_RGB8_Planar, ImageType_YUV420SP_NV12 ,ImageType_YUV420SP_NV21 , ImageType_YUV422, ImageType_Mono8
|
|
|
|
|
|
|
|
|
|
import config as configMap
|
|
|
|
|
|
|
|
|
|
# 全局变量
|
|
|
|
|
SN_MAP = {} # {sn: camera_instance}
|
|
|
|
|
|
|
|
|
|
@ -52,7 +51,6 @@ def initialize_devices():
|
|
|
|
|
SN_MAP[serial_number] = camera
|
|
|
|
|
print(f"Successfully added device {serial_number} to SN_MAP")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pic(sn):
|
|
|
|
|
camera = SN_MAP.get(sn)
|
|
|
|
|
|
|
|
|
|
@ -65,19 +63,15 @@ def pic(sn):
|
|
|
|
|
print(f"No config found for SN: {sn}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
time_on = config.get("time_on", 0) # 延时开始(毫秒)
|
|
|
|
|
time_off = config.get("time_off", 0) # 拍照总时长(毫秒)
|
|
|
|
|
time_hop = config.get("time_hop", 0) # 每次拍照间隔(毫秒)
|
|
|
|
|
|
|
|
|
|
end_time = time.time() + (time_off / 1000.0)
|
|
|
|
|
time_on = config.get("time_on", 0) # 延时开始(毫秒)
|
|
|
|
|
print(f"Delaying start by {time_on}ms...")
|
|
|
|
|
time.sleep(time_on / 1000.0) # 转成秒
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frame_count = 0
|
|
|
|
|
|
|
|
|
|
saved_tiff_files = [] # 用于存储保存的 TIFF 文件路径
|
|
|
|
|
print(f"Start continuous capturing for {time_off}ms...")
|
|
|
|
|
saved_files = {
|
|
|
|
|
"depth": [],
|
|
|
|
|
"color": [],
|
|
|
|
|
"pcd": []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 开始取流
|
|
|
|
|
ret = camera.MV3D_RGBD_Start()
|
|
|
|
|
@ -86,56 +80,67 @@ def pic(sn):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
while time.time() < end_time:
|
|
|
|
|
stFrameData = MV3D_RGBD_FRAME_DATA()
|
|
|
|
|
|
|
|
|
|
# 获取帧数据
|
|
|
|
|
ret = camera.MV3D_RGBD_FetchFrame(pointer(stFrameData), 5000)
|
|
|
|
|
if ret == MV3D_RGBD_OK:
|
|
|
|
|
frame_count += 1
|
|
|
|
|
|
|
|
|
|
for i in range(stFrameData.nImageCount):
|
|
|
|
|
image_info = stFrameData.stImageData[i]
|
|
|
|
|
|
|
|
|
|
# 保存深度图
|
|
|
|
|
if image_info.enImageType == ImageType_Depth:
|
|
|
|
|
file_name = configMap.save_path("depth","-Depth")
|
|
|
|
|
ret_save = camera.MV3D_RGBD_SaveImage(pointer(image_info), FileType_TIFF, file_name)
|
|
|
|
|
print("Saved depth image." if ret_save == MV3D_RGBD_OK else "Failed to save depth image.")
|
|
|
|
|
if ret_save == MV3D_RGBD_OK:
|
|
|
|
|
saved_tiff_files.append(file_name) # 记录保存的 TIFF 文件路径
|
|
|
|
|
# print(f"Saved depth image: {file_name}")
|
|
|
|
|
else:
|
|
|
|
|
print("Failed to save depth image.")
|
|
|
|
|
|
|
|
|
|
# 保存彩色图
|
|
|
|
|
elif image_info.enImageType in (
|
|
|
|
|
stFrameData = MV3D_RGBD_FRAME_DATA()
|
|
|
|
|
|
|
|
|
|
# 获取单帧数据
|
|
|
|
|
ret = camera.MV3D_RGBD_FetchFrame(pointer(stFrameData), 5000)
|
|
|
|
|
if ret == MV3D_RGBD_OK:
|
|
|
|
|
|
|
|
|
|
for i in range(stFrameData.nImageCount):
|
|
|
|
|
image_info = stFrameData.stImageData[i]
|
|
|
|
|
|
|
|
|
|
# 保存深度图
|
|
|
|
|
if image_info.enImageType == ImageType_Depth:
|
|
|
|
|
file_name = configMap.save_path("depth", "-Depth")
|
|
|
|
|
ret_save = camera.MV3D_RGBD_SaveImage(pointer(image_info), FileType_TIFF, file_name)
|
|
|
|
|
print("Saved depth image." if ret_save == MV3D_RGBD_OK else "Failed to save depth image.")
|
|
|
|
|
if ret_save == MV3D_RGBD_OK:
|
|
|
|
|
saved_files["depth"].append(file_name)
|
|
|
|
|
|
|
|
|
|
# 点云转换与保存
|
|
|
|
|
stPointCloudImage = MV3D_RGBD_IMAGE_DATA()
|
|
|
|
|
ret = camera.MV3D_RGBD_MapDepthToPointCloud(pointer(stFrameData.stImageData[i]), pointer(stPointCloudImage))
|
|
|
|
|
if MV3D_RGBD_OK != ret:
|
|
|
|
|
print("_MapDepthToPointCloud() Run failed...")
|
|
|
|
|
else:
|
|
|
|
|
print(
|
|
|
|
|
"_MapDepthToPointCloud() Run Succeed: framenum (%d) height(%d) width(%d) len (%d)!" % (
|
|
|
|
|
stPointCloudImage.nFrameNum,
|
|
|
|
|
stPointCloudImage.nHeight, stPointCloudImage.nWidth, stPointCloudImage.nDataLen))
|
|
|
|
|
strMode = string_at(stPointCloudImage.pData, stPointCloudImage.nDataLen)
|
|
|
|
|
sValue = struct.unpack('f' * int(stPointCloudImage.nHeight * stPointCloudImage.nWidth * 3),
|
|
|
|
|
strMode)
|
|
|
|
|
pcd_file = point.sValue_to_pcd(sValue, stPointCloudImage, sn)
|
|
|
|
|
if pcd_file:
|
|
|
|
|
saved_files["pcd"].append(pcd_file)
|
|
|
|
|
|
|
|
|
|
# 保存彩色图
|
|
|
|
|
elif image_info.enImageType in (
|
|
|
|
|
ImageType_RGB8_Planar, ImageType_YUV420SP_NV12,
|
|
|
|
|
ImageType_YUV420SP_NV21, ImageType_YUV422
|
|
|
|
|
):
|
|
|
|
|
file_name = configMap.save_path("color","-_Color")
|
|
|
|
|
ret_save = camera.MV3D_RGBD_SaveImage(pointer(image_info), FileType_BMP, file_name)
|
|
|
|
|
print("Saved color image." if ret_save == MV3D_RGBD_OK else "Failed to save color image.")
|
|
|
|
|
):
|
|
|
|
|
file_name = configMap.save_path("color", "-_Color")
|
|
|
|
|
ret_save = camera.MV3D_RGBD_SaveImage(pointer(image_info), FileType_BMP, file_name)
|
|
|
|
|
print("Saved color image." if ret_save == MV3D_RGBD_OK else "Failed to save color image.")
|
|
|
|
|
if ret_save == MV3D_RGBD_OK:
|
|
|
|
|
saved_files["color"].append(file_name)
|
|
|
|
|
|
|
|
|
|
# 保存灰度图
|
|
|
|
|
elif image_info.enImageType == ImageType_Mono8:
|
|
|
|
|
file_name = configMap.save_path("Mono","-_Mono")
|
|
|
|
|
ret_save = camera.MV3D_RGBD_SaveImage(pointer(image_info), FileType_BMP, file_name)
|
|
|
|
|
print("Saved mono image." if ret_save == MV3D_RGBD_OK else "Failed to save mono image.")
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print(f"Unknown image type: {image_info.enImageType}")
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print("Failed to fetch frame.")
|
|
|
|
|
|
|
|
|
|
time.sleep(time_hop / 1000.0) # 控制采集频率
|
|
|
|
|
else:
|
|
|
|
|
print("Failed to fetch frame.")
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
# 停止取流
|
|
|
|
|
camera.MV3D_RGBD_Stop()
|
|
|
|
|
print("Continuous capture completed.")
|
|
|
|
|
return saved_tiff_files
|
|
|
|
|
print("Single capture completed.")
|
|
|
|
|
|
|
|
|
|
# 输出结果路径
|
|
|
|
|
print("Saved files:")
|
|
|
|
|
for key, paths in saved_files.items():
|
|
|
|
|
if paths:
|
|
|
|
|
print(f" {key.upper()}:")
|
|
|
|
|
for path in paths:
|
|
|
|
|
print(f" - {path}")
|
|
|
|
|
|
|
|
|
|
return saved_files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize_devices()
|
|
|
|
|
|