@@ -43,17 +43,8 @@ class EdgeImpulseRunnerFacade:
4343
4444 def __init__ (self ):
4545 """Initialize the EdgeImpulseRunnerFacade with the API path."""
46- infra = load_brick_compose_file (self .__class__ )
47- for k , v in infra ["services" ].items ():
48- self .host = k
49- self .infra = v
50- break # Only one service is expected
51-
52- self .host = resolve_address (self .host )
53-
54- self .port = 1337 # Default EI HTTP port
55- self .url = f"http://{ self .host } :{ self .port } "
56- logger .warning (f"[{ self .__class__ .__name__ } ] Host: { self .host } - Ports: { self .port } - URL: { self .url } " )
46+ self .url = _get_ei_url (self .__class__ )
47+ logger .warning (f"[{ self .__class__ .__name__ } ] URL: { self .url } " )
5748
5849 def infer_from_file (self , image_path : str ) -> dict | None :
5950 if not image_path or image_path == "" :
@@ -124,47 +115,51 @@ def process(self, item):
124115 logger .error (f"[{ self .__class__ } ] Error processing file { item } : { e } " )
125116 return None
126117
127- def infer_from_features (self , features : list ) -> dict | None :
128- """Infer from features using the Edge Impulse API.
129-
118+ @staticmethod
119+ def infer_from_features (cls , features : list ) -> dict | None :
120+ """
121+ Infer from features using the Edge Impulse API.
130122 Args:
131- features (list): A list of features to send to the Edge Impulse API .
132-
123+ features (list): Features to send.
124+ cls: The class to use for infra loading.
133125 Returns:
134- dict | None: The response from the Edge Impulse API as a dictionary, or None if an error occurs .
126+ dict | None: API response or None.
135127 """
136128 try :
137- response = requests .post (f"{ self .url } /api/features" , json = {"features" : features })
129+ url = _get_ei_url (cls )
130+ model_info = EdgeImpulseRunnerFacade .get_model_info (cls )
131+ features = features [: int (model_info .input_features_count )]
132+
133+ response = requests .post (f"{ url } /api/features" , json = {"features" : features })
138134 if response .status_code == 200 :
139135 return response .json ()
140136 else :
141- logger .warning (f"[{ self . __class__ } ] error: { response .status_code } . Message: { response .text } " )
137+ logger .warning (f"[{ cls . __name__ } ] error: { response .status_code } . Message: { response .text } " )
142138 return None
143139 except Exception as e :
144- logger .error (f"[{ self . __class__ .__name__ } ] Error: { e } " )
140+ logger .error (f"[{ cls .__name__ } ] Error: { e } " )
145141 return None
146142
147- def get_model_info (self ) -> EdgeImpulseModelInfo | None :
143+ @staticmethod
144+ def get_model_info (cls ) -> EdgeImpulseModelInfo | None :
148145 """Get model information from the Edge Impulse API.
149146
150147 Returns:
151148 model_info (EdgeImpulseModelInfo | None): An instance of EdgeImpulseModelInfo containing model details, None if an error occurs.
152149 """
153- if not self .host or not self .port :
154- logger .error (f"[{ self .__class__ } ] Host or port not set. Cannot fetch model info." )
155- return None
150+ url = _get_ei_url (cls )
156151
157152 http_client = HttpClient (total_retries = 6 ) # Initialize the HTTP client with retry logic
158153 try :
159- response = http_client .request_with_retry (f"{ self . url } /api/info" )
154+ response = http_client .request_with_retry (f"{ url } /api/info" )
160155 if response .status_code == 200 :
161- logger .debug (f"[{ self . __class__ . __name__ } ] Fetching model info from { self . url } /api/info -> { response .status_code } { response .json } " )
156+ logger .debug (f"[{ cls . __name__ } ] Fetching model info from { url } /api/info -> { response .status_code } { response .json } " )
162157 return EdgeImpulseModelInfo (response .json ())
163158 else :
164- logger .warning (f"[{ self . __class__ } ] Error fetching model info: { response .status_code } . Message: { response .text } " )
159+ logger .warning (f"[{ cls } ] Error fetching model info: { response .status_code } . Message: { response .text } " )
165160 return None
166161 except Exception as e :
167- logger .error (f"[{ self . __class__ } ] Error fetching model info: { e } " )
162+ logger .error (f"[{ cls } ] Error fetching model info: { e } " )
168163 return None
169164 finally :
170165 http_client .close () # Close the HTTP client session
@@ -237,3 +232,14 @@ def _extract_anomaly_score(self, item: dict):
237232 return class_results ["anomaly" ]
238233
239234 return None
235+
236+
237+ def _get_ei_url (cls ):
238+ infra = load_brick_compose_file (cls )
239+ for k , v in infra ["services" ].items ():
240+ host = k
241+ break
242+ host = resolve_address (host )
243+ port = 1337
244+ url = f"http://{ host } :{ port } "
245+ return url
0 commit comments