Addax-Data-Science commited on
Commit
1d492c9
·
verified ·
1 Parent(s): e5b48cb

Upload inference.py

Browse files
Files changed (1) hide show
  1. inference.py +18 -0
inference.py CHANGED
@@ -314,3 +314,21 @@ class ModelInference:
314
  class_names[class_id_str] = class_name
315
 
316
  return class_names
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  class_names[class_id_str] = class_name
315
 
316
  return class_names
317
+
318
+ def get_tensor(self, crop: Image.Image):
319
+ """Preprocess a crop into a numpy array for batch inference."""
320
+ img = np.array(crop)
321
+ img = cv2.resize(img, (self.img_size, self.img_size))
322
+ return img
323
+
324
+ def classify_batch(self, batch):
325
+ """Run inference on a batch of preprocessed numpy arrays."""
326
+ probs = self.model.predict(batch, verbose=0)
327
+ results = []
328
+ for p in probs:
329
+ classifications = [
330
+ [self.class_ids[i], float(p[i])]
331
+ for i in range(len(self.class_ids))
332
+ ]
333
+ results.append(classifications)
334
+ return results