Spaces:
Running
on
Zero
Running
on
Zero
from gradio_client import Client, handle_file | |
import numpy as np | |
from PIL import Image | |
import io | |
def test_gradio_api(): | |
client = Client("Hakureirm/NailongKiller") | |
result = client.predict( | |
img=handle_file("example1.jpg"), | |
api_name="/predict" | |
) | |
# 如果结果是临时文件路径 | |
if isinstance(result, str): | |
# 读取图像数据并直接转换为 PIL Image | |
img = Image.open(result) | |
# 保存到指定位置 | |
img.save("detection_result.jpg") | |
# 删除临时文件 | |
import os | |
os.remove(result) | |
print("检测结果已保存为 detection_result.jpg") | |
return "已完成检测并保存结果" | |
if __name__ == "__main__": | |
try: | |
message = test_gradio_api() | |
print(message) | |
except Exception as e: | |
print(f"错误: {str(e)}") |