Hakureirm commited on
Commit
f69b08f
·
1 Parent(s): 1cd7103

Migrate to ZeroGPU and update requirements for compatibility

Browse files
Files changed (1) hide show
  1. app.py +4 -2
app.py CHANGED
@@ -4,6 +4,7 @@ from fastapi import FastAPI
4
  from PIL import Image
5
  import torch
6
  import spaces
 
7
 
8
  # 初始化 FastAPI 和模型
9
  app = FastAPI()
@@ -14,8 +15,9 @@ model = YOLO('NailongKiller.yolo11n.pt').to(device)
14
 
15
  @spaces.GPU
16
  def predict(img):
17
- # 使用 PIL 直接转换为 PyTorch 张量
18
- img_resized = Image.fromarray(img).resize((640, 640))
 
19
  img_tensor = torch.tensor(img_resized, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(device)
20
  results = model.predict(img_tensor)
21
  return results[0].plot()
 
4
  from PIL import Image
5
  import torch
6
  import spaces
7
+ import numpy as np
8
 
9
  # 初始化 FastAPI 和模型
10
  app = FastAPI()
 
15
 
16
  @spaces.GPU
17
  def predict(img):
18
+ # PIL 图像转换为 numpy 数组
19
+ img_resized = np.array(Image.fromarray(img).resize((640, 640)))
20
+ # 将 numpy 数组转换为 PyTorch 张量
21
  img_tensor = torch.tensor(img_resized, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).to(device)
22
  results = model.predict(img_tensor)
23
  return results[0].plot()