Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,15 +20,19 @@ import pandas as pd
|
|
| 20 |
# Suppress all warnings
|
| 21 |
warnings.filterwarnings("ignore")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
yolo_config_dir = "/tmp/Ultralytics"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
os.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# --- Logging Configuration ---
|
| 31 |
-
# Configure logging before any other operations
|
| 32 |
logging.basicConfig(
|
| 33 |
level=logging.INFO,
|
| 34 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
@@ -62,18 +66,14 @@ class YOLOv8Model:
|
|
| 62 |
def __init__(self, model_path='yolov8n.pt'):
|
| 63 |
try:
|
| 64 |
logger.info("Initializing YOLOv8 model")
|
| 65 |
-
|
| 66 |
-
# Temporarily suppress stdout for model loading
|
| 67 |
original_stdout = sys.stdout
|
| 68 |
sys.stdout = open(os.devnull, 'w')
|
| 69 |
-
|
| 70 |
try:
|
| 71 |
self.model = YOLO(model_path)
|
|
|
|
| 72 |
finally:
|
| 73 |
sys.stdout.close()
|
| 74 |
sys.stdout = original_stdout
|
| 75 |
-
|
| 76 |
-
logger.info("YOLOv8 model loaded successfully")
|
| 77 |
except Exception as e:
|
| 78 |
logger.error(f"Failed to load YOLOv8 model: {e}")
|
| 79 |
raise
|
|
@@ -98,6 +98,7 @@ def preprocess_frame(frame):
|
|
| 98 |
|
| 99 |
def capture_rtsp_frames(rtsp_url: str):
|
| 100 |
try:
|
|
|
|
| 101 |
cap = cv2.VideoCapture(rtsp_url)
|
| 102 |
if not cap.isOpened():
|
| 103 |
logger.error(f"Failed to open RTSP stream: {rtsp_url}")
|
|
@@ -122,6 +123,7 @@ def save_snapshot(frame):
|
|
| 122 |
filename = f"snapshot_{int(time.time())}.jpg"
|
| 123 |
snapshot_path = Path("/snapshots") / filename
|
| 124 |
os.makedirs("/snapshots", exist_ok=True)
|
|
|
|
| 125 |
cv2.imwrite(str(snapshot_path), frame)
|
| 126 |
return f"/snapshots/{filename}"
|
| 127 |
except Exception as e:
|
|
@@ -181,7 +183,6 @@ async def lifespan(app: FastAPI):
|
|
| 181 |
logger.error(f"Startup error: {e}")
|
| 182 |
raise
|
| 183 |
yield
|
| 184 |
-
logger
|
| 185 |
logger.info("FastAPI application shutting down")
|
| 186 |
yolo_model = None
|
| 187 |
|
|
|
|
| 20 |
# Suppress all warnings
|
| 21 |
warnings.filterwarnings("ignore")
|
| 22 |
|
| 23 |
+
# Set up YOLO_CONFIG_DIR
|
| 24 |
yolo_config_dir = "/tmp/Ultralytics"
|
| 25 |
+
try:
|
| 26 |
+
if os.path.exists(yolo_config_dir):
|
| 27 |
+
shutil.rmtree(yolo_config_dir)
|
| 28 |
+
os.makedirs(yolo_config_dir, exist_ok=True)
|
| 29 |
+
os.chmod(yolo_config_dir, 0o777) # Ensure directory is writable
|
| 30 |
+
os.environ["YOLO_CONFIG_DIR"] = yolo_config_dir
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Failed to set up YOLO_CONFIG_DIR: {e}")
|
| 33 |
+
raise
|
| 34 |
|
| 35 |
# --- Logging Configuration ---
|
|
|
|
| 36 |
logging.basicConfig(
|
| 37 |
level=logging.INFO,
|
| 38 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
| 66 |
def __init__(self, model_path='yolov8n.pt'):
|
| 67 |
try:
|
| 68 |
logger.info("Initializing YOLOv8 model")
|
|
|
|
|
|
|
| 69 |
original_stdout = sys.stdout
|
| 70 |
sys.stdout = open(os.devnull, 'w')
|
|
|
|
| 71 |
try:
|
| 72 |
self.model = YOLO(model_path)
|
| 73 |
+
logger.info("YOLOv8 model loaded successfully")
|
| 74 |
finally:
|
| 75 |
sys.stdout.close()
|
| 76 |
sys.stdout = original_stdout
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
logger.error(f"Failed to load YOLOv8 model: {e}")
|
| 79 |
raise
|
|
|
|
| 98 |
|
| 99 |
def capture_rtsp_frames(rtsp_url: str):
|
| 100 |
try:
|
| 101 |
+
logger.info(f"Attempting to connect to RTSP stream: {rtsp_url}")
|
| 102 |
cap = cv2.VideoCapture(rtsp_url)
|
| 103 |
if not cap.isOpened():
|
| 104 |
logger.error(f"Failed to open RTSP stream: {rtsp_url}")
|
|
|
|
| 123 |
filename = f"snapshot_{int(time.time())}.jpg"
|
| 124 |
snapshot_path = Path("/snapshots") / filename
|
| 125 |
os.makedirs("/snapshots", exist_ok=True)
|
| 126 |
+
os.chmod("/snapshots", 0o777) # Ensure directory is writable
|
| 127 |
cv2.imwrite(str(snapshot_path), frame)
|
| 128 |
return f"/snapshots/{filename}"
|
| 129 |
except Exception as e:
|
|
|
|
| 183 |
logger.error(f"Startup error: {e}")
|
| 184 |
raise
|
| 185 |
yield
|
|
|
|
| 186 |
logger.info("FastAPI application shutting down")
|
| 187 |
yolo_model = None
|
| 188 |
|