Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,6 @@ import cv2
|
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
-
from ultralytics import YOLO
|
6 |
-
import time
|
7 |
import os
|
8 |
import json
|
9 |
import logging
|
@@ -11,6 +9,12 @@ import matplotlib.pyplot as plt
|
|
11 |
from datetime import datetime
|
12 |
from collections import Counter
|
13 |
from typing import List, Dict, Any, Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Set up logging
|
16 |
logging.basicConfig(
|
@@ -38,7 +42,7 @@ frame_count: int = 0
|
|
38 |
# Debug: Check environment
|
39 |
print(f"Torch version: {torch.__version__}")
|
40 |
print(f"Gradio version: {gr.__version__}")
|
41 |
-
print(f"Ultralytics version: {
|
42 |
print(f"CUDA available: {torch.cuda.is_available()}")
|
43 |
|
44 |
# Load custom YOLO model
|
@@ -49,9 +53,8 @@ if device == "cuda":
|
|
49 |
model.half() # Use half-precision (FP16)
|
50 |
print(f"Model classes: {model.names}")
|
51 |
|
52 |
-
# Mock service functions
|
53 |
def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) -> str:
|
54 |
-
"""Mock map generation: returns a placeholder image path."""
|
55 |
map_path = "map_temp.png"
|
56 |
plt.figure(figsize=(4, 4))
|
57 |
plt.scatter([x[1] for x in gps_coords], [x[0] for x in gps_coords], c='blue', label='GPS Points')
|
@@ -64,11 +67,9 @@ def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) ->
|
|
64 |
return map_path
|
65 |
|
66 |
def send_to_salesforce(data: Dict[str, Any]) -> None:
|
67 |
-
"""Mock Salesforce dispatch: logs data."""
|
68 |
logging.info(f"Mock Salesforce dispatch: {json.dumps(data, indent=2)}")
|
69 |
|
70 |
def update_metrics(detections: List[Dict[str, Any]]) -> Dict[str, Any]:
|
71 |
-
"""Compute detection metrics."""
|
72 |
counts = Counter([det["label"] for det in detections])
|
73 |
return {
|
74 |
"items": [{"type": k, "count": v} for k, v in counts.items()],
|
@@ -77,7 +78,6 @@ def update_metrics(detections: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
77 |
}
|
78 |
|
79 |
def generate_line_chart() -> Optional[str]:
|
80 |
-
"""Generate detection trend chart."""
|
81 |
if not detected_counts:
|
82 |
return None
|
83 |
plt.figure(figsize=(4, 2))
|
@@ -148,7 +148,7 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
148 |
for detection in results[0].boxes:
|
149 |
cls = int(detection.cls)
|
150 |
conf = float(detection.conf)
|
151 |
-
box = detection.xyxy[0].cpu().numpy().astype(int).tolist()
|
152 |
label = model.names[cls]
|
153 |
frame_detections.append({"label": label, "box": box, "conf": conf})
|
154 |
log_entries.append(f"Frame {frame_count}: Detected {label} with confidence {conf:.2f}")
|
@@ -164,7 +164,7 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
164 |
frame_path = os.path.join(OUTPUT_DIR, f"frame_{frame_count:04d}.jpg")
|
165 |
cv2.imwrite(frame_path, annotated_frame)
|
166 |
|
167 |
-
gps_coord = [17.385044 + (frame_count * 0.0001), 78.486671 + (frame_count * 0.0001)]
|
168 |
gps_coordinates.append(gps_coord)
|
169 |
for det in frame_detections:
|
170 |
det["gps"] = gps_coord
|
|
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
|
|
|
|
5 |
import os
|
6 |
import json
|
7 |
import logging
|
|
|
9 |
from datetime import datetime
|
10 |
from collections import Counter
|
11 |
from typing import List, Dict, Any, Optional
|
12 |
+
from ultralytics import YOLO
|
13 |
+
import ultralytics
|
14 |
+
import time
|
15 |
+
|
16 |
+
# Set YOLO config directory early to avoid warning
|
17 |
+
os.environ["YOLO_CONFIG_DIR"] = "/tmp/Ultralytics"
|
18 |
|
19 |
# Set up logging
|
20 |
logging.basicConfig(
|
|
|
42 |
# Debug: Check environment
|
43 |
print(f"Torch version: {torch.__version__}")
|
44 |
print(f"Gradio version: {gr.__version__}")
|
45 |
+
print(f"Ultralytics version: {ultralytics.__version__}") # Fixed: Use ultralytics.__version__
|
46 |
print(f"CUDA available: {torch.cuda.is_available()}")
|
47 |
|
48 |
# Load custom YOLO model
|
|
|
53 |
model.half() # Use half-precision (FP16)
|
54 |
print(f"Model classes: {model.names}")
|
55 |
|
56 |
+
# Mock service functions
|
57 |
def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) -> str:
|
|
|
58 |
map_path = "map_temp.png"
|
59 |
plt.figure(figsize=(4, 4))
|
60 |
plt.scatter([x[1] for x in gps_coords], [x[0] for x in gps_coords], c='blue', label='GPS Points')
|
|
|
67 |
return map_path
|
68 |
|
69 |
def send_to_salesforce(data: Dict[str, Any]) -> None:
|
|
|
70 |
logging.info(f"Mock Salesforce dispatch: {json.dumps(data, indent=2)}")
|
71 |
|
72 |
def update_metrics(detections: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
|
73 |
counts = Counter([det["label"] for det in detections])
|
74 |
return {
|
75 |
"items": [{"type": k, "count": v} for k, v in counts.items()],
|
|
|
78 |
}
|
79 |
|
80 |
def generate_line_chart() -> Optional[str]:
|
|
|
81 |
if not detected_counts:
|
82 |
return None
|
83 |
plt.figure(figsize=(4, 2))
|
|
|
148 |
for detection in results[0].boxes:
|
149 |
cls = int(detection.cls)
|
150 |
conf = float(detection.conf)
|
151 |
+
box = detection.xyxy[0].cpu().numpy().astype(int).tolist()
|
152 |
label = model.names[cls]
|
153 |
frame_detections.append({"label": label, "box": box, "conf": conf})
|
154 |
log_entries.append(f"Frame {frame_count}: Detected {label} with confidence {conf:.2f}")
|
|
|
164 |
frame_path = os.path.join(OUTPUT_DIR, f"frame_{frame_count:04d}.jpg")
|
165 |
cv2.imwrite(frame_path, annotated_frame)
|
166 |
|
167 |
+
gps_coord = [17.385044 + (frame_count * 0.0001), 78.486671 + (frame_count * 0.0001)]
|
168 |
gps_coordinates.append(gps_coord)
|
169 |
for det in frame_detections:
|
170 |
det["gps"] = gps_coord
|