0

I am using OpenCV version 4.5.5.64 in which it shows me an error saying cv2.imshow() function is not implemented. But if I upgrade the OpenCV to the latest version, the code does not run and gives me an error.

The below code is giving me this error:

line 31, in align_image
    cv2.imshow("Align Image", capture.get_image())
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1274: 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'

I have already uninstalled the opencv-python-headless package. My code:

import cv2
import numpy as np
from tkinter import *
from tkinter import messagebox
import imutils
import easyocr
import csv
from matplotlib import pyplot as plt

class ImageCapture:
    def __init__(self):
        self.cap = cv2.VideoCapture(0)
        self.ret, self.img = self.cap.read()

    def capture_image(self):
        ret, self.img = self.cap.read()

    def release_capture(self):
        self.cap.release()

    def get_image(self):
        return self.img


def align_image():
    capture = ImageCapture()

    messagebox.showinfo("Align Image", "Please align the number plate in the frame. Press any key to capture the image.")

    while True:
        cv2.imshow("Align Image", capture.get_image())
        if cv2.waitKey(1) & 0xFF != 255:
            break

    capture.capture_image()
    capture.release_capture()
    cv2.destroyAllWindows()

    cv2.imwrite("aligned_image.jpg", capture.get_image())
    process_image(capture.get_image())


def process_image(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    bfilter = cv2.bilateralFilter(gray, 11, 17, 17)  # Noise reduction
    edged = cv2.Canny(bfilter, 30, 200)  # Edge detection
    keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(keypoints)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
    location = None
    for contour in contours:
        approx = cv2.approxPolyDP(contour, 10, True)
        if len(approx) == 4:
            location = approx
            break
    mask = np.zeros(gray.shape, np.uint8)
    new_image = cv2.drawContours(mask, [location], 0, 255, -1)
    new_image = cv2.bitwise_and(img, img, mask=mask)
    (x, y) = np.where(mask == 255)
    (x1, y1) = (np.min(x), np.min(y))
    (x2, y2) = (np.max(x), np.max(y))
    cropped_image = gray[x1:x2 + 1, y1:y2 + 1]
    reader = easyocr.Reader(['en'])
    result = reader.readtext(cropped_image)
    if len(result) > 0:
        text = result[0][-2]
        save_to_csv(text)
        print("Number plate:", text)
        show_image_with_text(img, location, text)
    else:
        print("No number plate detected.")


def save_to_csv(number_plate):
    csv_file = "number_plates.csv"
    with open(csv_file, mode='a', newline='') as file:
        writer = csv.writer(file)
        writer.writerow([number_plate])
    print("Number plate saved to", csv_file)


def show_image_with_text(img, location, text):
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img, text=text, org=(location[0][0][0], location[1][0][1] + 60), fontFace=font, fontScale=1,
                color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
    cv2.rectangle(img, tuple(location[0][0]), tuple(location[2][0]), (0, 255, 0), 3)
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB) if len(img.shape) == 3 else img, cmap='gray')
    plt.axis('off')
    plt.show()


window = Tk()
button = Button(text="Capture Image", command=align_image)
button.pack()
window.mainloop()

What is the problem?

ano
  • 27
  • 2
  • 1
    You need to uninstall `opencv-python-headless` and then install `opencv-python`. The word "headless" means it does not support a GUI, so it can't display anything. – Tim Roberts Jun 25 '23 at 23:04
  • its show an error of after pip uninstalling opencv-python-headlessv- import cv2 ModuleNotFoundError: No module named 'cv2'. Even when all this module are present in my system-opencv-contrib-python 4.5.5.62 opencv-contrib-python-headless 4.5.5.64 opencv-python 4.5.5.64 – Simanta Sarkar Jun 25 '23 at 23:31
  • Did you REINSTALL `opencv-python`? Are you doing this from inside an IDE that might have its own private version of Python? – Tim Roberts Jun 25 '23 at 23:32
  • Yes, I have reinstalled opencv-python on the main windows terminal – Simanta Sarkar Jun 25 '23 at 23:39
  • How did you do that, exactly? – Tim Roberts Jun 26 '23 at 00:11
  • First, I used the command pip uninstall opencv-python-headless and pip uninstall opencv-python in the terminal. Then using the pip install opencv-python command I reinstalled the opencv-python – Simanta Sarkar Jun 26 '23 at 00:17
  • OK, but are you using an IDE? If you type `python` and then `import cv2`, does it work? Something has to be different. – Tim Roberts Jun 26 '23 at 00:26
  • If I type python in the terminal and then type import cv2 it import the cv2 in terminal as it says nothing. But when I run the code in VS code it show no module name cv2 – Simanta Sarkar Jun 26 '23 at 00:37
  • And there it is. I asked you TWICE if you were using an IDE, and you didn't answer. VSCode has its OWN private installation of Python. You can tell it to use your main one, or you can install the package through VSCode. – Tim Roberts Jun 26 '23 at 00:46
  • I have installed all the modules in the windows terminal as well as in terminal of VS code, I have done all the operating you told in VS code's terminal and still the error persist – Simanta Sarkar Jun 26 '23 at 01:18

0 Answers0