Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
add debug info
Browse files- oss_utils.py +10 -1
- simulation.py +47 -5
oss_utils.py
CHANGED
|
@@ -25,12 +25,21 @@ def list_oss_files(folder_path: str) -> List[str]:
|
|
| 25 |
"""列出OSS文件夹中的所有文件"""
|
| 26 |
files = []
|
| 27 |
try:
|
|
|
|
|
|
|
| 28 |
for obj in oss2.ObjectIterator(bucket, prefix=folder_path):
|
| 29 |
if not obj.key.endswith('/'): # 排除目录本身
|
| 30 |
files.append(obj.key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return sorted(files, key=lambda x: os.path.splitext(x)[0])
|
| 32 |
except Exception as e:
|
| 33 |
-
print(f"Error listing OSS files: {str(e)}")
|
|
|
|
|
|
|
| 34 |
return []
|
| 35 |
|
| 36 |
def download_oss_file(oss_path: str, local_path: str):
|
|
|
|
| 25 |
"""列出OSS文件夹中的所有文件"""
|
| 26 |
files = []
|
| 27 |
try:
|
| 28 |
+
print(f"🔍 OSS DEBUG: Listing files with prefix: '{folder_path}'")
|
| 29 |
+
file_count = 0
|
| 30 |
for obj in oss2.ObjectIterator(bucket, prefix=folder_path):
|
| 31 |
if not obj.key.endswith('/'): # 排除目录本身
|
| 32 |
files.append(obj.key)
|
| 33 |
+
file_count += 1
|
| 34 |
+
if file_count <= 5: # 只输出前5个文件用于调试
|
| 35 |
+
print(f"🔍 OSS DEBUG: Found file: {obj.key}")
|
| 36 |
+
|
| 37 |
+
print(f"🔍 OSS DEBUG: Total files found: {len(files)}")
|
| 38 |
return sorted(files, key=lambda x: os.path.splitext(x)[0])
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"❌ OSS DEBUG: Error listing OSS files: {str(e)}")
|
| 41 |
+
print(f"❌ OSS DEBUG: Exception type: {type(e).__name__}")
|
| 42 |
+
print(f"❌ OSS DEBUG: folder_path was: '{folder_path}'")
|
| 43 |
return []
|
| 44 |
|
| 45 |
def download_oss_file(oss_path: str, local_path: str):
|
simulation.py
CHANGED
|
@@ -204,22 +204,51 @@ def create_final_video_from_oss_images(result_folder: str, task_id: str, request
|
|
| 204 |
local_image_dir = os.path.join(user_dir, task_id, "final_images")
|
| 205 |
os.makedirs(local_image_dir, exist_ok=True)
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
try:
|
| 208 |
# 1. 获取OSS上的所有图片文件
|
|
|
|
| 209 |
oss_files = list_oss_files(image_folder)
|
|
|
|
|
|
|
|
|
|
| 210 |
image_files = [f for f in oss_files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
|
|
|
|
|
|
| 211 |
|
| 212 |
if not image_files:
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
# 2. 按文件名排序确保时间顺序正确
|
| 216 |
image_files.sort(key=lambda x: os.path.splitext(os.path.basename(x))[0])
|
|
|
|
| 217 |
|
| 218 |
# 3. 下载所有图片到本地
|
| 219 |
frames = []
|
| 220 |
width, height = 0, 0
|
| 221 |
|
| 222 |
-
|
|
|
|
| 223 |
try:
|
| 224 |
filename = os.path.basename(oss_path)
|
| 225 |
local_path = os.path.join(local_image_dir, filename)
|
|
@@ -230,12 +259,18 @@ def create_final_video_from_oss_images(result_folder: str, task_id: str, request
|
|
| 230 |
if frame is not None:
|
| 231 |
if width == 0: # 第一次获取图像尺寸
|
| 232 |
height, width = frame.shape[:2]
|
|
|
|
| 233 |
frames.append(frame)
|
| 234 |
|
|
|
|
|
|
|
|
|
|
| 235 |
except Exception as e:
|
| 236 |
-
print(f"Error processing {oss_path}: {e}")
|
| 237 |
continue
|
| 238 |
|
|
|
|
|
|
|
| 239 |
if not frames:
|
| 240 |
raise gr.Error("No valid images could be processed for final video")
|
| 241 |
|
|
@@ -244,6 +279,9 @@ def create_final_video_from_oss_images(result_folder: str, task_id: str, request
|
|
| 244 |
os.makedirs(final_video_dir, exist_ok=True)
|
| 245 |
final_video_path = os.path.join(final_video_dir, "final_video.mp4")
|
| 246 |
|
|
|
|
|
|
|
|
|
|
| 247 |
# 使用OpenCV创建视频
|
| 248 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 249 |
out = cv2.VideoWriter(final_video_path, fourcc, fps, (width, height))
|
|
@@ -253,11 +291,15 @@ def create_final_video_from_oss_images(result_folder: str, task_id: str, request
|
|
| 253 |
out.release()
|
| 254 |
|
| 255 |
# 5. 转换为H.264格式
|
|
|
|
| 256 |
h264_video_path = convert_to_h264(final_video_path)
|
| 257 |
|
| 258 |
-
print(f"Final video created: {h264_video_path} with {len(frames)} frames at {fps} fps")
|
| 259 |
return h264_video_path
|
| 260 |
|
| 261 |
except Exception as e:
|
| 262 |
-
print(f"Error creating final video from OSS images: {e}")
|
|
|
|
|
|
|
|
|
|
| 263 |
raise gr.Error(f"Failed to create final video: {str(e)}")
|
|
|
|
| 204 |
local_image_dir = os.path.join(user_dir, task_id, "final_images")
|
| 205 |
os.makedirs(local_image_dir, exist_ok=True)
|
| 206 |
|
| 207 |
+
# 添加调试信息
|
| 208 |
+
print(f"🔍 DEBUG: result_folder = {result_folder}")
|
| 209 |
+
print(f"🔍 DEBUG: task_id = {task_id}")
|
| 210 |
+
print(f"🔍 DEBUG: image_folder = {image_folder}")
|
| 211 |
+
print(f"🔍 DEBUG: user_dir = {user_dir}")
|
| 212 |
+
print(f"🔍 DEBUG: local_image_dir = {local_image_dir}")
|
| 213 |
+
|
| 214 |
try:
|
| 215 |
# 1. 获取OSS上的所有图片文件
|
| 216 |
+
print(f"🔍 DEBUG: Listing files in OSS folder: {image_folder}")
|
| 217 |
oss_files = list_oss_files(image_folder)
|
| 218 |
+
print(f"🔍 DEBUG: Total files found in OSS: {len(oss_files)}")
|
| 219 |
+
print(f"🔍 DEBUG: First 10 files: {oss_files[:10]}")
|
| 220 |
+
|
| 221 |
image_files = [f for f in oss_files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 222 |
+
print(f"🔍 DEBUG: Image files found: {len(image_files)}")
|
| 223 |
+
print(f"🔍 DEBUG: First 5 image files: {image_files[:5]}")
|
| 224 |
|
| 225 |
if not image_files:
|
| 226 |
+
# 如果没有找到图片,尝试直接在result_folder中查找
|
| 227 |
+
print(f"🔍 DEBUG: No images in {image_folder}, trying {result_folder}")
|
| 228 |
+
oss_files_direct = list_oss_files(result_folder)
|
| 229 |
+
print(f"🔍 DEBUG: Files in result_folder: {len(oss_files_direct)}")
|
| 230 |
+
print(f"🔍 DEBUG: Direct files: {oss_files_direct[:10]}")
|
| 231 |
+
|
| 232 |
+
# 查找所有子目录中的图片
|
| 233 |
+
all_image_files = [f for f in oss_files_direct if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 234 |
+
print(f"🔍 DEBUG: All image files in result_folder: {len(all_image_files)}")
|
| 235 |
+
|
| 236 |
+
if all_image_files:
|
| 237 |
+
image_files = all_image_files
|
| 238 |
+
print(f"🔍 DEBUG: Using image files from result_folder: {len(image_files)}")
|
| 239 |
+
else:
|
| 240 |
+
raise gr.Error("No images found in OSS for final video creation")
|
| 241 |
|
| 242 |
# 2. 按文件名排序确保时间顺序正确
|
| 243 |
image_files.sort(key=lambda x: os.path.splitext(os.path.basename(x))[0])
|
| 244 |
+
print(f"🔍 DEBUG: Sorted image files (first 5): {[os.path.basename(f) for f in image_files[:5]]}")
|
| 245 |
|
| 246 |
# 3. 下载所有图片到本地
|
| 247 |
frames = []
|
| 248 |
width, height = 0, 0
|
| 249 |
|
| 250 |
+
print(f"🔍 DEBUG: Starting to download {len(image_files)} images...")
|
| 251 |
+
for i, oss_path in enumerate(image_files):
|
| 252 |
try:
|
| 253 |
filename = os.path.basename(oss_path)
|
| 254 |
local_path = os.path.join(local_image_dir, filename)
|
|
|
|
| 259 |
if frame is not None:
|
| 260 |
if width == 0: # 第一次获取图像尺寸
|
| 261 |
height, width = frame.shape[:2]
|
| 262 |
+
print(f"🔍 DEBUG: Image dimensions: {width}x{height}")
|
| 263 |
frames.append(frame)
|
| 264 |
|
| 265 |
+
if (i + 1) % 10 == 0: # 每10张图片输出一次进度
|
| 266 |
+
print(f"🔍 DEBUG: Downloaded {i + 1}/{len(image_files)} images")
|
| 267 |
+
|
| 268 |
except Exception as e:
|
| 269 |
+
print(f"❌ Error processing {oss_path}: {e}")
|
| 270 |
continue
|
| 271 |
|
| 272 |
+
print(f"🔍 DEBUG: Successfully loaded {len(frames)} frames")
|
| 273 |
+
|
| 274 |
if not frames:
|
| 275 |
raise gr.Error("No valid images could be processed for final video")
|
| 276 |
|
|
|
|
| 279 |
os.makedirs(final_video_dir, exist_ok=True)
|
| 280 |
final_video_path = os.path.join(final_video_dir, "final_video.mp4")
|
| 281 |
|
| 282 |
+
print(f"🔍 DEBUG: Creating video at: {final_video_path}")
|
| 283 |
+
print(f"🔍 DEBUG: Video parameters: {len(frames)} frames, {fps} fps, {width}x{height}")
|
| 284 |
+
|
| 285 |
# 使用OpenCV创建视频
|
| 286 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 287 |
out = cv2.VideoWriter(final_video_path, fourcc, fps, (width, height))
|
|
|
|
| 291 |
out.release()
|
| 292 |
|
| 293 |
# 5. 转换为H.264格式
|
| 294 |
+
print(f"🔍 DEBUG: Converting to H.264...")
|
| 295 |
h264_video_path = convert_to_h264(final_video_path)
|
| 296 |
|
| 297 |
+
print(f"✅ Final video created: {h264_video_path} with {len(frames)} frames at {fps} fps")
|
| 298 |
return h264_video_path
|
| 299 |
|
| 300 |
except Exception as e:
|
| 301 |
+
print(f"❌ Error creating final video from OSS images: {e}")
|
| 302 |
+
print(f"❌ Exception type: {type(e).__name__}")
|
| 303 |
+
import traceback
|
| 304 |
+
print(f"❌ Traceback: {traceback.format_exc()}")
|
| 305 |
raise gr.Error(f"Failed to create final video: {str(e)}")
|