try1
Browse files
app.py
CHANGED
@@ -15,74 +15,46 @@ st.set_page_config(
|
|
15 |
)
|
16 |
|
17 |
class DogBehaviorAnalyzer:
|
18 |
-
|
19 |
-
behaviors = {
|
20 |
-
'tail_wagging': {'description': 'Your dog is happy and excited!', 'threshold': 0.75},
|
21 |
-
'barking': {'description': 'Your dog is trying to communicate or alert you.', 'threshold': 0.80},
|
22 |
-
'ears_perked': {'description': 'Your dog is alert and interested.', 'threshold': 0.70},
|
23 |
-
'lying_down': {'description': 'Your dog is relaxed and comfortable.', 'threshold': 0.85},
|
24 |
-
'jumping': {'description': 'Your dog is energetic and playful!', 'threshold': 0.75}
|
25 |
-
}
|
26 |
def __init__(self):
|
27 |
-
#
|
28 |
self.model = models.resnet50(pretrained=True)
|
29 |
-
|
30 |
-
# Replace the last fully connected layer for our specific number of classes
|
31 |
-
num_ftrs = self.model.fc.in_features
|
32 |
-
self.model.fc = torch.nn.Linear(num_ftrs, len(self.behaviors))
|
33 |
self.model.eval()
|
34 |
|
35 |
-
#
|
36 |
self.transform = transforms.Compose([
|
37 |
transforms.Resize((224, 224)),
|
38 |
-
transforms.RandomHorizontalFlip(),
|
39 |
-
transforms.RandomRotation(10),
|
40 |
-
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
|
41 |
transforms.ToTensor(),
|
42 |
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
43 |
std=[0.229, 0.224, 0.225])
|
44 |
])
|
45 |
|
46 |
-
#
|
47 |
self.behaviors = {
|
48 |
-
'tail_wagging':
|
49 |
-
'barking':
|
50 |
-
'ears_perked':
|
51 |
-
'lying_down':
|
52 |
-
'jumping':
|
53 |
}
|
54 |
|
55 |
-
|
56 |
def analyze_frame(self, frame):
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
# Filter behaviors based on confidence thresholds
|
76 |
-
behaviors = []
|
77 |
-
for behavior, score in zip(self.behaviors.keys(), confidence_scores):
|
78 |
-
if score > self.behaviors[behavior]['threshold']:
|
79 |
-
behaviors.append((behavior, score))
|
80 |
-
|
81 |
-
return behaviors
|
82 |
-
|
83 |
-
except Exception as e:
|
84 |
-
print(f"Error analyzing frame: {str(e)}")
|
85 |
-
return []
|
86 |
|
87 |
def main():
|
88 |
st.title("π Dog Language Understanding")
|
@@ -91,17 +63,8 @@ def main():
|
|
91 |
# Initialize analyzer
|
92 |
analyzer = DogBehaviorAnalyzer()
|
93 |
|
94 |
-
#
|
95 |
-
|
96 |
-
st.write("""
|
97 |
-
This model uses a fine-tuned ResNet50 architecture trained on dog behavior data.
|
98 |
-
- Supports multiple behavior detection
|
99 |
-
- Real-time analysis
|
100 |
-
- Confidence scoring
|
101 |
-
""")
|
102 |
-
|
103 |
-
# File uploader with more supported formats
|
104 |
-
video_file = st.file_uploader("Upload Video", type=['mp4', 'avi', 'mov', 'mkv'])
|
105 |
|
106 |
if video_file is not None:
|
107 |
# Save uploaded file temporarily
|
@@ -117,90 +80,87 @@ def main():
|
|
117 |
with col1:
|
118 |
st.subheader("Video Preview")
|
119 |
video_placeholder = st.empty()
|
120 |
-
|
121 |
with col2:
|
122 |
st.subheader("Real-time Analysis")
|
123 |
analysis_placeholder = st.empty()
|
124 |
|
125 |
# Progress bar
|
126 |
progress_bar = st.progress(0)
|
127 |
-
|
128 |
# Analysis results storage
|
129 |
behavior_counts = {behavior: 0 for behavior in analyzer.behaviors.keys()}
|
130 |
-
|
131 |
frame_count = 0
|
132 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
133 |
-
|
134 |
while cap.isOpened():
|
135 |
ret, frame = cap.read()
|
136 |
if not ret:
|
137 |
break
|
138 |
-
|
139 |
frame_count += 1
|
140 |
progress = frame_count / total_frames
|
141 |
progress_bar.progress(progress)
|
142 |
-
|
143 |
# Update video preview
|
144 |
video_placeholder.image(
|
145 |
cv2.cvtColor(frame, cv2.COLOR_BGR2RGB),
|
146 |
channels="RGB",
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
# Analyze frame
|
151 |
detected_behaviors = analyzer.analyze_frame(frame)
|
152 |
for behavior in detected_behaviors:
|
153 |
behavior_counts[behavior] += 1
|
154 |
-
|
155 |
-
|
156 |
analysis_text = "Detected Behaviors:\n\n"
|
157 |
for behavior, count in behavior_counts.items():
|
158 |
if count > 0:
|
159 |
-
|
160 |
-
analysis_text +=
|
161 |
-
|
162 |
-
f" {analyzer.behaviors[behavior]['description']}\n\n")
|
163 |
-
|
164 |
analysis_placeholder.text_area(
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
time.sleep(0.1) # Add small delay for visualization
|
172 |
-
|
173 |
-
cap.release()
|
174 |
|
|
|
|
|
175 |
# Final summary
|
176 |
st.subheader("Analysis Summary")
|
177 |
st.write("Overall behavior analysis of your dog:")
|
178 |
-
|
179 |
# Create summary metrics
|
180 |
col1, col2, col3 = st.columns(3)
|
181 |
-
|
182 |
with col1:
|
183 |
most_common = max(behavior_counts.items(), key=lambda x: x[1])[0]
|
184 |
st.metric("Most Common Behavior", most_common.replace('_', ' ').title())
|
185 |
-
|
186 |
with col2:
|
187 |
total_behaviors = sum(behavior_counts.values())
|
188 |
st.metric("Total Behaviors Detected", total_behaviors)
|
189 |
-
|
190 |
with col3:
|
191 |
behavior_variety = len([b for b in behavior_counts.values() if b > 0])
|
192 |
st.metric("Behavior Variety", f"{behavior_variety} types")
|
193 |
-
|
194 |
# Recommendations
|
195 |
st.subheader("Recommendations")
|
196 |
if total_behaviors > 0:
|
197 |
st.write("""
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
else:
|
205 |
st.write("No behaviors detected. Try uploading a different video with clearer dog movements.")
|
206 |
|
|
|
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")
|
|
|
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
|
|
|
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 |
|