Spaces:
Sleeping
Sleeping
add debug info
Browse filesFiles changed (2)
- oss_utils.py+10-1
- simulation.py+47-5
24 unmodified lines25262728293031323334353624 unmodified lines"""列出OSS文件夹中的所有文件"""files = []try:for obj in oss2.ObjectIterator(bucket, prefix=folder_path):if not obj.key.endswith('/'): # 排除目录本身files.append(obj.key)return sorted(files, key=lambda x: os.path.splitext(x)[0])except Exception as e:print(f"Error listing OSS files: {str(e)}")return []def download_oss_file(oss_path: str, local_path: str):24 unmodified lines25262728293031323334353637383940414243444524 unmodified lines"""列出OSS文件夹中的所有文件"""files = []try:print(f"🔍 OSS DEBUG: Listing files with prefix: '{folder_path}'")file_count = 0for obj in oss2.ObjectIterator(bucket, prefix=folder_path):if not obj.key.endswith('/'): # 排除目录本身files.append(obj.key)file_count += 1if file_count <= 5: # 只输出前5个文件用于调试print(f"🔍 OSS DEBUG: Found file: {obj.key}")print(f"🔍 OSS DEBUG: Total files found: {len(files)}")return sorted(files, key=lambda x: os.path.splitext(x)[0])except Exception as e:print(f"❌ OSS DEBUG: Error listing OSS files: {str(e)}")print(f"❌ OSS DEBUG: Exception type: {type(e).__name__}")print(f"❌ OSS DEBUG: folder_path was: '{folder_path}'")return []def download_oss_file(oss_path: str, local_path: str):
203 unmodified lines2042052062072082092102112122132142152162172182192202212222232242254 unmodified lines2302312322332342352362372382392402412 unmodified lines2442452462472482493 unmodified lines253254255256257258259260261262263203 unmodified lineslocal_image_dir = os.path.join(user_dir, task_id, "final_images")os.makedirs(local_image_dir, exist_ok=True)try:# 1. 获取OSS上的所有图片文件oss_files = list_oss_files(image_folder)image_files = [f for f in oss_files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]if not image_files:raise gr.Error("No images found in OSS for final video creation")# 2. 按文件名排序确保时间顺序正确image_files.sort(key=lambda x: os.path.splitext(os.path.basename(x))[0])# 3. 下载所有图片到本地frames = []width, height = 0, 0for oss_path in image_files:try:filename = os.path.basename(oss_path)local_path = os.path.join(local_image_dir, filename)4 unmodified linesif frame is not None:if width == 0: # 第一次获取图像尺寸height, width = frame.shape[:2]frames.append(frame)except Exception as e:print(f"Error processing {oss_path}: {e}")continueif not frames:raise gr.Error("No valid images could be processed for final video")2 unmodified linesos.makedirs(final_video_dir, exist_ok=True)final_video_path = os.path.join(final_video_dir, "final_video.mp4")# 使用OpenCV创建视频fourcc = cv2.VideoWriter_fourcc(*'mp4v')out = cv2.VideoWriter(final_video_path, fourcc, fps, (width, height))3 unmodified linesout.release()# 5. 转换为H.264格式h264_video_path = convert_to_h264(final_video_path)print(f"Final video created: {h264_video_path} with {len(frames)} frames at {fps} fps")return h264_video_pathexcept Exception as e:print(f"Error creating final video from OSS images: {e}")raise gr.Error(f"Failed to create final video: {str(e)}")203 unmodified lines2042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532544 unmodified lines2592602612622632642652662672682692702712722732742752762 unmodified lines2792802812822832842852862873 unmodified lines291292293294295296297298299300301302303304305203 unmodified lineslocal_image_dir = os.path.join(user_dir, task_id, "final_images")os.makedirs(local_image_dir, exist_ok=True)# 添加调试信息print(f"🔍 DEBUG: result_folder = {result_folder}")print(f"🔍 DEBUG: task_id = {task_id}")print(f"🔍 DEBUG: image_folder = {image_folder}")print(f"🔍 DEBUG: user_dir = {user_dir}")print(f"🔍 DEBUG: local_image_dir = {local_image_dir}")try:# 1. 获取OSS上的所有图片文件print(f"🔍 DEBUG: Listing files in OSS folder: {image_folder}")oss_files = list_oss_files(image_folder)print(f"🔍 DEBUG: Total files found in OSS: {len(oss_files)}")print(f"🔍 DEBUG: First 10 files: {oss_files[:10]}")image_files = [f for f in oss_files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]print(f"🔍 DEBUG: Image files found: {len(image_files)}")print(f"🔍 DEBUG: First 5 image files: {image_files[:5]}")if not image_files:# 如果没有找到图片,尝试直接在result_folder中查找print(f"🔍 DEBUG: No images in {image_folder}, trying {result_folder}")oss_files_direct = list_oss_files(result_folder)print(f"🔍 DEBUG: Files in result_folder: {len(oss_files_direct)}")print(f"🔍 DEBUG: Direct files: {oss_files_direct[:10]}")# 查找所有子目录中的图片all_image_files = [f for f in oss_files_direct if f.lower().endswith(('.png', '.jpg', '.jpeg'))]print(f"🔍 DEBUG: All image files in result_folder: {len(all_image_files)}")if all_image_files:image_files = all_image_filesprint(f"🔍 DEBUG: Using image files from result_folder: {len(image_files)}")else:raise gr.Error("No images found in OSS for final video creation")# 2. 按文件名排序确保时间顺序正确image_files.sort(key=lambda x: os.path.splitext(os.path.basename(x))[0])print(f"🔍 DEBUG: Sorted image files (first 5): {[os.path.basename(f) for f in image_files[:5]]}")# 3. 下载所有图片到本地frames = []width, height = 0, 0print(f"🔍 DEBUG: Starting to download {len(image_files)} images...")for i, oss_path in enumerate(image_files):try:filename = os.path.basename(oss_path)local_path = os.path.join(local_image_dir, filename)4 unmodified linesif frame is not None:if width == 0: # 第一次获取图像尺寸height, width = frame.shape[:2]print(f"🔍 DEBUG: Image dimensions: {width}x{height}")frames.append(frame)if (i + 1) % 10 == 0: # 每10张图片输出一次进度print(f"🔍 DEBUG: Downloaded {i + 1}/{len(image_files)} images")except Exception as e:print(f"❌ Error processing {oss_path}: {e}")continueprint(f"🔍 DEBUG: Successfully loaded {len(frames)} frames")if not frames:raise gr.Error("No valid images could be processed for final video")2 unmodified linesos.makedirs(final_video_dir, exist_ok=True)final_video_path = os.path.join(final_video_dir, "final_video.mp4")print(f"🔍 DEBUG: Creating video at: {final_video_path}")print(f"🔍 DEBUG: Video parameters: {len(frames)} frames, {fps} fps, {width}x{height}")# 使用OpenCV创建视频fourcc = cv2.VideoWriter_fourcc(*'mp4v')out = cv2.VideoWriter(final_video_path, fourcc, fps, (width, height))3 unmodified linesout.release()# 5. 转换为H.264格式print(f"🔍 DEBUG: Converting to H.264...")h264_video_path = convert_to_h264(final_video_path)print(f"✅ Final video created: {h264_video_path} with {len(frames)} frames at {fps} fps")return h264_video_pathexcept Exception as e:print(f"❌ Error creating final video from OSS images: {e}")print(f"❌ Exception type: {type(e).__name__}")import tracebackprint(f"❌ Traceback: {traceback.format_exc()}")raise gr.Error(f"Failed to create final video: {str(e)}")
oss_utils.py+10-1
simulation.py+47-5