syurein commited on
Commit
a345b5c
·
1 Parent(s): c2e61ba

エラーハンドリングの強化

Browse files
Files changed (1) hide show
  1. LLM_package.py +13 -4
LLM_package.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from google import genai
2
 
3
  import json
@@ -62,12 +63,20 @@ class GeminiInference:
62
  for obj in data:
63
  if 'box_2d' in obj and 'label' in obj:
64
  coords = obj['box_2d']
65
- norm = [c / 1000.0 for c in coords]
 
 
 
 
 
 
 
 
66
  parsed.append({'label': obj['label'], 'box_2d': norm})
67
  return parsed
68
  class ObjectDetector:
69
- def __init__(self, API_KEY=None):
70
- self.model = GeminiInference(API_KEY)
71
  self.prompt_objects=None
72
  self.text=None
73
  self.scene=None
@@ -129,4 +138,4 @@ class ObjectDetector:
129
  print(f"Failed to parse risk_level from response. Error: {e}")
130
  print(f"Response content: {response_json_str}")
131
  risk_level = 0 # パース失敗時はデフォルト値0を返す
132
- return risk_level
 
1
+ # LLM_package.py の修正
2
  from google import genai
3
 
4
  import json
 
63
  for obj in data:
64
  if 'box_2d' in obj and 'label' in obj:
65
  coords = obj['box_2d']
66
+ # box_2dの各要素が0-1000の範囲であることを確認し、0-1の範囲に正規化
67
+ # ただし、Geminiの出力がすでに0-1で返される可能性も考慮し、
68
+ # 値が1を超える場合は1000で割り、1を超えない場合はそのまま使用
69
+ norm = []
70
+ for c in coords:
71
+ if c > 1.0001: # 1をわずかに超える値であれば1000スケールと判断
72
+ norm.append(c / 1000.0)
73
+ else: # 1以下の値であればすでに0-1スケールと判断
74
+ norm.append(c)
75
  parsed.append({'label': obj['label'], 'box_2d': norm})
76
  return parsed
77
  class ObjectDetector:
78
+ def __init__(self, api_key=os.getenv('GEMINI_API_KEY')): # API_KEYをapi_keyに変更し、デフォルト値を設定
79
+ self.model = GeminiInference(api_key) # ここもapi_keyを使用
80
  self.prompt_objects=None
81
  self.text=None
82
  self.scene=None
 
138
  print(f"Failed to parse risk_level from response. Error: {e}")
139
  print(f"Response content: {response_json_str}")
140
  risk_level = 0 # パース失敗時はデフォルト値0を返す
141
+ return risk_level