Spaces:
Runtime error
Runtime error
Fredrickkk
commited on
Commit
•
ba21bf6
1
Parent(s):
e61fc62
Upload 3 files
Browse files- app.py +123 -0
- packages.txt +1 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image as PILImage
|
3 |
+
import onnxruntime
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
import copy
|
7 |
+
import os
|
8 |
+
import time
|
9 |
+
from fonts.cv_puttext import cv2ImgAddText
|
10 |
+
|
11 |
+
from car_plate.yolov5_plate_onnx_infer import init_car_plate_detect_model, init_car_plate_rec_model, detect_plate, rec_plate
|
12 |
+
import re
|
13 |
+
|
14 |
+
import json
|
15 |
+
|
16 |
+
def cv_imread(path):
|
17 |
+
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
|
18 |
+
return img
|
19 |
+
|
20 |
+
def allFilePath(rootPath, allFileList):
|
21 |
+
fileList = os.listdir(rootPath)
|
22 |
+
for temp in fileList:
|
23 |
+
if os.path.isfile(os.path.join(rootPath, temp)):
|
24 |
+
allFileList.append(os.path.join(rootPath, temp))
|
25 |
+
else:
|
26 |
+
allFilePath(os.path.join(rootPath, temp), allFileList)
|
27 |
+
|
28 |
+
def draw_car_attribute(text, img0, n, rect):
|
29 |
+
try:
|
30 |
+
rect = [int(x) for x in rect]
|
31 |
+
labelSize = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
|
32 |
+
sp_size = labelSize[1] + round(1.6 * labelSize[0][1])
|
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 |
+
img0 = cv2.rectangle(img0, top_left, bottom_right, (255, 255, 255), cv2.FILLED)
|
36 |
+
img0 = cv2ImgAddText(img0, text, rect[0], int(rect[1] + n * sp_size), (0, 0, 0), 18)
|
37 |
+
return img0
|
38 |
+
except Exception as e:
|
39 |
+
print("Error drawing car attribute: ", e)
|
40 |
+
return img0
|
41 |
+
|
42 |
+
# Ensure this modified function is used in the draw_result function.
|
43 |
+
|
44 |
+
def draw_result(img0, result_list):
|
45 |
+
for result in result_list:
|
46 |
+
if not result:
|
47 |
+
continue
|
48 |
+
n = 0
|
49 |
+
rect = result.get('rect')
|
50 |
+
if rect and len(rect) == 4: # Ensure rect has four elements
|
51 |
+
cv2.rectangle(img0, (int(rect[0]), int(rect[1])), (int(rect[2]), int(rect[3])), (255, 0, 0), 2)
|
52 |
+
if 'plate' in result and len(result['plate']) > 0:
|
53 |
+
for plate_ in result['plate']:
|
54 |
+
plate_no = plate_.get('plate_no')
|
55 |
+
if plate_no and len(plate_no) > 0:
|
56 |
+
result_plate = "PLATE NUMBER: " + plate_no
|
57 |
+
plate_rect = plate_.get('rect')
|
58 |
+
if plate_rect and len(plate_rect) == 4: # Check if plate_rect is correctly formed
|
59 |
+
cv2.rectangle(img0, (int(plate_rect[0]), int(plate_rect[1])), (int(plate_rect[2]), int(plate_rect[3])), (0, 255, 0), 2)
|
60 |
+
img0 = draw_car_attribute(result_plate, img0, n, plate_rect)
|
61 |
+
n += 1
|
62 |
+
else:
|
63 |
+
print("Invalid 'rect' data:", rect)
|
64 |
+
return img0
|
65 |
+
|
66 |
+
class Predict:
|
67 |
+
def __init__(self):
|
68 |
+
providers = ['CPUExecutionProvider']
|
69 |
+
car_plate_detect_model_path = r"weights/best_exp3.onnx" # plate detect onnx
|
70 |
+
car_plate_rec_model_path = r"weights/plate_rec_color_0820.onnx" # plate recognition onnx
|
71 |
+
self.session_detect_plate = init_car_plate_detect_model(car_plate_detect_model_path, providers) #
|
72 |
+
self.session_rec_plate = init_car_plate_rec_model(car_plate_rec_model_path, providers) #
|
73 |
+
|
74 |
+
def predict(self, img_data):
|
75 |
+
img0 = copy.deepcopy(img_data)
|
76 |
+
result_list = []
|
77 |
+
outputs = detect_plate(img0, self.session_detect_plate, 640)
|
78 |
+
plate_list = rec_plate(outputs, img0, self.session_rec_plate)
|
79 |
+
plate_dict_list = []
|
80 |
+
for result in plate_list:
|
81 |
+
plate_dict = {}
|
82 |
+
plate_no = result['plate_no']
|
83 |
+
plate_rect = result['rect']
|
84 |
+
plate_lands = result['landmarks']
|
85 |
+
plate_dict['plate_no'] = plate_no
|
86 |
+
plate_dict['rect'] = plate_rect
|
87 |
+
plate_dict['landmarks'] = plate_lands
|
88 |
+
plate_dict_list.append(plate_dict)
|
89 |
+
if plate_dict_list:
|
90 |
+
result_list.append({'rect': plate_rect, 'plate': plate_dict_list})
|
91 |
+
return result_list
|
92 |
+
|
93 |
+
# 在使用 car_plate_demo 之前实例化 Predict 类
|
94 |
+
car_plate_predictor = Predict()
|
95 |
+
|
96 |
+
# Define a function to wrap the prediction and visualization
|
97 |
+
def car_plate_demo(input_image):
|
98 |
+
# Convert the PIL image to an OpenCV image
|
99 |
+
input_image = cv2.cvtColor(np.array(input_image), cv2.COLOR_RGB2BGR)
|
100 |
+
|
101 |
+
# Call the predictor to get the results
|
102 |
+
result_list = car_plate_predictor.predict(input_image)
|
103 |
+
|
104 |
+
# Draw the results on the image
|
105 |
+
output_image = draw_result(copy.deepcopy(input_image), result_list)
|
106 |
+
|
107 |
+
# Convert the OpenCV image back to PIL to display in Gradio
|
108 |
+
output_image_pil = PILImage.fromarray(cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB))
|
109 |
+
|
110 |
+
return output_image_pil
|
111 |
+
|
112 |
+
demo = gr.Interface(
|
113 |
+
fn=car_plate_demo,
|
114 |
+
inputs=gr.Image(label="Upload your car image"),
|
115 |
+
outputs=gr.Image(label="Processed Image"),
|
116 |
+
examples=["whitecar.jpg", "low-light-black-car.jpg"]
|
117 |
+
)
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
# Run the Gradio app
|
122 |
+
if __name__ == "__main__":
|
123 |
+
demo.launch()
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python3-opencv
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy
|
3 |
+
opencv-python-headless
|
4 |
+
Pillow
|
5 |
+
onnxruntime
|
6 |
+
opencv-python
|
7 |
+
jinja2
|
8 |
+
numpy
|
9 |
+
matplotlib
|
10 |
+
scikit-learn
|