Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import sys
|
|
| 3 |
import subprocess
|
| 4 |
import logging
|
| 5 |
import warnings
|
| 6 |
-
from importlib import import_module
|
| 7 |
import cv2
|
| 8 |
import gradio as gr
|
| 9 |
import torch
|
|
@@ -21,58 +20,66 @@ import uuid
|
|
| 21 |
from multiprocessing import Pool, cpu_count
|
| 22 |
from functools import partial
|
| 23 |
|
| 24 |
-
# Handle Ultralytics config directory warning
|
| 25 |
-
os.environ['YOLO_CONFIG_DIR'] = '/tmp/Ultralytics'
|
| 26 |
-
os.makedirs('/tmp/Ultralytics', exist_ok=True)
|
| 27 |
-
|
| 28 |
# ==========================
|
| 29 |
-
#
|
| 30 |
# ==========================
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# Setup logging
|
| 34 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 35 |
logger = logging.getLogger(__name__)
|
| 36 |
|
| 37 |
-
try:
|
| 38 |
-
from bytetrack import BYTETracker
|
| 39 |
-
logger.info("ByteTrack successfully imported")
|
| 40 |
-
except ImportError:
|
| 41 |
-
logger.warning("ByteTrack not found, installing dependencies...")
|
| 42 |
-
|
| 43 |
-
subprocess.run(
|
| 44 |
-
[sys.executable, "-m", "pip", "install", "-q", "cython", "lap", "numpy"],
|
| 45 |
-
check=True
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
subprocess.run(
|
| 49 |
-
[sys.executable, "-m", "pip", "install", "-q", "git+https://github.com/ifzhang/ByteTrack.git@main"],
|
| 50 |
-
check=True
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
for module in list(sys.modules.keys()):
|
| 54 |
-
if module.startswith('bytetrack') or module.startswith('yolox'):
|
| 55 |
-
del sys.modules[module]
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
-
from bytetrack import BYTETracker
|
| 59 |
-
logger.info("ByteTrack successfully installed and imported")
|
| 60 |
-
except ImportError as e:
|
| 61 |
-
logger.error(f"Failed to install/import ByteTrack: {e}")
|
| 62 |
-
raise RuntimeError("Could not initialize ByteTrack tracker") from e
|
| 63 |
-
|
| 64 |
-
warnings.filterwarnings("default")
|
| 65 |
# ==========================
|
| 66 |
-
# ByteTrack
|
| 67 |
# ==========================
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# ==========================
|
| 78 |
# Optimized Configuration
|
|
|
|
| 3 |
import subprocess
|
| 4 |
import logging
|
| 5 |
import warnings
|
|
|
|
| 6 |
import cv2
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
|
|
|
| 20 |
from multiprocessing import Pool, cpu_count
|
| 21 |
from functools import partial
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# ==========================
|
| 24 |
+
# Configuration and Setup
|
| 25 |
# ==========================
|
| 26 |
+
# Handle Ultralytics config directory
|
| 27 |
+
os.environ['YOLO_CONFIG_DIR'] = '/tmp/Ultralytics'
|
| 28 |
+
os.makedirs('/tmp/Ultralytics', exist_ok=True)
|
| 29 |
|
| 30 |
+
# Setup logging
|
| 31 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 32 |
logger = logging.getLogger(__name__)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# ==========================
|
| 35 |
+
# ByteTrack Implementation
|
| 36 |
# ==========================
|
| 37 |
+
class BYTETracker:
|
| 38 |
+
"""Custom implementation of ByteTrack to avoid installation issues"""
|
| 39 |
+
def __init__(self, track_thresh=0.5, track_buffer=30, match_thresh=0.8, frame_rate=30):
|
| 40 |
+
try:
|
| 41 |
+
from yolox.tracker.byte_tracker import BYTETracker as OriginalBYTETracker
|
| 42 |
+
self.tracker = OriginalBYTETracker(
|
| 43 |
+
track_thresh=track_thresh,
|
| 44 |
+
track_buffer=track_buffer,
|
| 45 |
+
match_thresh=match_thresh,
|
| 46 |
+
frame_rate=frame_rate
|
| 47 |
+
)
|
| 48 |
+
self._original = True
|
| 49 |
+
except ImportError:
|
| 50 |
+
logger.warning("Using simplified ByteTrack implementation")
|
| 51 |
+
self._original = False
|
| 52 |
+
self.track_thresh = track_thresh
|
| 53 |
+
self.track_buffer = track_buffer
|
| 54 |
+
self.match_thresh = match_thresh
|
| 55 |
+
self.frame_rate = frame_rate
|
| 56 |
+
self.tracked_objects = {}
|
| 57 |
+
self.next_id = 1
|
| 58 |
+
|
| 59 |
+
def update(self, dets, scores, cls):
|
| 60 |
+
if self._original:
|
| 61 |
+
return self.tracker.update(dets, scores, cls)
|
| 62 |
+
|
| 63 |
+
# Simplified tracking logic for fallback
|
| 64 |
+
if len(dets) == 0:
|
| 65 |
+
return []
|
| 66 |
+
|
| 67 |
+
tracks = []
|
| 68 |
+
for i, (det, score, cl) in enumerate(zip(dets, scores, cls)):
|
| 69 |
+
if score < self.track_thresh:
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
x, y, w, h = det
|
| 73 |
+
track_id = self.next_id
|
| 74 |
+
self.next_id += 1
|
| 75 |
+
tracks.append({
|
| 76 |
+
'id': track_id,
|
| 77 |
+
'bbox': [x, y, w, h],
|
| 78 |
+
'score': score,
|
| 79 |
+
'cls': cl
|
| 80 |
+
})
|
| 81 |
+
return tracks
|
| 82 |
+
|
| 83 |
|
| 84 |
# ==========================
|
| 85 |
# Optimized Configuration
|