Spaces:
Sleeping
Sleeping
Denny Lulak
commited on
Commit
·
dbae2f1
1
Parent(s):
a0d19b2
fix
Browse files- api_service.py +49 -0
- model.onnx +3 -0
- requirements.txt +7 -0
api_service.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
import onnxruntime as ort
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Add CORS if calling from browser
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Configuration (same as your Gradio app)
|
| 19 |
+
MODEL_ONNX_PATH = "model.onnx"
|
| 20 |
+
INPUT_SIZE = 640
|
| 21 |
+
CLASS_NAMES = ["class0", "class1"]
|
| 22 |
+
CONF_THRESHOLD = 0.5
|
| 23 |
+
IOU_THRESHOLD = 0.45
|
| 24 |
+
|
| 25 |
+
# Load ONNX model (from your existing code)
|
| 26 |
+
ort_session = ort.InferenceSession(
|
| 27 |
+
MODEL_ONNX_PATH,
|
| 28 |
+
providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
@app.post("/detect")
|
| 32 |
+
async def detect(file: UploadFile = File(...)):
|
| 33 |
+
# Read image
|
| 34 |
+
contents = await file.read()
|
| 35 |
+
nparr = np.frombuffer(contents, np.uint8)
|
| 36 |
+
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 37 |
+
|
| 38 |
+
# Use your existing predict() function
|
| 39 |
+
results = predict(image)
|
| 40 |
+
return results
|
| 41 |
+
|
| 42 |
+
# Copy your existing predict() and helper functions here
|
| 43 |
+
def predict(image: np.ndarray) -> dict:
|
| 44 |
+
# ... paste all your processing logic from app.py ...
|
| 45 |
+
return {"detections": [...]}
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
import uvicorn
|
| 49 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
model.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39582e24d58748bb0304397f40f5d88bff8f7c7a1584bbec11e9f9490fb12690
|
| 3 |
+
size 37908587
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
numpy
|
| 3 |
+
onnxruntime-gpu
|
| 4 |
+
opencv-python
|
| 5 |
+
Pillow
|
| 6 |
+
torch
|
| 7 |
+
ultralytics
|