Spaces:
Sleeping
Sleeping
Update index.html
Browse files- index.html +0 -108
index.html
CHANGED
|
@@ -185,114 +185,6 @@
|
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
-
|
| 189 |
-
Отлично, Артемий 🚀
|
| 190 |
-
Вот финальный комплект файлов для Hugging Face Space — с **frame skipping** (каждый 2‑й кадр), повышенным качеством JPEG (95), и рабочим фронтом на `<canvas>`.
|
| 191 |
-
|
| 192 |
-
---
|
| 193 |
-
|
| 194 |
-
## 📂 Структура проекта
|
| 195 |
-
```
|
| 196 |
-
/GrechnikNet-Space
|
| 197 |
-
├── app.py
|
| 198 |
-
├── index.html
|
| 199 |
-
├── requirements.txt
|
| 200 |
-
├── Dockerfile
|
| 201 |
-
└── best.pt
|
| 202 |
-
```
|
| 203 |
-
|
| 204 |
-
---
|
| 205 |
-
|
| 206 |
-
## 🐍 app.py
|
| 207 |
-
```python
|
| 208 |
-
from fastapi import FastAPI, UploadFile, File, Form
|
| 209 |
-
from fastapi.responses import JSONResponse, Response, FileResponse
|
| 210 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 211 |
-
from ultralytics import YOLO
|
| 212 |
-
import torch
|
| 213 |
-
import cv2
|
| 214 |
-
import numpy as np
|
| 215 |
-
|
| 216 |
-
app = FastAPI()
|
| 217 |
-
|
| 218 |
-
# Разрешаем фронту обращаться к API
|
| 219 |
-
app.add_middleware(
|
| 220 |
-
CORSMiddleware,
|
| 221 |
-
allow_origins=["*"],
|
| 222 |
-
allow_methods=["*"],
|
| 223 |
-
allow_headers=["*"],
|
| 224 |
-
)
|
| 225 |
-
|
| 226 |
-
# Загружаем модель
|
| 227 |
-
model = YOLO("best.pt")
|
| 228 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 229 |
-
model.to(device)
|
| 230 |
-
|
| 231 |
-
@app.get("/")
|
| 232 |
-
def root():
|
| 233 |
-
return FileResponse("index.html")
|
| 234 |
-
|
| 235 |
-
def read_image_to_bgr(file_bytes: bytes) -> np.ndarray:
|
| 236 |
-
img_array = np.frombuffer(file_bytes, dtype=np.uint8)
|
| 237 |
-
img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
| 238 |
-
return img
|
| 239 |
-
|
| 240 |
-
def annotate_bgr(results) -> np.ndarray:
|
| 241 |
-
return results[0].plot()
|
| 242 |
-
|
| 243 |
-
def results_to_json(results):
|
| 244 |
-
r = results[0]
|
| 245 |
-
boxes = r.boxes
|
| 246 |
-
out = []
|
| 247 |
-
if boxes is not None and len(boxes) > 0:
|
| 248 |
-
xyxy = boxes.xyxy.cpu().numpy()
|
| 249 |
-
conf = boxes.conf.cpu().numpy()
|
| 250 |
-
cls = boxes.cls.cpu().numpy().astype(int)
|
| 251 |
-
names = r.names
|
| 252 |
-
for i in range(len(xyxy)):
|
| 253 |
-
x1, y1, x2, y2 = xyxy[i].tolist()
|
| 254 |
-
out.append({
|
| 255 |
-
"bbox": [x1, y1, x2, y2],
|
| 256 |
-
"conf": float(conf[i]),
|
| 257 |
-
"class_id": int(cls[i]),
|
| 258 |
-
"class_name": names[int(cls[i])] if names else str(cls[i])
|
| 259 |
-
})
|
| 260 |
-
return {"detections": out}
|
| 261 |
-
|
| 262 |
-
@app.post("/predict")
|
| 263 |
-
async def predict(
|
| 264 |
-
file: UploadFile = File(...),
|
| 265 |
-
conf: float = Form(0.25),
|
| 266 |
-
iou: float = Form(0.45),
|
| 267 |
-
return_image: int = Form(1)
|
| 268 |
-
):
|
| 269 |
-
data = await file.read()
|
| 270 |
-
bgr = read_image_to_bgr(data)
|
| 271 |
-
if bgr is None:
|
| 272 |
-
return JSONResponse({"error": "Invalid image"}, status_code=400)
|
| 273 |
-
|
| 274 |
-
results = model.predict(
|
| 275 |
-
source=bgr,
|
| 276 |
-
conf=conf,
|
| 277 |
-
iou=iou,
|
| 278 |
-
imgsz=480, # меньше размер для скорости
|
| 279 |
-
verbose=False
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
-
if return_image == 1:
|
| 283 |
-
annotated = annotate_bgr(results)
|
| 284 |
-
ok, buf = cv2.imencode(".jpg", annotated, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
|
| 285 |
-
if not ok:
|
| 286 |
-
return JSONResponse({"error": "Encode failed"}, status_code=500)
|
| 287 |
-
return Response(content=buf.tobytes(), media_type="image/jpeg")
|
| 288 |
-
else:
|
| 289 |
-
return JSONResponse(results_to_json(results))
|
| 290 |
-
```
|
| 291 |
-
|
| 292 |
-
---
|
| 293 |
-
|
| 294 |
-
## 🌐 index.html
|
| 295 |
-
```html
|
| 296 |
<!DOCTYPE html>
|
| 297 |
<html>
|
| 298 |
<head>
|
|
|
|
| 185 |
|
| 186 |
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
<!DOCTYPE html>
|
| 189 |
<html>
|
| 190 |
<head>
|