File size: 4,851 Bytes
a4d3de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Cattle & Buffalo Breed Classification API

An AI-powered image classification API that identifies the breed of Indian cattle and buffaloes from a photograph, built for **Smart India Hackathon (SIH) 2025**.

## Problem Statement

| | |
|---|---|
| **PS Number** | SIH25004 |
| **Title** | Image Based Breed Recognition for Cattle and Buffaloes of India |
| **Organization** | Ministry of Fisheries, Animal Husbandry & Dairying |
| **Category** | Software |
| **Theme** | Agriculture, FoodTech & Rural Development |

Field workers under the **Bharat Pashudhan App (BPA)** manually record the breed of cattle and buffaloes during registration, which frequently leads to **breed misclassification** due to the sheer diversity of indigenous breeds and lack of expert knowledge on the ground. This reduces the accuracy of national livestock data and hampers breeding programs run under the **Rashtriya Gokul Mission (RGM)**.

This project addresses that gap with an AI-driven solution: a worker uploads/captures an image of an animal, and the system automatically suggests the most probable breed with a confidence score β€” improving data accuracy and reducing dependency on manual expertise, so it can be integrated into BPA as a breed-suggestion assist.

## What This Repo Contains

This repo hosts the **backend inference API only**. It loads a trained Keras model (`Cattle.Keras`) and exposes it over HTTP so a separate frontend/UI (built by a teammate) can consume it.

- Accepts an uploaded animal image
- Returns the **predicted breed label** and **probability scores for all 50 supported breeds**
- Secured with an API key so only authorized clients can call it

## Supported Breeds (50)

Purnea, motu, Himachali Pahari, kherigarh, ghumsari, gaolao, Dangi, Sahiwal, Kankrej, Hariana, nagori, Pulikulam, Kosali, Kangayam, bhelai, Konkan Kapila, Shweta Kapila, Kenkatha, Nimari, ponwar, Lakhimi, Vechur, Krishna_Valley, Nari, Hallikar, Punganur, Amritmahal, Tharparkar, Khariar, Khillari, Ayrshire, badri, thutho, Red_Sindhi, siri, Deoni, Bargur, Poda Thirupu, bachaur, Ongole, Malnad_gidda, dagri, Ladakhi, Mewati, Gir, Rathi, gangatari, Umblachery, Red kandhari, malvi

(Full list also available programmatically via the `/classes` endpoint.)

## Repo Structure

```
your-repo/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py             # FastAPI app β€” /predict (key-protected), /health, /classes
β”‚   └── class_names.py      # CLASS_NAMES list (50 breeds)
β”‚
β”œβ”€β”€ models/
β”‚   └── Cattle.Keras        # trained model (use Git LFS if large)
β”‚
β”œβ”€β”€ test_client.py          # CLI script to test the API
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ Dockerfile                # container build
β”œβ”€β”€ .env.example              # template (safe to commit β€” no real secrets)
β”œβ”€β”€ .env                        # real API_KEY + MODEL_PATH β€” NOT committed
β”œβ”€β”€ .gitignore
└── README.md
```

## Tech Stack

- **Model**: TensorFlow / Keras (CNN-based image classifier)
- **API**: FastAPI + Uvicorn
- **Image processing**: Pillow, NumPy
- **Deployment**: Docker

## Setup

### 1. Clone and install dependencies
```bash
git clone <your-repo-url>
cd your-repo
pip install -r requirements.txt
```

### 2. Add your model
Place your trained model file at `models/Cattle.Keras`.

### 3. Configure environment
```bash
cp .env.example .env
```
Then edit `.env` and set a real `API_KEY`:
```bash
python -c "import secrets; print(secrets.token_hex(24))"
```

### 4. Run locally
```bash
cd app
uvicorn main:app --reload
```
API will be available at `http://localhost:8000`.

### 5. Run with Docker (alternative)
```bash
docker build -t cattle-api .
docker run -e API_KEY=your_key_here -p 8000:8000 cattle-api
```

## API Reference

### `GET /health`
Basic health check and model info.

### `GET /classes`
Returns the list of all supported breed classes.

### `POST /predict`
Upload an image and get breed predictions. Requires header `x-api-key`.

**Request**
```
POST /predict
Header: x-api-key: <your key>
Body (form-data): file=<image>
```

**Response**
```json
{
  "predicted_label": "Sahiwal",
  "confidence": 0.87,
  "probabilities": {
    "Purnea": 0.001,
    "Sahiwal": 0.87,
    "...": "... all 50 classes"
  }
}
```

### Testing the API
```bash
python test_client.py path/to/image.jpg --url http://localhost:8000
```

## Integration Notes for Frontend/UI Developer

- Send the API key as an `x-api-key` header on every request to `/predict`
- Image should be sent as `multipart/form-data` under the field name `file`
- `probabilities` in the response is a dict of all 50 breeds β€” sort it client-side to show top-N results
- `/health` and `/classes` are open (no key required) for convenience

## Team

_Add your team name and member names here._

## License

_Add license info if applicable._