169 lines
7.3 KiB
Python
169 lines
7.3 KiB
Python
import os, sys, time, shutil, subprocess, shlex, json,oss2,redis
|
|
import platform
|
|
if platform.system() == 'Windows':
|
|
sys.path.append('e:\\libs\\')
|
|
else:
|
|
sys.path.append('/data/deploy/make3d/make2/libs/')
|
|
import tools_to_xmps
|
|
import config, libs, libs_db
|
|
|
|
def get_xmps(psid,pid):
|
|
#从共享盘上下载对齐信息,判断是否有
|
|
print(f"判断是否存在 {os.path.join(config.sharedir, 'xmps_rate', psid, 'texture')} -- {os.path.exists(os.path.join(config.sharedir, 'xmps_rate', psid, 'texture'))}")
|
|
if os.path.exists(os.path.join(config.sharedir, 'xmps_rate', psid, 'texture')) == False:
|
|
#从oss上下载对齐信息
|
|
prefix = f'xmps_rate/{psid}/mesh/'
|
|
filelist = oss2.ObjectIteratorV2(config.oss_bucket, prefix=prefix)
|
|
for file in filelist:
|
|
if file.key.endswith('.xmp'):
|
|
filename = file.key.split('/')[-1]
|
|
print('正在下载:', file.key)
|
|
# os.makedirs(os.path.join(config.sharedir, 'xmps_rate', psid, 'mesh'), exist_ok=True)
|
|
config.oss_bucket.get_object_to_file(file.key, os.path.join(config.sharedir,'xmps_rate', 'mesh', filename))
|
|
|
|
prefix = f'xmps_rate/{psid}/texture/'
|
|
filelist = oss2.ObjectIteratorV2(config.oss_bucket, prefix=prefix)
|
|
for file in filelist:
|
|
if file.key.endswith('.xmp'):
|
|
filename = file.key.split('/')[-1]
|
|
print('正在下载:', file.key)
|
|
# os.makedirs(os.path.join(config.sharedir, 'xmps_rate', psid, 'texture'), exist_ok=True)
|
|
config.oss_bucket.get_object_to_file(file.key, os.path.join(config.sharedir,'xmps_rate', 'texture', filename))
|
|
|
|
#将共享盘的对齐信息复制到工作目录,photo1 和 photo2
|
|
shutil.copytree(os.path.join(config.sharedir, 'xmps_rate', psid, 'mesh'), os.path.join(config.workdir, pid, 'photo1'),dirs_exist_ok=True)
|
|
shutil.copytree(os.path.join(config.sharedir, 'xmps_rate', psid, 'texture'), os.path.join(config.workdir, pid, 'photo2'),dirs_exist_ok=True)
|
|
else:
|
|
#将共享盘的对齐信息复制到工作目录,photo1 和 photo2
|
|
shutil.copytree(os.path.join(config.sharedir, 'xmps_rate', psid, 'mesh'), os.path.join(config.workdir, pid, 'photo1'),dirs_exist_ok=True)
|
|
shutil.copytree(os.path.join(config.sharedir, 'xmps_rate', psid, 'texture'), os.path.join(config.workdir, pid, 'photo2'),dirs_exist_ok=True)
|
|
|
|
print('下载xmps完成')
|
|
|
|
#获取photo2 的对齐信息
|
|
def get_photo2_align_info(pid):
|
|
pid = str(pid)
|
|
psid = libs.getPSid(pid)
|
|
|
|
#判断是否存在该目录
|
|
if os.path.exists(os.path.join(config.workdir, pid)) == False:
|
|
#下载照片
|
|
libs.down_from_oss(config.oss_bucket, config.workdir, pid)
|
|
else:
|
|
#移除文件夹
|
|
shutil.rmtree(os.path.join(config.workdir, pid), ignore_errors=True)
|
|
#下载照片
|
|
libs.down_from_oss(config.oss_bucket, config.workdir, pid)
|
|
|
|
#判断是否存在该目录
|
|
print(f"影棚号:{psid} , {config.oss_bucket.object_exists(f'xmps_rate/{psid}/')}")
|
|
filename = f'xmps_rate/{psid}/{psid}.rcbox'
|
|
isRate = False
|
|
if not config.oss_bucket.object_exists(filename):
|
|
#提示是否进行 xmps_rate 文件夹的创建
|
|
yes_or_not = input('当前该影棚没有对应的对齐坐标,是否创建?(y/n)')
|
|
if yes_or_not == 'y':
|
|
tools_to_xmps.create_rate_xmps(pid)
|
|
isRate = True
|
|
else:
|
|
sys.exit(0)
|
|
|
|
#旋转图片
|
|
if isRate == False:
|
|
print(f"旋转图片:{pid}")
|
|
tools_to_xmps.rateImages(pid)
|
|
|
|
#下载对齐信息
|
|
get_xmps(psid,pid)
|
|
|
|
#执行对齐指令
|
|
cmd = f'{config.rcbin} {config.r2["init"]} -setInstanceName {pid} \
|
|
-save "{os.path.join(config.workdir, pid, f"{pid}.rcproj")}" \
|
|
-addFolder "{os.path.join(config.workdir, pid, "photo2")}" \
|
|
-detectMarkers "D:\\make2\\config\\detectMarkers.config.xml" \
|
|
{libs.get_defineDistances(psid)} -align -align -align -align \
|
|
-exportRegistration "D://{pid}/reg/{pid}_registration.out" "D://make2/config/registration_export_config.xml" -quit'
|
|
print(cmd)
|
|
cmd = shlex.split(cmd)
|
|
res = subprocess.run(cmd)
|
|
|
|
#遍历 D://{pid}/reg/ 目录下的文件
|
|
for file in os.listdir(os.path.join(config.workdir, pid, 'reg')):
|
|
if file.endswith('.jpg'):
|
|
#记录到文本文件中
|
|
print(f"记录文件:{file}")
|
|
with open(os.path.join(config.workdir, pid, 'reg', f'{pid}_imgList.txt'), 'a+') as f:
|
|
f.write(f"{os.path.join(config.workdir, pid, 'photo2', file)}\n")
|
|
|
|
|
|
#遍历 reg 目录 上传到oss 上制定的目录
|
|
for file in os.listdir(os.path.join(config.workdir, pid, 'reg')):
|
|
if not file.endswith('.jpg'):
|
|
print(f'上传文件:{file}')
|
|
config.oss_bucket.put_object_from_file(f'objs/auto/{pid}/repeat_build/{file}', os.path.join(config.workdir, pid, 'reg', file))
|
|
else:
|
|
print(f'上传文件:{file}')
|
|
#上传到指定的目录
|
|
config.oss_bucket.put_object_from_file(f'objs/auto/{pid}/repeat_build/jpgs/{file}', os.path.join(config.workdir, pid, 'reg', file))
|
|
|
|
#处理完成后塞入到指定的队列
|
|
r = create_redis_connection()
|
|
r.rpush('ai:ai_repeat_build',pid)
|
|
|
|
|
|
def create_redis_connection():
|
|
"""创建 Redis 连接,若连接失败则重试"""
|
|
while True:
|
|
try:
|
|
r = redis.Redis(host="106.14.158.208", password="kcV2000", port=6379, db=6)
|
|
# 尝试进行一次操作,检查连接是否有效
|
|
r.ping() # ping 操作是一个简单的连接测试
|
|
print("Redis连接成功!")
|
|
return r
|
|
except ConnectionError:
|
|
print("Redis连接失败,正在重试...")
|
|
time.sleep(5)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
start = time.time()
|
|
if len(sys.argv) == 2:
|
|
pids = sys.argv[1]
|
|
for pid in pids.split(','):
|
|
get_photo2_align_info(pid)
|
|
print(f'{pid}共费时{time.time() - start}秒')
|
|
else:
|
|
r = create_redis_connection() # 使用重连函数来创建连接
|
|
while True:
|
|
try:
|
|
if r.llen('ai:ai_repeat_for_align_info') == 0:
|
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+"-没有查询到重建任务在队列中,等待10s在查询")
|
|
time.sleep(10)
|
|
continue
|
|
|
|
pid = r.lpop('ai:ai_repeat_for_align_info')
|
|
#判断是否为空
|
|
if pid is None:
|
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+"-读取的PID为空")
|
|
continue
|
|
#判断是否为数字
|
|
try:
|
|
pid = int(pid.decode('utf-8'))
|
|
except Exception as e:
|
|
print(f'pid:{pid} 不是数字')
|
|
continue
|
|
|
|
#执行任务
|
|
get_photo2_align_info(pid)
|
|
print(f'{pid}共费时{time.time() - start}秒')
|
|
continue
|
|
except Exception as e:
|
|
print(f'出现异常:{e}')
|
|
time.sleep(30)
|
|
r = create_redis_connection()
|
|
continue
|
|
|
|
|
|
|
|
|
|
|