PrashanthB461 commited on
Commit
2b1cb8b
·
verified ·
1 Parent(s): ebfa2db

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +22 -5
utils.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import cv2
3
  import numpy as np
 
4
  from reportlab.lib.pagesizes import letter
5
  from reportlab.pdfgen import canvas
6
  from reportlab.lib.units import inch
@@ -52,14 +53,14 @@ def calculate_safety_score(violations):
52
 
53
  async def generate_violation_pdf(violations, score, output_dir):
54
  try:
55
- pdf_filename = f"violations_{int(asyncio.get_event_loop().time())}.pdf"
56
  pdf_path = os.path.join(output_dir, pdf_filename)
57
  pdf_file = BytesIO()
58
  c = canvas.Canvas(pdf_file, pagesize=letter)
59
  c.setFont("Helvetica-Bold", 16)
60
  c.drawString(1 * inch, 10 * inch, "Worksite Safety Violation Report")
61
  c.setFont("Helvetica", 12)
62
- c.drawString(1 * inch, 9.5 * inch, f"Date: {asyncio.get_event_loop().time():.0f}")
63
  c.setFont("Helvetica-Bold", 14)
64
  c.drawString(1 * inch, 8.7 * inch, f"Safety Compliance Score: {score}%")
65
  c.setFont("Helvetica", 10)
@@ -97,8 +98,8 @@ async def upload_pdf_to_salesforce(sf, pdf_file, record_id):
97
  try:
98
  encoded_pdf = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
99
  content_version = sf.ContentVersion.create({
100
- "Title": f"Safety_Violation_Report_{int(asyncio.get_event_loop().time())}",
101
- "PathOnClient": f"safety_violation_{int(asyncio.get_event_loop().time())}.pdf",
102
  "VersionData": encoded_pdf,
103
  "FirstPublishLocationId": record_id
104
  })
@@ -154,4 +155,20 @@ def blur_faces(frame):
154
  roi = frame[y:y+h, x:x+w]
155
  roi = cv2.GaussianBlur(roi, (23, 23), 30)
156
  frame[y:y+h, x:x+w] = roi
157
- return frame
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import cv2
3
  import numpy as np
4
+ import time
5
  from reportlab.lib.pagesizes import letter
6
  from reportlab.pdfgen import canvas
7
  from reportlab.lib.units import inch
 
53
 
54
  async def generate_violation_pdf(violations, score, output_dir):
55
  try:
56
+ pdf_filename = f"violations_{int(time.time())}.pdf"
57
  pdf_path = os.path.join(output_dir, pdf_filename)
58
  pdf_file = BytesIO()
59
  c = canvas.Canvas(pdf_file, pagesize=letter)
60
  c.setFont("Helvetica-Bold", 16)
61
  c.drawString(1 * inch, 10 * inch, "Worksite Safety Violation Report")
62
  c.setFont("Helvetica", 12)
63
+ c.drawString(1 * inch, 9.5 * inch, f"Date: {time.strftime('%Y-%m-%d')}")
64
  c.setFont("Helvetica-Bold", 14)
65
  c.drawString(1 * inch, 8.7 * inch, f"Safety Compliance Score: {score}%")
66
  c.setFont("Helvetica", 10)
 
98
  try:
99
  encoded_pdf = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
100
  content_version = sf.ContentVersion.create({
101
+ "Title": f"Safety_Violation_Report_{int(time.time())}",
102
+ "PathOnClient": f"safety_violation_{int(time.time())}.pdf",
103
  "VersionData": encoded_pdf,
104
  "FirstPublishLocationId": record_id
105
  })
 
155
  roi = frame[y:y+h, x:x+w]
156
  roi = cv2.GaussianBlur(roi, (23, 23), 30)
157
  frame[y:y+h, x:x+w] = roi
158
+ return frame
159
+
160
+ def clean_output_directory(max_age_seconds=86400):
161
+ output_dir = os.path.join("static", "output")
162
+ if not os.path.exists(output_dir):
163
+ return
164
+ current_time = time.time()
165
+ for filename in os.listdir(output_dir):
166
+ file_path = os.path.join(output_dir, filename)
167
+ if os.path.isfile(file_path):
168
+ file_age = current_time - os.path.getmtime(file_path)
169
+ if file_age > max_age_seconds:
170
+ try:
171
+ os.remove(file_path)
172
+ logger.info(f"Removed old output file: {file_path}")
173
+ except Exception as e:
174
+ logger.error(f"Failed to remove {file_path}: {e}")