You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
8.3 KiB
189 lines
8.3 KiB
import os, sys, time, shutil, subprocess, shlex, json,oss2,redis,requests |
|
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_new_studio_rate', psid, 'texture')} -- {os.path.exists(os.path.join(config.sharedir, 'xmps_new_studio_rate', psid, 'texture'))}") |
|
if os.path.exists(os.path.join(config.sharedir, 'xmps_new_studio_rate', psid, 'texture')) == False: |
|
# #从oss上下载对齐信息 |
|
# prefix = f'xmps_new_studio_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_new_studio_rate', psid, 'mesh'), exist_ok=True) |
|
# config.oss_bucket.get_object_to_file(file.key, os.path.join(config.sharedir,'xmps_new_studio_rate', 'mesh', filename)) |
|
|
|
prefix = f'xmps_new_studio_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_new_studio_rate', psid, 'texture'), exist_ok=True) |
|
config.oss_bucket.get_object_to_file(file.key, os.path.join(config.sharedir,'xmps_new_studio_rate', 'texture', filename)) |
|
|
|
#将共享盘的对齐信息复制到工作目录,photo1 和 photo2 |
|
# shutil.copytree(os.path.join(config.sharedir, 'xmps_new_studio_rate', psid, 'mesh'), os.path.join(config.workdir, pid, 'photo1'),dirs_exist_ok=True) |
|
shutil.copytree(os.path.join(config.sharedir, 'xmps_new_studio_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_new_studio_rate', psid, 'mesh'), os.path.join(config.workdir, pid, 'photo1'),dirs_exist_ok=True) |
|
shutil.copytree(os.path.join(config.sharedir, 'xmps_new_studio_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_photo2(config.oss_bucket, config.workdir, pid) |
|
else: |
|
pass |
|
# #移除文件夹 |
|
# shutil.rmtree(os.path.join(config.workdir, pid), ignore_errors=True) |
|
# #下载照片 |
|
# libs.down_from_oss_photo2(config.oss_bucket, config.workdir, pid) |
|
|
|
#判断是否存在该目录 |
|
print(f"影棚号:{psid} , {config.oss_bucket.object_exists(f'xmps_new_studio_rate/{psid}/')}") |
|
filename = f'xmps_new_studio_rate/{psid}/{psid}.rcbox' |
|
isRate = False |
|
if not config.oss_bucket.object_exists(filename): |
|
#提示是否进行 xmps_new_studio_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")}" \ |
|
-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") |
|
|
|
alignNums = 0 |
|
#遍历 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}/new_studio_align_info/{file}', os.path.join(config.workdir, pid, 'reg', file)) |
|
else: |
|
print(f'上传文件:{file}') |
|
#上传到指定的目录 |
|
alignNums += 1 |
|
config.oss_bucket.put_object_from_file(f'objs/auto/{pid}/new_studio_align_info/jpgs/{file}', os.path.join(config.workdir, pid, 'reg', file)) |
|
|
|
#判断对齐的图片数量和photo2的数量是否一致,不一致要进行通知处理 |
|
photosNums = len(os.listdir(os.path.join(config.workdir, pid, "photo2"))) |
|
if alignNums != photosNums: |
|
notify(f'影棚号:{psid} , PID:{pid},没有对齐,照片数量:{photosNums} 对齐的图片数量:{alignNums}') |
|
|
|
#处理完成后塞入到指定的队列 |
|
r = create_redis_connection() |
|
r.rpush('ai:ai_build_mesh',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) |
|
|
|
|
|
def notify(content): |
|
|
|
if content == "": |
|
return "content 不能为空" |
|
|
|
for user_agent_id in ["18950403426","13600957301"]: |
|
data = { |
|
'userId': user_agent_id, |
|
'message': content, |
|
} |
|
headers = {'Content-Type': 'application/json'} |
|
message_send_url = "https://mp.api.suwa3d.com/api/qyNotify/sendMessage?userId="+user_agent_id+"&message="+content |
|
response = requests.post(message_send_url, data=json.dumps(data), headers=headers) |
|
|
|
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:new_studio_for_align_info') == 0: |
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+"-没有查询到任务在队列中,等待10s在查询") |
|
time.sleep(10) |
|
continue |
|
|
|
pid = r.lpop('ai:new_studio_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 |
|
|
|
|
|
|
|
|
|
|