Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requirements: opencv-python>=4.1.2 not found and is required by YOLOv5 Jetson Nano #3073

Closed
HorusMaster opened this issue May 7, 2021 · 6 comments · Fixed by #3298 or #3495
Closed
Labels
bug Something isn't working

Comments

@HorusMaster
Copy link

HorusMaster commented May 7, 2021

🐛 Bug

I build OpenCV on jetson nano from scratch and also created a symlink to the site packages.
ln -s /usr/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so /home/jetson/.local/lib/python3.6/site-packages/

I reproduced the following example (python3 example.py):

import cv2
import torch
from PIL import Image
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
for f in ['zidane.jpg', 'bus.jpg']: # download 2 images
print(f'Downloading {f}...')
torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/releases/download/v1.0/' + f, f)
img1 = Image.open('zidane.jpg') # PIL image
img2 = cv2.imread('bus.jpg')[:, :, ::-1] # OpenCV image (BGR to RGB)
imgs = [img1, img2] # batch of images
results = model(imgs, size=640) # includes NMS
results.print()
results.save() # or .show()

Output

jetson@jetson-desktop:~$ python3 example.py
Using cache found in /home/jetson/.cache/torch/hub/ultralytics_yolov5_master
requirements: opencv-python>=4.1.2 not found and is required by YOLOv5, attempting auto-update...
/bin/sh: 1: pip: not found
Traceback (most recent call last):
File "/home/jetson/.cache/torch/hub/ultralytics_yolov5_master/utils/general.py", line 126, in check_requirements
pkg.require(r)
File "/home/jetson/.local/lib/python3.6/site-packages/pkg_resources/init.py", line 886, in require
needed = self.resolve(parse_requirements(requirements))
File "/home/jetson/.local/lib/python3.6/site-packages/pkg_resources/init.py", line 772, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'opencv-python>=4.1.2' distribution was not found and is required by the application

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "example.py", line 6, in
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
File "/home/jetson/.local/lib/python3.6/site-packages/torch/hub.py", line 339, in load
model = _load_local(repo_or_dir, model, *args, **kwargs)
File "/home/jetson/.local/lib/python3.6/site-packages/torch/hub.py", line 365, in _load_local
hub_module = import_module(MODULE_HUBCONF, hubconf_path)
File "/home/jetson/.local/lib/python3.6/site-packages/torch/hub.py", line 74, in import_module
spec.loader.exec_module(module)
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/jetson/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py", line 18, in
check_requirements(Path(file).parent / 'requirements.txt', exclude=('tensorboard', 'pycocotools', 'thop'))
File "/home/jetson/.cache/torch/hub/ultralytics_yolov5_master/utils/general.py", line 130, in check_requirements
print(subprocess.check_output(f"pip install '{r}'", shell=True).decode())
File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
**kwargs).stdout
File "/usr/lib/python3.6/subprocess.py", line 438, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'pip install 'opencv-python>=4.1.2'' returned non-zero exit status 127.

Environment

  • OS: [e.g. Ubuntu] Jetpack 4.5.0
  • GPU Tegra Jetson nano
  • Python 3.6.9
  • Ubuntu 18.04
  • Pytorch: 1.8.1
  • Torchvision: 0.9.1
  • OpenCV 4.5.1

Additional context

The system recognizes the opencv Version correctly:
jetson@jetson-desktop:~$ python3 -c "import cv2; print(cv2.version)"
4.5.1

@HorusMaster HorusMaster added the bug Something isn't working label May 7, 2021
@github-actions
Copy link
Contributor

github-actions bot commented May 7, 2021

👋 Hello @HorusMaster, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented May 9, 2021

@HorusMaster thanks for raising this bug report!

I think your seeing a similar issue to others. We have a check_requirements() function that relies on the pkg_resources package, which is responsible for the problem. It seems that pkg_resources fails to recognize properly installed dependencies on occasion. I found some followup information here:

FWIW, both pkg_resources and directly invoking setup.py install are semi-deprecated and are mostly unsupported.

The replacement for pkg_resources.distribution(...).version is importlib.metadata.version(...) (backported to pre-3.7 Python as importlib_metadata), though I doubt that will solve anyone's problem if the installation metadata is simply not present.

More context about the setup.py install deprecation here, but the short answer is that you should always prefer pip install to setup.py install, even in a package manager. A few people (particularly those involved in Arch linux packaging) have been working on minimal PEP 517 build frontend to build wheels and a minimal wheel installer, so possibly early adoption of those technologies will work better for you, @PureTryOut.
googleapis/python-api-core#27 (comment)

@glenn-jocher
Copy link
Member

glenn-jocher commented May 9, 2021

@HorusMaster can you compare the output of the three commands below and report back if your opencv-installation is recognized in each one?

python command 1

import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
print(installed_packages_list)

python command 2

from importlib import metadata
metadata.metadata('opencv-python')['Version']

bash command

pip freeze

@PureTryOut
Copy link

@glenn-jocher please consider not mentioning unrelated users when you quote people. I got a notification because of your comment mentioning me, but I have nothing to do with this issue or even project.

@glenn-jocher
Copy link
Member

@HorusMaster good news 😃! Your original issue may now be fixed ✅ in PR #3298. This PR increases robustness to failures in check_requirements() function during attempted package installs. To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload with model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

@glenn-jocher glenn-jocher linked a pull request Jun 7, 2021 that will close this issue
@glenn-jocher
Copy link
Member

@HorusMaster good news 😃! Your original issue may now be fixed ✅ in PR #3495. This PR adds opencv-python to the hubconf.py check_requirements() exclude list, allowing for 3rd party or contrib versions of OpenCV to be used. To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload with model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants