Update README.md
Browse files
README.md
CHANGED
|
@@ -46,58 +46,55 @@ This model classifies cattle and buffalo breeds using computer vision. It's base
|
|
| 46 |
### Using ONNX Runtime
|
| 47 |
|
| 48 |
```python
|
|
|
|
| 49 |
import onnxruntime as ort
|
| 50 |
import numpy as np
|
| 51 |
-
from PIL import Image
|
| 52 |
import json
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
transform = transforms.Compose([
|
| 66 |
transforms.Resize(256),
|
| 67 |
transforms.CenterCrop(224),
|
| 68 |
transforms.ToTensor(),
|
| 69 |
-
transforms.Normalize(
|
| 70 |
-
mean=[0.485, 0.456, 0.406],
|
| 71 |
-
std=[0.229, 0.224, 0.225]
|
| 72 |
-
)
|
| 73 |
])
|
| 74 |
-
|
| 75 |
-
image = Image.open(image_path).convert('RGB')
|
| 76 |
-
tensor = transform(image).unsqueeze(0)
|
| 77 |
-
return tensor.numpy()
|
| 78 |
-
|
| 79 |
-
# Predict breed
|
| 80 |
-
def predict_breed(image_path):
|
| 81 |
-
# Get features
|
| 82 |
-
input_data = preprocess_image(image_path)
|
| 83 |
features = session.run(None, {'input': input_data})[0][0]
|
| 84 |
-
|
| 85 |
-
# Calculate similarities
|
| 86 |
similarities = {}
|
| 87 |
for breed, prototype in prototypes['prototypes'].items():
|
| 88 |
-
similarity = np.dot(features, prototype)
|
| 89 |
similarities[breed] = float(similarity)
|
| 90 |
-
|
| 91 |
-
# Get top prediction
|
| 92 |
predicted_breed = max(similarities, key=similarities.get)
|
| 93 |
confidence = similarities[predicted_breed]
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
```
|
| 102 |
|
| 103 |
### Integration with Mobile Apps
|
|
|
|
| 46 |
### Using ONNX Runtime
|
| 47 |
|
| 48 |
```python
|
| 49 |
+
from huggingface_hub import hf_hub_download
|
| 50 |
import onnxruntime as ort
|
| 51 |
import numpy as np
|
|
|
|
| 52 |
import json
|
| 53 |
+
from PIL import Image
|
| 54 |
+
from torchvision import transforms
|
| 55 |
+
|
| 56 |
+
def setup_model():
|
| 57 |
+
print("📥 Downloading model from Hugging Face...")
|
| 58 |
+
model_path = hf_hub_download("vishnuamar/cattle-breed-classifier", "model.onnx")
|
| 59 |
+
prototypes_path = hf_hub_download("vishnuamar/cattle-breed-classifier", "prototypes.json")
|
| 60 |
+
session = ort.InferenceSession(model_path)
|
| 61 |
+
with open(prototypes_path, 'r') as f:
|
| 62 |
+
prototypes = json.load(f)
|
| 63 |
+
print("✅ Model ready!")
|
| 64 |
+
return session, prototypes
|
| 65 |
+
|
| 66 |
+
def predict_breed(session, prototypes, image_path):
|
| 67 |
+
image = Image.open(image_path).convert('RGB')
|
| 68 |
transform = transforms.Compose([
|
| 69 |
transforms.Resize(256),
|
| 70 |
transforms.CenterCrop(224),
|
| 71 |
transforms.ToTensor(),
|
| 72 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
|
|
|
|
|
|
|
|
|
| 73 |
])
|
| 74 |
+
input_data = transform(image).unsqueeze(0).numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
features = session.run(None, {'input': input_data})[0][0]
|
|
|
|
|
|
|
| 76 |
similarities = {}
|
| 77 |
for breed, prototype in prototypes['prototypes'].items():
|
| 78 |
+
similarity = np.dot(features, np.array(prototype))
|
| 79 |
similarities[breed] = float(similarity)
|
|
|
|
|
|
|
| 80 |
predicted_breed = max(similarities, key=similarities.get)
|
| 81 |
confidence = similarities[predicted_breed]
|
| 82 |
+
buffalo_breeds = ['Bhadawari', 'Jaffarbadi', 'Mehsana', 'Murrah', 'Surti']
|
| 83 |
+
animal_type = 'Buffalo' if predicted_breed in buffalo_breeds else 'Cattle'
|
| 84 |
+
return {
|
| 85 |
+
'breed': predicted_breed,
|
| 86 |
+
'confidence': confidence,
|
| 87 |
+
'animal_type': animal_type,
|
| 88 |
+
'all_scores': similarities
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
session, prototypes = setup_model()
|
| 93 |
+
image_path = "path/to/your/image.jpg" # Change this to your test image
|
| 94 |
+
result = predict_breed(session, prototypes, image_path)
|
| 95 |
+
print(f"\n🐄 Animal: {result['animal_type']}")
|
| 96 |
+
print(f"Breed: {result['breed']}")
|
| 97 |
+
print(f"Confidence: {result['confidence']:.4f}")
|
| 98 |
```
|
| 99 |
|
| 100 |
### Integration with Mobile Apps
|