Python调用deepstream-pose-estimation-app遇URI无效错误求解决
解决DeepStream Pose Estimation App动态路径参数无效的问题
问题根源
DeepStream的输入参数依赖GStreamer的URI解析规则,动态拼接的路径常因以下原因触发无效URI错误:
- 路径格式不符合URI规范(如Windows下的反斜杠
\) - 相对路径未被正确识别
- 路径含空格/特殊字符未转义
- 路径实际不存在(打印看起来正确但实际路径错误)
修复方案
1. 转换为标准POSIX格式的绝对路径
使用pathlib模块将路径转换为正斜杠分隔的绝对路径,避免系统差异导致的格式问题:
from pathlib import Path import os video_path = Path("your/video/dir") / "tst.avi" # 转换为绝对路径并转为POSIX格式(正斜杠) absolute_posix_path = video_path.resolve().as_posix()
2. 显式添加file:// URI前缀
DeepStream对本地文件的URI解析需要符合GStreamer规范,显式添加前缀可避免相对路径识别问题:
if os.name == 'nt': # Windows系统需要三个斜杠:file:///C:/xxx/xxx.avi input_uri = f"file:///{absolute_posix_path}" else: # Linux/macOS用两个斜杠:file:///home/user/xxx.avi input_uri = f"file://{absolute_posix_path}"
3. 转义路径中的特殊字符/空格
如果路径包含空格或特殊字符,需要用shlex.quote()进行转义,避免命令解析错误:
import shlex escaped_uri = shlex.quote(input_uri)
4. 提前校验路径存在性
动态拼接路径可能因目录错误导致文件不存在,提前校验可排除这类问题:
if not video_path.exists(): print(f"文件不存在:{video_path}") continue
完整示例代码
import subprocess from pathlib import Path import os import shlex def batch_process_videos(video_dir): video_dir = Path(video_dir).resolve() # 遍历目录下所有AVI文件 for video_file in video_dir.glob("*.avi"): # 校验文件存在 if not video_file.exists(): print(f"跳过不存在的文件:{video_file}") continue # 生成标准URI posix_path = video_file.as_posix() if os.name == 'nt': input_uri = f"file:///{posix_path}" else: input_uri = f"file://{posix_path}" # 转义特殊字符 escaped_uri = shlex.quote(input_uri) # 构建输出JSON文件名(和视频同名) output_json = video_dir / f"{video_file.stem}.json" # 构建执行命令 cmd = f"./deepstream-pose-estimation-app --input {escaped_uri} --output {shlex.quote(output_json.as_posix())}" # 执行命令并捕获输出 print(f"开始处理:{video_file.name}") result = subprocess.run(cmd, shell=True, capture_output=True, text=True) if result.returncode == 0: print(f"处理完成:{output_json.name}") else: print(f"处理失败:{video_file.name},错误信息:{result.stderr}") if __name__ == "__main__": # 替换为你的视频目录路径 batch_process_videos("./videos")
关键说明
固定路径能正常运行的原因是:当路径为当前工作目录下的相对路径时,DeepStream会自动补全为符合要求的格式;但动态拼接的路径可能跨目录、含系统特殊格式(如Windows反斜杠),导致URI解析失败。通过上述步骤统一路径格式为GStreamer兼容的URI,即可解决问题。
内容的提问来源于stack exchange,提问作者batuman
相关产品推荐
相关产品推荐

