代码之家  ›  专栏  ›  技术社区  ›  Alexander Sellin

了解如何使库认识到我具有所需的依赖关系

  •  1
  • Alexander Sellin  · 技术社区  · 2 年前

    我已经开始了一个项目,我需要使用一个名为MuJoCo的开源软件 Mujoco Documentation 它是一个倾向于RL(强化学习)的物理模拟软件。由于我正在开发的应用程序是创建一个人工智能,为了方便起见,我也在python中工作。

    我遇到的问题是,MuJoCo使用一个名为“mediapy”的库来可视化帧(视频)序列,而该库又使用一个称为“ffmpeg”的库。问题是mediapy无法识别“ffmpeg”安装在我的电脑(Mac、Intel)上。当我深入源代码试图找到问题时,我发现“mediapy”通过运行以下命令来检查“ffmpeg”的安装:

    import shutil
    import typing
    import os
    
    _Path = typing.Union[str, 'os.PathLike[str]']
    
    class _Config:
      ffmpeg_name_or_path: _Path = 'ffmpeg'
      show_save_dir: _Path | None = None
    
    _config = _Config()
    
    def _search_for_ffmpeg_path() -> str | None:
      """Returns a path to the ffmpeg program, or None if not found."""
      if filename := shutil.which(_config.ffmpeg_name_or_path):
        return str(filename)
      return None 
    

    然后,我导航到我所有安装的库所在的文件夹opt/anconda3/envs/myenv/lib/python3.10/site-packages,发现“ffmpeg”文件夹包含所有其他库包含的所有内容。所以它显然存在于那里。然而,当我运行上面的代码时,我会得到“None”的返回。我显然遗漏了一些显而易见的东西,但似乎找不到。

    供参考,这是提示该问题的代码:

    tippe_top = """
    <mujoco model="tippe top">
      <option integrator="RK4"/>
    
      <asset>
        <texture name="grid" type="2d" builtin="checker" rgb1=".1 .2 .3"
         rgb2=".2 .3 .4" width="300" height="300"/>
        <material name="grid" texture="grid" texrepeat="8 8" reflectance=".2"/>
      </asset>
    
      <worldbody>
        <geom size=".2 .2 .01" type="plane" material="grid"/>
        <light pos="0 0 .6"/>
        <camera name="closeup" pos="0 -.1 .07" xyaxes="1 0 0 0 1 2"/>
        <body name="top" pos="0 0 .02">
          <freejoint/>
          <geom name="ball" type="sphere" size=".02" />
          <geom name="stem" type="cylinder" pos="0 0 .02" size="0.004 .008"/>
          <geom name="ballast" type="box" size=".023 .023 0.005"  pos="0 0 -.015"
           contype="0" conaffinity="0" group="3"/>
        </body>
      </worldbody>
    
      <keyframe>
        <key name="spinning" qpos="0 0 0.02 1 0 0 0" qvel="0 0 0 0 1 200" />
      </keyframe>
    </mujoco>
    """
    model = mujoco.MjModel.from_xml_string(tippe_top)
    renderer = mujoco.Renderer(model)
    data = mujoco.MjData(model)
    
    duration = 7    # (seconds)
    framerate = 60  # (Hz)
    
    # Simulate and display video.
    frames = []
    mujoco.mj_resetDataKeyframe(model, data, 0)  # Reset the state to keyframe 0
    while data.time < duration:
      mujoco.mj_step(model, data)
      if len(frames) < data.time * framerate:
        renderer.update_scene(data, "closeup")
        pixels = renderer.render()
        frames.append(pixels)
    
    media.show_video(frames, fps=framerate)
    

    随后是生成的错误消息:

    {
        "name": "RuntimeError",
        "message": "Program 'ffmpeg' is not found; perhaps install ffmpeg using 'apt install ffmpeg'.",
        "stack": "---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    Cell In[4], line 14
         11     pixels = renderer.render()
         12     frames.append(pixels)
    ---> 14 media.show_video(frames, fps=framerate)
    
    File ~/opt/anaconda3/envs/Exjobb/lib/python3.10/site-packages/mediapy/__init__.py:1858, in show_video(images, title, **kwargs)
       1835 def show_video(
       1836     images: Iterable[_NDArray], *, title: str | None = None, **kwargs: Any
       1837 ) -> str | None:
       1838   \"\"\"Displays a video in the IPython notebook and optionally saves it to a file.
       1839 
       1840   See `show_videos`.
       (...)
       1856     html string if `return_html` is `True`.
       1857   \"\"\"
    -> 1858   return show_videos([images], [title], **kwargs)
    
    File ~/opt/anaconda3/envs/Exjobb/lib/python3.10/site-packages/mediapy/__init__.py:1940, in show_videos(videos, titles, width, height, downsample, columns, fps, bps, qp, codec, ylabel, html_class, return_html, **kwargs)
       1938   video = [resize_image(image, (h, w)) for image in video]
       1939   first_image = video[0]
    -> 1940 data = compress_video(
       1941     video, metadata=metadata, fps=fps, bps=bps, qp=qp, codec=codec
       1942 )
       1943 if title is not None and _config.show_save_dir:
       1944   suffix = _filename_suffix_from_codec(codec)
    
    File ~/opt/anaconda3/envs/Exjobb/lib/python3.10/site-packages/mediapy/__init__.py:1777, in compress_video(images, codec, **kwargs)
       1775 with tempfile.TemporaryDirectory() as directory_name:
       1776   tmp_path = pathlib.Path(directory_name) / f'file{suffix}'
    -> 1777   write_video(tmp_path, images, codec=codec, **kwargs)
       1778   return tmp_path.read_bytes()
    
    File ~/opt/anaconda3/envs/Exjobb/lib/python3.10/site-packages/mediapy/__init__.py:1747, in write_video(path, images, **kwargs)
       1745   dtype = np.dtype(np.uint16)
       1746 kwargs = {'metadata': getattr(images, 'metadata', None), **kwargs}
    -> 1747 with VideoWriter(path, shape=shape, dtype=dtype, **kwargs) as writer:
       1748   for image in images:
       1749     writer.add_image(image)
    
    File ~/opt/anaconda3/envs/Exjobb/lib/python3.10/site-packages/mediapy/__init__.py:1567, in VideoWriter.__enter__(self)
       1566 def __enter__(self) -> 'VideoWriter':
    -> 1567   ffmpeg_path = _get_ffmpeg_path()
       1568   input_pix_fmt = self._get_pix_fmt(self.dtype, self.input_format)
       1569   try:
    
    File ~/opt/anaconda3/envs/Exjobb/lib/python3.10/site-packages/mediapy/__init__.py:1167, in _get_ffmpeg_path()
       1165 path = _search_for_ffmpeg_path()
       1166 if not path:
    -> 1167   raise RuntimeError(
       1168       f\"Program '{_config.ffmpeg_name_or_path}' is not found;\"
       1169       \" perhaps install ffmpeg using 'apt install ffmpeg'.\"
       1170   )
       1171 return path
    
    RuntimeError: Program 'ffmpeg' is not found; perhaps install ffmpeg using 'apt install ffmpeg'."
    }
    

    我们非常感谢您的帮助。

    我已尝试更改“shutil.wich”命令正在搜索的文件的名称。我试过重新安装这些库,也试过使用不同的库(似乎有两个“ffmpeg-python”,版本0.2和ffmpeg(在我的目录中显示为“imageio_ffmpeg”)版本0.4.9(但在pip网站上显示为1.4))。

    1 回复  |  直到 2 年前
        1
  •  3
  •   AKX Bryan Oakley    2 年前

    moviepy 需要 ffmpeg command-line tool ,而不是 ffmpeg-python Python库或 imageio_ffmpeg Python库,所以正在查找 site-packages 是无用的。

    shutil.which() 它也不寻找库,而是寻找命令,所以在那里更改内容也没有任何好处。

    如果你有 ffmpeg 通过键入安装 ffmpeg 在您的命令行上。

    如果你有 Homebrew ,您可以使用安装该工具

    brew install ffmpeg
    

    或通过 ffmpeg.org .