ham / app.py
hpa666's picture
Update app.py
0690eae
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:melanocytic nevi", 1:"mel:melanoma", 2:"bkl:benign keratosis-like lesions ", 3:"bcc:basal cell carcinoma ", 4:"akiec:Actinic keratoses and intraepithelial carcinoma / Bowen's disease ", 5:"vasc:vascular lesions", 6:"df:dermatofibroma"}
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 flip_text(x):
# return x[::-1]
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 flip_image(x):
# return np.fliplr(x)
# 定义处理函数,该函数接受上传的ZIP文件并处理其中的图像
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()])
# a = process_zip_file('/gemini/code/皮肤.zip')
# print(a)
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文件)"):
# with gr.Row():
input_component = gr.File(label="上传ZIP文件") #file指定路径
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()