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?