error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support
Asked Answered
H

15

47

I was working on a sign language detection project on jupyter notebook. While running the code for live detection I encountered an error as shown below:

OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-1drr4hl0\opencv\modules\highgui\src\window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

The code that caused this error is:

while True: 
    ret, frame = cap.read()
    image_np = np.array(frame)
    
    input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
    detections = detect_fn(input_tensor)
    
    num_detections = int(detections.pop('num_detections'))
    detections = {key: value[0, :num_detections].numpy()
                  for key, value in detections.items()}
    detections['num_detections'] = num_detections

    # detection_classes should be ints.
    detections['detection_classes'] = detections['detection_classes'].astype(np.int64)

    label_id_offset = 1
    image_np_with_detections = image_np.copy()

    viz_utils.visualize_boxes_and_labels_on_image_array(
                image_np_with_detections,
                detections['detection_boxes'],
                detections['detection_classes']+label_id_offset,
                detections['detection_scores'],
                category_index,
                use_normalized_coordinates=True,
                max_boxes_to_draw=5,
                min_score_thresh=.5,
                agnostic_mode=False)

    cv2.imshow('object detection',  cv2.resize(image_np_with_detections, (800, 600)))
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cap.release()
        break

NB: I installed OpenCV using using pip install.

Handwork answered 16/4, 2021 at 6:55 Comment(1)
It would be nice if the code in this question could be replaced by a minimal reproducible example. Most of this code doesn't really seem relevant for the specific issue, and the relevant imports are missing. I guess that only cv2.imshow() is the method causing this error, so only a few lines of code are necessary to demonstrate this.Alverson
K
79

I saw this solution under the comment of the first answer by Karthik Thilakan:

pip uninstall opencv-python-headless -y 

pip install opencv-python --upgrade

This worked for me in the conda environment.

Kenaz answered 13/1, 2022 at 17:28 Comment(0)
L
58

This solution seems to work for a majority of users, but not all. If you are in this case, see the proposed answer by Sachin Mohan.

I had the exact same error using yolov5, on Windows 10. Reinstalling the library by typing

pip uninstall opencv-python 

then

pip install opencv-python

worked for me.

Literary answered 22/4, 2021 at 9:19 Comment(2)
Tried as greg245 mentioned with no success. The commands suggested by Karthik Thilakan worked for meProgenitive
uninstall+install does not "rebuild the library". it reinstalls the package. the real issue is a conflict between multiple packages of opencv, likely one of them being a headless package.Proctology
O
9

I installed another GPU and finally upgraded to Tensorflow 2 this week and suddenly, the same issue arose. I finally found my mistake and why uninstalling and reinstalling opencv works for some people. The issue is stated clearly in a text file in your opencv-python dist-packages named METADATA.

It states;

There are four different packages (see options 1, 2, 3 and 4 below) and you should SELECT ONLY ONE OF THEM. Do not install multiple different packages in the same environment.

Further, the file says that;

You should always use these packages if you do not use cv2.imshow et al. or you are using some other package (such as PyQt) than OpenCV to create your GUI.

referring to

Packages for server (headless) environments ... (with) no GUI library dependencies

So, if you run;

pip list | grep opencv

and the result is more than one opencv version, you've likely found your problem. While an uninstall & reinstall of opencv might solve your problem, a more masterful solution is to simply uninstall the headless version as that is the one that does not care about GUIs, as it should be used in server environments.

Operation answered 29/8, 2021 at 17:42 Comment(1)
Hit his when I upgraded Tensorflow. pip uninstall opencv-python-headless fixed it. Ubuntu 20.04, python 3.8, Opencv 4.5.4Kucik
C
6

I had this exact same issue a few weeks back and I'd like to perhaps complement some of the answers touching the headless elephant in the room.

My complex project incorporates a few in-house subprojects by other colleagues. These tend to be developed and tested independently, so no cross-contamination occurs. However, since one of them used opencv-python and another went with opencv-python-headless, the final build installed both.

THIS IS THE PROBLEM!

Whenever I had both, a number of functions, particularly those pertaining visualisation, now failed. Worse: pip list revealed both opencv- versions installed! To make matters worse, whenever I uninstalled and installed again opencv-python (a simple --upgrade never worked, as it claimed the latest version was there and nothing needed upgrading), then it started working. We all hate witchcraft, so...

I went down the compilation rabbit hole and obviously nothing good was there to be found.

How does pip know it?

