File size: 859 Bytes
49dfadb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)}")