Update app.py
Browse files
app.py
CHANGED
@@ -117,22 +117,55 @@ class SafetyMonitor:
|
|
117 |
return image
|
118 |
|
119 |
def process_frame(self, frame):
|
120 |
-
"""Main processing pipeline for safety analysis."""
|
121 |
if frame is None:
|
122 |
return None, "No image provided"
|
123 |
-
|
124 |
try:
|
125 |
-
# Detect objects in the image using YOLO
|
126 |
bbox_data, labels = self.detect_objects(frame)
|
127 |
frame_with_boxes = self.draw_bounding_boxes(frame, bbox_data, labels)
|
128 |
-
|
129 |
-
# Get analysis from Groq's model
|
130 |
analysis, _ = self.analyze_frame(frame)
|
131 |
-
return frame_with_boxes, analysis
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
except Exception as e:
|
134 |
print(f"Processing error: {str(e)}")
|
135 |
return None, f"Error processing image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
def create_monitor_interface():
|
138 |
monitor = SafetyMonitor()
|
|
|
117 |
return image
|
118 |
|
119 |
def process_frame(self, frame):
|
120 |
+
"""Main processing pipeline for dynamic safety analysis."""
|
121 |
if frame is None:
|
122 |
return None, "No image provided"
|
123 |
+
|
124 |
try:
|
125 |
+
# Detect objects dynamically in the image using YOLO
|
126 |
bbox_data, labels = self.detect_objects(frame)
|
127 |
frame_with_boxes = self.draw_bounding_boxes(frame, bbox_data, labels)
|
128 |
+
|
129 |
+
# Get dynamic safety analysis from Groq's model
|
130 |
analysis, _ = self.analyze_frame(frame)
|
|
|
131 |
|
132 |
+
# Dynamically parse the analysis to find any safety issues flagged
|
133 |
+
safety_issues = self.parse_safety_analysis(analysis)
|
134 |
+
|
135 |
+
# Dynamically link detected objects to safety issues
|
136 |
+
for issue in safety_issues:
|
137 |
+
if 'helmet' in issue.lower():
|
138 |
+
for idx, bbox in enumerate(bbox_data):
|
139 |
+
x1, y1, x2, y2, conf, class_id = bbox
|
140 |
+
if labels[int(class_id)] == 'person':
|
141 |
+
# Dynamically label the missing helmet issue for detected persons
|
142 |
+
cv2.putText(frame_with_boxes, "No Helmet!", (int(x1), int(y1) - 20),
|
143 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
|
144 |
+
cv2.rectangle(frame_with_boxes, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
|
145 |
+
|
146 |
+
# Add more dynamic checks here for gloves, boots, etc.
|
147 |
+
if 'glove' in issue.lower():
|
148 |
+
for idx, bbox in enumerate(bbox_data):
|
149 |
+
x1, y1, x2, y2, conf, class_id = bbox
|
150 |
+
if labels[int(class_id)] == 'person':
|
151 |
+
# Dynamically label missing gloves for detected persons
|
152 |
+
cv2.putText(frame_with_boxes, "No Gloves!", (int(x1), int(y1) - 20),
|
153 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
|
154 |
+
cv2.rectangle(frame_with_boxes, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 255), 2)
|
155 |
+
|
156 |
+
return frame_with_boxes, analysis
|
157 |
+
|
158 |
except Exception as e:
|
159 |
print(f"Processing error: {str(e)}")
|
160 |
return None, f"Error processing image: {str(e)}"
|
161 |
+
|
162 |
+
def parse_safety_analysis(self, analysis):
|
163 |
+
"""Dynamically parse the safety analysis to identify issues."""
|
164 |
+
safety_issues = []
|
165 |
+
for line in analysis.split('\n'):
|
166 |
+
if "missing" in line.lower() or "no" in line.lower():
|
167 |
+
safety_issues.append(line.strip())
|
168 |
+
return safety_issues
|
169 |
|
170 |
def create_monitor_interface():
|
171 |
monitor = SafetyMonitor()
|