capradeepgujaran commited on
Commit
bd1163f
1 Parent(s): 07117f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -63
app.py CHANGED
@@ -112,66 +112,54 @@ def create_monitor_interface():
112
  """Get coordinates based on position description."""
113
  # Basic regions
114
  regions = {
 
 
115
  'top-left': (0, 0, width//3, height//3),
116
  'top': (width//3, 0, 2*width//3, height//3),
117
  'top-right': (2*width//3, 0, width, height//3),
118
- 'center-left': (0, height//3, width//3, 2*height//3),
119
- 'center': (width//3, height//3, 2*width//3, 2*height//3),
120
- 'center-right': (2*width//3, height//3, width, 2*height//3),
121
  'bottom-left': (0, 2*height//3, width//3, height),
122
  'bottom': (width//3, 2*height//3, 2*width//3, height),
123
  'bottom-right': (2*width//3, 2*height//3, width, height),
124
- 'left': (0, height//4, width//3, 3*height//4),
125
- 'right': (2*width//3, height//4, width, 3*height//4)
126
  }
127
 
128
  # Find best matching region
129
- best_match = 'center'
130
- max_words = 0
131
- pos_lower = position.lower()
 
132
 
133
- for region in regions.keys():
134
- words = region.split('-')
135
- matches = sum(1 for word in words if word in pos_lower)
136
- if matches > max_words:
137
- max_words = matches
138
- best_match = region
139
-
140
- return regions[best_match]
141
 
142
  for idx, obs in enumerate(observations):
143
  color = self.colors[idx % len(self.colors)]
144
 
145
- # Parse location and description
146
- parts = obs.split(':')
147
- if len(parts) >= 2:
148
- position = parts[0]
149
- description = ':'.join(parts[1:])
150
-
151
- # Get region coordinates
152
- x1, y1, x2, y2 = get_region_coordinates(position)
153
-
154
- # Draw rectangle
155
- cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
156
-
157
- # Add label with background
158
- label = description[:50] + "..." if len(description) > 50 else description
159
- label_size, _ = cv2.getTextSize(label, font, font_scale, thickness)
160
-
161
- # Position text above the box
162
- text_x = max(0, x1)
163
- text_y = max(label_size[1] + padding, y1 - padding)
164
-
165
- # Draw text background
166
- cv2.rectangle(image,
167
- (text_x, text_y - label_size[1] - padding),
168
- (text_x + label_size[0] + padding, text_y),
169
- color, -1)
170
-
171
- # Draw text
172
- cv2.putText(image, label,
173
- (text_x + padding//2, text_y - padding//2),
174
- font, font_scale, (255, 255, 255), thickness)
175
 
176
  return image
177
 
@@ -180,27 +168,34 @@ def create_monitor_interface():
180
  return None, "No image provided"
181
 
182
  analysis = self.analyze_frame(frame)
183
- display_frame = self.resize_image(frame.copy())
184
 
185
- # Parse observations from the analysis
186
  observations = []
187
- for line in analysis.split('\n'):
188
- line = line.strip()
189
- if line.startswith('-'):
190
- # Extract text between <location> tags if present
191
- if '<location>' in line and '</location>' in line:
192
- start = line.find('<location>') + len('<location>')
193
- end = line.find('</location>')
194
- observation = line[end + len('</location>'):].strip()
195
- else:
196
- observation = line[1:].strip() # Remove the dash
197
- if observation:
198
- observations.append(observation)
 
 
 
 
 
199
 
200
- # Draw observations on the image
201
- annotated_frame = self.draw_observations(display_frame, observations)
 
 
202
 
203
- return annotated_frame, analysis
204
 
205
  # Create the main interface
206
  monitor = SafetyMonitor()
 
112
  """Get coordinates based on position description."""
113
  # Basic regions
114
  regions = {
115
+ 'center': (width//3, height//3, 2*width//3, 2*height//3),
116
+ 'background': (0, 0, width, height),
117
  'top-left': (0, 0, width//3, height//3),
118
  'top': (width//3, 0, 2*width//3, height//3),
119
  'top-right': (2*width//3, 0, width, height//3),
120
+ 'left': (0, height//3, width//3, 2*height//3),
121
+ 'right': (2*width//3, height//3, width, 2*height//3),
 
122
  'bottom-left': (0, 2*height//3, width//3, height),
123
  'bottom': (width//3, 2*height//3, 2*width//3, height),
124
  'bottom-right': (2*width//3, 2*height//3, width, height),
125
+ 'ground': (0, 2*height//3, width, height)
 
126
  }
127
 
128
  # Find best matching region
129
+ position = position.lower()
130
+ for key in regions.keys():
131
+ if key in position:
132
+ return regions[key]
133
 
134
+ return regions['center'] # Default to center if no match
 
 
 
 
 
 
 
135
 
136
  for idx, obs in enumerate(observations):
137
  color = self.colors[idx % len(self.colors)]
138
 
139
+ # Get coordinates for this observation
140
+ x1, y1, x2, y2 = get_region_coordinates(obs['location'])
141
+
142
+ # Draw rectangle
143
+ cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
144
+
145
+ # Add label with background
146
+ label = obs['description'][:50] + "..." if len(obs['description']) > 50 else obs['description']
147
+ label_size, _ = cv2.getTextSize(label, font, font_scale, thickness)
148
+
149
+ # Position text above the box
150
+ text_x = max(0, x1)
151
+ text_y = max(label_size[1] + padding, y1 - padding)
152
+
153
+ # Draw text background
154
+ cv2.rectangle(image,
155
+ (text_x, text_y - label_size[1] - padding),
156
+ (text_x + label_size[0] + padding, text_y),
157
+ color, -1)
158
+
159
+ # Draw text
160
+ cv2.putText(image, label,
161
+ (text_x + padding//2, text_y - padding//2),
162
+ font, font_scale, (255, 255, 255), thickness)
 
 
 
 
 
 
163
 
164
  return image
165
 
 
168
  return None, "No image provided"
169
 
170
  analysis = self.analyze_frame(frame)
171
+ display_frame = frame.copy()
172
 
173
+ # Parse observations from the formatted response
174
  observations = []
175
+ lines = analysis.split('\n')
176
+ for line in lines:
177
+ # Look for location tags in the line
178
+ if '<location>' in line and '</location>' in line:
179
+ start = line.find('<location>') + len('<location>')
180
+ end = line.find('</location>')
181
+ location = line[start:end].strip()
182
+
183
+ # Get the description that follows the location tag
184
+ desc_start = line.find('</location>') + len('</location>:')
185
+ description = line[desc_start:].strip()
186
+
187
+ if location and description:
188
+ observations.append({
189
+ 'location': location,
190
+ 'description': description
191
+ })
192
 
193
+ # Draw observations if we found any
194
+ if observations:
195
+ annotated_frame = self.draw_observations(display_frame, observations)
196
+ return annotated_frame, analysis
197
 
198
+ return display_frame, analysis
199
 
200
  # Create the main interface
201
  monitor = SafetyMonitor()