LiamKhoaLe commited on
Commit
bc634a9
·
1 Parent(s): 427ee43

Update API handler

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -303,15 +303,16 @@ def stream(uid:str):
303
  # ─── Detect animal/wildlife ─────────────────────────────────────────────────
304
  # Init clients
305
  # https://universe.roboflow.com/team-hope-mmcyy/hydroquest
306
- robo_fish = InferenceHTTPClient(
307
- api_url="https://detect.roboflow.com",
308
- api_key=os.getenv("ROBOFLOW_KEY", "")
309
- )
310
- # https://universe.roboflow.com/sky-sd2zq/bird_only-pt0bm/model/1
311
- robo_bird = InferenceHTTPClient(
312
- api_url="https://detect.roboflow.com",
313
- api_key=os.getenv("ROBOFLOW_KEY", "")
314
- )
 
315
  # Animal detection endpoint (animal, fish, bird as target classes)
316
  @app.post("/animal/")
317
  async def detect_animals(file: UploadFile = File(...)):
@@ -342,8 +343,12 @@ async def detect_animals(file: UploadFile = File(...)):
342
  # 2. Roboflow Fish
343
  try:
344
  print("[Animal] Detecting via Roboflow Fish model…")
345
- fish_preds = robo_fish.infer(img_path, model_id="hydroquest/1")
346
- for pred in fish_preds.get("predictions", []):
 
 
 
 
347
  if pred["confidence"] >= 0.75:
348
  x1 = int(pred["x"] - pred["width"] / 2)
349
  y1 = int(pred["y"] - pred["height"] / 2)
@@ -356,8 +361,12 @@ async def detect_animals(file: UploadFile = File(...)):
356
  # 3. Roboflow Bird
357
  try:
358
  print("[Animal] Detecting via Roboflow Bird model…")
359
- bird_preds = robo_bird.infer(img_path, model_id="bird_only-pt0bm/1")
360
- for pred in bird_preds.get("predictions", []):
 
 
 
 
361
  if pred["confidence"] >= 0.75:
362
  x1 = int(pred["x"] - pred["width"] / 2)
363
  y1 = int(pred["y"] - pred["height"] / 2)
@@ -366,6 +375,7 @@ async def detect_animals(file: UploadFile = File(...)):
366
  detections.append(((x1, y1, x2, y2), "Bird Alert"))
367
  except Exception as e:
368
  print("[Roboflow Bird Error]", e)
 
369
  # Count detection
370
  print(f"[Animal] Total detections: {len(detections)}")
371
  # Write label
 
303
  # ─── Detect animal/wildlife ─────────────────────────────────────────────────
304
  # Init clients
305
  # https://universe.roboflow.com/team-hope-mmcyy/hydroquest
306
+ import base64, requests
307
+ def roboflow_infer(image_path, api_url, api_key):
308
+ with open(image_path, "rb") as f:
309
+ img_b64 = base64.b64encode(f.read()).decode()
310
+ res = requests.post(api_url, json={
311
+ "api_key": api_key,
312
+ "image": img_b64
313
+ }, headers={"Content-Type": "application/json"})
314
+ return res.json()
315
+
316
  # Animal detection endpoint (animal, fish, bird as target classes)
317
  @app.post("/animal/")
318
  async def detect_animals(file: UploadFile = File(...)):
 
343
  # 2. Roboflow Fish
344
  try:
345
  print("[Animal] Detecting via Roboflow Fish model…")
346
+ fish_response = roboflow_infer(
347
+ img_path,
348
+ "https://detect.roboflow.com/hydroquest/1",
349
+ api_url=os.getenv("ROBOFLOW_KEY", "")
350
+ )
351
+ for pred in fish_response.get("predictions", []):
352
  if pred["confidence"] >= 0.75:
353
  x1 = int(pred["x"] - pred["width"] / 2)
354
  y1 = int(pred["y"] - pred["height"] / 2)
 
361
  # 3. Roboflow Bird
362
  try:
363
  print("[Animal] Detecting via Roboflow Bird model…")
364
+ bird_response = roboflow_infer(
365
+ img_path,
366
+ "https://detect.roboflow.com/bird_only-pt0bm/1",
367
+ api_url=os.getenv("ROBOFLOW_KEY", "")
368
+ )
369
+ for pred in bird_response.get("predictions", []):
370
  if pred["confidence"] >= 0.75:
371
  x1 = int(pred["x"] - pred["width"] / 2)
372
  y1 = int(pred["y"] - pred["height"] / 2)
 
375
  detections.append(((x1, y1, x2, y2), "Bird Alert"))
376
  except Exception as e:
377
  print("[Roboflow Bird Error]", e)
378
+
379
  # Count detection
380
  print(f"[Animal] Total detections: {len(detections)}")
381
  # Write label