Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,8 +18,10 @@ from simple_salesforce import Salesforce
|
|
| 18 |
DEFAULT_MODEL_PATH = "models/yolov8_safety.pt"
|
| 19 |
FALLBACK_MODEL = "yolov8n.pt"
|
| 20 |
MODEL_PATH = os.getenv("SAFETY_MODEL_PATH", DEFAULT_MODEL_PATH)
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
VIOLATION_LABELS = {
|
| 25 |
0: "no_helmet",
|
|
@@ -46,14 +48,14 @@ except Exception as e:
|
|
| 46 |
raise
|
| 47 |
|
| 48 |
# ==========================
|
| 49 |
-
# Salesforce connection
|
| 50 |
# ==========================
|
| 51 |
def connect_to_salesforce():
|
| 52 |
sf = Salesforce(
|
| 53 |
username="prashanth1ai@safety.com",
|
| 54 |
password="SaiPrash461",
|
| 55 |
security_token="AP4AQnPoidIKPvSvNEfAHyoK",
|
| 56 |
-
domain="login"
|
| 57 |
)
|
| 58 |
return sf
|
| 59 |
|
|
@@ -70,7 +72,8 @@ def push_report_to_salesforce(score, violations, pdf_filename):
|
|
| 70 |
violations_count = len(violations)
|
| 71 |
violations_details = violations_to_text(violations)
|
| 72 |
|
| 73 |
-
|
|
|
|
| 74 |
pdf_url = f"{PUBLIC_PDF_BASE_URL}{os.path.basename(pdf_filename)}"
|
| 75 |
|
| 76 |
try:
|
|
@@ -107,7 +110,7 @@ def calculate_safety_score(violations):
|
|
| 107 |
# ==========================
|
| 108 |
def generate_pdf_report(violations, snapshots, score):
|
| 109 |
try:
|
| 110 |
-
pdf_path = os.path.join(
|
| 111 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 112 |
width, height = letter
|
| 113 |
|
|
@@ -130,7 +133,7 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 130 |
|
| 131 |
snapshot = next((s for s in snapshots if s["frame"] == v["frame"] and s["violation"] == v["violation"]), None)
|
| 132 |
if snapshot:
|
| 133 |
-
local_img_path = os.path.join(
|
| 134 |
if os.path.exists(local_img_path):
|
| 135 |
img = ImageReader(local_img_path)
|
| 136 |
c.drawImage(img, 50, y - 100, width=200, height=150)
|
|
@@ -155,7 +158,7 @@ def generate_pdf_report(violations, snapshots, score):
|
|
| 155 |
def process_video(video_data, frame_skip=5, max_frames=100):
|
| 156 |
try:
|
| 157 |
print("Processing video data...")
|
| 158 |
-
video_path = os.path.join(
|
| 159 |
with open(video_path, "wb") as f:
|
| 160 |
f.write(video_data)
|
| 161 |
print(f"Video saved to {video_path}")
|
|
@@ -197,12 +200,12 @@ def process_video(video_data, frame_skip=5, max_frames=100):
|
|
| 197 |
}
|
| 198 |
violations.append(violation)
|
| 199 |
|
| 200 |
-
snapshot_path = os.path.join(
|
| 201 |
cv2.imwrite(snapshot_path, frame)
|
| 202 |
snapshots.append({
|
| 203 |
"violation": label,
|
| 204 |
"frame": frame_count,
|
| 205 |
-
"snapshot_url": f"https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/output/{os.path.basename(snapshot_path)}"
|
| 206 |
})
|
| 207 |
|
| 208 |
frame_count += 1
|
|
|
|
| 18 |
DEFAULT_MODEL_PATH = "models/yolov8_safety.pt"
|
| 19 |
FALLBACK_MODEL = "yolov8n.pt"
|
| 20 |
MODEL_PATH = os.getenv("SAFETY_MODEL_PATH", DEFAULT_MODEL_PATH)
|
| 21 |
+
|
| 22 |
+
# Use static/output for publicly accessible files
|
| 23 |
+
STATIC_OUTPUT_DIR = "static/output"
|
| 24 |
+
os.makedirs(STATIC_OUTPUT_DIR, exist_ok=True)
|
| 25 |
|
| 26 |
VIOLATION_LABELS = {
|
| 27 |
0: "no_helmet",
|
|
|
|
| 48 |
raise
|
| 49 |
|
| 50 |
# ==========================
|
| 51 |
+
# Salesforce connection (hardcoded creds for testing)
|
| 52 |
# ==========================
|
| 53 |
def connect_to_salesforce():
|
| 54 |
sf = Salesforce(
|
| 55 |
username="prashanth1ai@safety.com",
|
| 56 |
password="SaiPrash461",
|
| 57 |
security_token="AP4AQnPoidIKPvSvNEfAHyoK",
|
| 58 |
+
domain="login"
|
| 59 |
)
|
| 60 |
return sf
|
| 61 |
|
|
|
|
| 72 |
violations_count = len(violations)
|
| 73 |
violations_details = violations_to_text(violations)
|
| 74 |
|
| 75 |
+
# Updated public URL base with static/output path
|
| 76 |
+
PUBLIC_PDF_BASE_URL = "https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/"
|
| 77 |
pdf_url = f"{PUBLIC_PDF_BASE_URL}{os.path.basename(pdf_filename)}"
|
| 78 |
|
| 79 |
try:
|
|
|
|
| 110 |
# ==========================
|
| 111 |
def generate_pdf_report(violations, snapshots, score):
|
| 112 |
try:
|
| 113 |
+
pdf_path = os.path.join(STATIC_OUTPUT_DIR, f"report_{int(time.time())}.pdf")
|
| 114 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 115 |
width, height = letter
|
| 116 |
|
|
|
|
| 133 |
|
| 134 |
snapshot = next((s for s in snapshots if s["frame"] == v["frame"] and s["violation"] == v["violation"]), None)
|
| 135 |
if snapshot:
|
| 136 |
+
local_img_path = os.path.join(STATIC_OUTPUT_DIR, os.path.basename(snapshot["snapshot_url"]))
|
| 137 |
if os.path.exists(local_img_path):
|
| 138 |
img = ImageReader(local_img_path)
|
| 139 |
c.drawImage(img, 50, y - 100, width=200, height=150)
|
|
|
|
| 158 |
def process_video(video_data, frame_skip=5, max_frames=100):
|
| 159 |
try:
|
| 160 |
print("Processing video data...")
|
| 161 |
+
video_path = os.path.join(STATIC_OUTPUT_DIR, f"temp_{int(time.time())}.mp4")
|
| 162 |
with open(video_path, "wb") as f:
|
| 163 |
f.write(video_data)
|
| 164 |
print(f"Video saved to {video_path}")
|
|
|
|
| 200 |
}
|
| 201 |
violations.append(violation)
|
| 202 |
|
| 203 |
+
snapshot_path = os.path.join(STATIC_OUTPUT_DIR, f"snapshot_{frame_count}_{label}.jpg")
|
| 204 |
cv2.imwrite(snapshot_path, frame)
|
| 205 |
snapshots.append({
|
| 206 |
"violation": label,
|
| 207 |
"frame": frame_count,
|
| 208 |
+
"snapshot_url": f"https://huggingface.co/spaces/PrashanthB461/AI_Safety_Demo1/resolve/main/static/output/{os.path.basename(snapshot_path)}"
|
| 209 |
})
|
| 210 |
|
| 211 |
frame_count += 1
|