import gradio as gr import torch from PIL import Image import torchvision.transforms as transforms def img2label(left, right): left = Image.fromarray(left.astype('uint8'), 'RGB') right = Image.fromarray(right.astype('uint8'), 'RGB') # To reverse the mirror image of the right eye, the translation would be r2l = transforms.RandomHorizontalFlip(p=1) right = r2l(right) # adjust image left_img = my_transforms(left).to(device) right_img = my_transforms(right).to(device) # 读取模型 model = torch.load('densenet_FD_e4_l5e-4_b32.pkl', map_location='cpu').to(device) # 模型推理 with torch.no_grad(): output = model(left=left_img.unsqueeze(0), right=right_img.unsqueeze(0)) # 结果处理 output = torch.sigmoid(output.squeeze(0)) output_ = output.cpu().numpy().tolist() res_dict = {LABELS[i]: output_[i] for i in range(len(output_))} pred = torch.nonzero(output > 0.4).view(-1) pred = pred.cpu().numpy().tolist() if len(pred) == 0 or (len(pred) == 1 and pred[0] == 0): return res_dict, LABELS[0] res = '' for i in pred: if i == 0: continue res += ', ' + LABELS[i] return res_dict, '目前的身体状态:' + res[2:] if __name__ == '__main__': device = torch.device("cpu") # 标题 title = "Intelligent Health Diagnosis and Analysis System based on OCT Images" description = "After uploading and inputting the fundus images of both the left and right eyes, click the 'submit' button to intelligently analyze potential diseases based on the binocular oct images!" \ "The system includes a total of 8 categories of diseases: diabetes, glaucoma, cataracts, age-related macular degeneration, hypertension, pathological myopia, other diseases, and normal (healthy)." # transforms设置 norm_mean = [0.485, 0.456, 0.406] norm_std = [0.229, 0.224, 0.225] my_transforms = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(norm_mean, norm_std) ]) # 标签设置 LABELS = {0: 'Normal Healthy', 1: 'Diabetes', 2: 'Glaucoma', 3: 'Cataracts', 4: 'Age-related macular degeneration', 5: 'Hypertension', 6: 'Pathological myopia', 7: 'Other diseases'} left_img_dir = 'left.jpg' right_img_dir = 'right.jpg' examples = [[left_img_dir, right_img_dir]] # r = img2label(left_img_dir, right_img_dir) demo = gr.Interface(fn=img2label, inputs=[gr.inputs.Image(), gr.inputs.Image()], outputs=["label", "text"], examples=examples, title=title, description=description) demo.launch()