stphtan94117 commited on
Commit
a7faf38
·
1 Parent(s): 64de2f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -80
app.py CHANGED
@@ -1,84 +1,53 @@
 
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
  from ultralytics import YOLO
9
 
10
- # 載入車框權重
11
- car_mask_weights = 'mask.pt'
12
- car_mask_model = torch.hub.load('./yolov5', 'custom', path=car_mask_weights, source="local")
13
- car_mask_model.conf = 0.7 # 車框信心指數
14
-
15
- # 載入車號權重
16
- plate_weights = 'plate.pt'
17
- plate_model = torch.hub.load('./yolov5', 'custom', path=plate_weights, source="local")
18
- plate_model.conf = 0.5 # 車號信心指數
19
- 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] # 過濾'-'符號
20
-
21
- # 設定輸入和輸出資料夾
22
- input_folder = 'input/'
23
- output_folder = 'output/'
24
-
25
- # 確保輸出資料夾存在
26
- Path(output_folder).mkdir(parents=True, exist_ok=True)
27
-
28
- # 將預測邏輯封裝成函數
29
- def predict_license_plate(im):
30
- # 使用車框權重進行預測
31
- car_mask_results = car_mask_model(im, size=1280)
32
-
33
- # 取得車框預測的座標和類別
34
- car_mask_boxes = car_mask_results.pandas().xyxy[0]
35
-
36
- # 對每個車框進行車號預測
37
- plate_json = {} # 將車號依序存成json
38
- for i, box in car_mask_boxes.iterrows():
39
- # 切割車框圖片
40
- x1, y1, x2, y2 = box['xmin'], box['ymin'], box['xmax'], box['ymax']
41
-
42
- # 放寬車牌裁切範圍
43
- expand_factor = 0.1 # 調整擴大比例
44
- expand_width = int((x2 - x1) * expand_factor)
45
- expand_height = int((y2 - y1) * expand_factor)
46
- x1 -= expand_width
47
- y1 -= expand_height
48
- x2 += expand_width
49
- y2 += expand_height
50
-
51
- # 限制裁切範圍在圖片邊界內
52
- x1 = max(x1, 0)
53
- y1 = max(y1, 0)
54
- x2 = min(x2, im.width)
55
- y2 = min(y2, im.height)
56
-
57
- car_mask_image = im.crop((x1, y1, x2, y2))
58
-
59
- # 使用車號權重進行預測
60
- plate_results = plate_model(car_mask_image, size=640)
61
-
62
- # 提取車號預測的結果
63
- plate_labels = plate_results.pandas().xyxy[0].sort_values('xmin')['name'].tolist()
64
-
65
- # 儲存圖片和預測結果
66
- plate_image_name = f'{Path(image_file).stem}_plate{i}.jpg'
67
- plate_image_path = Path(output_folder) / plate_image_name
68
- car_mask_image.save(plate_image_path) # 儲存圖片
69
-
70
- licence_plate = ''.join(plate_labels)
71
- plate_json[plate_image_name] = licence_plate
72
-
73
- return [Image.fromarray(car_mask_results.ims[0]), plate_json]
74
-
75
- # 將函數包裝成 Gradio 介面
76
- inputs = gr.Image(type='pil', label="Original Image")
77
- outputs = [gr.Image(type="pil", label="Output Image"),
78
- gr.JSON(label="Output JSON")]
79
-
80
- title = "License_Plate_Prediction"
81
- description = "Predict license plates using YOLOv5."
82
- examples = [Image.open('1.jpg'), Image.open('2.jpg')]
83
-
84
- gr.Interface(predict_license_plate, inputs, outputs, title=title, description=description, examples=examples).launch(enable_queue=True)
 
1
+ import gradio as gr
2
  import torch
 
3
  from PIL import Image
4
+ import json
 
 
 
5
  from ultralytics import YOLO
6
 
7
+
8
+ # Images
9
+ torch.hub.download_url_to_file(
10
+ 'https://i.imgur.com/4GmZXID.jpg', '1.jpg')
11
+ torch.hub.download_url_to_file(
12
+ 'https://i.imgur.com/ktIGRvs.jpg', '2.jpg')
13
+ torch.hub.download_url_to_file(
14
+ 'https://i.imgur.com/fSEsXoE.jpg', '3.jpg')
15
+ torch.hub.download_url_to_file(
16
+ 'https://i.imgur.com/lsVJRzd.jpg', '4.jpg')
17
+ torch.hub.download_url_to_file(
18
+ 'https://i.imgur.com/1OFmJd1.jpg', '5.jpg')
19
+ torch.hub.download_url_to_file(
20
+ 'https://i.imgur.com/GhfAWMJ.jpg', '6.jpg')
21
+
22
+ # Model
23
+ # model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update
24
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='plate.pt', source="local")
25
+
26
+
27
+ def yolo(im):
28
+ model.conf = 0.6 # NMS confidence threshold
29
+ # g = (size / max(im.size)) # gain
30
+ # im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
31
+
32
+ results = model(im, size=1280) # inference
33
+ results.render() # updates results.imgs with boxes and labels
34
+
35
+ df = results.pandas().xyxy[0].sort_values('xmin')[['name']].to_json(orient="records") # 可以把[['name']]刪除即可顯示全部
36
+ res = json.loads(df)
37
+
38
+ return [Image.fromarray(results.ims[0]), res]
39
+
40
+
41
+
42
+
43
+
44
+ inputs = gr.inputs.Image(type='pil', label="Original Image")
45
+ outputs = [gr.outputs.Image(type="pil", label="Output Image"),
46
+ gr.outputs.JSON(label="Output JSON")]
47
+
48
+ title = "TW_plate_number"
49
+ description = "TW_plate_number"
50
+
51
+
52
+ examples = [['1.jpg'], ['2.jpg'], ['3.jpg'], ['4.jpg'], ['5.jpg'], ['6.jpg']]
53
+ gr.Interface(yolo, inputs, outputs, title=title, description=description, examples=examples, theme="huggingface").launch(enable_queue=True)