enhnacements
Browse files
app.py
CHANGED
@@ -6,6 +6,11 @@ from PIL import Image
|
|
6 |
import torch
|
7 |
from torchvision import transforms, models
|
8 |
import time
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Set page config
|
11 |
st.set_page_config(
|
@@ -28,13 +33,53 @@ class DogBehaviorAnalyzer:
|
|
28 |
std=[0.229, 0.224, 0.225])
|
29 |
])
|
30 |
|
31 |
-
#
|
32 |
self.behaviors = {
|
33 |
-
'tail_wagging':
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
}
|
39 |
|
40 |
def analyze_frame(self, frame):
|
@@ -56,9 +101,27 @@ class DogBehaviorAnalyzer:
|
|
56 |
|
57 |
return behaviors
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def main():
|
60 |
st.title("🐕 Dog Language Understanding")
|
61 |
-
st.write("Upload a video of your dog to analyze their behavior!")
|
62 |
|
63 |
# Initialize analyzer
|
64 |
analyzer = DogBehaviorAnalyzer()
|
@@ -84,12 +147,14 @@ def main():
|
|
84 |
with col2:
|
85 |
st.subheader("Real-time Analysis")
|
86 |
analysis_placeholder = st.empty()
|
|
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
# Analysis results storage
|
92 |
behavior_counts = {behavior: 0 for behavior in analyzer.behaviors.keys()}
|
|
|
93 |
|
94 |
frame_count = 0
|
95 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
@@ -114,25 +179,54 @@ def main():
|
|
114 |
detected_behaviors = analyzer.analyze_frame(frame)
|
115 |
for behavior in detected_behaviors:
|
116 |
behavior_counts[behavior] += 1
|
|
|
117 |
|
118 |
-
# Update analysis display
|
119 |
-
analysis_text = "Detected Behaviors:\n\n"
|
120 |
for behavior, count in behavior_counts.items():
|
121 |
if count > 0:
|
|
|
122 |
analysis_text += f"• {behavior.replace('_', ' ').title()}: {count} times\n"
|
123 |
-
analysis_text += f" {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
analysis_placeholder.text_area(
|
126 |
"Analysis Results",
|
127 |
analysis_text,
|
128 |
-
height=300
|
129 |
-
key=f"analysis_{frame_count}" # Added unique key for each frame
|
130 |
)
|
131 |
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
|
|
|
|
134 |
cap.release()
|
135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
# Final summary
|
137 |
st.subheader("Analysis Summary")
|
138 |
st.write("Overall behavior analysis of your dog:")
|
@@ -152,11 +246,16 @@ def main():
|
|
152 |
behavior_variety = len([b for b in behavior_counts.values() if b > 0])
|
153 |
st.metric("Behavior Variety", f"{behavior_variety} types")
|
154 |
|
155 |
-
#
|
156 |
-
st.subheader("Recommendations")
|
157 |
if total_behaviors > 0:
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
160 |
- Maintain regular exercise routines
|
161 |
- Provide mental stimulation through toys and training
|
162 |
- Continue positive reinforcement training
|
|
|
6 |
import torch
|
7 |
from torchvision import transforms, models
|
8 |
import time
|
9 |
+
import plotly.graph_objects as go
|
10 |
+
from PIL import Image, ImageDraw
|
11 |
+
import base64
|
12 |
+
from io import BytesIO
|
13 |
+
import pandas as pd
|
14 |
|
15 |
# Set page config
|
16 |
st.set_page_config(
|
|
|
33 |
std=[0.229, 0.224, 0.225])
|
34 |
])
|
35 |
|
36 |
+
# Enhanced behavior mappings with emotions and tips
|
37 |
self.behaviors = {
|
38 |
+
'tail_wagging': {
|
39 |
+
'emotion': 'Happy and excited',
|
40 |
+
'description': 'Your dog is expressing joy and enthusiasm!',
|
41 |
+
'tips': [
|
42 |
+
'This is a great time for positive reinforcement training',
|
43 |
+
'Consider engaging in play or exercise',
|
44 |
+
'Use this excitement for teaching new tricks'
|
45 |
+
]
|
46 |
+
},
|
47 |
+
'barking': {
|
48 |
+
'emotion': 'Alert or communicative',
|
49 |
+
'description': 'Your dog is trying to communicate or alert you.',
|
50 |
+
'tips': [
|
51 |
+
'Check what triggered the barking',
|
52 |
+
'Use positive reinforcement for quiet behavior',
|
53 |
+
'Consider training "quiet" and "speak" commands'
|
54 |
+
]
|
55 |
+
},
|
56 |
+
'ears_perked': {
|
57 |
+
'emotion': 'Alert and interested',
|
58 |
+
'description': 'Your dog is focused and attentive.',
|
59 |
+
'tips': [
|
60 |
+
'Great moment for training exercises',
|
61 |
+
'Consider mental stimulation activities',
|
62 |
+
'Use this attention for bonding exercises'
|
63 |
+
]
|
64 |
+
},
|
65 |
+
'lying_down': {
|
66 |
+
'emotion': 'Relaxed and comfortable',
|
67 |
+
'description': 'Your dog is calm and at ease.',
|
68 |
+
'tips': [
|
69 |
+
'Perfect time for gentle petting',
|
70 |
+
'Maintain a peaceful environment',
|
71 |
+
'Consider quiet bonding activities'
|
72 |
+
]
|
73 |
+
},
|
74 |
+
'jumping': {
|
75 |
+
'emotion': 'Excited and playful',
|
76 |
+
'description': 'Your dog is energetic and seeking interaction!',
|
77 |
+
'tips': [
|
78 |
+
'Channel energy into structured play',
|
79 |
+
'Practice "four paws on floor" training',
|
80 |
+
'Consider agility exercises'
|
81 |
+
]
|
82 |
+
}
|
83 |
}
|
84 |
|
85 |
def analyze_frame(self, frame):
|
|
|
101 |
|
102 |
return behaviors
|
103 |
|
104 |
+
def create_animation(self, behavior):
|
105 |
+
"""Create simple animations for behaviors"""
|
106 |
+
# Create a simple animation frame
|
107 |
+
img = Image.new('RGB', (200, 200), 'white')
|
108 |
+
draw = ImageDraw.Draw(img)
|
109 |
+
|
110 |
+
if behavior == 'tail_wagging':
|
111 |
+
# Draw a simple tail wagging animation
|
112 |
+
draw.arc([50, 50, 150, 150], 0, 180, fill='black', width=2)
|
113 |
+
elif behavior == 'barking':
|
114 |
+
# Draw speech-bubble like shapes
|
115 |
+
draw.ellipse([50, 50, 150, 150], outline='black', width=2)
|
116 |
+
|
117 |
+
# Convert to base64 for display
|
118 |
+
buffered = BytesIO()
|
119 |
+
img.save(buffered, format="PNG")
|
120 |
+
return base64.b64encode(buffered.getvalue()).decode()
|
121 |
+
|
122 |
def main():
|
123 |
st.title("🐕 Dog Language Understanding")
|
124 |
+
st.write("Upload a video of your dog to analyze their behavior and emotions!")
|
125 |
|
126 |
# Initialize analyzer
|
127 |
analyzer = DogBehaviorAnalyzer()
|
|
|
147 |
with col2:
|
148 |
st.subheader("Real-time Analysis")
|
149 |
analysis_placeholder = st.empty()
|
150 |
+
emotion_placeholder = st.empty()
|
151 |
|
152 |
+
# Progress bar
|
153 |
+
progress_bar = st.progress(0)
|
154 |
+
|
155 |
# Analysis results storage
|
156 |
behavior_counts = {behavior: 0 for behavior in analyzer.behaviors.keys()}
|
157 |
+
current_emotions = set()
|
158 |
|
159 |
frame_count = 0
|
160 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
179 |
detected_behaviors = analyzer.analyze_frame(frame)
|
180 |
for behavior in detected_behaviors:
|
181 |
behavior_counts[behavior] += 1
|
182 |
+
current_emotions.add(behavior)
|
183 |
|
184 |
+
# Update analysis display with enhanced information
|
185 |
+
analysis_text = "Detected Behaviors & Emotions:\n\n"
|
186 |
for behavior, count in behavior_counts.items():
|
187 |
if count > 0:
|
188 |
+
behavior_info = analyzer.behaviors[behavior]
|
189 |
analysis_text += f"• {behavior.replace('_', ' ').title()}: {count} times\n"
|
190 |
+
analysis_text += f" Emotion: {behavior_info['emotion']}\n"
|
191 |
+
analysis_text += f" {behavior_info['description']}\n\n"
|
192 |
+
|
193 |
+
# Add animation for the behavior
|
194 |
+
animation_data = analyzer.create_animation(behavior)
|
195 |
+
st.image(f"data:image/png;base64,{animation_data}",
|
196 |
+
width=100,
|
197 |
+
caption=f"{behavior.replace('_', ' ').title()} Animation")
|
198 |
|
199 |
analysis_placeholder.text_area(
|
200 |
"Analysis Results",
|
201 |
analysis_text,
|
202 |
+
height=300
|
|
|
203 |
)
|
204 |
|
205 |
+
# Display training tips
|
206 |
+
if len(current_emotions) > 0:
|
207 |
+
st.subheader("Training Tips")
|
208 |
+
for behavior in current_emotions:
|
209 |
+
tips = analyzer.behaviors[behavior]['tips']
|
210 |
+
for tip in tips:
|
211 |
+
st.info(tip)
|
212 |
|
213 |
+
time.sleep(0.1)
|
214 |
+
|
215 |
cap.release()
|
216 |
|
217 |
+
# Final summary with enhanced visualizations
|
218 |
+
st.subheader("Analysis Summary")
|
219 |
+
|
220 |
+
# Create emotion timeline
|
221 |
+
emotions_df = pd.DataFrame(list(current_emotions), columns=['Emotion'])
|
222 |
+
fig = go.Figure(data=[go.Scatter(x=emotions_df.index,
|
223 |
+
y=emotions_df['Emotion'],
|
224 |
+
mode='lines+markers')])
|
225 |
+
fig.update_layout(title='Emotional Journey',
|
226 |
+
xaxis_title='Time',
|
227 |
+
yaxis_title='Emotion')
|
228 |
+
st.plotly_chart(fig)
|
229 |
+
|
230 |
# Final summary
|
231 |
st.subheader("Analysis Summary")
|
232 |
st.write("Overall behavior analysis of your dog:")
|
|
|
246 |
behavior_variety = len([b for b in behavior_counts.values() if b > 0])
|
247 |
st.metric("Behavior Variety", f"{behavior_variety} types")
|
248 |
|
249 |
+
# Enhanced recommendations
|
250 |
+
st.subheader("Personalized Recommendations")
|
251 |
if total_behaviors > 0:
|
252 |
+
dominant_behavior = max(behavior_counts.items(), key=lambda x: x[1])[0]
|
253 |
+
st.write(f"""
|
254 |
+
Based on the analysis, here are personalized recommendations for {dominant_behavior}:
|
255 |
+
|
256 |
+
{' '.join(analyzer.behaviors[dominant_behavior]['tips'])}
|
257 |
+
|
258 |
+
General recommendations:
|
259 |
- Maintain regular exercise routines
|
260 |
- Provide mental stimulation through toys and training
|
261 |
- Continue positive reinforcement training
|