|
from .extractor import CharExtractor |
|
from ultralytics import YOLO |
|
import numpy as np |
|
import cv2 |
|
|
|
|
|
class CharExtraction: |
|
def __init__(self, wPlatePath, wCharacterPath, classList, sizePlate, conf): |
|
self.wPlatePath = wPlatePath |
|
self.wCharacterPath = wCharacterPath |
|
self.classList = classList |
|
self.width, self.height = sizePlate |
|
self.conf = conf |
|
|
|
self.model_plat = YOLO(self.wPlatePath) |
|
self.model_char = YOLO(self.wCharacterPath) |
|
|
|
self.extractor = CharExtractor() |
|
|
|
def predict(self, image, conf): |
|
if conf == None: |
|
self.conf = 0.3 |
|
else: |
|
self.conf = conf |
|
|
|
charpredict = [] |
|
platePrediction = self.model_plat(image, conf=self.conf, verbose=False) |
|
platBoxes, croppedPlateImg, confident = self._get_cropped_plate(platePrediction) |
|
|
|
|
|
for croppImg in croppedPlateImg: |
|
characterPrediction = self.model_char(croppImg, conf=self.conf, verbose=False) |
|
charpredict.append(characterPrediction) |
|
|
|
|
|
characterBBOX, characterClass = self._get_bboxClass_char(charpredict, self.classList) |
|
|
|
plateNumber = self.extractor._extract_bbox(characterBBOX, characterClass) |
|
|
|
|
|
|
|
|
|
return platBoxes, plateNumber, confident |
|
|
|
def _get_cropped_plate(self, imgPlate): |
|
croppedImage = [] |
|
boxes = imgPlate[0].boxes |
|
originalImage = imgPlate[0].orig_img |
|
confid = boxes.conf.numpy() |
|
h,w,c = originalImage.shape |
|
if len(boxes) != 0: |
|
for box in boxes.xyxy: |
|
x1,y1,x2,y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3]) |
|
croppedPlateImage = originalImage[y1:y2, x1:x2, :] |
|
croppedPlateImage = cv2.resize(croppedPlateImage, (self.width, self.height)) |
|
croppedImage.append(croppedPlateImage) |
|
else: |
|
croppedImage.append(originalImage) |
|
|
|
return boxes.xyxy, croppedImage, confid |
|
|
|
def _get_bboxClass_char(self, imgCroppedPlate, classList): |
|
boxChar, classC = [], [] |
|
for imgCropped in imgCroppedPlate: |
|
boxes = imgCropped[0].boxes |
|
if len(boxes.cls) != 0: |
|
|
|
classId = [int(x) for x in boxes.cls.numpy()] |
|
|
|
classCharacter = np.array(classList[classId]) |
|
characterBBOX = boxes.xywh |
|
boxChar.append(characterBBOX) |
|
classC.append(classCharacter) |
|
|
|
|
|
|
|
|
|
return boxChar, classC |
|
|