Spaces:
Sleeping
Sleeping
stphtan94117
commited on
Commit
·
a7faf38
1
Parent(s):
64de2f9
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,53 @@
|
|
|
|
1 |
import torch
|
2 |
-
import json
|
3 |
from PIL import Image
|
4 |
-
|
5 |
-
import gradio as gr
|
6 |
-
import numpy as np
|
7 |
-
import cv2
|
8 |
from ultralytics import YOLO
|
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 |
-
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|