Update app.py
Browse files
app.py
CHANGED
|
@@ -18,20 +18,6 @@ def create_monitor_interface():
|
|
| 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]
|
| 23 |
-
|
| 24 |
-
if height > self.max_image_size[1] or width > self.max_image_size[0]:
|
| 25 |
-
aspect = width / height
|
| 26 |
-
if width > height:
|
| 27 |
-
new_width = self.max_image_size[0]
|
| 28 |
-
new_height = int(new_width / aspect)
|
| 29 |
-
else:
|
| 30 |
-
new_height = self.max_image_size[1]
|
| 31 |
-
new_width = int(new_height * aspect)
|
| 32 |
-
return cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
| 33 |
-
return image
|
| 34 |
-
|
| 35 |
def analyze_frame(self, frame: np.ndarray) -> str:
|
| 36 |
if frame is None:
|
| 37 |
return ""
|
|
@@ -57,19 +43,29 @@ def create_monitor_interface():
|
|
| 57 |
completion = self.client.chat.completions.create(
|
| 58 |
model=self.model_name,
|
| 59 |
messages=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
{
|
| 61 |
"role": "user",
|
| 62 |
"content": [
|
| 63 |
{
|
| 64 |
"type": "text",
|
| 65 |
-
"text": """Analyze this image for safety concerns. For each
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
},
|
| 74 |
{
|
| 75 |
"type": "image_url",
|
|
@@ -78,23 +74,34 @@ def create_monitor_interface():
|
|
| 78 |
}
|
| 79 |
}
|
| 80 |
]
|
| 81 |
-
},
|
| 82 |
-
{
|
| 83 |
-
"role": "assistant",
|
| 84 |
-
"content": ""
|
| 85 |
}
|
| 86 |
],
|
| 87 |
-
temperature=0.
|
| 88 |
max_tokens=500,
|
| 89 |
-
|
| 90 |
-
stream=False,
|
| 91 |
-
stop=None
|
| 92 |
)
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]
|
| 100 |
regions = {
|
|
@@ -109,10 +116,19 @@ def create_monitor_interface():
|
|
| 109 |
'bottom-right': (2*width//3, 2*height//3, width, height)
|
| 110 |
}
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
|
|
|
|
|
|
| 116 |
return regions['center']
|
| 117 |
|
| 118 |
def draw_observations(self, image, observations):
|
|
@@ -128,27 +144,23 @@ def create_monitor_interface():
|
|
| 128 |
if len(parts) >= 2:
|
| 129 |
position = parts[0]
|
| 130 |
description = ':'.join(parts[1:])
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 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 |
|
| 153 |
return image
|
| 154 |
|
|
@@ -157,8 +169,8 @@ def create_monitor_interface():
|
|
| 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()
|
|
@@ -170,14 +182,19 @@ def create_monitor_interface():
|
|
| 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 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
# Create the main interface
|
| 181 |
monitor = SafetyMonitor()
|
| 182 |
|
| 183 |
with gr.Blocks() as demo:
|
|
@@ -205,13 +222,6 @@ def create_monitor_interface():
|
|
| 205 |
outputs=[output_image, analysis_text]
|
| 206 |
)
|
| 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
|
| 216 |
|
| 217 |
demo = create_monitor_interface()
|
|
|
|
| 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 analyze_frame(self, frame: np.ndarray) -> str:
|
| 22 |
if frame is None:
|
| 23 |
return ""
|
|
|
|
| 43 |
completion = self.client.chat.completions.create(
|
| 44 |
model=self.model_name,
|
| 45 |
messages=[
|
| 46 |
+
{
|
| 47 |
+
"role": "system",
|
| 48 |
+
"content": "You are a safety analysis expert. Analyze images for safety concerns and provide detailed observations."
|
| 49 |
+
},
|
| 50 |
{
|
| 51 |
"role": "user",
|
| 52 |
"content": [
|
| 53 |
{
|
| 54 |
"type": "text",
|
| 55 |
+
"text": """Analyze this image for safety concerns and risks. For each issue you identify:
|
| 56 |
+
|
| 57 |
+
1. Specify the exact location in the image where the issue is visible
|
| 58 |
+
2. Describe what the safety concern is
|
| 59 |
+
3. Include any relevant details about PPE, posture, equipment, or environmental hazards
|
| 60 |
+
|
| 61 |
+
Format EACH observation exactly like this:
|
| 62 |
+
- <location>position:detailed description of the concern</location>
|
| 63 |
+
|
| 64 |
+
Example format:
|
| 65 |
+
- <location>center:Worker bending incorrectly while lifting heavy materials</location>
|
| 66 |
+
- <location>top-right:Missing safety guardrail near elevated platform</location>
|
| 67 |
+
|
| 68 |
+
Provide multiple observations if you see multiple issues."""
|
| 69 |
},
|
| 70 |
{
|
| 71 |
"type": "image_url",
|
|
|
|
| 74 |
}
|
| 75 |
}
|
| 76 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
| 78 |
],
|
| 79 |
+
temperature=0.5, # Increased for more varied observations
|
| 80 |
max_tokens=500,
|
| 81 |
+
stream=False
|
|
|
|
|
|
|
| 82 |
)
|
| 83 |
+
|
| 84 |
+
response = completion.choices[0].message.content
|
| 85 |
+
print(f"Raw response: {response}") # For debugging
|
| 86 |
+
return response
|
| 87 |
+
|
| 88 |
except Exception as e:
|
| 89 |
print(f"Analysis error: {str(e)}")
|
| 90 |
return ""
|
| 91 |
|
| 92 |
+
def resize_image(self, image):
|
| 93 |
+
height, width = image.shape[:2]
|
| 94 |
+
if height > self.max_image_size[1] or width > self.max_image_size[0]:
|
| 95 |
+
aspect = width / height
|
| 96 |
+
if width > height:
|
| 97 |
+
new_width = self.max_image_size[0]
|
| 98 |
+
new_height = int(new_width / aspect)
|
| 99 |
+
else:
|
| 100 |
+
new_height = self.max_image_size[1]
|
| 101 |
+
new_width = int(new_height * aspect)
|
| 102 |
+
return cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
|
| 103 |
+
return image
|
| 104 |
+
|
| 105 |
def get_region_coordinates(self, position: str, image_shape: tuple) -> tuple:
|
| 106 |
height, width = image_shape[:2]
|
| 107 |
regions = {
|
|
|
|
| 116 |
'bottom-right': (2*width//3, 2*height//3, width, height)
|
| 117 |
}
|
| 118 |
|
| 119 |
+
# Try to match the position with regions
|
| 120 |
+
matched_region = None
|
| 121 |
+
max_match_length = 0
|
| 122 |
+
position_lower = position.lower()
|
| 123 |
+
|
| 124 |
+
for region_name in regions:
|
| 125 |
+
if region_name in position_lower:
|
| 126 |
+
if len(region_name) > max_match_length:
|
| 127 |
+
matched_region = region_name
|
| 128 |
+
max_match_length = len(region_name)
|
| 129 |
|
| 130 |
+
if matched_region:
|
| 131 |
+
return regions[matched_region]
|
| 132 |
return regions['center']
|
| 133 |
|
| 134 |
def draw_observations(self, image, observations):
|
|
|
|
| 144 |
if len(parts) >= 2:
|
| 145 |
position = parts[0]
|
| 146 |
description = ':'.join(parts[1:])
|
| 147 |
+
|
| 148 |
+
x1, y1, x2, y2 = self.get_region_coordinates(position, image.shape)
|
| 149 |
+
|
| 150 |
+
# Draw rectangle
|
| 151 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
|
| 152 |
+
|
| 153 |
+
# Add label with background
|
| 154 |
+
label = description[:50] + "..." if len(description) > 50 else description
|
| 155 |
+
label_size = cv2.getTextSize(label, font, font_scale, thickness)[0]
|
| 156 |
+
|
| 157 |
+
label_x = max(0, min(x1, width - label_size[0]))
|
| 158 |
+
label_y = max(20, y1 - 5)
|
| 159 |
+
|
| 160 |
+
cv2.rectangle(image, (label_x, label_y - 20),
|
| 161 |
+
(label_x + label_size[0], label_y), color, -1)
|
| 162 |
+
cv2.putText(image, label, (label_x, label_y - 5),
|
| 163 |
+
font, font_scale, (255, 255, 255), thickness)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
return image
|
| 166 |
|
|
|
|
| 169 |
return None, "No image provided"
|
| 170 |
|
| 171 |
analysis = self.analyze_frame(frame)
|
| 172 |
+
print(f"Analysis received: {analysis}") # Debug print
|
| 173 |
|
|
|
|
| 174 |
observations = []
|
| 175 |
for line in analysis.split('\n'):
|
| 176 |
line = line.strip()
|
|
|
|
| 182 |
if observation and ':' in observation:
|
| 183 |
observations.append(observation)
|
| 184 |
|
| 185 |
+
print(f"Parsed observations: {observations}") # Debug print
|
| 186 |
+
|
| 187 |
display_frame = frame.copy()
|
| 188 |
if observations:
|
| 189 |
annotated_frame = self.draw_observations(display_frame, observations)
|
| 190 |
return annotated_frame, analysis
|
| 191 |
+
|
| 192 |
+
# If no observations were found but we got some analysis
|
| 193 |
+
if analysis and not analysis.isspace():
|
| 194 |
+
return display_frame, analysis
|
| 195 |
+
|
| 196 |
+
return display_frame, "Please try again - no safety analysis was generated."
|
| 197 |
|
|
|
|
| 198 |
monitor = SafetyMonitor()
|
| 199 |
|
| 200 |
with gr.Blocks() as demo:
|
|
|
|
| 222 |
outputs=[output_image, analysis_text]
|
| 223 |
)
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
return demo
|
| 226 |
|
| 227 |
demo = create_monitor_interface()
|