capradeepgujaran
commited on
Commit
•
bd1163f
1
Parent(s):
07117f0
Update app.py
Browse files
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 |
-
'
|
119 |
-
'
|
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 |
-
'
|
125 |
-
'right': (2*width//3, height//4, width, 3*height//4)
|
126 |
}
|
127 |
|
128 |
# Find best matching region
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
132 |
|
133 |
-
|
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 |
-
#
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
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 =
|
184 |
|
185 |
-
# Parse observations from the
|
186 |
observations = []
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
-
# Draw observations
|
201 |
-
|
|
|
|
|
202 |
|
203 |
-
return
|
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()
|