fig_img
Browse files
app.py
CHANGED
@@ -1,7 +1,68 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
-
demo
|
7 |
-
demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
+
import sys
|
3 |
+
print(sys.version_info)
|
4 |
+
|
5 |
import gradio as gr
|
6 |
+
#from florence import model_init, draw_image
|
7 |
+
#from wikai import analyze_dial, ocr_and_od
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
from aimodel import model_init, read_meter, visualization
|
10 |
+
from test_rect import read_meter as read_meter_rect
|
11 |
+
from PIL import Image
|
12 |
+
from io import BytesIO
|
13 |
+
import logging
|
14 |
+
#logging.basicConfig(level=logging.DEBUG)
|
15 |
+
print("Loading model...")
|
16 |
+
@spaces.GPU
|
17 |
+
def process_image(input_image:Image, meter_type:str):
|
18 |
+
fl, fl_ft = model_init(hack=False)
|
19 |
+
if meter_type == "方形儀表":
|
20 |
+
value, img = read_meter_rect(input_image, fl, fl_ft)
|
21 |
+
return img, f"辨識結果: PA={value}", None
|
22 |
+
assert meter_type == "圓形儀表"
|
23 |
+
plt.clf()
|
24 |
+
print("process_image")
|
25 |
+
W, H = 640, 480
|
26 |
+
if input_image is None:
|
27 |
+
return None, None
|
28 |
+
meter_result = read_meter(input_image, fl, fl_ft)
|
29 |
+
img, fig = visualization(meter_result)
|
30 |
+
buf = BytesIO()
|
31 |
+
fig.savefig(buf, format='png')
|
32 |
+
buf.seek(0)
|
33 |
+
fig_img = Image.open(buf)
|
34 |
+
plt.clf()
|
35 |
+
return img, f"辨識結果: PSI={meter_result.needle_psi:.1f} kg/cm²={meter_result.needle_kg_cm2:.2f} ", fig_img
|
36 |
+
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
|
39 |
+
gr.Markdown("## 指針辨識系統\n請選擇儀表類型,上傳圖片,或點擊Submit")
|
40 |
+
|
41 |
+
with gr.Row():
|
42 |
+
with gr.Column():
|
43 |
+
with gr.Row():
|
44 |
+
clear_button = gr.ClearButton()
|
45 |
+
submit_button = gr.Button("Submit", variant="primary")
|
46 |
+
meter_type_dropdown = gr.Dropdown(choices=["圓形儀表", "方形儀表"], label="選擇選項")
|
47 |
+
image_input = gr.Image(type="pil", label="上傳圖片")
|
48 |
+
with gr.Column():
|
49 |
+
number_output = gr.Textbox(label="辨識結果", placeholder="辨識結果")
|
50 |
+
image_output = gr.Image(label="輸出圖片")
|
51 |
+
plot_output = gr.Image(label="模型結果")
|
52 |
+
|
53 |
+
clear_button.add([image_input, image_output, number_output])
|
54 |
+
|
55 |
+
image_input.upload(
|
56 |
+
fn=process_image,
|
57 |
+
inputs=[image_input, meter_type_dropdown],
|
58 |
+
outputs=[image_output, number_output, plot_output],
|
59 |
+
queue=False
|
60 |
+
)
|
61 |
|
62 |
+
submit_button.click(
|
63 |
+
fn=process_image,
|
64 |
+
inputs=[image_input, meter_type_dropdown],
|
65 |
+
outputs=[image_output, number_output, plot_output],
|
66 |
+
)
|
67 |
|
68 |
+
demo.launch(debug=True)
|
|