File size: 6,098 Bytes
427dba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import streamlit as st
import pandas as pd
import random
import joblib
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import os

# Load cattle data
DATA_FILE = 'cattle_data.csv'

def load_data():
    # Check if the dataset exists
    if os.path.exists(DATA_FILE):
        return pd.read_csv(DATA_FILE)
    else:
        return pd.DataFrame(columns=['TAG ID', 'BREED', 'AGE', 'SURGERY', 'REPRODUCTION CYCLES', 'OTHER'])

# Function to generate random sensor data for a specific cow breed
def generate_sensor_data(cow_breed):
    cow_data = {
        'Holstein Friesian': {'body_temp': (38.0, 39.0), 'rumination': (600, 800), 'steps': (8000, 12000), 'behavior_score': (6, 8)},
        'Girolando': {'body_temp': (38.0, 39.0), 'rumination': (650, 750), 'steps': (7000, 10000), 'behavior_score': (7, 9)},
        'Jersey': {'body_temp': (38.0, 39.0), 'rumination': (650, 750), 'steps': (7500, 10500), 'behavior_score': (6, 8)},
        'Sahiwal': {'body_temp': (38.0, 39.0), 'rumination': (550, 700), 'steps': (6500, 9000), 'behavior_score': (7, 9)},
        'Kankrej': {'body_temp': (38.0, 39.0), 'rumination': (600, 750), 'steps': (7000, 10000), 'behavior_score': (7, 9)}
    }

    data = cow_data.get(cow_breed)
    if not data:
        return None

    sensor_data = {
        'Body Temperature (°C)': round(random.uniform(data['body_temp'][0], data['body_temp'][1]), 2),
        'Rumination (min/day)': random.randint(data['rumination'][0], data['rumination'][1]),
        'Step Count (steps/day)': random.randint(data['steps'][0], data['steps'][1]),
        'Behavior Score (1-10)': random.randint(data['behavior_score'][0], data['behavior_score'][1])
    }
    return sensor_data

# Load the data and preprocess
df = load_data()

# Feature Engineering - Create features like daily trends or differences if needed
df['milk_yield_change'] = df['AGE'].diff().fillna(0)  # Just an example to create a derived feature

# Merge sensor data with cattle profile
cow_breeds = ['Holstein Friesian', 'Girolando', 'Jersey', 'Sahiwal', 'Kankrej']
sensor_data_list = []
for breed in cow_breeds:
    sensor_data = generate_sensor_data(breed)
    if sensor_data:
        sensor_data['TAG ID'] = random.randint(1, 100)  # Random TAG ID for simulation
        sensor_data['BREED'] = breed
        sensor_data_list.append(sensor_data)

sensor_df = pd.DataFrame(sensor_data_list)

# Merge with the original cattle profile dataset
df = pd.merge(df, sensor_df, on='TAG ID', how='left')

# Model Training (Disease Prediction)
X = df[['Body Temperature (°C)', 'Rumination (min/day)', 'Step Count (steps/day)', 'Behavior Score (1-10)', 'AGE']]
y = df['OTHER'].apply(lambda x: 1 if x == 'Healthy' else 0)  # Convert health status to binary (0 = not healthy, 1 = healthy)

# Data Preprocessing
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.3, random_state=42)

# Train a Random Forest Classifier
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Save the trained model
joblib.dump(model, 'disease_prediction_model.pkl')
joblib.dump(scaler, 'scaler.pkl')

# Evaluate the model
y_pred = model.predict(X_test)
st.write("Model Evaluation Results")
st.text(classification_report(y_test, y_pred))

# Streamlit App
st.title('Cattle Profile & Disease Prediction Dashboard')

# Register New Cow form
st.header("➕ Register New Cow")
tag_id = st.number_input("TAG ID", min_value=1, step=1)
breed = st.selectbox("Breed", cow_breeds)
age = st.number_input("Age (in years)", min_value=0, step=1)
surgery = st.text_input("Surgery")
reproduction_cycles = st.number_input("Reproduction Cycles", min_value=0, step=1)
other = st.text_input("Other Health Details")

submit = st.button("Register Cow")
if submit:
    new_cow = pd.DataFrame([{
        'TAG ID': tag_id,
        'BREED': breed,
        'AGE': age,
        'SURGERY': surgery,
        'REPRODUCTION CYCLES': reproduction_cycles,
        'OTHER': other
    }])
    df = pd.concat([df, new_cow], ignore_index=True)
    st.write("Cow registered successfully!")

# Select RFID dropdown to view details
st.header("🔢 Select RFID Tag ID")
selected_rfid = st.selectbox("Select RFID Tag ID", df['TAG ID'].unique())

if selected_rfid:
    cow_info = df[df['TAG ID'] == selected_rfid].iloc[0]
    st.write(f"**TAG ID:** {cow_info['TAG ID']}")
    st.write(f"**BREED:** {cow_info['BREED']}")
    st.write(f"**AGE:** {cow_info['AGE']} years")
    st.write(f"**SURGERY:** {cow_info['SURGERY']}")
    st.write(f"**REPRODUCTION CYCLES:** {cow_info['REPRODUCTION CYCLES']}")
    st.write(f"**OTHER HEALTH DETAILS:** {cow_info['OTHER']}")

    # Predict disease status based on the selected cow's data
    selected_data = scaler.transform([[
        cow_info['Body Temperature (°C)'],
        cow_info['Rumination (min/day)'],
        cow_info['Step Count (steps/day)'],
        cow_info['Behavior Score (1-10)'],
        cow_info['AGE']
    ]])
    disease_prediction = model.predict(selected_data)
    disease_status = "Healthy" if disease_prediction[0] == 1 else "Not Healthy"
    st.write(f"Predicted Health Status: {disease_status}")

# Display cow milk yield trends
st.header("🐄 Cow Milk Yield Trends")
milk_data = {
    'Tag ID': df['TAG ID'],
    'Date': pd.date_range(start='2025-04-01', periods=len(df), freq='D'),
    'Milk Yield (L)': np.random.uniform(10, 22, len(df)),
}
milk_df = pd.DataFrame(milk_data)

# Plot trends
st.subheader('Milk Yield Trend for Each Cow')
plt.figure(figsize=(10, 6))
sns.lineplot(data=milk_df, x='Date', y='Milk Yield (L)', hue='Tag ID', marker='o')
plt.title('Milk Yield Trend (Day-wise for Each Cow)')
plt.xlabel('Date')
plt.ylabel('Milk Yield (L)')
plt.xticks(rotation=45)
st.pyplot()

# Save updated data back to CSV
df.to_csv(DATA_FILE, index=False)