Spaces:
Running
on
Zero
Running
on
Zero
Update core/describe_scene.py
Browse files- core/describe_scene.py +61 -59
core/describe_scene.py
CHANGED
@@ -1,59 +1,61 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import logging
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
description["scene_summary"]["
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
"
|
55 |
-
"
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import logging
|
3 |
+
|
4 |
+
# Setup logging
|
5 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
6 |
+
logger = logging.getLogger(__name__)
|
7 |
+
|
8 |
+
def describe_scene(detection=None, segmentation=None, depth=None):
|
9 |
+
"""
|
10 |
+
Generates a structured scene summary with metrics for detection, segmentation, and depth.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
detection (list): List of detected objects with class names and bounding boxes.
|
14 |
+
segmentation (numpy.ndarray): Segmentation mask as a 2D numpy array.
|
15 |
+
depth (numpy.ndarray): Depth map as a 2D numpy array.
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
dict: Structured scene description with metrics.
|
19 |
+
"""
|
20 |
+
logger.info("Generating scene summary...")
|
21 |
+
description = {"scene_summary": {}}
|
22 |
+
|
23 |
+
# Detection Summary with Metrics
|
24 |
+
if detection:
|
25 |
+
logger.info("Adding detection results to scene summary.")
|
26 |
+
description["scene_summary"]["objects"] = detection
|
27 |
+
confidences = [obj.get("confidence", 0) for obj in detection]
|
28 |
+
description["scene_summary"]["detection_metrics"] = {
|
29 |
+
"objects_detected": len(detection),
|
30 |
+
"average_confidence": float(np.mean(confidences)) if confidences else 0.0
|
31 |
+
}
|
32 |
+
|
33 |
+
# Segmentation Summary with Coverage Metrics
|
34 |
+
if segmentation is not None:
|
35 |
+
logger.info("Summarizing segmentation coverage.")
|
36 |
+
unique, counts = np.unique(segmentation, return_counts=True)
|
37 |
+
total = segmentation.size
|
38 |
+
coverage = [
|
39 |
+
{"class_id": int(class_id), "coverage": f"{(count / total) * 100:.2f}%"}
|
40 |
+
for class_id, count in zip(unique, counts)
|
41 |
+
]
|
42 |
+
dominant_class = max(coverage, key=lambda x: float(x["coverage"].strip('%')))
|
43 |
+
description["scene_summary"]["segmentation_summary"] = coverage
|
44 |
+
description["scene_summary"]["dominant_class"] = dominant_class
|
45 |
+
|
46 |
+
# Depth Summary with Metrics
|
47 |
+
if depth is not None:
|
48 |
+
logger.info("Summarizing depth information.")
|
49 |
+
mean_depth = float(np.mean(depth))
|
50 |
+
min_depth = float(np.min(depth))
|
51 |
+
max_depth = float(np.max(depth))
|
52 |
+
std_depth = float(np.std(depth))
|
53 |
+
description["scene_summary"]["depth_summary"] = {
|
54 |
+
"mean_depth": mean_depth,
|
55 |
+
"min_depth": min_depth,
|
56 |
+
"max_depth": max_depth,
|
57 |
+
"std_depth": std_depth
|
58 |
+
}
|
59 |
+
|
60 |
+
logger.info("Scene summary generation complete.")
|
61 |
+
return description
|