ombhojane commited on
Commit
98a1522
·
verified ·
1 Parent(s): dc56a44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -34
app.py CHANGED
@@ -21,19 +21,22 @@ st.set_page_config(
21
  class DogBehaviorAnalyzer:
22
  def __init__(self):
23
  self.behaviors = {
24
- 'tail_wagging': {'threshold': 0.6, 'description': 'Your dog is displaying happiness and excitement!'},
25
- 'movement': {'threshold': 0.5, 'description': 'Your dog is active and moving around.'},
26
- 'stationary': {'threshold': 0.7, 'description': 'Your dog is calm and still.'},
27
- 'high_activity': {'threshold': 0.65, 'description': 'Your dog is very energetic!'}
28
  }
29
 
30
  # Motion detection parameters
31
  self.history = []
32
  self.max_history = 10
33
  self.prev_frame = None
 
34
 
35
  def detect_motion(self, frame):
36
- """Detect motion in frame"""
 
 
37
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
38
  gray = cv2.GaussianBlur(gray, (21, 21), 0)
39
 
@@ -42,57 +45,74 @@ class DogBehaviorAnalyzer:
42
  return 0.0
43
 
44
  frame_delta = cv2.absdiff(self.prev_frame, gray)
45
- thresh = cv2.threshold(frame_delta, 25, 255, cv2.THRESH_BINARY)[1]
46
  thresh = cv2.dilate(thresh, None, iterations=2)
47
 
 
48
  motion_score = np.sum(thresh > 0) / thresh.size
49
  self.prev_frame = gray
50
 
51
- return motion_score
 
52
 
 
 
 
53
  def detect_color_changes(self, frame):
54
- """Detect significant color changes that might indicate tail wagging"""
 
55
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
56
- lower = np.array([0, 0, 0])
57
- upper = np.array([180, 255, 255])
58
- mask = cv2.inRange(hsv, lower, upper)
59
-
60
- if len(self.history) > 0:
61
- prev_mask = self.history[-1]
62
- diff = cv2.absdiff(mask, prev_mask)
63
- change_score = np.sum(diff > 0) / diff.size
64
- else:
65
- change_score = 0.0
 
 
 
 
 
 
66
 
67
  self.history.append(mask)
68
  if len(self.history) > self.max_history:
69
  self.history.pop(0)
70
 
71
- return change_score
72
 
73
  def analyze_frame(self, frame):
74
- """Analyze frame using motion and color change detection"""
75
  motion_score = self.detect_motion(frame)
76
  color_change_score = self.detect_color_changes(frame)
77
 
78
  detected_behaviors = []
79
 
80
- # Detect tail wagging based on localized color changes
81
- if color_change_score > self.behaviors['tail_wagging']['threshold']:
82
- detected_behaviors.append(('tail_wagging', color_change_score))
83
-
84
- # Detect movement
85
- if motion_score > self.behaviors['movement']['threshold']:
86
- detected_behaviors.append(('movement', motion_score))
87
-
88
- # Detect stationary behavior
89
- if motion_score < self.behaviors['stationary']['threshold']:
90
- detected_behaviors.append(('stationary', 1.0 - motion_score))
91
-
92
- # Detect high activity
93
  if motion_score > self.behaviors['high_activity']['threshold']:
94
  detected_behaviors.append(('high_activity', motion_score))
95
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  return detected_behaviors
97
 
98
  def main():
 
21
  class DogBehaviorAnalyzer:
22
  def __init__(self):
23
  self.behaviors = {
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
  # Motion detection parameters
31
  self.history = []
32
  self.max_history = 10
33
  self.prev_frame = None
34
+ self.motion_history = deque(maxlen=5) # Store recent motion scores
35
 
36
  def detect_motion(self, frame):
37
+ """Detect motion in frame with improved sensitivity"""
38
+ # Resize frame for consistent motion detection
39
+ frame = cv2.resize(frame, (300, 300))
40
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
41
  gray = cv2.GaussianBlur(gray, (21, 21), 0)
42
 
 
45
  return 0.0
46
 
47
  frame_delta = cv2.absdiff(self.prev_frame, gray)
48
+ thresh = cv2.threshold(frame_delta, 20, 255, cv2.THRESH_BINARY)[1] # Lower threshold for better sensitivity
49
  thresh = cv2.dilate(thresh, None, iterations=2)
50
 
51
+ # Calculate motion score
52
  motion_score = np.sum(thresh > 0) / thresh.size
53
  self.prev_frame = gray
54
 
55
+ # Add to motion history
56
+ self.motion_history.append(motion_score)
57
 
58
+ # Return average of recent motion scores for stability
59
+ return np.mean(self.motion_history) if len(self.motion_history) > 0 else motion_score
60
+
61
  def detect_color_changes(self, frame):
62
+ """Detect significant color changes with improved sensitivity"""
63
+ frame = cv2.resize(frame, (300, 300))
64
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
65
+
66
+ # Define range for common dog colors
67
+ color_ranges = [
68
+ (np.array([0, 30, 30]), np.array([30, 255, 255])), # Brown/Red
69
+ (np.array([0, 0, 0]), np.array([180, 50, 255])), # White/Gray/Black
70
+ ]
71
+
72
+ total_change_score = 0
73
+ for lower, upper in color_ranges:
74
+ mask = cv2.inRange(hsv, lower, upper)
75
+
76
+ if len(self.history) > 0:
77
+ prev_mask = self.history[-1]
78
+ diff = cv2.absdiff(mask, prev_mask)
79
+ change_score = np.sum(diff > 0) / diff.size
80
+ total_change_score = max(total_change_score, change_score)
81
 
82
  self.history.append(mask)
83
  if len(self.history) > self.max_history:
84
  self.history.pop(0)
85
 
86
+ return total_change_score
87
 
88
  def analyze_frame(self, frame):
89
+ """Analyze frame with improved behavior detection logic"""
90
  motion_score = self.detect_motion(frame)
91
  color_change_score = self.detect_color_changes(frame)
92
 
93
  detected_behaviors = []
94
 
95
+ # High activity detection (running, jumping)
 
 
 
 
 
 
 
 
 
 
 
 
96
  if motion_score > self.behaviors['high_activity']['threshold']:
97
  detected_behaviors.append(('high_activity', motion_score))
98
+
99
+ # Regular movement detection
100
+ elif motion_score > self.behaviors['movement']['threshold']:
101
+ detected_behaviors.append(('movement', motion_score))
102
+
103
+ # Stationary detection - only if very little motion
104
+ elif motion_score < self.behaviors['stationary']['threshold']:
105
+ detected_behaviors.append(('stationary', 1.0 - motion_score))
106
+
107
+ # Tail wagging detection - based on localized color changes
108
+ if color_change_score > self.behaviors['tail_wagging']['threshold']:
109
+ detected_behaviors.append(('tail_wagging', color_change_score))
110
+
111
+ # Debug information
112
+ if not detected_behaviors:
113
+ st.sidebar.write(f"Debug - Motion Score: {motion_score:.4f}")
114
+ st.sidebar.write(f"Debug - Color Change Score: {color_change_score:.4f}")
115
+
116
  return detected_behaviors
117
 
118
  def main():