ombhojane commited on
Commit
c900cba
Β·
verified Β·
1 Parent(s): 6715760

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -78
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
- class DogLanguageInterpreter:
 
 
 
 
 
 
 
9
  def __init__(self):
10
- # Initialize any models or configurations here
11
- self.emotions = {
12
- 'tail_wagging': 'Happy and excited',
13
- 'ears_perked': 'Alert and interested',
14
- 'lying_down': 'Relaxed or tired',
15
- 'barking': 'Trying to communicate',
16
- 'jumping': 'Enthusiastic and playful'
17
- }
18
-
19
- def analyze_video(self, video_file):
20
- # Placeholder for video analysis logic
21
- # In a real implementation, this would use CV models
22
- return {
23
- 'primary_emotion': 'Happy and excited',
24
- 'confidence': 0.85,
25
- 'actions_detected': ['tail_wagging', 'jumping'],
26
- 'recommendations': [
27
- 'Your dog seems very playful!',
28
- 'Consider engaging in some active play',
29
- 'Reward this positive energy with treats'
30
- ]
31
  }
32
 
33
- def main():
34
- st.set_page_config(
35
- page_title="Dog Language Interpreter",
36
- page_icon="πŸ•",
37
- layout="wide"
38
- )
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Header
41
- st.title("πŸ• Pet Dog Language Understanding")
42
- st.markdown("""
43
- Upload a video of your dog, and our AI will help interpret their behavior and emotions!
44
- """)
45
 
46
- # Initialize interpreter
47
- interpreter = DogLanguageInterpreter()
48
 
49
  # File uploader
50
- video_file = st.file_uploader("Upload a video of your dog", type=['mp4', 'mov', 'avi'])
51
 
52
- if video_file:
53
- # Save video to temporary file
54
  tfile = tempfile.NamedTemporaryFile(delete=False)
55
  tfile.write(video_file.read())
56
 
57
- # Display video
58
- st.video(tfile.name)
59
-
60
- # Analysis button
61
- if st.button("Analyze Dog Behavior"):
62
- with st.spinner("Analyzing your dog's behavior..."):
63
- # Simulate processing time
64
- time.sleep(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- # Get analysis results
67
- results = interpreter.analyze_video(tfile.name)
68
-
69
- # Display results in columns
70
- col1, col2 = st.columns(2)
71
-
72
- with col1:
73
- st.subheader("Primary Emotion")
74
- st.info(results['primary_emotion'])
75
- st.metric("Confidence", f"{results['confidence']*100:.1f}%")
76
-
77
- with col2:
78
- st.subheader("Actions Detected")
79
- for action in results['actions_detected']:
80
- st.success(action.replace('_', ' ').title())
81
-
82
- # Recommendations
83
- st.subheader("Recommendations")
84
- for rec in results['recommendations']:
85
- st.write("β€’ " + rec)
86
-
87
- # Additional information
88
- with st.expander("About Dog Body Language"):
89
- st.markdown("""
90
- ### Common Dog Behaviors and Their Meanings:
91
- - **Tail Wagging**: Usually indicates happiness, but the position matters
92
- - **Ears Position**: Can show alertness, submission, or aggression
93
- - **Body Posture**: Reveals confidence or anxiety levels
94
- - **Facial Expressions**: Can indicate stress, happiness, or attention
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- Remember that each dog is unique and may express emotions differently!
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()