Download app.py from xfys/detect: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/spaces/xfys/detect/resolve/main/app.py
- Command line
-
hf download hf://spaces/xfys/detect/app.py
-
curl -L -o app.py https://huggingface.co/spaces/xfys/detect/resolve/main/app.py
1.85 kB
| import gradio.inputs | |
| import torch | |
| import gradio as gr | |
| from PIL import Image | |
| import tempfile | |
| import detect | |
| import os | |
| import shutil | |
| # result = detect.run(source=img, data="VOCdevkit/FLIR.yaml", weights="./best.pt") | |
| def de(image): | |
| # 创建临时文件夹 | |
| temp_path = tempfile.TemporaryDirectory(dir="./") | |
| temp_dir = temp_path.name | |
| # 临时图片的路径 | |
| temp_image_path = os.path.join(temp_dir, f"temp.jpg") | |
| # 存储临时图片 | |
| img = Image.fromarray(image) | |
| img.save(temp_image_path) | |
| # 结果图片的存储目录 | |
| temp_result_path = os.path.join(temp_dir, "tempresult") | |
| # 对临时图片进行检测 | |
| detect.run(source=temp_image_path, data="OCdevkit/FLIR.yaml", weights="./runs/train/exp/weights/best.pt", project=f'./{temp_dir}',name = 'tempresult', hide_conf=True) | |
| # 结果图片的路径 | |
| temp_result_path = os.path.join(temp_result_path, os.listdir(temp_result_path)[0]) | |
| # 读取结果图片 | |
| result_image = Image.open(temp_result_path).copy() | |
| # 删除临时文件夹 | |
| temp_path.cleanup() | |
| return result_image | |
| example_image= [ | |
| ["./VOCdevkit/images/train/video-2SReBn5LtAkL5HMj2-frame-005072-MA7NCLQGoqq9aHaiL.jpg"], | |
| ["./VOCdevkit/images/train/video-2rsjnZFyGQGeynfbv-frame-003708-6fPQbB7jtibwaYAE7.jpg"], | |
| ["./VOCdevkit/images/train/video-2SReBn5LtAkL5HMj2-frame-000317-HTgPBFgZyPdwQnNvE.jpg"], | |
| ["./VOCdevkit/images/val/video-jNQtRj6NGycZDEXpe-frame-002515-J3YntG8ntvZheKK3P.jpg"], | |
| ["./VOCdevkit/images/val/video-kDDWXrnLSoSdHCZ7S-frame-003063-eaKjPvPskDPjenZ8S.jpg"], | |
| ["./VOCdevkit/images/val/video-r68Yr9RPWEp5fW2ZF-frame-000333-X6K5iopqbmjKEsSqN.jpg"] | |
| ] | |
| iface = gr.Interface(fn=de, inputs=gr.Image(label="上传一张红外图像,仅支持jpg格式"), outputs="image", examples=example_image, share=True) | |
| iface.launch(share=True) | |