if you check into your .venv\Lib\site-packages, you'll find the following two folders:

  • opencv_python-4.5.4.60.dist-info
  • opencv_python-headless-4.5.4.60.dist-info

or whatever your version might be. These are the folders where pip gets its metadata from, but not where the actual code is. In fact, you don't do import opencv-..., but rather import cv2.

You do import cv2 in both cases! In fact, -headless is a crippled drop-in for the real thing. So, if you look up in your list, you'll find a cv2 folder. Both libraries deposit their code in this folder. As we know, when it comes to saving files, the last on the scene wins.

Order! Order!

(Ok, I miss John Bercow.)

Now, both libraries saving to the same folder, what is the order? Since they don't depend on one another, and in my case where poetry is being used to manage dependencies, alphabetical order is the default, and (drumroll) -headless comes last.

Solution

At some point, I just decided to go nuts and remove -headless altogether. I am not the CV dev in the team, so I was just grasping for straws , but... it worked! That's when I looked int the whole drop-in thing.

My colleagues were developing with a simple requirements.txt file, so when it came to gathering requirements in a nice proper pyproject.toml file, I just left the -headless option out.

Bottom line

You can't have both. Whenever you have multi-part projects, I highly advise to run through the pip list after the environment is built and check for the couple. If you find both, always remove the -headless, as it is a subset of the main one.

Achtung: check your .venv\pyvenv.cfg for a line with:

  • include-system-site-packages = true

This line means your project will be importing any libraries (other than the standard ones) from your global Python install and if you happen to have the -headless in the global environment, you're still in trouble.

Corrugate answered 20/2, 2022 at 21:52 Comment(0)
C
5

I had the same problem when I wrote a similar program, but issue was with different versions of opencv packages.

You can check them with the command:

pip list | grep opencv

My output was:

opencv-contrib-python 4.5.5.62

opencv-python 4.5.5.62

opencv-python-headless 4.5.4.60

And it turned out that opencv-python-headless must be version 4.5.4 for the program to run properly. So the solution was to change the opencv-python version to be the same as opencv-python-headless. So in that case you can run:

pip install opencv-python==4.5.4.60

worked for me.

Cloudland answered 20/2, 2022 at 3:34 Comment(0)
L
1

This solved the issue for me:

pip uninstall opencv-python
pip uninstall opencv-contrib-python

pip install opencv-contrib-python
pip install opencv-python
Linares answered 17/5, 2022 at 14:39 Comment(0)
S
1

try:

pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless

and then reinstall the OpenCV:

pip uninstall opencv-python
pip install opencv-python
Shona answered 26/1, 2023 at 18:39 Comment(0)
S
1

When I uninstalled and reinstalled OpenCV, the problem went away.

Shirleeshirleen answered 20/2, 2023 at 22:18 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Righteousness
S
0

I was trying to move a set of files to my Windows10 from Ubuntu 18.04 LTD, and running a cli for inference and the same error as mentioned in the opening post cropped up......I was checking on the versions of Open-CV and Open-CV Headless in both Ubuntu and Windows and they were exactly the same......While it was executing on Ubuntu, it threw the error in Windows......I removed Open-CV Headless and upgraded the Open-CV, and used the same set of commands and Windows started to execute the CLI for inferencing.......

Sigh answered 14/9, 2021 at 5:0 Comment(0)
I
0

This error is mostly with Pycharm Ide , I resolved it by changing the project interpreter None of the given solution in the internet worked for me.

Irreducible answered 1/10, 2021 at 18:3 Comment(0)
A
0

for streamlit cloud use opencv-python-headless but in other platforms use opencv-python

Amaranthine answered 7/10, 2022 at 13:29 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as code, citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Lac
B
0

If you are using Pycharm, just simply change the interpreter and try to run your application again. It solves my problem with this error.

Bartlet answered 31/5 at 23:40 Comment(0)
P
-1

you can also save image with single command and then open it from drive. cv2.imwrite("TestImage.jpg",img)

No need to waste time on cv2.imshow()

Peptide answered 24/6, 2022 at 14:24 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Fleuron
S
-1

install opencv-python 4.7.0.68, as opencv-python 4.7.0.72 won't work with your code.

    pip uninstall opencv-python
    pip install opencv-python==4.7.0.68
Selfsustaining answered 24/3, 2023 at 13:26 Comment(0)
M
-2

pip uninstall opencv-python-headless pip uninstall opencv-contrib-python-headless pip uninstall opencv-contrib-python pip uninstall opencv-python

pip install opencv-python pip install opencv-contrib-python

check out official docs

Management answered 21/2 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.