Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from reportlab.lib.utils import ImageReader
|
|
| 11 |
import base64
|
| 12 |
from PIL import Image
|
| 13 |
from simple_salesforce import Salesforce, SalesforceMalformedRequest
|
|
|
|
| 14 |
|
| 15 |
# ==========================
|
| 16 |
# Configuration
|
|
@@ -85,8 +86,15 @@ def push_report_to_salesforce(score, violations, pdf_url):
|
|
| 85 |
violations_count = len(violations)
|
| 86 |
violations_details = violations_to_text(violations)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Create the record in the custom object Safety_Video_Report__c
|
| 89 |
-
print(f"Creating Salesforce record with compliance score: {score} and violations found: {violations_count}")
|
| 90 |
record = sf.Safety_Video_Report__c.create({
|
| 91 |
"Compliance_Score__c": score,
|
| 92 |
"Violations_Found__c": violations_count,
|
|
@@ -155,21 +163,38 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 155 |
img = ImageReader(local_img_path)
|
| 156 |
c.drawImage(img, 50, y - 100, width=200, height=150)
|
| 157 |
y -= 170
|
|
|
|
|
|
|
| 158 |
|
| 159 |
if y < 50:
|
| 160 |
c.showPage()
|
| 161 |
y = height - 50
|
| 162 |
|
| 163 |
c.save()
|
| 164 |
-
print(f"PDF generated at {pdf_path}")
|
| 165 |
|
| 166 |
-
# Verify
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
# Generate public URL for the PDF
|
| 171 |
pdf_url = f"{BASE_PUBLIC_URL}{pdf_filename}"
|
| 172 |
-
print(f"Public PDF URL: {pdf_url}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
return pdf_path, pdf_url
|
| 175 |
|
|
@@ -182,11 +207,11 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 182 |
# ==========================
|
| 183 |
def process_video(video_data, frame_skip=5, max_frames=100):
|
| 184 |
try:
|
| 185 |
-
print("Processing video data...")
|
| 186 |
video_path = os.path.join(STATIC_OUTPUT_DIR, f"temp_{int(time.time())}.mp4")
|
| 187 |
with open(video_path, "wb") as f:
|
| 188 |
f.write(video_data)
|
| 189 |
-
print(f"Video saved to {video_path}")
|
| 190 |
|
| 191 |
video = cv2.VideoCapture(video_path)
|
| 192 |
if not video.isOpened():
|
|
@@ -228,11 +253,15 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 228 |
snapshot_filename = f"snapshot_{frame_count}_{label}.jpg"
|
| 229 |
snapshot_path = os.path.join(STATIC_OUTPUT_DIR, snapshot_filename)
|
| 230 |
cv2.imwrite(snapshot_path, frame)
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
frame_count += 1
|
| 238 |
processed_frame_count += 1
|
|
@@ -246,6 +275,7 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 246 |
|
| 247 |
video.release()
|
| 248 |
os.remove(video_path)
|
|
|
|
| 249 |
|
| 250 |
score = calculate_safety_score(violations)
|
| 251 |
pdf_path, pdf_url = generate_pdf_report(violations, snapshots, score)
|
|
|
|
| 11 |
import base64
|
| 12 |
from PIL import Image
|
| 13 |
from simple_salesforce import Salesforce, SalesforceMalformedRequest
|
| 14 |
+
import requests
|
| 15 |
|
| 16 |
# ==========================
|
| 17 |
# Configuration
|
|
|
|
| 86 |
violations_count = len(violations)
|
| 87 |
violations_details = violations_to_text(violations)
|
| 88 |
|
| 89 |
+
# Verify PDF URL accessibility
|
| 90 |
+
print(f"🔍 Verifying PDF URL: {pdf_url}")
|
| 91 |
+
response = requests.head(pdf_url, timeout=5)
|
| 92 |
+
if response.status_code != 200:
|
| 93 |
+
print(f"⚠️ PDF URL not accessible: {response.status_code}")
|
| 94 |
+
pdf_url = "" # Fallback to empty URL to avoid saving invalid link
|
| 95 |
+
|
| 96 |
# Create the record in the custom object Safety_Video_Report__c
|
| 97 |
+
print(f"📝 Creating Salesforce record with compliance score: {score} and violations found: {violations_count}")
|
| 98 |
record = sf.Safety_Video_Report__c.create({
|
| 99 |
"Compliance_Score__c": score,
|
| 100 |
"Violations_Found__c": violations_count,
|
|
|
|
| 163 |
img = ImageReader(local_img_path)
|
| 164 |
c.drawImage(img, 50, y - 100, width=200, height=150)
|
| 165 |
y -= 170
|
| 166 |
+
else:
|
| 167 |
+
print(f"⚠️ Snapshot not found at {local_img_path}")
|
| 168 |
|
| 169 |
if y < 50:
|
| 170 |
c.showPage()
|
| 171 |
y = height - 50
|
| 172 |
|
| 173 |
c.save()
|
| 174 |
+
print(f"✅ PDF generated at {pdf_path}")
|
| 175 |
|
| 176 |
+
# Verify file existence with retry
|
| 177 |
+
for _ in range(5):
|
| 178 |
+
if os.path.exists(pdf_path):
|
| 179 |
+
break
|
| 180 |
+
print(f"⏳ Waiting for PDF at {pdf_path}")
|
| 181 |
+
time.sleep(1)
|
| 182 |
+
else:
|
| 183 |
+
raise Exception(f"PDF file not found at {pdf_path} after retries")
|
| 184 |
|
| 185 |
# Generate public URL for the PDF
|
| 186 |
pdf_url = f"{BASE_PUBLIC_URL}{pdf_filename}"
|
| 187 |
+
print(f"📎 Public PDF URL: {pdf_url}")
|
| 188 |
+
|
| 189 |
+
# Verify URL accessibility
|
| 190 |
+
try:
|
| 191 |
+
response = requests.head(pdf_url, timeout=5)
|
| 192 |
+
if response.status_code == 200:
|
| 193 |
+
print(f"✅ PDF URL is accessible")
|
| 194 |
+
else:
|
| 195 |
+
print(f"⚠️ PDF URL not accessible: HTTP {response.status_code}")
|
| 196 |
+
except requests.RequestException as e:
|
| 197 |
+
print(f"⚠️ Failed to verify PDF URL: {e}")
|
| 198 |
|
| 199 |
return pdf_path, pdf_url
|
| 200 |
|
|
|
|
| 207 |
# ==========================
|
| 208 |
def process_video(video_data, frame_skip=5, max_frames=100):
|
| 209 |
try:
|
| 210 |
+
print("📹 Processing video data...")
|
| 211 |
video_path = os.path.join(STATIC_OUTPUT_DIR, f"temp_{int(time.time())}.mp4")
|
| 212 |
with open(video_path, "wb") as f:
|
| 213 |
f.write(video_data)
|
| 214 |
+
print(f"💾 Video saved to {video_path}")
|
| 215 |
|
| 216 |
video = cv2.VideoCapture(video_path)
|
| 217 |
if not video.isOpened():
|
|
|
|
| 253 |
snapshot_filename = f"snapshot_{frame_count}_{label}.jpg"
|
| 254 |
snapshot_path = os.path.join(STATIC_OUTPUT_DIR, snapshot_filename)
|
| 255 |
cv2.imwrite(snapshot_path, frame)
|
| 256 |
+
if os.path.exists(snapshot_path):
|
| 257 |
+
snapshots.append({
|
| 258 |
+
"violation": label,
|
| 259 |
+
"frame": frame_count,
|
| 260 |
+
"snapshot_url": f"{BASE_PUBLIC_URL}{snapshot_filename}"
|
| 261 |
+
})
|
| 262 |
+
print(f"📸 Snapshot saved at {snapshot_path}")
|
| 263 |
+
else:
|
| 264 |
+
print(f"⚠️ Snapshot not saved at {snapshot_path}")
|
| 265 |
|
| 266 |
frame_count += 1
|
| 267 |
processed_frame_count += 1
|
|
|
|
| 275 |
|
| 276 |
video.release()
|
| 277 |
os.remove(video_path)
|
| 278 |
+
print(f"🗑️ Temporary video file removed: {video_path}")
|
| 279 |
|
| 280 |
score = calculate_safety_score(violations)
|
| 281 |
pdf_path, pdf_url = generate_pdf_report(violations, snapshots, score)
|