Replace inference-sdk with requests, pin Python 3.10
Browse files- README.md +1 -0
- requirements.txt +0 -1
- solution.py +52 -30
README.md
CHANGED
|
@@ -4,6 +4,7 @@ emoji: π¦
|
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
|
|
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
python_version: "3.10"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
requirements.txt
CHANGED
|
@@ -45,4 +45,3 @@ scipy
|
|
| 45 |
packaging
|
| 46 |
filelock==3.29.0
|
| 47 |
gradio
|
| 48 |
-
inference-sdk
|
|
|
|
| 45 |
packaging
|
| 46 |
filelock==3.29.0
|
| 47 |
gradio
|
|
|
solution.py
CHANGED
|
@@ -27,14 +27,38 @@ from transformers import pipeline as hf_pipeline
|
|
| 27 |
from ultralytics import YOLO
|
| 28 |
from paddleocr import PaddleOCR
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# ββ CONSTANTS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
COCO_PERSON = 0
|
|
@@ -329,17 +353,16 @@ class TrafficViolationDetector:
|
|
| 329 |
|
| 330 |
# Detect Wrong Way using Roboflow API
|
| 331 |
ww_boxes = []
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
print("[Warning] Wrong-way API error:", e)
|
| 343 |
|
| 344 |
def is_wrong_way(v_box):
|
| 345 |
for wb in ww_boxes:
|
|
@@ -376,17 +399,16 @@ class TrafficViolationDetector:
|
|
| 376 |
ww = is_wrong_way(car_box)
|
| 377 |
|
| 378 |
sb_viols = 0
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
print("[Warning] Seatbelt API error:", e)
|
| 390 |
|
| 391 |
# Check for violation first, then do plate OCR if violation exists
|
| 392 |
if sb_viols > 0 or ww:
|
|
|
|
| 27 |
from ultralytics import YOLO
|
| 28 |
from paddleocr import PaddleOCR
|
| 29 |
|
| 30 |
+
import requests as _requests
|
| 31 |
+
import base64 as _base64
|
| 32 |
+
|
| 33 |
+
ROBOFLOW_API_KEY = "SEsiEStxDAHdOx2SCo3k"
|
| 34 |
+
|
| 35 |
+
def _roboflow_infer(image_input, model_id):
|
| 36 |
+
"""Call the Roboflow serverless API using plain requests (no inference-sdk needed)."""
|
| 37 |
+
try:
|
| 38 |
+
if isinstance(image_input, str):
|
| 39 |
+
with open(image_input, "rb") as f:
|
| 40 |
+
img_bytes = f.read()
|
| 41 |
+
elif isinstance(image_input, np.ndarray):
|
| 42 |
+
_, buf = cv2.imencode(".jpg", image_input)
|
| 43 |
+
img_bytes = buf.tobytes()
|
| 44 |
+
else:
|
| 45 |
+
return {}
|
| 46 |
+
img_b64 = _base64.b64encode(img_bytes).decode("utf-8")
|
| 47 |
+
url = f"https://serverless.roboflow.com/{model_id}"
|
| 48 |
+
resp = _requests.post(
|
| 49 |
+
url,
|
| 50 |
+
params={"api_key": ROBOFLOW_API_KEY},
|
| 51 |
+
json={"image": [{"type": "base64", "value": img_b64}]},
|
| 52 |
+
timeout=30,
|
| 53 |
+
)
|
| 54 |
+
# Try v2 response format first, fall back to v1
|
| 55 |
+
data = resp.json()
|
| 56 |
+
if isinstance(data, list) and len(data) > 0:
|
| 57 |
+
return data[0]
|
| 58 |
+
return data
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"[Warning] Roboflow API error for {model_id}: {e}")
|
| 61 |
+
return {}
|
| 62 |
|
| 63 |
# ββ CONSTANTS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 64 |
COCO_PERSON = 0
|
|
|
|
| 353 |
|
| 354 |
# Detect Wrong Way using Roboflow API
|
| 355 |
ww_boxes = []
|
| 356 |
+
try:
|
| 357 |
+
result = _roboflow_infer(img, "wrong-way-driving-detection-gqdmg/1")
|
| 358 |
+
for pred in result.get('predictions', []):
|
| 359 |
+
if "wrong" in pred.get('class', '').lower():
|
| 360 |
+
px, py, pw, ph = pred['x'], pred['y'], pred['width'], pred['height']
|
| 361 |
+
wx1, wy1 = px - pw/2, py - ph/2
|
| 362 |
+
wx2, wy2 = px + pw/2, py + ph/2
|
| 363 |
+
ww_boxes.append([wx1, wy1, wx2, wy2])
|
| 364 |
+
except Exception as e:
|
| 365 |
+
print("[Warning] Wrong-way API error:", e)
|
|
|
|
| 366 |
|
| 367 |
def is_wrong_way(v_box):
|
| 368 |
for wb in ww_boxes:
|
|
|
|
| 399 |
ww = is_wrong_way(car_box)
|
| 400 |
|
| 401 |
sb_viols = 0
|
| 402 |
+
ccrop = img[max(0, y1):min(h_img, y2), max(0, x1):min(w_img, x2)]
|
| 403 |
+
if ccrop.size > 0:
|
| 404 |
+
try:
|
| 405 |
+
res = _roboflow_infer(ccrop, "seat-belt-detection-udcfg/5")
|
| 406 |
+
for pred in res.get('predictions', []):
|
| 407 |
+
cls_name = pred.get('class', '').lower()
|
| 408 |
+
if "no" in cls_name and "seatbelt" in cls_name:
|
| 409 |
+
sb_viols += 1
|
| 410 |
+
except Exception as e:
|
| 411 |
+
print("[Warning] Seatbelt API error:", e)
|
|
|
|
| 412 |
|
| 413 |
# Check for violation first, then do plate OCR if violation exists
|
| 414 |
if sb_viols > 0 or ww:
|