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.
This error message usually occurs when you are using OpenCV, a popular computer vision library in Python. The error message indicates that the function you are trying to use is not implemented in the library and needs to be rebuilt with support for one of the specified platforms (Windows, GTK+ 2.x, or Cocoa).
Method 1: Rebuild OpenCV with GTK+ 2.x support
To fix the error «Python: how to fix error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support?» with «Rebuild OpenCV with GTK+ 2.x support», follow the steps below:
-
Download and install GTK+ 2.x from the official website.
-
Download the latest version of OpenCV source code from the official website.
-
Extract the OpenCV source code to a directory of your choice.
-
Open a command prompt and navigate to the directory where you extracted the OpenCV source code.
-
Create a new directory called «build» in the same directory as the extracted source code.
-
Navigate to the «build» directory using the command prompt.
-
Run the following command to configure OpenCV with GTK+ 2.x support:
cmake -D WITH_GTK_2_X=ON ..
-
After the configuration is complete, run the following command to build OpenCV:
-
After the build is complete, run the following command to install OpenCV:
-
Finally, update your Python environment to use the newly built OpenCV library.
Here is an example Python code snippet that uses the newly built OpenCV library:
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this code snippet, we first load an image using the cv2.imread()
function. We then convert the image to grayscale using the cv2.cvtColor()
function. Finally, we display the grayscale image using the cv2.imshow()
function and wait for a key press using the cv2.waitKey()
function before closing the window using the cv2.destroyAllWindows()
function.
Method 2: Rebuild OpenCV with Cocoa support
To fix the error «(-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support?» in Python, we can rebuild OpenCV with Cocoa support. Here are the steps to do so:
-
Install CMake: CMake is an open-source, cross-platform family of tools designed to build, test and package software. You can download and install it from the official website.
-
Download OpenCV source code: You can download the source code from the official website or clone it from GitHub using the following command:
git clone https://github.com/opencv/opencv.git
-
Create a build directory: Create a new directory inside the OpenCV folder and name it «build». This directory will be used to build the OpenCV library.
-
Configure CMake: Open the CMake GUI and set the source directory to the OpenCV folder and the build directory to the «build» folder. Click on the «Configure» button and select your compiler. Then, check the box next to «WITH_COCOA» and click on the «Generate» button.
-
Build OpenCV: Open the terminal and navigate to the «build» folder. Then, run the following command:
This will build the OpenCV library with Cocoa support. The «-j8» option specifies the number of parallel jobs to run. You can change this number based on your CPU’s number of cores.
-
Install OpenCV: Run the following command to install the OpenCV library:
This will install the library in your system’s default library directory.
After following these steps, you should be able to use OpenCV with Cocoa support in your Python code without encountering the «(-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support?» error.
Here is an example Python code that uses OpenCV with Cocoa support:
import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example, we load an image, convert it to grayscale using the cv2.cvtColor() function, and display the grayscale image using the cv2.imshow() function. The cv2.waitKey() and cv2.destroyAllWindows() functions are used to wait for a key press and close the window, respectively.
Method 3: Rebuild OpenCV with Windows support
To fix the error «Python: how to fix error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support?» with «Rebuild OpenCV with Windows support», you can follow the following steps:
-
Download the latest version of OpenCV from the official website and extract it to a folder.
-
Install CMake and add it to the system path.
-
Open CMake and set the source code path to the extracted OpenCV folder.
-
Set the build directory to a new folder, for example, «build».
-
Click «Configure» and select your Visual Studio version.
-
Make sure «WITH_WIN32UI» is checked.
-
Click «Generate» to generate the Visual Studio solution.
-
Open the generated solution in Visual Studio.
-
Set the build configuration to «Release».
-
Build the solution by clicking «Build Solution» in the «Build» menu.
-
After the build is complete, copy the «cv2.pyd» file from the «build/python/2.7/x64» folder to your Python «site-packages» folder.
-
Test the installation by importing the «cv2» module in Python and running some OpenCV code.
Here is the code example for importing the «cv2» module:
import cv2
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Grayscale Image", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
This code loads an image, converts it to grayscale, and displays the grayscale image using OpenCV.
Method 4: Use a pre-built OpenCV package with the desired platform support
To fix the error «(-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support?» in Python, you can use a pre-built OpenCV package with the desired platform support. Here are the steps to follow:
-
First, make sure you have the correct version of OpenCV installed for your platform. You can download pre-built packages from the official OpenCV website.
-
Next, import the cv2 module in your Python code:
- Then, load an image using the imread() function:
img = cv2.imread('image.jpg')
- Finally, display the image using the imshow() function:
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
These steps should fix the error and allow you to use OpenCV functions in your Python code.
The error you’re encountering indicates that the version of OpenCV you’re using was built without GUI support for window management. To resolve this issue and display the camera feed in a window, you can either rebuild OpenCV with GUI support or use an alternative library to display the camera feed. Here are two possible solutions:
Solution 1: Rebuild OpenCV with GUI Support (Recommended)
Ensure you have CMake installed on your system.
Rebuild OpenCV with GUI support using CMake. You can do this by following these steps:
Download the OpenCV source code from the official OpenCV website: https://opencv.org/releases/
Extract the downloaded source code archive to a directory.
Open a terminal or command prompt and navigate to the directory where you extracted the OpenCV source code.
Create a build directory within the OpenCV source directory and navigate to it:
Configure the build using CMake with GUI support enabled:
using :
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
After successful configuration, build and install OpenCV:
make
sudo make install
Once the installation is complete, you should have OpenCV with GUI support.
Update your Python environment to use the newly built OpenCV library. You may need to uninstall the previous OpenCV package and install the new one.
Operating environment: Windows10
Python version: 3.6
OpenCV Version: 3.4.3.18
Because an image library multi-template-matching is installed, the OpenCV error when the Python program that processes an image is run again.
cv2.error: OpenCV(4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1274: error: (-2:Unspecified e
rror) 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'
Solution:
- Open CMD, enter
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# The above command is to set the default download of the PIP download package to Tsinghua source. The download speed will speed up. If you use the PIP to download the package, you don't have to worry about the problem of slow download.
pip install opencv-contrib-python
# Download OpenCV-Contrib-Python, this package can solve the above problem
Run the program to see if it is wrong
If it is also an error, it may be version compatibility problem, because PIP defaults to download the latest version of the package, may conflict with your old version of other packages
- Open CMD, enter PIP Show OpenCV-Python, find the version number, such as:
Version:3.4.3.18
- General OpenCV-Contrib-Python corresponds to the OpenCV-Python version, so install, open CMD, enter:
pip install opencv-contrib-python==3.4.3.18
#
Similar compatibility issues can be resolved with this idea, and they will give it to how to solve other compatibility issues.
- Open URL: https://pi.org/project/opencv-python/# History, find the specified version of the OpenCV package, see what year is released
- Then, OpenCV-Python in the above URL is then changed to OpenCV-Contrib-Python, ie https:/pypi.org/project/opencv-contrib-python/# History, find OpenCV-Python in the computer Almost released OpenCV-Contrib-Python released by month
We have found that OpenCV-Python and OpenCV-Contrib-Python are the same, and the release time is almost consistent, but it may not be so clever, or you need to be old and realistic. Release time to solve Compatibility problem
Then you install the specified version of the package, use the command pip install package name == version number, this method can solve the bag compatibility problem to maximize
Issue
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’
Despite of trying many solutions that I found online I’m still getting the same error.
The code that encountered 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
Please help me!
Solution
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!
Answered By — Sachin Mohan