Spaces:
Running
on
Zero
Running
on
Zero
Upload enhanced_scene_describer.py
Browse files- enhanced_scene_describer.py +18 -33
enhanced_scene_describer.py
CHANGED
|
@@ -123,7 +123,7 @@ class EnhancedSceneDescriber:
|
|
| 123 |
def generate_description(self, scene_type: str, detected_objects: List[Dict], confidence: float,
|
| 124 |
lighting_info: Dict, functional_zones: List[str], enable_landmark: bool = True,
|
| 125 |
scene_scores: Optional[Dict] = None, spatial_analysis: Optional[Dict] = None,
|
| 126 |
-
image_dimensions: Optional[Tuple[int, int]] = None,
|
| 127 |
places365_info: Optional[Dict] = None,
|
| 128 |
object_statistics: Optional[Dict] = None) -> str:
|
| 129 |
try:
|
|
@@ -815,47 +815,36 @@ class EnhancedSceneDescriber:
|
|
| 815 |
max_categories_to_return: Optional[int] = None,
|
| 816 |
max_total_objects: Optional[int] = None) -> List[Dict]:
|
| 817 |
"""
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
Args:
|
| 821 |
-
detected_objects: 檢測到的物件列表
|
| 822 |
-
min_prominence_score: 最小重要性分數閾值,預設為0.5
|
| 823 |
-
max_categories_to_return: 可選的最大返回類別數量限制
|
| 824 |
-
max_total_objects: 可選的最大返回物件總數限制
|
| 825 |
-
|
| 826 |
-
Returns:
|
| 827 |
-
List[Dict]: 重要物件列表
|
| 828 |
"""
|
| 829 |
try:
|
| 830 |
-
#
|
| 831 |
prominent_objects = self.object_description_generator.get_prominent_objects(
|
| 832 |
detected_objects,
|
| 833 |
min_prominence_score,
|
| 834 |
-
max_categories_to_return
|
| 835 |
)
|
| 836 |
|
| 837 |
-
#
|
| 838 |
if max_total_objects is not None and max_total_objects > 0:
|
| 839 |
-
# 限制總物件數量,保持重要性排序
|
| 840 |
prominent_objects = prominent_objects[:max_total_objects]
|
| 841 |
|
| 842 |
-
#
|
| 843 |
if max_categories_to_return is not None and max_categories_to_return > 0:
|
| 844 |
-
# 按類別分組物件
|
| 845 |
categories_seen = set()
|
| 846 |
filtered_objects = []
|
| 847 |
|
| 848 |
for obj in prominent_objects:
|
| 849 |
class_name = obj.get("class_name", "unknown")
|
|
|
|
|
|
|
| 850 |
if class_name not in categories_seen:
|
| 851 |
-
categories_seen
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
#
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
elif class_name in categories_seen:
|
| 858 |
-
# 如果是已見過的類別,仍然添加該物件
|
| 859 |
filtered_objects.append(obj)
|
| 860 |
|
| 861 |
return filtered_objects
|
|
@@ -1033,9 +1022,11 @@ class EnhancedSceneDescriber:
|
|
| 1033 |
|
| 1034 |
Returns:
|
| 1035 |
模板內容
|
| 1036 |
-
"""
|
|
|
|
| 1037 |
return self.template_manager.get_template(category, key)
|
| 1038 |
|
|
|
|
| 1039 |
def get_viewpoint_confidence(self, detected_objects: List[Dict]) -> Tuple[str, float]:
|
| 1040 |
"""
|
| 1041 |
獲取視角檢測結果及其信心度
|
|
@@ -1105,12 +1096,6 @@ class EnhancedSceneDescriber:
|
|
| 1105 |
self.logger.warning(f"Error getting text statistics: {str(e)}")
|
| 1106 |
return {"characters": 0, "words": 0, "sentences": 0}
|
| 1107 |
|
| 1108 |
-
def reload_templates(self):
|
| 1109 |
-
"""
|
| 1110 |
-
重新載入所有模板
|
| 1111 |
-
"""
|
| 1112 |
-
self.template_manager.reload_templates()
|
| 1113 |
-
|
| 1114 |
def get_configuration(self) -> Dict[str, Any]:
|
| 1115 |
"""
|
| 1116 |
獲取當前配置信息
|
|
@@ -1138,4 +1123,4 @@ class EnhancedSceneDescriber:
|
|
| 1138 |
region_analyzer=self.region_analyzer
|
| 1139 |
)
|
| 1140 |
except Exception as e:
|
| 1141 |
-
self.logger.error(f"Fallback component initialization failed: {str(e)}")
|
|
|
|
| 123 |
def generate_description(self, scene_type: str, detected_objects: List[Dict], confidence: float,
|
| 124 |
lighting_info: Dict, functional_zones: List[str], enable_landmark: bool = True,
|
| 125 |
scene_scores: Optional[Dict] = None, spatial_analysis: Optional[Dict] = None,
|
| 126 |
+
image_dimensions: Optional[Tuple[int, int]] = None,
|
| 127 |
places365_info: Optional[Dict] = None,
|
| 128 |
object_statistics: Optional[Dict] = None) -> str:
|
| 129 |
try:
|
|
|
|
| 815 |
max_categories_to_return: Optional[int] = None,
|
| 816 |
max_total_objects: Optional[int] = None) -> List[Dict]:
|
| 817 |
"""
|
| 818 |
+
獲取最重要的物件,避免重複過濾邏輯
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
"""
|
| 820 |
try:
|
| 821 |
+
# 第一步:獲取基礎的重要物件
|
| 822 |
prominent_objects = self.object_description_generator.get_prominent_objects(
|
| 823 |
detected_objects,
|
| 824 |
min_prominence_score,
|
| 825 |
+
max_categories_to_return=None
|
| 826 |
)
|
| 827 |
|
| 828 |
+
# 第二步:應用總數限制
|
| 829 |
if max_total_objects is not None and max_total_objects > 0:
|
|
|
|
| 830 |
prominent_objects = prominent_objects[:max_total_objects]
|
| 831 |
|
| 832 |
+
# 第三步:應用objects限制
|
| 833 |
if max_categories_to_return is not None and max_categories_to_return > 0:
|
|
|
|
| 834 |
categories_seen = set()
|
| 835 |
filtered_objects = []
|
| 836 |
|
| 837 |
for obj in prominent_objects:
|
| 838 |
class_name = obj.get("class_name", "unknown")
|
| 839 |
+
|
| 840 |
+
# 如果是新類別且未達到限制
|
| 841 |
if class_name not in categories_seen:
|
| 842 |
+
if len(categories_seen) < max_categories_to_return:
|
| 843 |
+
categories_seen.add(class_name)
|
| 844 |
+
filtered_objects.append(obj)
|
| 845 |
+
# 如果已達到類別限制,跳過新類別的物件
|
| 846 |
+
else:
|
| 847 |
+
# 直接添加已見過的objects
|
|
|
|
|
|
|
| 848 |
filtered_objects.append(obj)
|
| 849 |
|
| 850 |
return filtered_objects
|
|
|
|
| 1022 |
|
| 1023 |
Returns:
|
| 1024 |
模板內容
|
| 1025 |
+
"""
|
| 1026 |
+
|
| 1027 |
return self.template_manager.get_template(category, key)
|
| 1028 |
|
| 1029 |
+
|
| 1030 |
def get_viewpoint_confidence(self, detected_objects: List[Dict]) -> Tuple[str, float]:
|
| 1031 |
"""
|
| 1032 |
獲取視角檢測結果及其信心度
|
|
|
|
| 1096 |
self.logger.warning(f"Error getting text statistics: {str(e)}")
|
| 1097 |
return {"characters": 0, "words": 0, "sentences": 0}
|
| 1098 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1099 |
def get_configuration(self) -> Dict[str, Any]:
|
| 1100 |
"""
|
| 1101 |
獲取當前配置信息
|
|
|
|
| 1123 |
region_analyzer=self.region_analyzer
|
| 1124 |
)
|
| 1125 |
except Exception as e:
|
| 1126 |
+
self.logger.error(f"Fallback component initialization failed: {str(e)}")
|