guohanghui commited on
Commit
8d3f2eb
·
verified ·
1 Parent(s): 841013a

Update pest/source/src/utils.py

Browse files
Files changed (1) hide show
  1. pest/source/src/utils.py +25 -2
pest/source/src/utils.py CHANGED
@@ -144,9 +144,32 @@ def preprocess_image(image_path: str):
144
  def postprocess_results(detection_results):
145
  """Postprocess pest detection results."""
146
  try:
147
- # 加载类别标签
 
 
148
  class_labels = {}
149
- with open('IP102_classes.txt', 'r', encoding='utf-8') as f:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  for line in f:
151
  if line.strip():
152
  parts = line.strip().split(' ', 1)
 
144
  def postprocess_results(detection_results):
145
  """Postprocess pest detection results."""
146
  try:
147
+ import os
148
+
149
+ # 加载类别标签 - 尝试多种路径以适配Hugging Face环境
150
  class_labels = {}
151
+ possible_class_paths = [
152
+ 'IP102_classes.txt',
153
+ '/workspace/IP102_classes.txt',
154
+ '/app/IP102_classes.txt',
155
+ 'src/IP102_classes.txt',
156
+ 'source/src/IP102_classes.txt',
157
+ 'pest/source/src/IP102_classes.txt',
158
+ '/app/pest/source/src/IP102_classes.txt',
159
+ '/workspace/pest/source/src/IP102_classes.txt',
160
+ os.path.join(os.path.dirname(os.path.abspath(__file__)), 'IP102_classes.txt')
161
+ ]
162
+
163
+ class_file_path = None
164
+ for path in possible_class_paths:
165
+ if os.path.exists(path):
166
+ class_file_path = path
167
+ break
168
+
169
+ if class_file_path is None:
170
+ raise FileNotFoundError(f"Class labels file not found: IP102_classes.txt")
171
+
172
+ with open(class_file_path, 'r', encoding='utf-8') as f:
173
  for line in f:
174
  if line.strip():
175
  parts = line.strip().split(' ', 1)