Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ from PIL import Image as PILImage
|
|
7 |
import io
|
8 |
import os
|
9 |
import base64
|
10 |
-
import random
|
11 |
|
12 |
def create_monitor_interface():
|
13 |
api_key = os.getenv("GROQ_API_KEY")
|
@@ -18,9 +17,6 @@ def create_monitor_interface():
|
|
18 |
self.model_name = "llama-3.2-90b-vision-preview"
|
19 |
self.max_image_size = (800, 800)
|
20 |
self.colors = [(0, 0, 255), (255, 0, 0), (0, 255, 0), (255, 255, 0), (255, 0, 255)]
|
21 |
-
self.last_analysis_time = 0
|
22 |
-
self.analysis_interval = 2
|
23 |
-
self.last_observations = []
|
24 |
|
25 |
def resize_image(self, image):
|
26 |
height, width = image.shape[:2]
|
@@ -38,7 +34,7 @@ def create_monitor_interface():
|
|
38 |
|
39 |
def analyze_frame(self, frame: np.ndarray) -> str:
|
40 |
if frame is None:
|
41 |
-
return "
|
42 |
|
43 |
# Convert image
|
44 |
if len(frame.shape) == 2:
|
@@ -66,10 +62,14 @@ def create_monitor_interface():
|
|
66 |
"content": [
|
67 |
{
|
68 |
"type": "text",
|
69 |
-
"text": """Analyze this image for safety
|
70 |
-
1.
|
71 |
-
2.
|
72 |
-
|
|
|
|
|
|
|
|
|
73 |
},
|
74 |
{
|
75 |
"type": "image_url",
|
@@ -84,16 +84,16 @@ def create_monitor_interface():
|
|
84 |
"content": ""
|
85 |
}
|
86 |
],
|
87 |
-
temperature=0.
|
88 |
-
max_tokens=
|
89 |
top_p=1,
|
90 |
stream=False,
|
91 |
stop=None
|
92 |
)
|
93 |
return completion.choices[0].message.content
|
94 |
except Exception as e:
|
95 |
-
print(f"
|
96 |
-
return
|
97 |
|
98 |
def get_region_coordinates(self, position: str, image_shape: tuple) -> tuple:
|
99 |
height, width = image_shape[:2]
|
@@ -129,21 +129,24 @@ def create_monitor_interface():
|
|
129 |
position = parts[0]
|
130 |
description = ':'.join(parts[1:])
|
131 |
else:
|
132 |
-
|
133 |
-
description = obs
|
134 |
|
135 |
x1, y1, x2, y2 = self.get_region_coordinates(position, image.shape)
|
136 |
|
|
|
137 |
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
|
138 |
|
|
|
139 |
label = description[:50] + "..." if len(description) > 50 else description
|
140 |
label_size = cv2.getTextSize(label, font, font_scale, thickness)[0]
|
141 |
|
142 |
label_x = max(0, min(x1, width - label_size[0]))
|
143 |
label_y = max(20, y1 - 5)
|
144 |
|
|
|
145 |
cv2.rectangle(image, (label_x, label_y - 20),
|
146 |
(label_x + label_size[0], label_y), color, -1)
|
|
|
147 |
cv2.putText(image, label, (label_x, label_y - 5),
|
148 |
font, font_scale, (255, 255, 255), thickness)
|
149 |
|
@@ -153,29 +156,26 @@ def create_monitor_interface():
|
|
153 |
if frame is None:
|
154 |
return None, "No image provided"
|
155 |
|
156 |
-
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
observation = line[start:end].strip()
|
170 |
-
if observation:
|
171 |
-
observations.append(observation)
|
172 |
-
|
173 |
-
self.last_observations = observations
|
174 |
|
175 |
display_frame = frame.copy()
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
179 |
|
180 |
# Create the main interface
|
181 |
monitor = SafetyMonitor()
|
@@ -185,9 +185,9 @@ def create_monitor_interface():
|
|
185 |
|
186 |
with gr.Row():
|
187 |
input_image = gr.Image(label="Upload Image")
|
188 |
-
output_image = gr.Image(label="Analysis")
|
189 |
|
190 |
-
analysis_text = gr.Textbox(label="Safety
|
191 |
|
192 |
def analyze_image(image):
|
193 |
if image is None:
|
@@ -207,9 +207,9 @@ def create_monitor_interface():
|
|
207 |
|
208 |
gr.Markdown("""
|
209 |
## Instructions:
|
210 |
-
1. Upload an image to analyze
|
211 |
-
2. View
|
212 |
-
3.
|
213 |
""")
|
214 |
|
215 |
return demo
|
|
|
7 |
import io
|
8 |
import os
|
9 |
import base64
|
|
|
10 |
|
11 |
def create_monitor_interface():
|
12 |
api_key = os.getenv("GROQ_API_KEY")
|
|
|
17 |
self.model_name = "llama-3.2-90b-vision-preview"
|
18 |
self.max_image_size = (800, 800)
|
19 |
self.colors = [(0, 0, 255), (255, 0, 0), (0, 255, 0), (255, 255, 0), (255, 0, 255)]
|
|
|
|
|
|
|
20 |
|
21 |
def resize_image(self, image):
|
22 |
height, width = image.shape[:2]
|
|
|
34 |
|
35 |
def analyze_frame(self, frame: np.ndarray) -> str:
|
36 |
if frame is None:
|
37 |
+
return ""
|
38 |
|
39 |
# Convert image
|
40 |
if len(frame.shape) == 2:
|
|
|
62 |
"content": [
|
63 |
{
|
64 |
"type": "text",
|
65 |
+
"text": """Analyze this image for safety concerns. For each specific issue you identify, provide:
|
66 |
+
1. Exact location in the image (e.g., 'top-left', 'center', 'bottom-right', etc.)
|
67 |
+
2. Description of the safety concern
|
68 |
+
|
69 |
+
Format your response with each issue on a new line as:
|
70 |
+
- <location>position:detailed description of the safety concern</location>
|
71 |
+
|
72 |
+
Be specific about what you observe in the image."""
|
73 |
},
|
74 |
{
|
75 |
"type": "image_url",
|
|
|
84 |
"content": ""
|
85 |
}
|
86 |
],
|
87 |
+
temperature=0.2,
|
88 |
+
max_tokens=500,
|
89 |
top_p=1,
|
90 |
stream=False,
|
91 |
stop=None
|
92 |
)
|
93 |
return completion.choices[0].message.content
|
94 |
except Exception as e:
|
95 |
+
print(f"Analysis error: {str(e)}")
|
96 |
+
return ""
|
97 |
|
98 |
def get_region_coordinates(self, position: str, image_shape: tuple) -> tuple:
|
99 |
height, width = image_shape[:2]
|
|
|
129 |
position = parts[0]
|
130 |
description = ':'.join(parts[1:])
|
131 |
else:
|
132 |
+
continue
|
|
|
133 |
|
134 |
x1, y1, x2, y2 = self.get_region_coordinates(position, image.shape)
|
135 |
|
136 |
+
# Draw rectangle
|
137 |
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
|
138 |
|
139 |
+
# Add label with background
|
140 |
label = description[:50] + "..." if len(description) > 50 else description
|
141 |
label_size = cv2.getTextSize(label, font, font_scale, thickness)[0]
|
142 |
|
143 |
label_x = max(0, min(x1, width - label_size[0]))
|
144 |
label_y = max(20, y1 - 5)
|
145 |
|
146 |
+
# Draw background for text
|
147 |
cv2.rectangle(image, (label_x, label_y - 20),
|
148 |
(label_x + label_size[0], label_y), color, -1)
|
149 |
+
# Draw text
|
150 |
cv2.putText(image, label, (label_x, label_y - 5),
|
151 |
font, font_scale, (255, 255, 255), thickness)
|
152 |
|
|
|
156 |
if frame is None:
|
157 |
return None, "No image provided"
|
158 |
|
159 |
+
analysis = self.analyze_frame(frame)
|
160 |
|
161 |
+
# Parse observations
|
162 |
+
observations = []
|
163 |
+
for line in analysis.split('\n'):
|
164 |
+
line = line.strip()
|
165 |
+
if line.startswith('-'):
|
166 |
+
if '<location>' in line and '</location>' in line:
|
167 |
+
start = line.find('<location>') + len('<location>')
|
168 |
+
end = line.find('</location>')
|
169 |
+
observation = line[start:end].strip()
|
170 |
+
if observation and ':' in observation:
|
171 |
+
observations.append(observation)
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
display_frame = frame.copy()
|
174 |
+
if observations:
|
175 |
+
annotated_frame = self.draw_observations(display_frame, observations)
|
176 |
+
return annotated_frame, analysis
|
177 |
+
else:
|
178 |
+
return display_frame, "No safety concerns detected in the image."
|
179 |
|
180 |
# Create the main interface
|
181 |
monitor = SafetyMonitor()
|
|
|
185 |
|
186 |
with gr.Row():
|
187 |
input_image = gr.Image(label="Upload Image")
|
188 |
+
output_image = gr.Image(label="Analysis Results")
|
189 |
|
190 |
+
analysis_text = gr.Textbox(label="Safety Analysis", lines=5)
|
191 |
|
192 |
def analyze_image(image):
|
193 |
if image is None:
|
|
|
207 |
|
208 |
gr.Markdown("""
|
209 |
## Instructions:
|
210 |
+
1. Upload an image to analyze
|
211 |
+
2. View identified safety concerns with bounding boxes
|
212 |
+
3. Read detailed analysis results
|
213 |
""")
|
214 |
|
215 |
return demo
|