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
I had the exact same error using yolov5, on windows 10. Reinstalling the library by typing
then
worked for me.
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;
Further, the file says that;
referring to
So, if you run;
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.
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…….
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.
Few frustration hours later, saw this solution under the comment of the first answer by Karthik Thilakan
This worked for me in the conda environment. Thanks Karthik! 🙂
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:
My output was:
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:
worked for me.
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 withopencv-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 bothopencv-
versions installed! To make matters worse, whenever I uninstalled and installed againopencv-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 doimport opencv-...
, but ratherimport 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 acv2
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 properpyproject.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.This solved the issue for me:
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()
for streamlit cloud use opencv-python-headless
but in other platforms use opencv-python
try:
and then reinstall the OpenCV:
When I uninstalled and reinstalled OpenCV, the problem went away.
install opencv-python 4.7.0.68, as opencv-python 4.7.0.72 won’t work with your code.