File size: 2,372 Bytes
0cfb4f0
 
 
 
5856612
0cfb4f0
 
 
 
 
 
 
 
 
 
a8a1a7e
 
0cfb4f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5856612
0cfb4f0
 
 
 
 
5856612
0cfb4f0
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import spaces
import sys
print(sys.version_info)

import gradio as gr
#from florence import model_init, draw_image
#from wikai import analyze_dial, ocr_and_od
import matplotlib.pyplot as plt
from aimodel import model_init, read_meter, visualization
from test_rect import read_meter as read_meter_rect
from PIL import Image
from io import BytesIO
import logging
#logging.basicConfig(level=logging.DEBUG)
print("Loading model...")
fl, fl_ft = model_init(hack=False)
@spaces.GPU(duration=30)
def process_image(input_image:Image, meter_type:str):
    if meter_type == "方形儀表":    
        value, img = read_meter_rect(input_image, fl, fl_ft)
        return img, f"辨識結果: PA={value}", None
    assert meter_type == "圓形儀表"    
    plt.clf()
    print("process_image")
    W, H = 640, 480
    if input_image is None:
        return None, None    
    meter_result  = read_meter(input_image, fl, fl_ft)
    img, fig = visualization(meter_result)
    buf = BytesIO()
    fig.savefig(buf, format='png')
    buf.seek(0)
    fig_img = Image.open(buf)
    plt.clf()
    return img, f"辨識結果: PSI={meter_result.needle_psi:.1f}  kg/cm²={meter_result.needle_kg_cm2:.2f} ", fig_img

with gr.Blocks() as demo:

    gr.Markdown("## 指針辨識系統\n請選擇儀表類型,上傳圖片,或點擊Submit")

    with gr.Row():
        with gr.Column():
            with gr.Row():
                clear_button = gr.ClearButton()
                submit_button = gr.Button("Submit", variant="primary")
                meter_type_dropdown = gr.Dropdown(choices=["圓形儀表", "方形儀表"], label="選擇選項")
            image_input = gr.Image(type="pil", label="上傳圖片")
        with gr.Column():
            number_output = gr.Textbox(label="辨識結果", placeholder="辨識結果")
            image_output = gr.Image(label="輸出圖片")
            plot_output = gr.Image(label="模型結果")

    clear_button.add([image_input, image_output, number_output])

    image_input.upload(
        fn=process_image,
        inputs=[image_input, meter_type_dropdown],
        outputs=[image_output, number_output, plot_output],
        queue=False
    )

    submit_button.click(
        fn=process_image,
        inputs=[image_input, meter_type_dropdown],
        outputs=[image_output, number_output, plot_output],
    )

demo.launch(debug=True)