# import gradio as gr | |
# from segmentation import segment_image | |
# import numpy as np | |
# import cv2 | |
# # Image de test par défaut | |
# default_image_path = "./image.png" | |
# def segment_and_display(image_path=default_image_path): | |
# # Appeler la fonction de segmentation | |
# original_image, segmented_image = segment_image(image_path) | |
# # Retourner les images pour l'affichage | |
# return original_image, segmented_image | |
# # Charger l'image de test par défaut | |
# default_original_image, default_segmented_image = segment_image(default_image_path) | |
# # Interface Gradio | |
# iface = gr.Interface( | |
# fn=segment_and_display, | |
# inputs=gr.Image(type="filepath", label="Upload Image"), | |
# outputs=[ | |
# gr.Image(type="numpy", label="Original Image"), | |
# gr.Image(type="numpy", label="Segmented Image") | |
# ], | |
# title="Image Segmentation with K-means (k=2)", | |
# description="Upload an image or use the default test image to see the segmentation result.", | |
# examples=[ | |
# [default_image_path] | |
# ], | |
# live=True # Permet de voir les changements en temps réel | |
# ) | |
# # Afficher l'image de test par défaut lorsque l'interface est ouverte | |
# iface.launch(share=True, inline=True) | |
import gradio as gr # 导入 Gradio 库,用于创建基于 Web 的用户界面 | |
from segmentation import segment_image # 从 'segmentation.py' 文件中导入自定义的图像分割函数 segment_image | |
import numpy as np # 导入 NumPy 库,用于数值操作,特别是图像数据处理 | |
import cv2 # 导入 OpenCV 库,用于图像处理任务(尽管在 Gradio 部分未直接使用,但在 segment_image 中很可能被使用) | |
import requests # 导入 requests 库,用于向外部 API 发送 HTTP 请求 | |
import base64 # 导入 base64 库,用于图像数据的编码和解码 | |
# 定义图像分割函数 | |
def segment_and_display(image_path): | |
# 调用 segment_image 函数(来自 segmentation.py)获取原始图像和分割后的图像 | |
original_image, segmented_image = segment_image(image_path) | |
# 返回原始图像、分割后的图像以及图像路径,供后续分析使用 | |
return original_image, segmented_image, image_path | |
# 定义使用 MedGemma API 进行专业分析的函数 | |
def analyze_image_with_question(image_path, question): | |
# 以二进制读取模式打开图像文件 | |
with open(image_path, "rb") as img_file: | |
# 读取图像内容为字节 | |
image_bytes = img_file.read() | |
# 将图像字节编码为 base64 字符串,并解码为 UTF-8 格式,以便通过 API 传输 | |
image_base64 = base64.b64encode(image_bytes).decode("utf-8") | |
# 构造用于 API 请求的 JSON 数据体 | |
payload = { | |
"inputs": { | |
"image": image_base64, # 包含 base64 编码的图像数据 | |
"text": question # 包含用户的提问 | |
} | |
} | |
# 定义 API 请求的 HTTP 头,包括授权令牌 | |
headers = { | |
"Authorization": "Bearer YOUR_HF_API_TOKEN" # 占位符:请替换为你的实际 HuggingFace API 访问令牌 | |
} | |
# 向 MedGemma API 终端发送 POST 请求 | |
response = requests.post("https://api-inference.huggingface.co/models/google/medgemma-4b-it", | |
headers=headers, json=payload) | |
# 检查 API 请求是否成功(状态码为 200) | |
if response.status_code == 200: | |
# 解析 API 返回的 JSON 响应 | |
result = response.json() | |
# 提取并返回 API 响应中生成的文本 | |
return result[0]["generated_text"] | |
else: | |
# 如果 API 请求失败,返回错误消息,包括状态码和响应文本 | |
return f"Error: {response.status_code} - {response.text}" | |
# 使用 Gradio Blocks 组件创建界面,以实现自定义布局 | |
with gr.Blocks() as iface: | |
# 在界面中添加一个 Markdown 标题 | |
gr.Markdown("# 🧠 医学图像分割 + 专家分析") | |
# 创建一个行,用于水平排列组件 | |
with gr.Row(): | |
# 为图像输入创建一个列 | |
with gr.Column(): | |
# 图像输入组件,允许文件上传,type="filepath" 表示函数将接收临时文件的路径 | |
image_input = gr.Image(type="filepath", label="上传皮肤图像") | |
# 为显示输出图像创建另一个列 | |
with gr.Column(): | |
# 用于显示原始图像的输出组件(NumPy 数组) | |
original_output = gr.Image(type="numpy", label="原始图像") | |
# 用于显示分割后图像的输出组件(NumPy 数组) | |
segmented_output = gr.Image(type="numpy", label="分割后图像") | |
# 一个 Gradio State 组件,用于在函数调用之间存储图像文件路径 | |
image_path_state = gr.State() | |
# 当 image_input 组件发生变化时(即上传新图像时)定义一个动作 | |
image_input.change(fn=segment_and_display, # 调用 segment_and_display 函数 | |
inputs=image_input, # 将 image_input 组件作为输入传递 | |
outputs=[original_output, segmented_output, image_path_state]) # 更新这些输出组件 | |
# 为第二部分添加另一个 Markdown 标题 | |
gr.Markdown("## 🩺 询问上传图像相关的医学问题") | |
# 文本框,供用户输入医学问题 | |
question_input = gr.Textbox(label="输入你的问题(例如:'这是什么类型的病变?')") | |
# 文本框,用于显示 MedGemma 的回答 | |
answer_output = gr.Textbox(label="MedGemma 回答") | |
# 当 question_input 文本框被提交时(例如,按 Enter 键时)定义一个动作 | |
question_input.submit(fn=analyze_image_with_question, # 调用 analyze_image_with_question 函数 | |
inputs=[image_path_state, question_input], # 传递存储的图像路径和问题 | |
outputs=answer_output) # 更新 answer_output 文本框 | |
# 启动 Gradio 界面 | |
iface.launch() | |