如何在python3中安装COCO PythonAPI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49311195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:02:42  来源:igfitidea点击:

How to install COCO PythonAPI in python3

pythonmscoco

提问by Panfeng Li

It seems the COCO PythonAPIonly support python2. But peoples do use it in python3 environment.

似乎COCO PythonAPI只支持 python2。但是人们确实在 python3 环境中使用它。

I tried possible methods to install it, like

我尝试了可能的安装方法,例如

python3 setup.py build_ext --inplace
python3 setup.py install

But python3 setup.py installwill fail due to coco.pyand cocoeval.pycontainning python2 print function.

python3 setup.py install会因失败coco.pycocoeval.pycontainning python2打印功能。

Update: solved by updating the COCO PythonAPIproject. Leave this question for people facing the same issue.

更新:通过更新COCO PythonAPI项目解决。把这个问题留给面临同样问题的人。

回答by troymyname00

Try the following steps:

尝试以下步骤:

  1. Use git clone to clone the folder into your drive. In this case, it should be git clone https://github.com/cocodataset/cocoapi.git
  2. Use terminal to enter the directory, or open a terminal inside the directory
  3. Type in 2to3 . -w. Note that you might have to install a package to get 2to3. It is an elegant tool to convert code from Python2 to Python3; this code converts all .py files from Python2-compatible to Python3-compatible
  4. Use terminal to navigate to the setup folder
  5. Type in python3 setup.py install
  1. 使用 git clone 将文件夹克隆到您的驱动器中。在这种情况下,应该是git clone https://github.com/cocodataset/cocoapi.git
  2. 使用终端进入目录,或在目录内打开终端
  3. 输入2to3 . -w. 请注意,您可能必须安装一个软件包才能获得 2to3。它是一个优雅的工具,可以将代码从 Python2 转换为 Python3;此代码将所有 .py 文件从 Python2 兼容转换为 Python3 兼容
  4. 使用终端导航到安装文件夹
  5. 输入 python3 setup.py install

This should help you install COCO or any package intended for Python2, and run the package using Python3. Cheers!

这应该可以帮助您安装 COCO 或任何用于 Python2 的包,并使用 Python3 运行该包。干杯!

回答by Azim

Install

安装

  1. Instead of the official version (which has issues with python 3) use an alternative one. Install it on your local machine, globally(i.e., outside any virtual environment). You can do this by:

    pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

  2. Check if it is installed globally:

    pip freeze | grep "pycocotools"

  1. 而不是官方版本(它与 python 3 有问题)使用替代版本。将它安装在您的本地机器上,全局安装(即,在任何虚拟环境之外)。您可以通过以下方式执行此操作:

    pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

  2. 检查是否全局安装:

    pip freeze | grep "pycocotools"

You should see something like pycocotools==2.0.0in your output.

您应该pycocotools==2.0.0在输出中看到类似的内容。

  1. Now, inside your virtual-env (conda or whatever), first install numpyand cython(and maybe setuptoolsif it's not installed) using pip, and then:

    pip install pycocotools

  1. 现在,在您的虚拟环境(conda 或其他)中,首先使用pip安装numpycythonsetuptools如果未安装),然后:

    pip install pycocotools

Verify

核实

Inside your project, import (for example) from pycocotools import mask as maskand then print(mask.__author__). This should print out the author's name, which is tsungyi.

在您的项目中,导入(例如)from pycocotools import mask as mask然后print(mask.__author__). 这应该打印出作者的名字,即tsungyi

Where Is It?

它在哪里?

The installed package, like any other packages that are locally installed inside a virtual-env using pip, will go to External Librariesof your project, under site-packages. That means it is now part of your virtual-env and not part of your project. So, other users who may want to use your code, must repeat this installation on their virtual-env as well.

安装的包,就像使用pip本地安装在虚拟环境中的任何其他包一样,将转到项目的外部库,在site-packages 下。这意味着它现在是您的虚拟环境的一部分,而不是您项目的一部分。因此,其他可能想要使用您的代码的用户也必须在他们的 virtual-env 上重复此安装。



Troubleshooting:故障排除:

The main source of confusion is that either you did not install the required packagesbefore installing cocoapi, or you did install the required packages but for a different python version. And when you want to check if something is installed, you may check with, for instance, python3.6and see that it exists, but you are actually running all your commands with python3.7. So suppose you are using python3.7. You need to make sure that:

造成混淆的主要来源是在安装 cocoapi 之前没有安装所需的软件包,或者您确实安装了所需的软件包但安装了不同的 python 版本。当您想检查是否安装了某些东西时,您可以使用例如 python 3.6进行检查并查看它是否存在,但实际上您正在使用 python 3.7运行所有命令。所以假设您使用的是python3.7。您需要确保:

  1. python -Vgives you python3.7and NOT other version, and pip -Vgives you pip 19.2.3 from /home/<USER>/.local/lib/python3.7/site-packages/pip (python3.7), that actually matches with your default python version. If this is not the case, you can change your default python using sudo update-alternatives --config python, and following the one-step instruction.

  2. All the required packages are installed using the right pythonor pipversion. You can check this using pipand pip3to stop any differences that may cause an issue: pip freeze | grep "<SUBSTRING-NAME-OF-PACKAGE>"or pip show <PACKAGE-NAME>for more recent versions of pip.

  3. To install the required packages, after you made sure about (1), you need to run: sudo apt install python-setuptools python3.7-dev python3-wheel build-essentialand pip install numpy cython matplotlib

  1. python -V为您提供python3.7而不是其他版本,并pip -V为您提供pip 19.2.3 from /home/<USER>/.local/lib/python3.7/site-packages/pip (python3.7)实际上与您的默认 python 版本匹配的 。如果不是这种情况,您可以使用sudo update-alternatives --config python,并按照一步说明更改默认 python 。

  2. 所有必需的包都使用正确的pythonpip版本安装。您可以使用pippip3停止任何可能导致问题的差异来检查这一点: pip freeze | grep "<SUBSTRING-NAME-OF-PACKAGE>"或者pip show <PACKAGE-NAME>对于更新版本的pip

  3. 要安装所需的软件包,你肯定做对(1)后,你需要运行: sudo apt install python-setuptools python3.7-dev python3-wheel build-essentialpip install numpy cython matplotlib



Environment:环境:以上步骤均在Ubuntu 18.4Ubuntu 18.4python 3.6.8蟒蛇 3.6.8,pip 19.0.3点 19.0.3

回答by Lanka

I have completed it with a simple step

我用一个简单的步骤就完成了

pip install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"

** before that you need to install Visual C++ 2015 build tools on your path

** 在此之前,您需要在路径上安装 Visual C++ 2015 构建工具

enter image description here

在此处输入图片说明

回答by Ran Fang

There are alternative versions of the cocoapi that you can download and use too (I'm using python 3.5). Here's a solution that you might want to try out:
How to download and use object detection datasets (e.g. coco or pascal)

您也可以下载和使用 cocoapi 的其他版本(我使用的是 python 3.5)。这是您可能想要尝试的解决方案:
如何下载和使用对象检测数据集(例如 coco 或 pascal)