You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Colab中使用OpenPose处理Google Drive内的视频?

解决OpenPose处理Google Drive视频的问题

我帮你排查了几个关键问题,按以下步骤修改代码应该就能正常运行了:

1. 修复路径空格的核心问题

Google Drive挂载路径里的My Drive包含空格,直接把这种路径拼进shell命令里,会被解析成两个独立参数,导致OpenPose找不到正确的视频文件。这里有两种靠谱的解决方式:

方法A:用引号包裹路径

在拼接视频路径和输出目录时,用单引号把整个路径包起来,让shell把它当成一个完整参数:

pipe = subprocess.call(f"cd /content/openpose && /content/openpose/build/examples/openpose/openpose.bin --video '{InputVideo}' --write_json '{OutputDir}' --display 0 --render_pose 0 --face --hand", shell=True)

方法B:使用subprocess列表参数(更推荐,避免shell注入风险)

放弃shell=True,把命令拆成列表形式,这样不需要手动处理空格转义,安全性和稳定性都更高:

cmd = [
    "/content/openpose/build/examples/openpose/openpose.bin",
    "--video", InputVideo,
    "--write_json", OutputDir,
    "--display", "0",
    "--render_pose", "0",
    "--face",
    "--hand"
]
# 直接切换到openpose目录执行命令
pipe = subprocess.call(cmd, cwd="/content/openpose")

2. 统一输出目录的路径

你代码里OutputDir指向的是/content/output2/...,但创建目录时用的是/content/output/output_...,两个路径完全不匹配,OpenPose会因为找不到输出目录而报错。改成统一的路径结构:

# 先定义统一的输出根目录
output_root = "/content/output/"
if not os.path.exists(output_root):
    os.mkdir(output_root)

for video in os.listdir(VideoPath):
    VideoName = video[:-4]
    InputVideo = os.path.join(VideoPath, video)  # 用os.path.join拼接路径,避免手动拼写出错
    OutputDir = os.path.join(output_root, f"output_{VideoName}/")
    if not os.path.exists(OutputDir):
        os.mkdir(OutputDir)
    # 后续执行OpenPose命令...

3. 确认Google Drive的文件读取权限

挂载Drive后,部分文件可能会因为权限问题无法访问,可以先加一段代码检查:

import os
VideoPath = '/content/gdrive/My Drive/videos/'
for video in os.listdir(VideoPath):
    video_path = os.path.join(VideoPath, video)
    print(f"文件:{video},是否可读取:{os.access(video_path, os.R_OK)}")

如果显示不可读取,右键Drive里的视频文件夹,选择Share,确保权限设置为“Anyone with the link can view”(或者至少你的Colab账号有读取权限),然后重新挂载Drive即可。

完整修改后的代码

把以上修改整合后的可运行代码:

import subprocess
import os

# 替换为你的Drive视频路径
VideoPath = '/content/gdrive/My Drive/videos/'
output_root = "/content/output/"

# 创建输出根目录
if not os.path.exists(output_root):
    os.mkdir(output_root)

for video in os.listdir(VideoPath):
    # 跳过非视频文件(比如隐藏文件、临时文件)
    if not video.endswith(('.mp4', '.avi', '.mov')):
        continue
    VideoName = video[:-4]
    InputVideo = os.path.join(VideoPath, video)
    OutputDir = os.path.join(output_root, f"output_{VideoName}/")
    
    # 创建子输出目录
    if not os.path.exists(OutputDir):
        os.mkdir(OutputDir)
    
    # 使用列表形式调用OpenPose(推荐方式)
    cmd = [
        "/content/openpose/build/examples/openpose/openpose.bin",
        "--video", InputVideo,
        "--write_json", OutputDir,
        "--display", "0",
        "--render_pose", "0",
        "--face",
        "--hand"
    ]
    # 切换到openpose目录执行命令
    result = subprocess.call(cmd, cwd="/content/openpose")
    print(f"处理视频 {video} 完成,返回码:{result}")

内容的提问来源于stack exchange,提问作者brotherSoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 12:53:10