-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
100 lines (78 loc) · 2.31 KB
/
Copy pathapp.py
File metadata and controls
100 lines (78 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import JSONResponse
from ultralytics import YOLO
import cv2
import numpy as np
import tempfile
app = FastAPI()
# Load model once
model = YOLO("best.pt")
# Class mapping (optional)
model.model.names = {
0: "Chips",
1: "Ice Cream",
2: "Noodles",
3: "Cold Drinks"
}
names = model.model.names
# -------------------------------
# Home route (fixes Not Found)
# -------------------------------
@app.get("/")
def home():
return {"message": "YOLO Video API is running!"}
# -------------------------------
# Extract frames from video
# -------------------------------
def extract_frames_from_video(video_bytes):
# Write uploaded file to temp file
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
tmp.write(video_bytes)
temp_path = tmp.name
cap = cv2.VideoCapture(temp_path)
frames = []
while True:
ret, frame = cap.read()
if not ret:
break
frames.append(frame)
cap.release()
return frames
# -------------------------------
# YOLO Counting Logic
# -------------------------------
def detect_inventory_frames(frames):
counts = {}
for frame in frames:
results = model(frame)
for r in results:
for box in r.boxes:
cls_id = int(box.cls)
name = names[cls_id]
counts[name] = counts.get(name, 0) + 1
return counts
# -------------------------------
# Detect endpoint
# -------------------------------
@app.post("/detect")
async def detect_inventory(file: UploadFile = File(...)):
data = await file.read()
# First try to decode as IMAGE
np_arr = np.frombuffer(data, np.uint8)
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
# If image decoding fails → treat as VIDEO
if frame is None:
frames = extract_frames_from_video(data)
if len(frames) == 0:
return {"error": "Invalid image/video"}
counts = detect_inventory_frames(frames)
return JSONResponse(counts)
# IMAGE path
results = model(frame)
counts = {}
for r in results:
for box in r.boxes:
cls_id = int(box.cls)
name = names[cls_id]
counts[name] = counts.get(name, 0) + 1
return JSONResponse(counts)