File size: 2,130 Bytes
8cf157f
ee5e53a
 
 
 
 
8cf157f
 
ee5e53a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cf157f
ee5e53a
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import gradio as gr
from image_debug import analysis_image, compare
import numpy as np
import base64
from PIL import Image
import io


def numpy_to_base64(img_array):
    # 将 numpy array 转换为 PIL Image
    img_pil = Image.fromarray(img_array)

    # 创建一个字节流
    buffer = io.BytesIO()
    # 将图片保存到字节流
    img_pil.save(buffer, format="PNG")
    # 获取 base64 字符串
    img_str = base64.b64encode(buffer.getvalue()).decode()

    return img_str


def combine_analysis(result1, result2):
    if not result1 or not result2:
        return "请先完成两张图片的分析"
    result = compare(result1, result2)
    return result

def process_image1(image1):
    if image1 is None:
        return "请上传图片1"
    image1 = numpy_to_base64(image1)
    return analysis_image(image1)


def process_image2(image2):
    if image2 is None:
        return "请上传图片2"
    image2 = numpy_to_base64(image2)
    return analysis_image(image2)


with gr.Blocks(title="图片分析演示") as demo:
    gr.Markdown("# 双图片分析系统")

    with gr.Row():
        with gr.Column():
            image1_input = gr.Image(label="第一张图片")
            result1 = gr.Markdown(label="第一张图片分析结果")
            # 删除 btn1

        with gr.Column():
            image2_input = gr.Image(label="第二张图片")
            result2 = gr.Markdown(label="第二张图片分析结果")
            # 删除 btn2

    with gr.Row():
        combine_btn = gr.Button("综合分析", variant="primary")
    with gr.Row():
        final_result = gr.Markdown(label="综合分析结果")

    # 替换 btn1.click 为 image1_input.change
    image1_input.change(
        fn=process_image1,
        inputs=image1_input,
        outputs=result1
    )

    # 替换 btn2.click 为 image2_input.change
    image2_input.change(
        fn=process_image2,
        inputs=image2_input,
        outputs=result2
    )

    combine_btn.click(
        fn=combine_analysis,
        inputs=[result1, result2],
        outputs=final_result
    )

if __name__ == "__main__":
    demo.launch()