Vaibhava Lakshmi commited on
Commit
8ffab0d
·
verified ·
1 Parent(s): 2dc10d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -2,6 +2,10 @@ import os, json, cv2, numpy as np, gradio as gr
2
  from dataclasses import dataclass
3
  from typing import List, Dict, Tuple
4
 
 
 
 
 
5
  # ---------- Config ----------
6
  @dataclass
7
  class InspectConfig:
@@ -40,11 +44,11 @@ YOLO_MODEL_REPO = os.getenv("YOLO_MODEL_REPO", "") # e.g. "vaibhavi18092002
40
  YOLO_MODEL_FILE = os.getenv("YOLO_MODEL_FILE", "best.pt")
41
 
42
  def _ensure_weights():
 
43
  global YOLO_MODEL_PATH
44
  if os.path.exists(YOLO_MODEL_PATH):
45
  return
46
  if YOLO_MODEL_REPO:
47
- # single-file download from a HF model repo
48
  try:
49
  from huggingface_hub import hf_hub_download
50
  YOLO_MODEL_PATH = hf_hub_download(
@@ -71,6 +75,8 @@ def detect_boxes(frame: np.ndarray, cfg: InspectConfig = CFG) -> List[Dict]:
71
  res = _get_yolo().predict(source=frame, verbose=False, conf=0.25)[0]
72
  boxes = []
73
  H, W = frame.shape[:2]
 
 
74
  for x1, y1, x2, y2 in res.boxes.xyxy.cpu().numpy():
75
  x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
76
  x1, y1 = max(0, x1), max(0, y1)
@@ -309,8 +315,12 @@ with gr.Blocks(theme=theme, css=custom_css, fill_height=True) as demo:
309
  with gr.Column(scale=1, elem_classes=["section"]):
310
  dl_img = gr.File(label="Download annotated image")
311
  dl_json = gr.File(label="Download JSON report")
312
- img_btn.click(ui_image, inputs=[img_in, size_tol, appearance_thr, solidity_thr, seal_frac],
313
- outputs=[img_out, json_out, dl_img, dl_json])
 
 
 
 
314
  with gr.Tab("Video"):
315
  with gr.Row():
316
  with gr.Column(scale=1, elem_classes=["section"]):
@@ -324,8 +334,12 @@ with gr.Blocks(theme=theme, css=custom_css, fill_height=True) as demo:
324
  with gr.Column(scale=1, elem_classes=["section"]):
325
  dl_vid = gr.File(label="Download annotated video")
326
  dl_jsonl = gr.File(label="Download JSONL report")
327
- vid_btn.click(ui_video, inputs=[vid_in, size_tol, appearance_thr, solidity_thr, seal_frac],
328
- outputs=[vid_out, vjson_out, dl_vid, dl_jsonl])
 
 
 
 
329
 
330
  # In Spaces: no share=True, no host/port tweaks needed.
331
- demo.queue(concurrency_count=2, max_size=32).launch()
 
2
  from dataclasses import dataclass
3
  from typing import List, Dict, Tuple
4
 
5
+ # --- Quiet Ultralytics settings warning and ensure a writeable config dir ---
6
+ os.environ.setdefault("YOLO_CONFIG_DIR", "/tmp/Ultralytics")
7
+ os.makedirs("/tmp/Ultralytics", exist_ok=True)
8
+
9
  # ---------- Config ----------
10
  @dataclass
11
  class InspectConfig:
 
44
  YOLO_MODEL_FILE = os.getenv("YOLO_MODEL_FILE", "best.pt")
45
 
46
  def _ensure_weights():
47
+ """Ensure YOLO_MODEL_PATH exists; if not, try to download from a HF model repo."""
48
  global YOLO_MODEL_PATH
49
  if os.path.exists(YOLO_MODEL_PATH):
50
  return
51
  if YOLO_MODEL_REPO:
 
52
  try:
53
  from huggingface_hub import hf_hub_download
54
  YOLO_MODEL_PATH = hf_hub_download(
 
75
  res = _get_yolo().predict(source=frame, verbose=False, conf=0.25)[0]
76
  boxes = []
77
  H, W = frame.shape[:2]
78
+ if res.boxes is None or len(res.boxes) == 0:
79
+ return boxes
80
  for x1, y1, x2, y2 in res.boxes.xyxy.cpu().numpy():
81
  x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
82
  x1, y1 = max(0, x1), max(0, y1)
 
315
  with gr.Column(scale=1, elem_classes=["section"]):
316
  dl_img = gr.File(label="Download annotated image")
317
  dl_json = gr.File(label="Download JSON report")
318
+ img_btn.click(
319
+ ui_image,
320
+ inputs=[img_in, size_tol, appearance_thr, solidity_thr, seal_frac],
321
+ outputs=[img_out, json_out, dl_img, dl_json],
322
+ concurrency_limit=2 # Gradio 4: per-event limit
323
+ )
324
  with gr.Tab("Video"):
325
  with gr.Row():
326
  with gr.Column(scale=1, elem_classes=["section"]):
 
334
  with gr.Column(scale=1, elem_classes=["section"]):
335
  dl_vid = gr.File(label="Download annotated video")
336
  dl_jsonl = gr.File(label="Download JSONL report")
337
+ vid_btn.click(
338
+ ui_video,
339
+ inputs=[vid_in, size_tol, appearance_thr, solidity_thr, seal_frac],
340
+ outputs=[vid_out, vjson_out, dl_vid, dl_jsonl],
341
+ concurrency_limit=1 # keep video runs serialized
342
+ )
343
 
344
  # In Spaces: no share=True, no host/port tweaks needed.
345
+ demo.queue(max_size=32).launch()