Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from qai_hub_models.models.detr_resnet50 import Model
|
| 4 |
-
from PIL import Image, ImageDraw
|
| 5 |
-
import numpy as np
|
| 6 |
-
|
| 7 |
-
# 注册 AVIF 支持(根据所安装的插件选择一种)
|
| 8 |
-
try:
|
| 9 |
-
from pillow_avif import register_avif_opener
|
| 10 |
-
register_avif_opener()
|
| 11 |
-
except ImportError:
|
| 12 |
-
try:
|
| 13 |
-
import pillow_heif
|
| 14 |
-
pillow_heif.register_heif_opener()
|
| 15 |
-
except ImportError:
|
| 16 |
-
print("AVIF support not available. Please install 'pillow-avif-plugin' or 'pillow-heif'.")
|
| 17 |
-
|
| 18 |
-
# 加载模型
|
| 19 |
-
torch_model = Model.from_pretrained()
|
| 20 |
-
|
| 21 |
def detect_objects(image):
|
| 22 |
if image is None:
|
| 23 |
raise ValueError("No image uploaded!") # 检查图像是否为 None
|
|
@@ -53,44 +33,15 @@ def detect_objects(image):
|
|
| 53 |
box[3] *= original_image.height / 800
|
| 54 |
|
| 55 |
detections.append({
|
| 56 |
-
"label": f"Object {i}",
|
| 57 |
"confidence": round(score, 3),
|
| 58 |
"box": box,
|
| 59 |
})
|
| 60 |
-
# 绘制边界框和标签到原始图像上
|
| 61 |
-
draw = ImageDraw.Draw(original_image)
|
| 62 |
-
draw.rectangle(box[:4], outline="red", width=3) # 绘制矩形框,确保只取前四个坐标
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
return original_image, detections
|
| 67 |
-
|
| 68 |
-
# 创建 Gradio 接口,自动处理 AVIF 图像并转换为 PNG 格式以供显示和处理。
|
| 69 |
-
with gr.Blocks() as iface:
|
| 70 |
-
gr.Markdown("# Object Detection with DETR-ResNet50")
|
| 71 |
-
|
| 72 |
-
with gr.Row():
|
| 73 |
-
with gr.Column(scale=1):
|
| 74 |
-
image_input = gr.Image(type="numpy", label="Upload Image (supports PNG, JPEG, AVIF...)")
|
| 75 |
-
submit_button = gr.Button("Submit")
|
| 76 |
-
clear_button = gr.Button("Clear")
|
| 77 |
-
with gr.Column(scale=1):
|
| 78 |
-
output_image = gr.Image(label="Detected Image")
|
| 79 |
-
output_json = gr.JSON(label="Detections")
|
| 80 |
-
|
| 81 |
-
def on_submit(image):
|
| 82 |
-
try:
|
| 83 |
-
detected_image, detections = detect_objects(image)
|
| 84 |
-
return detected_image, detections
|
| 85 |
-
except Exception as e:
|
| 86 |
-
return None, {"error": str(e)}
|
| 87 |
-
|
| 88 |
-
def on_clear():
|
| 89 |
-
return None, None, None # 清空输入和输出
|
| 90 |
-
|
| 91 |
-
submit_button.click(on_submit, inputs=image_input, outputs=[output_image, output_json])
|
| 92 |
-
clear_button.click(on_clear, inputs=None, outputs=[image_input, output_image, output_json]) # 修复清除功能
|
| 93 |
-
|
| 94 |
-
# 启动应用程序
|
| 95 |
-
if __name__ == "__main__":
|
| 96 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def detect_objects(image):
|
| 2 |
if image is None:
|
| 3 |
raise ValueError("No image uploaded!") # 检查图像是否为 None
|
|
|
|
| 33 |
box[3] *= original_image.height / 800
|
| 34 |
|
| 35 |
detections.append({
|
| 36 |
+
"label": f"Object {i}",
|
| 37 |
"confidence": round(score, 3),
|
| 38 |
"box": box,
|
| 39 |
})
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# 检查并绘制边界框和标签到原始图像上
|
| 42 |
+
draw = ImageDraw.Draw(original_image)
|
| 43 |
+
if box[1] < box[3]: # 确保 y_min < y_max
|
| 44 |
+
draw.rectangle(box[:4], outline="red", width=3)
|
| 45 |
+
draw.text((box[0], box[1]), f"{detections[-1]['label']} ({detections[-1]['confidence']})", fill="red")
|
| 46 |
|
| 47 |
return original_image, detections
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|