added more behaviours
Browse files
app.py
CHANGED
|
@@ -24,7 +24,10 @@ class DogBehaviorAnalyzer:
|
|
| 24 |
'tail_wagging': {'threshold': 0.15, 'description': 'Your dog is displaying happiness and excitement!'},
|
| 25 |
'movement': {'threshold': 0.02, 'description': 'Your dog is active and moving around.'},
|
| 26 |
'stationary': {'threshold': 0.01, 'description': 'Your dog is calm and still.'},
|
| 27 |
-
'high_activity': {'threshold': 0.05, 'description': 'Your dog is very energetic!'}
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
self.suggestions = {
|
|
@@ -48,6 +51,24 @@ class DogBehaviorAnalyzer:
|
|
| 48 |
"Consider redirecting energy into agility training",
|
| 49 |
"A good play session with toys would be beneficial",
|
| 50 |
"Make sure fresh water is available"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
]
|
| 52 |
}
|
| 53 |
|
|
@@ -113,12 +134,17 @@ class DogBehaviorAnalyzer:
|
|
| 113 |
"""Analyze frame with improved behavior detection logic"""
|
| 114 |
motion_score = self.detect_motion(frame)
|
| 115 |
color_change_score = self.detect_color_changes(frame)
|
|
|
|
| 116 |
|
| 117 |
detected_behaviors = []
|
| 118 |
|
| 119 |
# High activity detection (running, jumping)
|
| 120 |
if motion_score > self.behaviors['high_activity']['threshold']:
|
| 121 |
detected_behaviors.append(('high_activity', motion_score))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# Regular movement detection
|
| 124 |
elif motion_score > self.behaviors['movement']['threshold']:
|
|
@@ -131,6 +157,15 @@ class DogBehaviorAnalyzer:
|
|
| 131 |
# Tail wagging detection - based on localized color changes
|
| 132 |
if color_change_score > self.behaviors['tail_wagging']['threshold']:
|
| 133 |
detected_behaviors.append(('tail_wagging', color_change_score))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# Debug information
|
| 136 |
if not detected_behaviors:
|
|
@@ -138,6 +173,41 @@ class DogBehaviorAnalyzer:
|
|
| 138 |
st.sidebar.write(f"Debug - Color Change Score: {color_change_score:.4f}")
|
| 139 |
|
| 140 |
return detected_behaviors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
def get_suggestions(self, detected_behaviors, behavior_counts):
|
| 143 |
"""Generate suggestions based on detected behaviors and their frequency"""
|
|
|
|
| 24 |
'tail_wagging': {'threshold': 0.15, 'description': 'Your dog is displaying happiness and excitement!'},
|
| 25 |
'movement': {'threshold': 0.02, 'description': 'Your dog is active and moving around.'},
|
| 26 |
'stationary': {'threshold': 0.01, 'description': 'Your dog is calm and still.'},
|
| 27 |
+
'high_activity': {'threshold': 0.05, 'description': 'Your dog is very energetic!'},
|
| 28 |
+
'barking': {'threshold': 0.10, 'description': 'Your dog is trying to communicate!'},
|
| 29 |
+
'jumping': {'threshold': 0.12, 'description': 'Your dog is showing excitement through jumping!'},
|
| 30 |
+
'ears_perked': {'threshold': 0.08, 'description': 'Your dog is alert and attentive!'}
|
| 31 |
}
|
| 32 |
|
| 33 |
self.suggestions = {
|
|
|
|
| 51 |
"Consider redirecting energy into agility training",
|
| 52 |
"A good play session with toys would be beneficial",
|
| 53 |
"Make sure fresh water is available"
|
| 54 |
+
],
|
| 55 |
+
'barking': [
|
| 56 |
+
"Try to identify what's triggering the barking",
|
| 57 |
+
"Practice 'quiet' command training",
|
| 58 |
+
"Redirect attention with engaging toys",
|
| 59 |
+
"Consider working on bark control exercises"
|
| 60 |
+
],
|
| 61 |
+
'jumping': [
|
| 62 |
+
"Practice the 'four paws on the floor' training",
|
| 63 |
+
"Redirect jumping energy into trick training",
|
| 64 |
+
"Work on impulse control exercises",
|
| 65 |
+
"Try teaching alternative greetings like 'sit' for attention"
|
| 66 |
+
],
|
| 67 |
+
'ears_perked': [
|
| 68 |
+
"Great time for sound recognition training",
|
| 69 |
+
"Practice attention and focus exercises",
|
| 70 |
+
"Good moment for environmental awareness training",
|
| 71 |
+
"Consider introducing new sounds or stimuli for enrichment"
|
| 72 |
]
|
| 73 |
}
|
| 74 |
|
|
|
|
| 134 |
"""Analyze frame with improved behavior detection logic"""
|
| 135 |
motion_score = self.detect_motion(frame)
|
| 136 |
color_change_score = self.detect_color_changes(frame)
|
| 137 |
+
audio_score = self.detect_audio(frame) if hasattr(frame, 'audio') else 0
|
| 138 |
|
| 139 |
detected_behaviors = []
|
| 140 |
|
| 141 |
# High activity detection (running, jumping)
|
| 142 |
if motion_score > self.behaviors['high_activity']['threshold']:
|
| 143 |
detected_behaviors.append(('high_activity', motion_score))
|
| 144 |
+
|
| 145 |
+
# Jumping detection (vertical motion)
|
| 146 |
+
if self.detect_vertical_motion(frame) > self.behaviors['jumping']['threshold']:
|
| 147 |
+
detected_behaviors.append(('jumping', motion_score * 1.2))
|
| 148 |
|
| 149 |
# Regular movement detection
|
| 150 |
elif motion_score > self.behaviors['movement']['threshold']:
|
|
|
|
| 157 |
# Tail wagging detection - based on localized color changes
|
| 158 |
if color_change_score > self.behaviors['tail_wagging']['threshold']:
|
| 159 |
detected_behaviors.append(('tail_wagging', color_change_score))
|
| 160 |
+
|
| 161 |
+
# Ears perked detection - based on ear region analysis
|
| 162 |
+
ears_score = self.detect_ear_position(frame)
|
| 163 |
+
if ears_score > self.behaviors['ears_perked']['threshold']:
|
| 164 |
+
detected_behaviors.append(('ears_perked', ears_score))
|
| 165 |
+
|
| 166 |
+
# Barking detection - based on audio analysis
|
| 167 |
+
if audio_score > self.behaviors['barking']['threshold']:
|
| 168 |
+
detected_behaviors.append(('barking', audio_score))
|
| 169 |
|
| 170 |
# Debug information
|
| 171 |
if not detected_behaviors:
|
|
|
|
| 173 |
st.sidebar.write(f"Debug - Color Change Score: {color_change_score:.4f}")
|
| 174 |
|
| 175 |
return detected_behaviors
|
| 176 |
+
|
| 177 |
+
def detect_vertical_motion(self, frame):
|
| 178 |
+
"""Detect vertical motion for jumping behavior"""
|
| 179 |
+
# Simple implementation - can be enhanced with more sophisticated motion tracking
|
| 180 |
+
if self.prev_frame is None:
|
| 181 |
+
return 0.0
|
| 182 |
+
|
| 183 |
+
frame = cv2.resize(frame, (300, 300))
|
| 184 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 185 |
+
|
| 186 |
+
# Calculate optical flow
|
| 187 |
+
flow = cv2.calcOpticalFlowFarneback(self.prev_frame, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
|
| 188 |
+
|
| 189 |
+
# Extract vertical motion component
|
| 190 |
+
vertical_motion = np.abs(flow[..., 1]).mean()
|
| 191 |
+
return vertical_motion
|
| 192 |
+
|
| 193 |
+
def detect_ear_position(self, frame):
|
| 194 |
+
"""Detect ear position for ears_perked behavior"""
|
| 195 |
+
# Placeholder implementation - can be enhanced with actual ear detection model
|
| 196 |
+
# For now, using simple edge detection in upper region of frame
|
| 197 |
+
frame = cv2.resize(frame, (300, 300))
|
| 198 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 199 |
+
|
| 200 |
+
# Focus on upper region where ears typically are
|
| 201 |
+
upper_region = gray[0:100, :]
|
| 202 |
+
edges = cv2.Canny(upper_region, 100, 200)
|
| 203 |
+
|
| 204 |
+
return np.sum(edges > 0) / edges.size
|
| 205 |
+
|
| 206 |
+
def detect_audio(self, frame):
|
| 207 |
+
"""Detect audio for barking behavior"""
|
| 208 |
+
# Placeholder - actual implementation would need audio processing
|
| 209 |
+
# Return 0 as this is just a placeholder
|
| 210 |
+
return 0.0
|
| 211 |
|
| 212 |
def get_suggestions(self, detected_behaviors, behavior_counts):
|
| 213 |
"""Generate suggestions based on detected behaviors and their frequency"""
|