Upload inference.py
Browse files73 unmodified lines74757677787980818298 unmodified lines18118218318418518618718813 unmodified lines20220320420520620720820921021121221321421521621 unmodified lines23823924024124224324424524673 unmodified lines"""global animal_model, MODEL_PATHif MODEL_PATH is None:raise RuntimeError("MODEL_PATH not set - must be injected by framework")if not MODEL_PATH.exists():raise FileNotFoundError(f"Model file not found: {MODEL_PATH}")98 unmodified linescrop: Cropped and preprocessed PIL ImageReturns:List of (class_id, confidence) tuples for ALL classes, sorted by confidence.Example: [("14", 0.99985), ("7", 0.00003), ...]Raises:RuntimeError: If model not loaded or inference fails13 unmodified lines# Extract probabilities: [0.0001, 0.0002, ..., 0.9998, ...]probs = results[0].probs.data.tolist()# Build list of (class_id, confidence) tuples# Class IDs are 0-indexed in YOLOv8 but we output as stringsclassifications = []for idx, class_name in names_dict.items():# YOLOv8 uses 0-based indexing, but we need 1-based for compatibility# with the taxonomy.csv and expected JSON outputclass_id_str = str(idx + 1) # Convert 0-indexed to 1-indexedconfidence = probs[idx]classifications.append((class_id_str, confidence))# Sort by confidence descending (already sorted by YOLOv8, but ensure it)classifications.sort(key=lambda x: x[1], reverse=True)21 unmodified lines"""global animal_model, MODEL_DIRif MODEL_DIR is None:raise RuntimeError("MODEL_DIR not set - must be injected by framework")# YOLOv8 models have class names built-in# We'll use those directly since they should match taxonomy.csvif animal_model is None:73 unmodified lines747576777879808198 unmodified lines18018118218318418518618713 unmodified lines20120220320420520620720820921021121221 unmodified lines23423523623723823924073 unmodified lines"""global animal_model, MODEL_PATH# MODEL_PATH is injected by framework before this function is called# Check that the path exists (framework guarantees it's not None)if not MODEL_PATH.exists():raise FileNotFoundError(f"Model file not found: {MODEL_PATH}")98 unmodified linescrop: Cropped and preprocessed PIL ImageReturns:List of (class_name, confidence) tuples for ALL classes, sorted by confidence.Example: [("giraffe", 0.99985), ("cattle", 0.00003), ...]Raises:RuntimeError: If model not loaded or inference fails13 unmodified lines# Extract probabilities: [0.0001, 0.0002, ..., 0.9998, ...]probs = results[0].probs.data.tolist()# Build list of (class_name, confidence) tuples# YOLOv8 names_dict is already in the correct format: {0: "porcupine", 1: "elephant", ...}classifications = []for idx, class_name in names_dict.items():confidence = probs[idx]classifications.append((class_name, confidence))# Sort by confidence descending (already sorted by YOLOv8, but ensure it)classifications.sort(key=lambda x: x[1], reverse=True)21 unmodified lines"""global animal_model, MODEL_DIR# MODEL_DIR is injected by framework (guaranteed not None)# YOLOv8 models have class names built-in# We'll use those directly since they should match taxonomy.csvif animal_model is None:
inference.py+8-14