Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,168 @@
|
|
1 |
import streamlit as st
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
import tempfile
|
|
|
|
|
|
|
6 |
import time
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def __init__(self):
|
10 |
-
# Initialize
|
11 |
-
self.
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
'
|
25 |
-
'
|
26 |
-
'
|
27 |
-
|
28 |
-
|
29 |
-
'Reward this positive energy with treats'
|
30 |
-
]
|
31 |
}
|
32 |
|
33 |
-
def
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
st.title("π
|
42 |
-
st.
|
43 |
-
Upload a video of your dog, and our AI will help interpret their behavior and emotions!
|
44 |
-
""")
|
45 |
|
46 |
-
# Initialize
|
47 |
-
|
48 |
|
49 |
# File uploader
|
50 |
-
video_file = st.file_uploader("Upload
|
51 |
|
52 |
-
if video_file:
|
53 |
-
# Save
|
54 |
tfile = tempfile.NamedTemporaryFile(delete=False)
|
55 |
tfile.write(video_file.read())
|
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 |
if __name__ == "__main__":
|
100 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
4 |
import tempfile
|
5 |
+
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(
|
12 |
+
page_title="Dog Language Understanding",
|
13 |
+
page_icon="π",
|
14 |
+
layout="wide"
|
15 |
+
)
|
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:
|
70 |
+
# Save uploaded file temporarily
|
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
|
78 |
+
col1, col2 = st.columns(2)
|
79 |
+
|
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:
|
100 |
+
break
|
101 |
|
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_column_width=True
|
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 |
+
)
|
130 |
+
|
131 |
+
time.sleep(0.1) # Add small delay for visualization
|
132 |
+
|
133 |
+
cap.release()
|
134 |
+
|
135 |
+
# Final summary
|
136 |
+
st.subheader("Analysis Summary")
|
137 |
+
st.write("Overall behavior analysis of your dog:")
|
138 |
+
|
139 |
+
# Create summary metrics
|
140 |
+
col1, col2, col3 = st.columns(3)
|
141 |
+
|
142 |
+
with col1:
|
143 |
+
most_common = max(behavior_counts.items(), key=lambda x: x[1])[0]
|
144 |
+
st.metric("Most Common Behavior", most_common.replace('_', ' ').title())
|
145 |
+
|
146 |
+
with col2:
|
147 |
+
total_behaviors = sum(behavior_counts.values())
|
148 |
+
st.metric("Total Behaviors Detected", total_behaviors)
|
149 |
+
|
150 |
+
with col3:
|
151 |
+
behavior_variety = len([b for b in behavior_counts.values() if b > 0])
|
152 |
+
st.metric("Behavior Variety", f"{behavior_variety} types")
|
153 |
|
154 |
+
# Recommendations
|
155 |
+
st.subheader("Recommendations")
|
156 |
+
if total_behaviors > 0:
|
157 |
+
st.write("""
|
158 |
+
Based on the analysis, here are some recommendations:
|
159 |
+
- Maintain regular exercise routines
|
160 |
+
- Provide mental stimulation through toys and training
|
161 |
+
- Continue positive reinforcement training
|
162 |
+
- Monitor your dog's body language for better communication
|
163 |
+
""")
|
164 |
+
else:
|
165 |
+
st.write("No behaviors detected. Try uploading a different video with clearer dog movements.")
|
166 |
|
167 |
if __name__ == "__main__":
|
168 |
main()
|