skip to Main Content

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:UsersappveyorAppDataLocalTemp1pip-req-build-1drr4hl0opencvmoduleshighguisrcwindow.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.

13

Answers


  1. Edit: 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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…….

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
  5. 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! 🙂

    Login or Signup to reply.
  6. 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.

    Login or Signup to reply.
  7. 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 .venvLibsite-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 .venvpyvenv.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.

    Login or Signup to reply.
  8. This solved the issue for me:

    pip uninstall opencv-python
    pip uninstall opencv-contrib-python
    
    pip install opencv-contrib-python
    pip install opencv-python
    
    Login or Signup to reply.
  9. 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()

    Login or Signup to reply.
  10. for streamlit cloud use opencv-python-headless
    but in other platforms use opencv-python

    Login or Signup to reply.
  11. 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
    
    Login or Signup to reply.
  12. When I uninstalled and reinstalled OpenCV, the problem went away.

    Login or Signup to reply.
  13. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search