我正在按照 本教程 运行 YOLO v8 来检测并自动识别视频中的车牌。我目前在视频中的 30:02 时间,他成功运行了代码。
这是我当前的代码:
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.COLOUR_BGR2GRAY)
_, license_plate_crop_thresh = cv2.threshold(license_plate_crop_gray, 64, 255, cv2.THRESH_BNARY_INV)
cv2.imshow('original_crop', license_plate_crop)
cv2.imshow('threshold', license_plate_crop_thresh)
cv2.waitKey(0)
但是,我的代码带来了以下错误:
Traceback (most recent call last):
File "/home/pi/yolotest.py", line 3, in <module>
from sort.sort import *
File "/home/pi/sort/sort.py", line 23, in <module>
matplotlib.use('TkAgg')
File "/home/pi/env/lib/python3.11/site-packages/matplotlib/__init__.py", line 1255, in use
plt.switch_backend(name)
File "/home/pi/env/lib/python3.11/site-packages/matplotlib/pyplot.py", line 415, in switch_backend
module = backend_registry.load_backend_module(newbackend)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pi/env/lib/python3.11/site-packages/matplotlib/backends/registry.py", line 323, in load_backend_module
return importlib.import_module(module_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pi/env/lib/python3.11/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module>
from . import _backend_tk
File "/home/pi/env/lib/python3.11/site-packages/matplotlib/backends/_backend_tk.py", line 16, in <module>
from PIL import Image, ImageTk
ImportError: cannot import name 'ImageTk' from 'PIL' (/usr/lib/python3/dist-packages/PIL/__init__.py)
请注意,我使用的是运行 Raspberry Pi OS 64 位的 Raspberry Pi 5 4 GB RAM。除此之外,所有系统包和库(包括下载的排序文件夹)都位于 python 虚拟环境中。请帮忙。
sudo apt-get install python3-pil python3-pil.imagetk
这适用于我在终端中的用例