Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from groq import Groq
|
5 |
-
import time
|
6 |
from PIL import Image as PILImage
|
7 |
import io
|
8 |
import base64
|
@@ -49,7 +48,7 @@ class RobustSafetyMonitor:
|
|
49 |
buffered = io.BytesIO()
|
50 |
frame_pil.save(buffered, format="JPEG", quality=95) # Ensure JPEG format
|
51 |
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
52 |
-
return img_base64 # Return only the base64 string
|
53 |
|
54 |
def detect_objects(self, frame):
|
55 |
"""Detect objects using YOLOv5."""
|
@@ -94,7 +93,10 @@ class RobustSafetyMonitor:
|
|
94 |
max_tokens=1024,
|
95 |
stream=False
|
96 |
)
|
97 |
-
|
|
|
|
|
|
|
98 |
except Exception as e:
|
99 |
print(f"Analysis error: {str(e)}")
|
100 |
return f"Analysis Error: {str(e)}", {}
|
@@ -114,9 +116,11 @@ class RobustSafetyMonitor:
|
|
114 |
cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, thickness)
|
115 |
|
116 |
# Link detected object to potential risks based on Llama Vision analysis
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
120 |
else:
|
121 |
label_text = f"{label} {conf:.2f}"
|
122 |
cv2.putText(image, label_text, (int(x1), int(y1) - 10), font, font_scale, color, thickness)
|
@@ -134,11 +138,8 @@ class RobustSafetyMonitor:
|
|
134 |
frame_with_boxes = self.draw_bounding_boxes(frame, bbox_data, labels, [])
|
135 |
|
136 |
# Get dynamic safety analysis from Llama Vision 3.2
|
137 |
-
|
138 |
|
139 |
-
# Dynamically parse the analysis to identify safety issues flagged
|
140 |
-
safety_issues = self.parse_safety_analysis(analysis)
|
141 |
-
|
142 |
# Update the frame with bounding boxes based on safety issues flagged
|
143 |
annotated_frame = self.draw_bounding_boxes(frame_with_boxes, bbox_data, labels, safety_issues)
|
144 |
|
@@ -149,11 +150,17 @@ class RobustSafetyMonitor:
|
|
149 |
return None, f"Error processing image: {str(e)}"
|
150 |
|
151 |
def parse_safety_analysis(self, analysis):
|
152 |
-
"""
|
153 |
safety_issues = []
|
154 |
for line in analysis.split('\n'):
|
155 |
if "risk" in line.lower() or "hazard" in line.lower():
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
return safety_issues
|
158 |
|
159 |
|
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from groq import Groq
|
|
|
5 |
from PIL import Image as PILImage
|
6 |
import io
|
7 |
import base64
|
|
|
48 |
buffered = io.BytesIO()
|
49 |
frame_pil.save(buffered, format="JPEG", quality=95) # Ensure JPEG format
|
50 |
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
51 |
+
return img_base64 # Return only the base64 string
|
52 |
|
53 |
def detect_objects(self, frame):
|
54 |
"""Detect objects using YOLOv5."""
|
|
|
93 |
max_tokens=1024,
|
94 |
stream=False
|
95 |
)
|
96 |
+
# Process and parse the response correctly
|
97 |
+
response = completion.choices[0].message.content
|
98 |
+
return self.parse_safety_analysis(response), response # Return parsed analysis and full response
|
99 |
+
|
100 |
except Exception as e:
|
101 |
print(f"Analysis error: {str(e)}")
|
102 |
return f"Analysis Error: {str(e)}", {}
|
|
|
116 |
cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, thickness)
|
117 |
|
118 |
# Link detected object to potential risks based on Llama Vision analysis
|
119 |
+
for safety_issue in safety_issues:
|
120 |
+
if safety_issue['object'].lower() in label.lower():
|
121 |
+
label_text = f"Risk: {safety_issue['description']}"
|
122 |
+
cv2.putText(image, label_text, (int(x1), int(y1) - 10), font, font_scale, (0, 0, 255), thickness)
|
123 |
+
break
|
124 |
else:
|
125 |
label_text = f"{label} {conf:.2f}"
|
126 |
cv2.putText(image, label_text, (int(x1), int(y1) - 10), font, font_scale, color, thickness)
|
|
|
138 |
frame_with_boxes = self.draw_bounding_boxes(frame, bbox_data, labels, [])
|
139 |
|
140 |
# Get dynamic safety analysis from Llama Vision 3.2
|
141 |
+
safety_issues, analysis = self.analyze_frame(frame)
|
142 |
|
|
|
|
|
|
|
143 |
# Update the frame with bounding boxes based on safety issues flagged
|
144 |
annotated_frame = self.draw_bounding_boxes(frame_with_boxes, bbox_data, labels, safety_issues)
|
145 |
|
|
|
150 |
return None, f"Error processing image: {str(e)}"
|
151 |
|
152 |
def parse_safety_analysis(self, analysis):
|
153 |
+
"""Parse the safety analysis to identify contextual issues and link to objects."""
|
154 |
safety_issues = []
|
155 |
for line in analysis.split('\n'):
|
156 |
if "risk" in line.lower() or "hazard" in line.lower():
|
157 |
+
# Extract object involved and description
|
158 |
+
parts = line.split(':', 1)
|
159 |
+
if len(parts) == 2:
|
160 |
+
safety_issues.append({
|
161 |
+
"object": parts[0].strip(),
|
162 |
+
"description": parts[1].strip()
|
163 |
+
})
|
164 |
return safety_issues
|
165 |
|
166 |
|