Spaces:
Sleeping
Sleeping
upload model.pt and app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from ultralytics import YOLO
|
5 |
+
from fastapi import FastAPI
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
model = YOLO('model.pt')
|
9 |
+
#device = torch.device('cuda')
|
10 |
+
#model.to(device)
|
11 |
+
@app.post("/detect/")
|
12 |
+
def detect(img):
|
13 |
+
try:
|
14 |
+
objects_dict = {}
|
15 |
+
img_arr = cv2.imread(img)
|
16 |
+
img_arr = cv2.resize(img_arr, (416, 416))
|
17 |
+
img_arr = np.array([img_arr.transpose(2, 0, 1)])
|
18 |
+
#img_arr = torch.from_numpy(img_arr).float().to(device)
|
19 |
+
results = model(img_arr)
|
20 |
+
|
21 |
+
for r in results:
|
22 |
+
n = len(r.boxes.cls)
|
23 |
+
|
24 |
+
for i in range(n):
|
25 |
+
cls = int(r.boxes.cls[i].cpu())
|
26 |
+
temp_obj = [int(r.boxes.conf[i].cpu()), r.boxes.xyxy[i].cpu().numpy()] #уверенность модели, координаты прямоугольника
|
27 |
+
|
28 |
+
if cls not in objects_dict:
|
29 |
+
objects_dict[cls] = [temp_obj]
|
30 |
+
else:
|
31 |
+
objects_dict[cls].append(temp_obj)
|
32 |
+
return objects_dict
|
33 |
+
|
34 |
+
except Exception as e:
|
35 |
+
print(str(e))
|
36 |
+
return {}
|
model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d0f1fe9c588ffb298ae4ae37110f3bc6645cc6a00579203efeaf37c224c7923f
|
3 |
+
size 311056786
|