@@ -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,53 @@ 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.
118+ @staticmethod
119+ def infer_from_features (cls , features : list ) -> dict | None :
120+ """
121+ Infer from features using the Edge Impulse API.
129122
130123 Args:
124+ cls: The class to use for infra loading.
131125 features (list): A list of features to send to the Edge Impulse API.
132126
133127 Returns:
134128 dict | None: The response from the Edge Impulse API as a dictionary, or None if an error occurs.
135129 """
136130 try :
137- response = requests .post (f"{ self .url } /api/features" , json = {"features" : features })
131+ url = _get_ei_url (cls )
132+ model_info = EdgeImpulseRunnerFacade .get_model_info (cls )
133+ features = features [: int (model_info .input_features_count )]
134+
135+ response = requests .post (f"{ url } /api/features" , json = {"features" : features })
138136 if response .status_code == 200 :
139137 return response .json ()
140138 else :
141- logger .warning (f"[{ self . __class__ } ] error: { response .status_code } . Message: { response .text } " )
139+ logger .warning (f"[{ cls . __name__ } ] error: { response .status_code } . Message: { response .text } " )
142140 return None
143141 except Exception as e :
144- logger .error (f"[{ self . __class__ .__name__ } ] Error: { e } " )
142+ logger .error (f"[{ cls .__name__ } ] Error: { e } " )
145143 return None
146144
147- def get_model_info (self ) -> EdgeImpulseModelInfo | None :
145+ @staticmethod
146+ def get_model_info (cls ) -> EdgeImpulseModelInfo | None :
148147 """Get model information from the Edge Impulse API.
149148
150149 Returns:
151150 model_info (EdgeImpulseModelInfo | None): An instance of EdgeImpulseModelInfo containing model details, None if an error occurs.
152151 """
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
152+ url = _get_ei_url (cls )
156153
157154 http_client = HttpClient (total_retries = 6 ) # Initialize the HTTP client with retry logic
158155 try :
159- response = http_client .request_with_retry (f"{ self . url } /api/info" )
156+ response = http_client .request_with_retry (f"{ url } /api/info" )
160157 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 } " )
158+ logger .debug (f"[{ cls . __name__ } ] Fetching model info from { url } /api/info -> { response .status_code } { response .json } " )
162159 return EdgeImpulseModelInfo (response .json ())
163160 else :
164- logger .warning (f"[{ self . __class__ } ] Error fetching model info: { response .status_code } . Message: { response .text } " )
161+ logger .warning (f"[{ cls } ] Error fetching model info: { response .status_code } . Message: { response .text } " )
165162 return None
166163 except Exception as e :
167- logger .error (f"[{ self . __class__ } ] Error fetching model info: { e } " )
164+ logger .error (f"[{ cls } ] Error fetching model info: { e } " )
168165 return None
169166 finally :
170167 http_client .close () # Close the HTTP client session
@@ -237,3 +234,14 @@ def _extract_anomaly_score(self, item: dict):
237234 return class_results ["anomaly" ]
238235
239236 return None
237+
238+
239+ def _get_ei_url (cls ):
240+ infra = load_brick_compose_file (cls )
241+ for k , v in infra ["services" ].items ():
242+ host = k
243+ break
244+ host = resolve_address (host )
245+ port = 1337
246+ url = f"http://{ host } :{ port } "
247+ return url
0 commit comments