pythonopencvcomputer-visionimshow

OpenCV IMSHOW error when running even when provided all parameters


I am using OpenCV for my program but I am getting an error during the cv2.imshow command. My program is trying to process an MP4 video and crop the licence plates from the cars involved.

I am following this tutorial for this purpose, and am on 30:02 time.

Here is my code:

from ultralytics import YOLO
import cv2
from sort.sort import *
import string
import easyocr

mot_tracker = Sort()
results = {}

coco_model = YOLO('yolov8n.pt')
license_plate_detector = YOLO('/home/pi/Desktop/license_plate_detector.pt')

cap = cv2.VideoCapture('/home/pi/Desktop/carvid.mp4')

# Initialize the OCR reader
reader = easyocr.Reader(['en'], gpu=False)

# Mapping dictionaries for character conversion
dict_char_to_int = {'O': '0',
                    'I': '1',
                    'J': '3',
                    'A': '4',
                    'G': '6',
                    'S': '5'}

dict_int_to_char = {'0': 'O',
                    '1': 'I',
                    '3': 'J',
                    '4': 'A',
                    '6': 'G',
                    '5': 'S'}


def get_car(license_plate, vehicle_track_ids):
    x1, y1, x2, y2, score, class_id = license_plate

    foundIt = False
    for j in range(len(vehicle_track_ids)):
        xcar1, ycar1, xcar2, ycar2, car_id = vehicle_track_ids[j]

        if x1 > xcar1 and y1 > ycar1 and x2 < xcar2 and y2 < ycar2:
            car_indx = j
            foundIt = True
            break

    if foundIt:
        return vehicle_track_ids[car_indx]

    return -1, -1, -1, -1, -1


ret = True
frame_nmr = -1

vehicles = [2, 3, 5, 7]

while ret:
    frame_nmr+=1
    ret,frame = cap.read()
    if ret and frame_nmr < 10:
        pass
        
        detections = coco_model(frame)[0]
        detections_ = []
        
        for detection in detections.boxes.data.tolist():
            x1, y1, x2, y2, score, class_id = detection
            
            if int(class_id) in vehicles:
                detections_.append([x1,y1,x2,y2,score])
        
        track_ids = mot_tracker.update(np.asarray(detections_))
        
        license_plates = license_plate_detector(frame)[0]
        for license_plate in license_plates.boxes.data.tolist():
            x1, y1, x2, y2, score, class_id = license_plate

            xcar1, ycar1, xcar2, ycar2, carid = get_car(license_plate, track_ids)
            
            license_plate_crop = frame[int(y1):int(y2), int(x1): int(x2), :]
            
            license_plate_crop_gray = cv2.cvtColor(license_plate_crop, cv2.COLOR_BGR2GRAY)
            _, license_plate_crop_thresh = cv2.threshold(license_plate_crop_gray, 64, 255, cv2.THRESH_BINARY_INV)
            
            cv2.imshow('original_crop', license_plate_crop)
            cv2.imshow('threshold', license_plate_crop_thresh)
            cv2.waitKey(0)

When running this, my output shows this:

Using CPU. Note: This module is much faster with a GPU.

0: 384x640 21 cars, 1 bus, 2 trucks, 483.8ms
Speed: 7.2ms preprocess, 483.8ms inference, 1.9ms postprocess per image at shape (1, 3, 384, 640)

0: 384x640 2 license_plates, 456.4ms
Speed: 5.9ms preprocess, 456.4ms inference, 0.9ms postprocess per image at shape (1, 3, 384, 640)
Traceback (most recent call last):
  File "/home/pi/yolotest.py", line 85, in <module>
    cv2.imshow('original_crop', license_plate_crop)
cv2.error: OpenCV(4.10.0) /io/opencv/modules/highgui/src/window.cpp:1301: 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'

Please help as I don't get what this is asking me todo.


Solution

  • pip uninstall opencv-python; pip install opencv-python
    

    fixed the problem using terminal command above