Spaces:
Sleeping
Sleeping
Update gully_drs_core/ball_detection.py
Browse files
gully_drs_core/ball_detection.py
CHANGED
|
@@ -2,11 +2,19 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
from deep_sort_realtime.deepsort_tracker import DeepSort
|
| 4 |
from scipy.interpolate import UnivariateSpline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Global tracker initialization
|
| 7 |
tracker = DeepSort(max_age=5)
|
| 8 |
|
| 9 |
-
def detect_ball_hsv(frame, hsv_values):
|
| 10 |
"""
|
| 11 |
Detects a ball in an image frame based on HSV color filtering.
|
| 12 |
|
|
@@ -57,7 +65,7 @@ def detect_ball_hsv(frame, hsv_values):
|
|
| 57 |
print(f"Error in HSV ball detection: {str(e)}")
|
| 58 |
return None, frame
|
| 59 |
|
| 60 |
-
def track_ball_and_estimate_speed(frame, ball_position, frame_rate=60):
|
| 61 |
"""Track ball and estimate speed with DeepSort."""
|
| 62 |
try:
|
| 63 |
dets = []
|
|
@@ -95,7 +103,7 @@ def track_ball_and_estimate_speed(frame, ball_position, frame_rate=60):
|
|
| 95 |
except Exception as e:
|
| 96 |
raise Exception(f"Error in ball tracking: {str(e)}")
|
| 97 |
|
| 98 |
-
def detect_lbw(frame, ball_position):
|
| 99 |
"""
|
| 100 |
Placeholder for LBW detection logic.
|
| 101 |
Implement based on your specific requirements.
|
|
@@ -109,4 +117,11 @@ def detect_lbw(frame, ball_position):
|
|
| 109 |
return annotated_frame
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Error in LBW detection: {str(e)}")
|
| 112 |
-
return frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from deep_sort_realtime.deepsort_tracker import DeepSort
|
| 4 |
from scipy.interpolate import UnivariateSpline
|
| 5 |
+
import platform
|
| 6 |
+
import asyncio
|
| 7 |
+
|
| 8 |
+
# Check if NumPy is available
|
| 9 |
+
try:
|
| 10 |
+
import numpy
|
| 11 |
+
except ImportError:
|
| 12 |
+
raise ImportError("NumPy is required but not found. Please install it with 'pip install numpy'.")
|
| 13 |
|
| 14 |
# Global tracker initialization
|
| 15 |
tracker = DeepSort(max_age=5)
|
| 16 |
|
| 17 |
+
async def detect_ball_hsv(frame, hsv_values):
|
| 18 |
"""
|
| 19 |
Detects a ball in an image frame based on HSV color filtering.
|
| 20 |
|
|
|
|
| 65 |
print(f"Error in HSV ball detection: {str(e)}")
|
| 66 |
return None, frame
|
| 67 |
|
| 68 |
+
async def track_ball_and_estimate_speed(frame, ball_position, frame_rate=60):
|
| 69 |
"""Track ball and estimate speed with DeepSort."""
|
| 70 |
try:
|
| 71 |
dets = []
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
raise Exception(f"Error in ball tracking: {str(e)}")
|
| 105 |
|
| 106 |
+
async def detect_lbw(frame, ball_position):
|
| 107 |
"""
|
| 108 |
Placeholder for LBW detection logic.
|
| 109 |
Implement based on your specific requirements.
|
|
|
|
| 117 |
return annotated_frame
|
| 118 |
except Exception as e:
|
| 119 |
print(f"Error in LBW detection: {str(e)}")
|
| 120 |
+
return frame
|
| 121 |
+
|
| 122 |
+
# Pyodide compatibility for asyncio
|
| 123 |
+
if platform.system() == "Emscripten":
|
| 124 |
+
asyncio.ensure_future(detect_ball_hsv(None, {})) # Example initialization
|
| 125 |
+
else:
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
asyncio.run(detect_ball_hsv(None, {}))
|