PolarisFTL commited on
Commit
d41b811
·
verified ·
1 Parent(s): a4eef97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -31
app.py CHANGED
@@ -1,43 +1,32 @@
1
  import gradio as gr
2
- import torch
3
- from torchvision import models, transforms
4
  from PIL import Image
 
 
 
5
 
6
- # 定义设备
7
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
 
9
- # 加载预训练的模型
10
- model = models.resnet18(pretrained=True)
11
- model = model.to(device)
12
- model.eval()
 
 
 
13
 
14
- # 图像预处理
15
- transform = transforms.Compose([
16
- transforms.Resize(256),
17
- transforms.CenterCrop(224),
18
- transforms.ToTensor(),
19
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
20
- ])
21
-
22
- # 加载类名称
23
- with open("model_data/rtts_classes.txt") as f:
24
- class_names = [line.strip() for line in f.readlines()]
25
-
26
- # 定义预测函数
27
- def predict(image):
28
- image = transform(image).unsqueeze(0).to(device)
29
- with torch.no_grad():
30
- outputs = model(image)
31
- _, predicted = outputs.max(1)
32
- return class_names[predicted]
33
 
34
  # 使用Gradio创建界面
35
  iface = gr.Interface(
36
- fn=predict,
37
  inputs=gr.inputs.Image(type="pil"),
38
- outputs=gr.outputs.Textbox(),
39
- title="图像分类器",
40
- description="上传一张图像,并让模型预测它的类别。",
 
 
 
 
41
  )
42
 
43
  # 启动应用
 
1
  import gradio as gr
 
 
2
  from PIL import Image
3
+ import os
4
+ from yolo import YOLO
5
+ from tqdm import tqdm
6
 
7
+ # 初始化YOLO模型
8
+ yolo = YOLO()
9
 
10
+ # 预测单张图像
11
+ def predict_single_image(image, crop=False, count=True):
12
+ try:
13
+ r_image = yolo.detect_image(image, crop=crop, count=count)
14
+ return r_image
15
+ except Exception as e:
16
+ return str(e)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # 使用Gradio创建界面
20
  iface = gr.Interface(
21
+ fn=predict_single_image,
22
  inputs=gr.inputs.Image(type="pil"),
23
+ outputs=gr.outputs.Image(type="pil"),
24
+ title="YOLO 图像检测器",
25
+ description="上传一张图像,使用YOLO模型进行对象检测。",
26
+ examples=[
27
+ ["img/example1.jpg"],
28
+ ["img/example2.jpg"]
29
+ ]
30
  )
31
 
32
  # 启动应用