Davida23 commited on
Commit
1dd4f80
·
1 Parent(s): e005d01

Upload 6 files

Browse files
scripts/__pycache__/bboxAnnotator.cpython-39.pyc ADDED
Binary file (878 Bytes). View file
 
scripts/__pycache__/charExtraction.cpython-39.pyc ADDED
Binary file (2.42 kB). View file
 
scripts/__pycache__/extractor.cpython-39.pyc ADDED
Binary file (2.35 kB). View file
 
scripts/bboxAnnotator.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+
4
+ class BBOXAnnotator:
5
+ def draw_bbox(self, ImgPath, bboxPlate, plateNumber):
6
+ img = ImgPath
7
+
8
+ for idc, bb in enumerate(bboxPlate):
9
+ # print('bboxp : ', bboxp)
10
+ # print('bb : ', bb)
11
+ x1, y1, x2, y2 = map(int, bb) # Ubah koordinat ke dalam integer
12
+ color = (0, 255, 0) # Warna bounding box (hijau)
13
+ thickness = 2 # Ketebalan garis bounding box
14
+
15
+ # Gambar bounding box pada gambar
16
+ cv2.rectangle(img, (x1, y1), (x2, y2), color, thickness)
17
+
18
+ # Tambahkan label nama di atas bounding box
19
+ if len(plateNumber) == len(bboxPlate):
20
+ print('plateNumber : ', plateNumber)
21
+ # print('idc : ', idc)
22
+ cv2.putText(img, plateNumber[idc], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness)
23
+
24
+ if len(plateNumber) == 0:
25
+ plateNumber = ['No Plate Detected']
26
+
27
+ return img, plateNumber
scripts/charExtraction.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from scripts.extractor import CharExtractor
2
+ from ultralytics import YOLO
3
+ import numpy as np
4
+ import cv2
5
+
6
+
7
+ class CharExtraction:
8
+ def __init__(self, wPlatePath, wCharacterPath, classList, sizePlate, conf):
9
+ self.wPlatePath = wPlatePath
10
+ self.wCharacterPath = wCharacterPath
11
+ self.classList = classList
12
+ self.width, self.height = sizePlate
13
+ self.conf = conf
14
+
15
+ self.model_plat = YOLO(self.wPlatePath)
16
+ self.model_char = YOLO(self.wCharacterPath)
17
+
18
+ self.extractor = CharExtractor()
19
+
20
+ def predict(self, image, conf):
21
+ if conf == None:
22
+ self.conf = 0.3
23
+ else:
24
+ self.conf = conf
25
+
26
+ charpredict = []
27
+ platePrediction = self.model_plat(image, conf=self.conf, verbose=False) ##Melakukan prediksi dan rectange gambar
28
+ platBoxes, croppedPlateImg, confident = self._get_cropped_plate(platePrediction)
29
+ # print('lcrop : ', len(croppedPlateImg))
30
+
31
+ for croppImg in croppedPlateImg:
32
+ characterPrediction = self.model_char(croppImg, conf=self.conf, verbose=False)
33
+ charpredict.append(characterPrediction)
34
+
35
+ # print('lchar : ', len(charpredict))
36
+ characterBBOX, characterClass = self._get_bboxClass_char(charpredict, self.classList)
37
+ # print('charBox : ', len(characterBBOX))
38
+ plateNumber = self.extractor._extract_bbox(characterBBOX, characterClass)
39
+ # print('Plate Number : ', plateNumber)
40
+
41
+ # print('plateboxes : ', platBoxes)
42
+ # print('platenum : ', plateNumber)
43
+ return platBoxes, plateNumber, confident
44
+
45
+ def _get_cropped_plate(self, imgPlate): # Get cropped plate image from whole image
46
+ croppedImage = [] ##Bisa berisi banyak bbox
47
+ boxes = imgPlate[0].boxes
48
+ originalImage = imgPlate[0].orig_img
49
+ confid = boxes.conf.numpy()
50
+ h,w,c = originalImage.shape
51
+ if len(boxes) != 0:
52
+ for box in boxes.xyxy:
53
+ x1,y1,x2,y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
54
+ croppedPlateImage = originalImage[y1:y2, x1:x2, :]
55
+ croppedPlateImage = cv2.resize(croppedPlateImage, (self.width, self.height))
56
+ croppedImage.append(croppedPlateImage)
57
+ else:
58
+ croppedImage.append(originalImage)
59
+
60
+ return boxes.xyxy, croppedImage, confid
61
+
62
+ def _get_bboxClass_char(self, imgCroppedPlate, classList):
63
+ boxChar, classC = [], []
64
+ for imgCropped in imgCroppedPlate:
65
+ boxes = imgCropped[0].boxes
66
+ if len(boxes.cls) != 0:
67
+ # print(boxes.cls)
68
+ classId = [int(x) for x in boxes.cls.numpy()]
69
+ # print('classId : ', classId)
70
+ classCharacter = np.array(classList[classId])
71
+ characterBBOX = boxes.xywh
72
+ boxChar.append(characterBBOX)
73
+ classC.append(classCharacter)
74
+ # else:
75
+ # classCharacter = []
76
+ # characterBBOX = []
77
+
78
+ return boxChar, classC
79
+
scripts/extractor.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+
5
+ class CharExtractor:
6
+ def __init__(self):
7
+ self.classes = None
8
+ self.class_char = []
9
+ self.y_class = None
10
+ self.x_class = None
11
+ self.bbox = None
12
+ self.x_bbox = None
13
+ self.y_bbox = None
14
+
15
+ def _extract_bbox(self, bboxes, classes):
16
+ plate_number = []
17
+ self.bbox = bboxes
18
+ self.classes = classes
19
+
20
+ for ix,bbox in enumerate(bboxes):
21
+ if len(bbox) != 0:
22
+ lowest_x, highest_y = self._get_xy(bbox, ix)
23
+ print('x_class : ', self.x_class)
24
+ # print('y_bbox : ', self.y_bbox)
25
+ for idx, point in enumerate(self.y_bbox):
26
+ x, y = point[0], point[1]
27
+ if y <= highest_y and x >= lowest_x and self.x_class.size > 1:
28
+ self.class_char.append(self.x_class[idx])
29
+
30
+ plate_num = ''.join(str(v) for v in self.class_char)
31
+ self.class_char.clear()
32
+
33
+ else:
34
+ plate_num = ''
35
+
36
+ plate_number.append(plate_num)
37
+
38
+ return plate_number
39
+
40
+ def _get_xy(self, bbox, ix):
41
+ y_id = bbox[:, 1].argsort()
42
+ # print('y_id : ', y_)
43
+ if len(bbox[y_id]) >= 4:
44
+ sorted_y = bbox[y_id][:-4]
45
+ self.y_bbox = sorted_y
46
+ # print('y_class : ', self.classes[ix])
47
+ # print('yid : ', y_id)
48
+ self.y_class = np.array(self.classes[ix][y_id])[:-4]
49
+ highest_y = max([x[1] for x in sorted_y])
50
+
51
+ x_id = sorted_y[:,0].argsort()
52
+ sorted_x = sorted_y[x_id]
53
+ self.x_bbox = sorted_x
54
+ # print('char : ', self.y_class)
55
+ self.x_class = self.y_class[x_id]
56
+ lowest_x = min([x[0] for x in sorted_x])
57
+
58
+ else:
59
+ sorted_y = bbox[y_id]
60
+ self.y_bbox = sorted_y
61
+ # print('y_class : ', self.classes)
62
+ # print('yid : ', y_id)
63
+ self.y_class = np.array(self.classes[ix][y_id])
64
+ highest_y = max([x[1] for x in sorted_y])
65
+
66
+ x_id = sorted_y[:,0].argsort()
67
+ sorted_x = sorted_y[x_id]
68
+ self.x_bbox = sorted_x
69
+ # print('char : ', self.y_class)
70
+ self.x_class = self.y_class
71
+ lowest_x = min([x[0] for x in sorted_x])
72
+
73
+ return lowest_x, highest_y
74
+
75
+
76
+