|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from tensorflow.keras.models import load_model |
|
from PIL import Image |
|
import cv2 |
|
import numpy as np |
|
import gradio as gr |
|
import zipfile |
|
from io import BytesIO |
|
|
|
ham_dict = {0: "nv(黑素细胞痣)", 1: "mel(黑色素瘤)", 2: "bkl(良性角化病样病变)", |
|
3: "bcc(基底细胞癌)", 4: "akiec(日光性角化病和上皮内癌/博温病)", |
|
5: "vasc(血管性病变)", 6: "df(皮肤纤维瘤)"} |
|
model = load_model('my_multi_model_2.h5') |
|
|
|
|
|
|
|
def classify_image(image): |
|
image = image.resize((224, 224)) |
|
image = np.array(image) |
|
image = np.expand_dims(image, axis=0) |
|
predictions = model.predict(image) |
|
predicted_class = np.argmax(predictions) |
|
str = ham_dict[predicted_class] |
|
print(str) |
|
class_name = str.split('(')[0] |
|
if class_name == 'bkl' or class_name == 'nv': |
|
result = '良性' |
|
else: |
|
result = '恶性' |
|
return f" 皮肤信息:{str} 诊断结果:{result}" |
|
|
|
|
|
|
|
|
|
|
|
def process_zip_file(zip_file): |
|
image_messages = {} |
|
print(zip_file.name) |
|
print(type(zip_file)) |
|
with zipfile.ZipFile(zip_file.name, "r") as zf: |
|
for file_name in zf.namelist(): |
|
if file_name.lower().endswith(('.png', '.jpg')): |
|
with zf.open(file_name) as img_file: |
|
img_data = img_file.read() |
|
img = Image.open(BytesIO(img_data)) |
|
image_result = classify_image(img) |
|
image_messages[file_name] = image_result |
|
if not image_messages: |
|
return "ZIP文件中没有图像文件" |
|
|
|
return "\n".join([f"{key.split('/')[-1]}: {value}" for key, value in image_messages.items()]) |
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("皮肤癌检测,提示:该模型诊断结果置信度为0.8,只能作为在线参考,及时就医才是王道!") |
|
with gr.Tab("研究目的"): |
|
gr.Markdown("通过检测识别出皮肤癌的发病机制,可以更好地防治和治疗皮肤癌。视频介绍皮肤癌的常见类型和发病机制。") |
|
gr.Video("ham.mp4") |
|
with gr.Tab("单张图片检测(上传png,jpg图片)"): |
|
input_interface = gr.Image(type='pil', label="上传图片") |
|
output_interface = gr.Textbox(label="预测结果") |
|
text_button = gr.Button("单张检测") |
|
with gr.Tab("多张图片检测(上传zip文件)"): |
|
|
|
input_component = gr.File(label="上传ZIP文件") |
|
out_ = gr.Textbox(label="预测结果") |
|
zip_button = gr.Button("多张检测") |
|
|
|
with gr.Accordion("注意事项"): |
|
gr.Markdown("1.只支持上传zip压缩包 \n 2.压缩包中的图片名用英语或数字表示(中文图片名检测会出现乱码,不方便辨别)\n 3.服务器算力有限,多图片处理得比较慢,请耐心等待,或换成单图片处理") |
|
gr.Markdown('[我的网站](https://hpa888.top/)') |
|
text_button.click(classify_image, inputs=input_interface, outputs=output_interface) |
|
zip_button.click(process_zip_file, inputs=input_component, outputs=out_) |
|
demo.launch() |
|
|