stphtan94117 commited on
Commit
a39ee44
1 Parent(s): c3dc61c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -53
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 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
- # article = "<p style='text-align: center'>TW_plate_number <a href=\"http://codh.rois.ac.jp/char-shape/\">日本古典籍くずし字データセット</a>.</p>"
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) # cache_examples=True,
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)