Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -22,31 +22,54 @@ def predict(img):
|
|
22 |
pad_h = max_size - original_size[1]
|
23 |
|
24 |
# 创建方形画布并保持宽高比
|
25 |
-
padded_img = Image.new('RGB', (max_size, max_size), (114, 114, 114))
|
26 |
padded_img.paste(input_image, (pad_w//2, pad_h//2))
|
27 |
|
28 |
# 转换为numpy数组并进行预测
|
29 |
img_array = np.array(padded_img)
|
30 |
|
31 |
# 进行预测
|
32 |
-
results = model.predict(
|
33 |
-
|
34 |
-
)
|
35 |
|
36 |
# 获取预测结果
|
37 |
-
result_img =
|
38 |
|
39 |
# 裁剪回原始尺寸
|
40 |
if pad_w > 0 or pad_h > 0:
|
41 |
result_img = result_img[pad_h//2:pad_h//2 + original_size[1],
|
42 |
pad_w//2:pad_w//2 + original_size[0]]
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
demo = gr.Interface(
|
47 |
fn=predict,
|
48 |
inputs=gr.Image(label="输入图片"),
|
49 |
-
outputs=
|
|
|
|
|
|
|
50 |
title="🐉 奶龙杀手 (NailongKiller)",
|
51 |
description="上传图片来检测奶龙 | Upload an image to detect Nailong",
|
52 |
examples=[["example1.jpg"]],
|
|
|
22 |
pad_h = max_size - original_size[1]
|
23 |
|
24 |
# 创建方形画布并保持宽高比
|
25 |
+
padded_img = Image.new('RGB', (max_size, max_size), (114, 114, 114))
|
26 |
padded_img.paste(input_image, (pad_w//2, pad_h//2))
|
27 |
|
28 |
# 转换为numpy数组并进行预测
|
29 |
img_array = np.array(padded_img)
|
30 |
|
31 |
# 进行预测
|
32 |
+
results = model.predict(img_array)
|
33 |
+
result = results[0]
|
|
|
34 |
|
35 |
# 获取预测结果
|
36 |
+
result_img = result.plot()
|
37 |
|
38 |
# 裁剪回原始尺寸
|
39 |
if pad_w > 0 or pad_h > 0:
|
40 |
result_img = result_img[pad_h//2:pad_h//2 + original_size[1],
|
41 |
pad_w//2:pad_w//2 + original_size[0]]
|
42 |
|
43 |
+
# 处理检测信息
|
44 |
+
info = {
|
45 |
+
"detected": len(result.boxes) > 0,
|
46 |
+
"count": len(result.boxes),
|
47 |
+
"confidences": []
|
48 |
+
}
|
49 |
+
|
50 |
+
if info["detected"]:
|
51 |
+
# 获取每个检测框的置信度
|
52 |
+
for box in result.boxes:
|
53 |
+
conf = float(box.conf[0])
|
54 |
+
info["confidences"].append(f"{conf:.2%}")
|
55 |
+
|
56 |
+
# 生成输出文本
|
57 |
+
output_text = f"""检测结果:
|
58 |
+
- 是否检测到目标: {'是' if info['detected'] else '否'}
|
59 |
+
- 检测到的目标数量: {info['count']}"""
|
60 |
+
|
61 |
+
if info["confidences"]:
|
62 |
+
output_text += f"\n- 置信度: {', '.join(info['confidences'])}"
|
63 |
+
|
64 |
+
return result_img, output_text
|
65 |
|
66 |
demo = gr.Interface(
|
67 |
fn=predict,
|
68 |
inputs=gr.Image(label="输入图片"),
|
69 |
+
outputs=[
|
70 |
+
gr.Image(label="检测结果", type="numpy"),
|
71 |
+
gr.Textbox(label="检测信息")
|
72 |
+
],
|
73 |
title="🐉 奶龙杀手 (NailongKiller)",
|
74 |
description="上传图片来检测奶龙 | Upload an image to detect Nailong",
|
75 |
examples=[["example1.jpg"]],
|