DurgaDeepak commited on
Commit
c5eda13
·
verified ·
1 Parent(s): 08c9cf1

Update core/describe_scene.py

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