Spaces:
Sleeping
Sleeping
Fredrickkk
commited on
Commit
•
e61fc62
1
Parent(s):
4ccfef8
Delete vehicle_attribute_v5.py
Browse files- vehicle_attribute_v5.py +0 -128
vehicle_attribute_v5.py
DELETED
@@ -1,128 +0,0 @@
|
|
1 |
-
import onnxruntime
|
2 |
-
import numpy as np
|
3 |
-
import cv2
|
4 |
-
import copy
|
5 |
-
import os
|
6 |
-
import time
|
7 |
-
from fonts.cv_puttext import cv2ImgAddText
|
8 |
-
|
9 |
-
from car_plate.yolov5_plate_onnx_infer import init_car_plate_detect_model, init_car_plate_rec_model, detect_plate, rec_plate
|
10 |
-
import re
|
11 |
-
|
12 |
-
import json
|
13 |
-
|
14 |
-
def cv_imread(path):
|
15 |
-
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
|
16 |
-
return img
|
17 |
-
|
18 |
-
def allFilePath(rootPath, allFileList):
|
19 |
-
fileList = os.listdir(rootPath)
|
20 |
-
for temp in fileList:
|
21 |
-
if os.path.isfile(os.path.join(rootPath, temp)):
|
22 |
-
allFileList.append(os.path.join(rootPath, temp))
|
23 |
-
else:
|
24 |
-
allFilePath(os.path.join(rootPath, temp), allFileList)
|
25 |
-
|
26 |
-
def draw_car_attribute(text, img0, n, rect):
|
27 |
-
try:
|
28 |
-
# Ensure all rectangle coordinates are integers
|
29 |
-
rect = [int(x) for x in rect]
|
30 |
-
labelSize = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
31 |
-
sp_size = labelSize[1] + round(1.6 * labelSize[0][1])
|
32 |
-
# Calculate the coordinates for the rectangle
|
33 |
-
top_left = (rect[0], int(rect[1] + n * sp_size))
|
34 |
-
bottom_right = (int(rect[0] + round(1.4 * labelSize[0][0])), rect[1] + (n + 1) * sp_size)
|
35 |
-
# Draw the rectangle and text
|
36 |
-
img0 = cv2.rectangle(img0, top_left, bottom_right, (255, 255, 255), cv2.FILLED)
|
37 |
-
img0 = cv2ImgAddText(img0, text, rect[0], int(rect[1] + n * sp_size), (0, 0, 0), 18)
|
38 |
-
return img0
|
39 |
-
except Exception as e:
|
40 |
-
print("Error drawing car attribute: ", e)
|
41 |
-
print("Text: ", text)
|
42 |
-
print("Coordinates: ", top_left, bottom_right)
|
43 |
-
return img0 # Return the image unchanged in case of an error
|
44 |
-
|
45 |
-
# Ensure this modified function is used in the draw_result function.
|
46 |
-
|
47 |
-
def draw_result(img0, result_list):
|
48 |
-
for result in result_list:
|
49 |
-
if not result:
|
50 |
-
continue
|
51 |
-
n = 0
|
52 |
-
rect = result.get('rect')
|
53 |
-
if rect and len(rect) == 4: # Ensure rect has four elements
|
54 |
-
cv2.rectangle(img0, (int(rect[0]), int(rect[1])), (int(rect[2]), int(rect[3])), (255, 0, 0), 2)
|
55 |
-
if 'plate' in result and len(result['plate']) > 0:
|
56 |
-
for plate_ in result['plate']:
|
57 |
-
plate_no = plate_.get('plate_no')
|
58 |
-
if plate_no and len(plate_no) > 0:
|
59 |
-
result_plate = "PLATE NUMBER: " + plate_no
|
60 |
-
plate_rect = plate_.get('rect')
|
61 |
-
if plate_rect and len(plate_rect) == 4: # Check if plate_rect is correctly formed
|
62 |
-
cv2.rectangle(img0, (int(plate_rect[0]), int(plate_rect[1])), (int(plate_rect[2]), int(plate_rect[3])), (0, 255, 0), 2)
|
63 |
-
img0 = draw_car_attribute(result_plate, img0, n, plate_rect)
|
64 |
-
n += 1
|
65 |
-
else:
|
66 |
-
print("Invalid 'rect' data:", rect)
|
67 |
-
return img0
|
68 |
-
|
69 |
-
class Predict:
|
70 |
-
def __init__(self):
|
71 |
-
providers = ['CPUExecutionProvider']
|
72 |
-
car_plate_detect_model_path = r"weights/best_exp3.onnx" # plate detect onnx
|
73 |
-
car_plate_rec_model_path = r"weights/plate_rec_color_0820.onnx" # plate recognition onnx
|
74 |
-
self.session_detect_plate = init_car_plate_detect_model(car_plate_detect_model_path, providers) #
|
75 |
-
self.session_rec_plate = init_car_plate_rec_model(car_plate_rec_model_path, providers) #
|
76 |
-
|
77 |
-
def predict(self, img_data):
|
78 |
-
img0 = copy.deepcopy(img_data)
|
79 |
-
result_list = []
|
80 |
-
outputs = detect_plate(img0, self.session_detect_plate, 640)
|
81 |
-
plate_list = rec_plate(outputs, img0, self.session_rec_plate)
|
82 |
-
plate_dict_list = []
|
83 |
-
for result in plate_list:
|
84 |
-
plate_dict = {}
|
85 |
-
plate_no = result['plate_no']
|
86 |
-
plate_rect = result['rect']
|
87 |
-
plate_lands = result['landmarks']
|
88 |
-
plate_dict['plate_no'] = plate_no
|
89 |
-
plate_dict['rect'] = plate_rect
|
90 |
-
plate_dict['landmarks'] = plate_lands
|
91 |
-
plate_dict_list.append(plate_dict)
|
92 |
-
if plate_dict_list:
|
93 |
-
result_list.append({'rect': plate_rect, 'plate': plate_dict_list})
|
94 |
-
return result_list
|
95 |
-
|
96 |
-
if __name__ == "__main__":
|
97 |
-
image_path = r"./canada" #
|
98 |
-
output_path = './result_new_img' #
|
99 |
-
file_list = []
|
100 |
-
allFilePath(image_path, file_list)
|
101 |
-
if not os.path.exists(output_path):
|
102 |
-
os.mkdir(output_path)
|
103 |
-
save_path = output_path
|
104 |
-
count = 0
|
105 |
-
sumTime = 0
|
106 |
-
|
107 |
-
pedestrain_predictor = Predict()
|
108 |
-
|
109 |
-
for pic_ in file_list:
|
110 |
-
if not (pic_.endswith(".jpg") or pic_.endswith(".png")):
|
111 |
-
continue
|
112 |
-
count += 1
|
113 |
-
time_begin = time.time()
|
114 |
-
img = cv_imread(pic_)
|
115 |
-
if img.shape[-1] == 4:
|
116 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
|
117 |
-
img0 = copy.deepcopy(img)
|
118 |
-
result_list = pedestrain_predictor.predict(img)
|
119 |
-
time_end = time.time()
|
120 |
-
time_gap = (time_end - time_begin)
|
121 |
-
sumTime += time_gap
|
122 |
-
print(count, pic_, time_gap)
|
123 |
-
img0 = draw_result(img0, result_list)
|
124 |
-
img_name = os.path.basename(pic_)
|
125 |
-
save_img_path = os.path.join(save_path, img_name)
|
126 |
-
cv2.imencode('.jpg', img0)[1].tofile(save_img_path)
|
127 |
-
|
128 |
-
print(f"cost time {sumTime}, {sumTime / (len(file_list)) * 1000} ms per img")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|