Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,6 @@ from PIL import Image
|
|
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 |
-
from tensorflow.keras import layers, Model
|
15 |
|
16 |
# Set page config
|
17 |
st.set_page_config(
|
@@ -22,155 +16,54 @@ st.set_page_config(
|
|
22 |
|
23 |
class DogBehaviorAnalyzer:
|
24 |
def __init__(self):
|
25 |
-
#
|
26 |
-
self.model =
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
'
|
40 |
-
'
|
41 |
-
'
|
42 |
-
'
|
43 |
-
'
|
44 |
}
|
45 |
|
46 |
-
def
|
47 |
-
|
48 |
-
|
49 |
-
video_input = layers.Input(shape=(None, 224, 224, 3))
|
50 |
-
sensor_input = layers.Input(shape=(None, sensor_features))
|
51 |
-
|
52 |
-
# CNN for video processing
|
53 |
-
cnn = layers.Conv2D(64, (3, 3), activation='relu')(video_input)
|
54 |
-
# ... additional CNN layers ...
|
55 |
-
|
56 |
-
# LSTM for temporal features
|
57 |
-
lstm = layers.LSTM(128, return_sequences=True)(cnn)
|
58 |
-
|
59 |
-
# Fusion layer
|
60 |
-
fusion = layers.Concatenate()([lstm, sensor_input])
|
61 |
-
|
62 |
-
# Output layer
|
63 |
-
output = layers.Dense(len(self.behaviors), activation='softmax')(fusion)
|
64 |
-
|
65 |
-
return Model(inputs=[video_input, sensor_input], outputs=output)
|
66 |
-
|
67 |
-
def analyze_frame(self, frame, sensor_data=None):
|
68 |
-
"""Enhanced frame analysis using fusion model"""
|
69 |
-
# Convert frame to appropriate format
|
70 |
-
processed_frame = self.preprocess_frame(frame)
|
71 |
-
|
72 |
-
if sensor_data is not None:
|
73 |
-
# Use fusion model for more accurate detection
|
74 |
-
predictions = self.fusion_model.predict([processed_frame, sensor_data])
|
75 |
-
else:
|
76 |
-
# Fallback to video-only analysis
|
77 |
-
predictions = self.model.predict(processed_frame)
|
78 |
-
|
79 |
-
# Apply confidence thresholds
|
80 |
-
detected_behaviors = []
|
81 |
-
for behavior, confidence in zip(self.behaviors.keys(), predictions[0]):
|
82 |
-
if confidence > self.behavior_thresholds[behavior]:
|
83 |
-
detected_behaviors.append({
|
84 |
-
'behavior': behavior,
|
85 |
-
'confidence': float(confidence),
|
86 |
-
'timestamp': time.time()
|
87 |
-
})
|
88 |
-
|
89 |
-
return detected_behaviors
|
90 |
-
|
91 |
-
def validate_detection(self, behaviors, previous_behaviors):
|
92 |
-
"""Add temporal consistency check"""
|
93 |
-
validated_behaviors = []
|
94 |
-
for behavior in behaviors:
|
95 |
-
# Check temporal consistency
|
96 |
-
if self.is_temporally_consistent(behavior, previous_behaviors):
|
97 |
-
validated_behaviors.append(behavior)
|
98 |
-
return validated_behaviors
|
99 |
-
|
100 |
-
def create_animation(self, behavior):
|
101 |
-
"""Create simple animations for behaviors"""
|
102 |
-
# Create a simple animation frame
|
103 |
-
img = Image.new('RGB', (200, 200), 'white')
|
104 |
-
draw = ImageDraw.Draw(img)
|
105 |
-
|
106 |
-
if behavior == 'tail_wagging':
|
107 |
-
# Draw a simple tail wagging animation
|
108 |
-
draw.arc([50, 50, 150, 150], 0, 180, fill='black', width=2)
|
109 |
-
elif behavior == 'barking':
|
110 |
-
# Draw speech-bubble like shapes
|
111 |
-
draw.ellipse([50, 50, 150, 150], outline='black', width=2)
|
112 |
-
|
113 |
-
# Convert to base64 for display
|
114 |
-
buffered = BytesIO()
|
115 |
-
img.save(buffered, format="PNG")
|
116 |
-
return base64.b64encode(buffered.getvalue()).decode()
|
117 |
-
|
118 |
-
def create_visualization(self, behavior, frame):
|
119 |
-
"""Create more accurate behavior visualizations"""
|
120 |
-
# Create overlay on actual frame instead of generic shapes
|
121 |
-
overlay = frame.copy()
|
122 |
|
123 |
-
#
|
124 |
-
|
|
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
elif behavior == 'sitting':
|
131 |
-
# Draw pose skeleton
|
132 |
-
self.draw_pose_skeleton(overlay, keypoints)
|
133 |
-
# ... other behaviors ...
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
"""Evaluate detection quality using metrics from the paper"""
|
139 |
-
metrics = {
|
140 |
-
'accuracy': 0,
|
141 |
-
'precision': 0,
|
142 |
-
'recall': 0,
|
143 |
-
'f_score': 0
|
144 |
-
}
|
145 |
-
|
146 |
-
# Calculate metrics based on paper formulas
|
147 |
-
true_positives = len([d for d in detections if d['confidence'] > 0.9])
|
148 |
-
false_positives = len([d for d in detections if d['confidence'] < 0.7])
|
149 |
|
150 |
-
|
151 |
-
# ... calculate other metrics ...
|
152 |
-
|
153 |
-
return metrics
|
154 |
-
|
155 |
-
def analyze_sequence(self, frames, window_size=30):
|
156 |
-
"""Analyze behavior over multiple frames"""
|
157 |
-
sequence_behaviors = []
|
158 |
-
|
159 |
-
for i in range(len(frames) - window_size):
|
160 |
-
window = frames[i:i+window_size]
|
161 |
-
frame_behaviors = [self.analyze_frame(f) for f in window]
|
162 |
-
|
163 |
-
# Apply temporal smoothing
|
164 |
-
smoothed_behavior = self.temporal_smoothing(frame_behaviors)
|
165 |
-
sequence_behaviors.append(smoothed_behavior)
|
166 |
-
|
167 |
-
return sequence_behaviors
|
168 |
|
169 |
def main():
|
170 |
st.title("🐕 Dog Language Understanding")
|
171 |
-
st.write("Upload a video of your dog to analyze their behavior
|
172 |
|
|
|
173 |
analyzer = DogBehaviorAnalyzer()
|
|
|
|
|
174 |
video_file = st.file_uploader("Upload Video", type=['mp4', 'avi', 'mov'])
|
175 |
|
176 |
if video_file is not None:
|
@@ -178,7 +71,7 @@ def main():
|
|
178 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
179 |
tfile.write(video_file.read())
|
180 |
|
181 |
-
# Video
|
182 |
cap = cv2.VideoCapture(tfile.name)
|
183 |
|
184 |
# Create columns for layout
|
@@ -187,18 +80,20 @@ def main():
|
|
187 |
with col1:
|
188 |
st.subheader("Video Preview")
|
189 |
video_placeholder = st.empty()
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
# Analysis results storage
|
192 |
behavior_counts = {behavior: 0 for behavior in analyzer.behaviors.keys()}
|
193 |
-
current_emotions = set()
|
194 |
|
195 |
frame_count = 0
|
196 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
197 |
|
198 |
-
# Progress bar
|
199 |
-
progress_bar = st.progress(0)
|
200 |
-
progress_text = st.empty()
|
201 |
-
|
202 |
while cap.isOpened():
|
203 |
ret, frame = cap.read()
|
204 |
if not ret:
|
@@ -207,67 +102,42 @@ def main():
|
|
207 |
frame_count += 1
|
208 |
progress = frame_count / total_frames
|
209 |
progress_bar.progress(progress)
|
210 |
-
progress_text.text(f"Analyzing video: {int(progress * 100)}%")
|
211 |
|
212 |
-
# Update video preview
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
)
|
219 |
|
220 |
# Analyze frame
|
221 |
detected_behaviors = analyzer.analyze_frame(frame)
|
222 |
for behavior in detected_behaviors:
|
223 |
behavior_counts[behavior] += 1
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
st.write(f"**Emotion:** {behavior_info['emotion']}")
|
238 |
-
st.write(f"**Description:** {behavior_info['description']}")
|
239 |
-
|
240 |
-
# Display behavior animation
|
241 |
-
animation_data = analyzer.create_animation(behavior)
|
242 |
-
st.image(
|
243 |
-
f"data:image/png;base64,{animation_data}",
|
244 |
-
width=100,
|
245 |
-
caption=f"{behavior.replace('_', ' ').title()} Visual"
|
246 |
-
)
|
247 |
-
|
248 |
-
# Display training tips
|
249 |
-
st.subheader("Training Tips:")
|
250 |
-
for tip in behavior_info['tips']:
|
251 |
-
st.info(tip)
|
252 |
-
|
253 |
-
# Create emotion timeline
|
254 |
-
if current_emotions:
|
255 |
-
st.subheader("Emotional Journey")
|
256 |
-
emotions_df = pd.DataFrame(list(current_emotions), columns=['Emotion'])
|
257 |
-
fig = go.Figure(data=[go.Scatter(
|
258 |
-
x=emotions_df.index,
|
259 |
-
y=emotions_df['Emotion'],
|
260 |
-
mode='lines+markers'
|
261 |
-
)])
|
262 |
-
fig.update_layout(
|
263 |
-
xaxis_title='Time',
|
264 |
-
yaxis_title='Emotion',
|
265 |
-
height=400
|
266 |
)
|
267 |
-
|
|
|
|
|
|
|
268 |
|
269 |
-
#
|
270 |
st.subheader("Analysis Summary")
|
|
|
|
|
|
|
271 |
col1, col2, col3 = st.columns(3)
|
272 |
|
273 |
with col1:
|
@@ -282,23 +152,18 @@ def main():
|
|
282 |
behavior_variety = len([b for b in behavior_counts.values() if b > 0])
|
283 |
st.metric("Behavior Variety", f"{behavior_variety} types")
|
284 |
|
285 |
-
#
|
|
|
286 |
if total_behaviors > 0:
|
287 |
-
st.
|
288 |
-
|
289 |
-
st.write(f"""
|
290 |
-
Based on the analysis, here are personalized recommendations for your dog's dominant behavior ({dominant_behavior.replace('_', ' ')}):
|
291 |
-
|
292 |
-
{' '.join(analyzer.behaviors[dominant_behavior]['tips'])}
|
293 |
-
|
294 |
-
**General recommendations:**
|
295 |
- Maintain regular exercise routines
|
296 |
- Provide mental stimulation through toys and training
|
297 |
- Continue positive reinforcement training
|
298 |
- Monitor your dog's body language for better communication
|
299 |
""")
|
300 |
else:
|
301 |
-
st.
|
302 |
|
303 |
if __name__ == "__main__":
|
304 |
main()
|
|
|
6 |
import torch
|
7 |
from torchvision import transforms, models
|
8 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Set page config
|
11 |
st.set_page_config(
|
|
|
16 |
|
17 |
class DogBehaviorAnalyzer:
|
18 |
def __init__(self):
|
19 |
+
# Initialize model (using pretrained ResNet for this example)
|
20 |
+
self.model = models.resnet50(pretrained=True)
|
21 |
+
self.model.eval()
|
22 |
+
|
23 |
+
# Define image transformations
|
24 |
+
self.transform = transforms.Compose([
|
25 |
+
transforms.Resize((224, 224)),
|
26 |
+
transforms.ToTensor(),
|
27 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
28 |
+
std=[0.229, 0.224, 0.225])
|
29 |
+
])
|
30 |
+
|
31 |
+
# Define behavior mappings
|
32 |
+
self.behaviors = {
|
33 |
+
'tail_wagging': 'Your dog is happy and excited!',
|
34 |
+
'barking': 'Your dog is trying to communicate or alert you.',
|
35 |
+
'ears_perked': 'Your dog is alert and interested.',
|
36 |
+
'lying_down': 'Your dog is relaxed and comfortable.',
|
37 |
+
'jumping': 'Your dog is energetic and playful!'
|
38 |
}
|
39 |
|
40 |
+
def analyze_frame(self, frame):
|
41 |
+
# Convert frame to PIL Image
|
42 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Transform image
|
45 |
+
input_tensor = self.transform(image)
|
46 |
+
input_batch = input_tensor.unsqueeze(0)
|
47 |
|
48 |
+
# Simulate behavior detection
|
49 |
+
# In a real implementation, you'd use a properly trained model
|
50 |
+
behaviors = []
|
51 |
+
confidence_scores = np.random.random(len(self.behaviors))
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
for behavior, score in zip(self.behaviors.keys(), confidence_scores):
|
54 |
+
if score > 0.7: # Threshold for detection
|
55 |
+
behaviors.append(behavior)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
65 |
+
|
66 |
+
# File uploader
|
67 |
video_file = st.file_uploader("Upload Video", type=['mp4', 'avi', 'mov'])
|
68 |
|
69 |
if video_file is not None:
|
|
|
71 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
72 |
tfile.write(video_file.read())
|
73 |
|
74 |
+
# Video analysis
|
75 |
cap = cv2.VideoCapture(tfile.name)
|
76 |
|
77 |
# Create columns for layout
|
|
|
80 |
with col1:
|
81 |
st.subheader("Video Preview")
|
82 |
video_placeholder = st.empty()
|
83 |
+
|
84 |
+
with col2:
|
85 |
+
st.subheader("Real-time Analysis")
|
86 |
+
analysis_placeholder = st.empty()
|
87 |
+
|
88 |
+
# Progress bar
|
89 |
+
progress_bar = st.progress(0)
|
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))
|
96 |
|
|
|
|
|
|
|
|
|
97 |
while cap.isOpened():
|
98 |
ret, frame = cap.read()
|
99 |
if not ret:
|
|
|
102 |
frame_count += 1
|
103 |
progress = frame_count / total_frames
|
104 |
progress_bar.progress(progress)
|
|
|
105 |
|
106 |
+
# Update video preview
|
107 |
+
video_placeholder.image(
|
108 |
+
cv2.cvtColor(frame, cv2.COLOR_BGR2RGB),
|
109 |
+
channels="RGB",
|
110 |
+
use_container_width=True # Changed from use_column_width
|
111 |
+
)
|
|
|
112 |
|
113 |
# Analyze frame
|
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" {analyzer.behaviors[behavior]}\n\n"
|
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 |
+
time.sleep(0.1) # Add small delay for visualization
|
133 |
+
|
134 |
+
cap.release()
|
135 |
|
136 |
+
# Final summary
|
137 |
st.subheader("Analysis Summary")
|
138 |
+
st.write("Overall behavior analysis of your dog:")
|
139 |
+
|
140 |
+
# Create summary metrics
|
141 |
col1, col2, col3 = st.columns(3)
|
142 |
|
143 |
with col1:
|
|
|
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 |
+
# Recommendations
|
156 |
+
st.subheader("Recommendations")
|
157 |
if total_behaviors > 0:
|
158 |
+
st.write("""
|
159 |
+
Based on the analysis, here are some recommendations:
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
- Maintain regular exercise routines
|
161 |
- Provide mental stimulation through toys and training
|
162 |
- Continue positive reinforcement training
|
163 |
- Monitor your dog's body language for better communication
|
164 |
""")
|
165 |
else:
|
166 |
+
st.write("No behaviors detected. Try uploading a different video with clearer dog movements.")
|
167 |
|
168 |
if __name__ == "__main__":
|
169 |
main()
|