Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 922
Karthik Thilakan Avatar asked Apr 16 '21 06:04

Karthik Thilakan


People also ask

What is cv2 namedWindow?

Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen.

Where should I install OpenCV?

You can install OpenCV anywhere on the system. The default location is C: . Finally, the installer will ask you for confirmation to install OpenCV on the system. Click on Install to continue.


5 Answers

I had the exact same error using yolov5, on windows 10. Rebuilding the library by typing

pip uninstall opencv-python 

then

pip install opencv-python

worked for me.

like image 200
greg245 Avatar answered Oct 20 '22 22:10

greg245


Few frustration hours later, 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. Thanks Karthik! :)

like image 34
Sachin Mohan Avatar answered Oct 20 '22 22:10

Sachin Mohan


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.

like image 39
jsfa11 Avatar answered Oct 20 '22 22:10

jsfa11


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.

like image 4
Szymon Marciniak Avatar answered Oct 20 '22 23:10

Szymon Marciniak


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.

like image 3
Ricardo Avatar answered Oct 20 '22 23:10

Ricardo