7

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

I tried possible methods to install it, like

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

But python3 setup.py install will fail due to coco.py and cocoeval.py containning python2 print function.

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

Panfeng Li
  • 3,321
  • 3
  • 26
  • 34

6 Answers6

11

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

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

troymyname00
  • 670
  • 1
  • 14
  • 32
  • 2
    Is this step mandatory? I just installed cocoapi without doing that `2to3` conversion first, and I didn't get any error from the installation. – avazula Aug 28 '18 at 14:03
  • @avazula If it worked for you without the conversion, that's great! What version of Python are you using? Do you use Anaconda for your environment? – troymyname00 Aug 28 '18 at 21:56
  • I'm not using any virtual environment (now I feel stupid for not doing it). My Python version is 3.5.2. I just cloned the repo & launched the setup with python3 & everything seemed fine. – avazula Aug 29 '18 at 07:11
  • You're missing one step to make it work. Before installing, you need to specify Python 3 source in Cython's setup.py, as detailed [here](https://stackoverflow.com/a/58116368/6374637). – Dean Mark Jul 12 '23 at 06:36
7

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

enter image description here

Lanka
  • 357
  • 3
  • 13
5

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"

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

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

    pip install pycocotools

Verify

Inside your project, import (for example) from pycocotools import mask as mask and then print(mask.__author__). This should print out the author's name, which is 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 Libraries of 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.


Troubleshooting:

The main source of confusion is that either you did not install the required packages before 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.6 and 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:

  1. python -V gives you python3.7 and NOT other version, and pip -V gives 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 python or pip version. You can check this using pip and pip3 to 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-essential and pip install numpy cython matplotlib


Environment: The above steps were tested on Ubuntu 18.4, python 3.6.8, pip 19.0.3.
Azim
  • 1,596
  • 18
  • 34
1

If you are struggling building pycocotools on Ubuntu 20.04 and python3.7 try this:

sudo apt-get install -y python3.7-dev
python3.7 -m pip install pycocotools>=2.0.1
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
0

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)

Reine_Ran_
  • 662
  • 7
  • 25
0

here's how i did successfully! (the reason is the gcc version)

  1. install the dependencies: cython (pip install cython), opencv (pip install opencv-python)

  2. check the gcc version by this command: gcc --version

  3. your output will be like this 'Command 'gcc' not found, but can be installed with: sudo apt install gcc '

  4. Type the below commands to install the gcc: sudo apt update

    sudo apt install build-essential

    sudo apt-get install manpages-dev

  5. now check again the gcc version(step2) if you get below output

    'gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.'

  6. now run the code for pycocotools installations:

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

  7. finally wait check if the installation is successful :

    'Successfully installed pycocotools-2.0'