ymj_data_collect/capture_only_image.py
2025-04-07 15:11:21 +08:00

259 lines
8.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -- coding: utf-8 --
import sys
import time
import os
import paramiko
from scp import SCPClient
import threading
from os import abort
from pathlib import Path
from clog import logger, set_logger_file_handler
HOST = "192.168.100.254"
PORT = 22
USERNAME = "root"
PASSWORD = "ebaina"
# CLOUD_COUNTS = [600000,800000,1000000,1500000,2000000]
CLOUD_COUNTS = [1500000]
DEFAULT_COUNT = 1500000
EXPS = []
# EXPS = [100 * 1000]
DEFAULT_EXP = int(11 * 1000)
PARENT_DIR_NAME = "宁德核电站0327-24-只拍照"
# PARENT_DIR_NAME = "test_1"
SAVE_DIR_NAME = ""
TEST_CLOUD = False
TEST_EXP = True
def ssh_execute_command(host, port, username, password, command):
ret = False
# 创建SSH客户端
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, port, username, password)
# 执行命令
stdin, stdout, stderr = client.exec_command(command)
# 实时读取标准输出
for line in iter(stdout.readline, ""):
print(line, end="") # 实时打印
sys.stdout.flush() # 刷新缓冲区,确保立即输出到终端
# 实时读取标准错误(如果有)
for line in iter(stderr.readline, ""):
print(f"ERROR: {line}", end="")
sys.stdout.flush()
# 关闭连接
client.close()
return True
def scp_get_file(host, port, username, password, remote_path, local_path):
ret = False
# 创建SSH客户端
client = paramiko.Transport((host, port))
client.connect(username=username, password=password)
# 创建SCP客户端
scp_client = SCPClient(client)
# 拉取文件
try:
scp_client.get(remote_path, local_path)
except Exception as e:
print(e)
return False
# 关闭连接
scp_client.close()
client.close()
return True
def scp_get_multi_file(host, port, username, password, remote_path, local_path):
ret = False
# 创建SSH客户端
client = paramiko.Transport((host, port))
client.connect(username=username, password=password)
# 创建SCP客户端
scp_client = SCPClient(client)
# 拉取文件
try:
for rp in remote_path:
scp_client.get(rp, local_path)
except Exception as e:
logger.error(f"拉取文件失败:{e}")
return False
# 关闭连接
scp_client.close()
client.close()
return True
def cap(exp=DEFAULT_EXP, cloud_count=DEFAULT_COUNT):
global SAVE_DIR_NAME
logger.debug(f"===================开处理============================")
# 先删除旧数据
logger.debug("删除旧数据")
ssh_execute_command(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD,
command="cd /root/app/detect-gui/ \n " +
"rm output.jpg \n"
)
logger.debug("删除旧数据完成")
logger.debug(f"曝光时间设置为:{exp}")
SAVE_DIR_NAME = f"exp_{exp}_cn_{cloud_count}"
logger.debug(f"保存路径为:{SAVE_DIR_NAME}")
# 如果文件夹不存在则创建
if not os.path.exists(Path(__file__).parent / "result" / PARENT_DIR_NAME / SAVE_DIR_NAME):
os.makedirs(Path(__file__).parent / "result" / PARENT_DIR_NAME / SAVE_DIR_NAME)
# ssh调用远程 borad_image_framework_tmp.py 进行拍摄图片雷达点云数据采集角点ROI数据生成
start_time = time.time()
command = (
"bash -l -c '"
"export LD_LIBRARY_PATH=/root/app/ss928_codec/mpp/out/lib:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64/lib:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64/lib/npu:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64/lib/svp_npu:"
"/lib/aarch64-linux-gnu:$LD_LIBRARY_PATH; "
"cd /root/app/detect-gui/ && "
f"/root/miniconda3/bin/python cap.py --exp={exp}'"
)
logger.debug(f"ssh_execute_command 命令:{command}")
ssh_execute_command(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD,
command=command,
)
end_time = time.time() # 记录结束时间
execution_time = end_time - start_time # 计算执行时间
logger.debug(f"borad_image_framework_tmp.py 程序执行时间:{execution_time}")
time.sleep(1) # 等待操作系统保存数据完毕
logger.debug("开始保存文件到本地")
start_time2 = time.time()
local_path = Path(__file__).parent / "result" / PARENT_DIR_NAME / SAVE_DIR_NAME
logger.debug(f"本地文件保存路径为:{local_path}")
scp_get_multi_file(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD,
remote_path=[
"/root/app/detect-gui/output.jpg"
],
local_path=local_path
)
end_time2 = time.time() # 记录结束时间
execution_time2 = end_time2 - start_time2
logger.debug("文件到本地结束")
logger.debug(f"scp_get_file获取文件到本地 程序执行时间:{execution_time2}")
logger.debug(f"===================开处理结束============================")
def cap_wide():
logger.debug(f"===================广角开始处理============================")
# 先删除旧数据
logger.debug("删除旧数据")
ssh_execute_command(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD,
command="cd /root/app/detect-gui/ \n " +
"rm wide_image_processed.png \n" +
"rm wide_image.png \n" +
"rm wide_image_roi.json \n"
)
logger.debug("删除旧数据完成")
start_time = time.time()
# command = "export LD_LIBRARY_PATH=/root/app/ss928_codec/mpp/out/lib:/root/app/detect-gui/vendors/image-framework/linux_arm64:/root/app/detect-gui/vendors/image-framework/linux_arm64/lib:/root/app/detect-gui/vendors/image-framework/linux_arm64/lib/npu:/root/app/detect-gui/vendors/image-framework/linux_arm64/lib/svp_npu:$LD_LIBRARY_PATH \n" + \
# "cd /root/app/detect-gui/ \n" + \
# f"/root/miniconda3/bin/python cap_wide_camera.py \n"
command = (
"bash -l -c '"
"export LD_LIBRARY_PATH=/root/app/ss928_codec/mpp/out/lib:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64/lib:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64/lib/npu:"
"/root/app/detect-gui/vendors/image-framework/linux_arm64/lib/svp_npu:"
"/lib/aarch64-linux-gnu:$LD_LIBRARY_PATH; "
"cd /root/app/detect-gui/ && "
f"/root/miniconda3/bin/python cap_wide_camera.py'"
)
logger.debug(f"ssh_execute_command 命令:{command}")
ssh_execute_command(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD,
command=command,
)
end_time = time.time() # 记录结束时间
execution_time = end_time - start_time # 计算执行时间
logger.debug(f"cap_wide_camera.py 程序执行时间:{execution_time}")
time.sleep(1) # 等待操作系统保存数据完毕
logger.debug("开始保存文件到本地")
start_time2 = time.time()
local_path = Path(__file__).parent / "result" / PARENT_DIR_NAME
logger.debug(f"本地文件保存路径为:{local_path}")
scp_get_multi_file(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD,
remote_path=[
"/root/app/detect-gui/wide_image_processed.png",
"/root/app/detect-gui/wide_image.png",
"/root/app/detect-gui/wide_image_roi.json",
],
local_path=local_path
)
end_time2 = time.time() # 记录结束时间
execution_time2 = end_time2 - start_time2
logger.debug("文件到本地结束")
logger.debug(f"scp_get_file获取文件到本地 程序执行时间:{execution_time2}")
logger.debug(f"===================广角开处理结束============================")
if __name__ == '__main__':
# 如果路径不存在则创建
if not os.path.exists(Path(__file__).parent / "result" / PARENT_DIR_NAME):
os.makedirs(Path(__file__).parent / "result" / PARENT_DIR_NAME)
# 设置日志保存路径
set_logger_file_handler(Path(__file__).parent / "result" / PARENT_DIR_NAME)
logger.debug("开始采集")
start_time = time.time() # 记录开始时间
cap()
end_time = time.time() # 记录结束时间
elapsed_time = end_time - start_time # 计算总时间
logger.success(f"采集完毕=========== 耗时: {elapsed_time:.2f}")
cap_wide()