import torch import gradio as gr import pathlib # TODO:在windows中使用linux中训练好的权重的方式 # temp = pathlib.PosixPath # pathlib.PosixPath = pathlib.WindowsPath import os def modify_file(file_path, old_string, new_string): # 读取文件内容 with open(file_path, 'r') as file: file_content = file.read() # 替换字符串 new_content = file_content.replace(old_string, new_string) # 写入文件 with open(file_path, 'w') as file: file.write(new_content) def main(): # 设置文件路径和需要替换的字符串 file_path = "/usr/local/lib/python3.10/site-packages/basicsr/data/degradations.py" old_string = "from torchvision.transforms.functional_tensor import rgb_to_grayscale" new_string = "from torchvision.transforms._functional_tensor import rgb_to_grayscale" # 检查文件是否存在 if os.path.exists(file_path): # 进行替换 modify_file(file_path, old_string, new_string) print("替换完成!") else: print("文件路径不存在!") if __name__ == "__main__": main() model = torch.hub.load("./yolov5", "custom", path="./yolov5/weights/yolov5x_anchor_ODConv.pt", source="local").to("cpu") title = "肺结节检测系统" desc = "这是一个基于Gradio的使用Yolov5实现的肺结节检测系统!" base_conf, base_iou = 0.25, 0.45 def det_image(img, conf_thres, iou_thres): model.conf = conf_thres model.iou = iou_thres return model(img).render()[0] # examples中的参数要和inputs中对应 # 获取摄像头拍照检测,修改inputs中的:inputs=[gr.Webcam(),...]就可以了,动态的更新添加属性:live=True # 如果将launch()更改为launch(share=True)则会将这个代码放在公网进行访问。 gr.Interface( inputs=["image", gr.Slider(minimum=0, maximum=1, value=base_conf), gr.Slider(minimum=0, maximum=1, value=base_iou)], outputs=["image"], fn=det_image, title=title, description=desc, live=True, examples=[["./yolov5/data/images/0004.png", base_conf, base_iou], ["./yolov5/data/images/0012.png", 0.3, base_iou]] ).launch(share=True) # 最后要记得将复原修改 # pathlib.PosixPath = temp