Spaces:
Sleeping
Sleeping
Denny Lulak commited on
Commit ·
c6700a6
1
Parent(s): 4c3081b
Fix
Browse filesFiles changed (3)
- app.py+27-1
- inference.py+58-42
- model.onnx+2-2
5 unmodified lines67891011125 unmodified lines# ConfigurationMODEL_ONNX_PATH = "model.onnx"CLASS_NAMES = ["Coffee_Franch_Original_Box_250g", "Coffee_Franch_Instant_Crema_80g"]INPUT_SIZE = 640# Initialize detector5 unmodified lines678910111213141516171819202122232425262728293031323334353637385 unmodified lines# ConfigurationMODEL_ONNX_PATH = "model.onnx"CLASS_NAMES = ['Butter_Dukat_Maslac_Stick_250g','Butter_Zbregov_Maslac_Stick_250g','Butter_Zdenka_Maslac_Stick_250g','Cheese_President_Gouda_Cube_250g','Chicken_Cekin_Pileca_Prsa_500g','Coffee_Franch_Crema_Bag_175g','Coffee_Franch_Crema_Box_250g','Coffee_Franch_Instant_Crema_80g','Coffee_Franch_Intense_Box_250g','Coffee_Franch_Original_Box_250g','Coffee_Franch_Sensual_Box_250g','Drink_CocaCola_Original_Bottle_1l','Flour_Mlineta_Brasno_Ostro_1kg','Juice_Vindi_Naranca_Nektar_1l','Ketchup_Zvijezda_Mild_Bottle_500g','Mayonnaise_Zvijezda_Delicate_Bottle_400g','Milk_Zbregov_Trajno_28_1l','Oil_Dijamant_Suncokretovo_Bottle_1l','Oil_Zvijezda_Suncokretovo_Ulje_1l','Pasta_Barilla_Fusilli_Box_500g','Rice_Gallo_Long_Grain_900g','Rice_Kplus_Arborio_BijeliDugi_1kg','Salt_SolanaPag_Sitna_Box_1kg','Spaghetti_PastaZara_Spaghettini_Bag_500g','Tuna_RioMare_Tonno_Oliva']INPUT_SIZE = 640# Initialize detector
61 unmodified lines626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911061 unmodified linesblob = canvas.astype(np.float32) / 255.0return blob.transpose(2, 0, 1)[None, ...], scale, (pw, ph)def postprocess(self,predictions: np.ndarray,original_shape: Tuple[int, int],scale: float,padding: Tuple[int, int],conf_threshold: float = 0.5,iou_threshold: float = 0.45) -> List[Dict]:predictions = np.squeeze(predictions).Tscores = np.max(predictions[:, 4:], axis=1)valid = scores > conf_thresholdpredictions = predictions[valid]if predictions.size == 0:return []boxes = predictions[:, :4]boxes[:, [0, 1]] = boxes[:, [0, 1]] - boxes[:, [2, 3]] / 2boxes[:, [2, 3]] = boxes[:, [0, 1]] + boxes[:, [2, 3]]pad_w, pad_h = paddingboxes[:, [0, 2]] = (boxes[:, [0, 2]] - pad_w) / scaleboxes[:, [1, 3]] = (boxes[:, [1, 3]] - pad_h) / scaleh, w = original_shapeboxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, w)boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, h)class_ids = np.argmax(predictions[:, 4:], axis=1)indices = self.nms(boxes, scores[valid], iou_threshold)return [{"class": self.class_names[int(class_ids[i])],"confidence": float(scores[valid][i]),"bbox": boxes[i].tolist(),"bbox_normalized": [float((boxes[i][0] + boxes[i][2])/2 / w),float((boxes[i][1] + boxes[i][3])/2 / h),float((boxes[i][2] - boxes[i][0]) / w),float((boxes[i][3] - boxes[i][1]) / h)]} for i in indices]def predict(self, image: np.ndarray) -> List[Dict]:"""Main prediction method"""61 unmodified lines626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512661 unmodified linesblob = canvas.astype(np.float32) / 255.0return blob.transpose(2, 0, 1)[None, ...], scale, (pw, ph)def postprocess(self,predictions: np.ndarray,original_shape: Tuple[int, int],scale: float,padding: Tuple[int, int],conf_threshold: float = 0.5,iou_threshold: float = 0.45) -> List[Dict]:# Reshape and transpose predictions to (8400, 29)predictions = np.squeeze(predictions).T# Split into box coordinates, objectness score, and class probabilitiesboxes = predictions[:, :4]obj_scores = predictions[:, 4]cls_scores = predictions[:, 5:5 + len(self.class_names)] # Dynamic class count# Combine objectness and class scoresfinal_scores = obj_scores[:, None] * cls_scores # (8400, num_classes)best_class_ids = np.argmax(final_scores, axis=1)best_scores = np.max(final_scores, axis=1)# Filter by confidence thresholdvalid = best_scores > conf_thresholdboxes = boxes[valid]best_scores = best_scores[valid]best_class_ids = best_class_ids[valid]if boxes.size == 0:return []# Convert from center_x, center_y, width, height to xyxy formatboxes[:, [0, 1]] = boxes[:, [0, 1]] - boxes[:, [2, 3]] / 2boxes[:, [2, 3]] = boxes[:, [0, 1]] + boxes[:, [2, 3]]# Adjust for padding and scalepad_w, pad_h = paddingboxes[:, [0, 2]] = (boxes[:, [0, 2]] - pad_w) / scaleboxes[:, [1, 3]] = (boxes[:, [1, 3]] - pad_h) / scale# Clip boxes to image boundariesh, w = original_shapeboxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, w)boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, h)# Perform NMSindices = self.nms(boxes, best_scores, iou_threshold)return [{"class": self.class_names[int(best_class_ids[i])],"confidence": float(best_scores[i]),"bbox": boxes[i].tolist(),"bbox_normalized": [float((boxes[i][0] + boxes[i][2])/2 / w),float((boxes[i][1] + boxes[i][3])/2 / h),float((boxes[i][2] - boxes[i][0]) / w),float((boxes[i][3] - boxes[i][1]) / h)]} for i in indices]def predict(self, image: np.ndarray) -> List[Dict]:"""Main prediction method"""
123version https://git-lfs.github.com/spec/v1oid sha256:39582e24d58748bb0304397f40f5d88bff8f7c7a1584bbec11e9f9490fb12690size 37908587123version https://git-lfs.github.com/spec/v1oid sha256:47fb25ba1e7fb96e8577581ecf4811788f36a0117ea9390859635bd89e4d439dsize 103663101
app.py+27-1
inference.py+58-42
model.onnx+2-2