Spaces:
Sleeping
Sleeping
stphtan94117
commited on
Commit
•
a39ee44
1
Parent(s):
c3dc61c
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,83 @@
|
|
1 |
-
import gradio as gr
|
2 |
import torch
|
3 |
-
from PIL import Image
|
4 |
import json
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
torch.hub.
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
|
|
2 |
import json
|
3 |
+
from PIL import Image
|
4 |
+
from pathlib import Path
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
import cv2
|
8 |
+
|
9 |
+
# 載入車框權重
|
10 |
+
car_mask_weights = 'mask.pt'
|
11 |
+
car_mask_model = torch.hub.load('./yolov5', 'custom', path=car_mask_weights, source="local")
|
12 |
+
car_mask_model.conf = 0.7 # 車框信心指數
|
13 |
+
|
14 |
+
# 載入車號權重
|
15 |
+
plate_weights = 'plate.pt'
|
16 |
+
plate_model = torch.hub.load('./yolov5', 'custom', path=plate_weights, source="local")
|
17 |
+
plate_model.conf = 0.5 # 車號信心指數
|
18 |
+
plate_model.classes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35] # 過濾'-'符號
|
19 |
+
|
20 |
+
# 設定輸入和輸出資料夾
|
21 |
+
input_folder = 'input/'
|
22 |
+
output_folder = 'output/'
|
23 |
+
|
24 |
+
# 確保輸出資料夾存在
|
25 |
+
Path(output_folder).mkdir(parents=True, exist_ok=True)
|
26 |
+
|
27 |
+
# 將預測邏輯封裝成函數
|
28 |
+
def predict_license_plate(im):
|
29 |
+
# 使用車框權重進行預測
|
30 |
+
car_mask_results = car_mask_model(im, size=1280)
|
31 |
+
|
32 |
+
# 取得車框預測的座標和類別
|
33 |
+
car_mask_boxes = car_mask_results.pandas().xyxy[0]
|
34 |
+
|
35 |
+
# 對每個車框進行車號預測
|
36 |
+
plate_json = {} # 將車號依序存成json
|
37 |
+
for i, box in car_mask_boxes.iterrows():
|
38 |
+
# 切割車框圖片
|
39 |
+
x1, y1, x2, y2 = box['xmin'], box['ymin'], box['xmax'], box['ymax']
|
40 |
+
|
41 |
+
# 放寬車牌裁切範圍
|
42 |
+
expand_factor = 0.1 # 調整擴大比例
|
43 |
+
expand_width = int((x2 - x1) * expand_factor)
|
44 |
+
expand_height = int((y2 - y1) * expand_factor)
|
45 |
+
x1 -= expand_width
|
46 |
+
y1 -= expand_height
|
47 |
+
x2 += expand_width
|
48 |
+
y2 += expand_height
|
49 |
+
|
50 |
+
# 限制裁切範圍在圖片邊界內
|
51 |
+
x1 = max(x1, 0)
|
52 |
+
y1 = max(y1, 0)
|
53 |
+
x2 = min(x2, im.width)
|
54 |
+
y2 = min(y2, im.height)
|
55 |
+
|
56 |
+
car_mask_image = im.crop((x1, y1, x2, y2))
|
57 |
+
|
58 |
+
# 使用車號權重進行預測
|
59 |
+
plate_results = plate_model(car_mask_image, size=640)
|
60 |
+
|
61 |
+
# 提取車號預測的結果
|
62 |
+
plate_labels = plate_results.pandas().xyxy[0].sort_values('xmin')['name'].tolist()
|
63 |
+
|
64 |
+
# 儲存圖片和預測結果
|
65 |
+
plate_image_name = f'{Path(image_file).stem}_plate{i}.jpg'
|
66 |
+
plate_image_path = Path(output_folder) / plate_image_name
|
67 |
+
car_mask_image.save(plate_image_path) # 儲存圖片
|
68 |
+
|
69 |
+
licence_plate = ''.join(plate_labels)
|
70 |
+
plate_json[plate_image_name] = licence_plate
|
71 |
+
|
72 |
+
return [Image.fromarray(car_mask_results.ims[0]), plate_json]
|
73 |
+
|
74 |
+
# 將函數包裝成 Gradio 介面
|
75 |
+
inputs = gr.Image(type='pil', label="Original Image")
|
76 |
+
outputs = [gr.Image(type="pil", label="Output Image"),
|
77 |
+
gr.JSON(label="Output JSON")]
|
78 |
+
|
79 |
+
title = "License_Plate_Prediction"
|
80 |
+
description = "Predict license plates using YOLOv5."
|
81 |
+
examples = [Image.open('input/1.jpg'), Image.open('input/2.jpg')]
|
82 |
+
|
83 |
+
gr.Interface(predict_license_plate, inputs, outputs, title=title, description=description, examples=examples).launch(enable_queue=True)
|