Spaces:
Sleeping
Sleeping
File size: 21,778 Bytes
af32e72 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | from __future__ import annotations
import traceback
import uuid
from pathlib import Path
from flask import Flask, jsonify, render_template, request, send_file
from werkzeug.utils import secure_filename
from .attendance_service import get_attendance_service
from .pipeline_loader import get_pipeline
from .engagement_loader import get_engagement_pipeline
from .cognitive_loader import get_cognitive_pipeline
from .combined_loader import get_combined_pipeline
from .classroom_loader import get_classroom_pipeline
from .config import (
BACKEND_DIR,
RUNTIME_DIR,
UPLOAD_DIR,
OUTPUT_DIR,
ATTENDANCE_DIR,
ALLOWED_EXTENSIONS,
ALLOWED_IMAGE_EXTENSIONS,
)
app = Flask(
__name__,
template_folder=str(BACKEND_DIR / "templates"),
static_folder=str(BACKEND_DIR / "static"),
)
app.config["MAX_CONTENT_LENGTH"] = 4 * 1024 * 1024 * 1024
def ensure_runtime_dirs() -> None:
# Create the configured runtime directories
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
(ATTENDANCE_DIR / "uploads").mkdir(parents=True, exist_ok=True)
(ATTENDANCE_DIR / "marked").mkdir(parents=True, exist_ok=True)
def allowed_video(filename: str) -> bool:
return Path(filename.lower()).suffix in ALLOWED_EXTENSIONS
def allowed_media(filename: str) -> bool:
return Path(filename.lower()).suffix in ALLOWED_EXTENSIONS | ALLOWED_IMAGE_EXTENSIONS
def artifact_url(job_id: str, kind: str) -> str:
return f"/api/jobs/{job_id}/download/{kind}"
def clip_url(job_id: str, relative_path: str) -> str:
return f"/api/jobs/{job_id}/clips/{relative_path}"
def attendance_artifact_url(filename: str) -> str:
return f"/api/attendance/artifacts/{filename}"
@app.get("/")
def index():
return render_template("index.html")
@app.get("/api/health")
def health():
return jsonify({"ok": True})
@app.get("/api/attendance/roster")
def attendance_roster():
try:
service = get_attendance_service()
return jsonify({"ok": True, "students": service.list_students(), "attendance": service.list_attendance(limit=20)})
except Exception as exc:
return jsonify({"ok": False, "error": str(exc)}), 500
@app.delete("/api/attendance/students/<student_id>")
def attendance_delete_student(student_id: str):
service = get_attendance_service()
try:
result = service.delete_student(student_id)
except KeyError:
return jsonify({"ok": False, "error": "Student not found."}), 404
except Exception as exc:
return jsonify({"ok": False, "error": str(exc)}), 500
return jsonify({"ok": True, **result})
@app.post("/api/attendance/enroll")
def attendance_enroll():
uploaded_files = request.files.getlist("media")
student_name = request.form.get("student_name", "").strip()
if not student_name:
return jsonify({"ok": False, "error": "Enter a student name."}), 400
if not uploaded_files:
return jsonify({"ok": False, "error": "Upload at least one photo or video."}), 400
service = get_attendance_service()
saved_paths: list[Path] = []
for uploaded_file in uploaded_files:
if not uploaded_file or not uploaded_file.filename:
continue
if not allowed_media(uploaded_file.filename):
return jsonify({"ok": False, "error": "Use image or video files for enrollment."}), 400
media_name = secure_filename(uploaded_file.filename)
media_path = ATTENDANCE_DIR / "uploads" / f"{uuid.uuid4().hex[:12]}_{media_name}"
media_path.parent.mkdir(parents=True, exist_ok=True)
uploaded_file.save(media_path)
saved_paths.append(media_path)
if not saved_paths:
return jsonify({"ok": False, "error": "No valid media files were uploaded."}), 400
try:
result = service.enroll_student(student_name=student_name, media_paths=saved_paths)
except Exception as exc:
return jsonify({"ok": False, "error": str(exc)}), 500
return jsonify({"ok": True, **result, "students": service.list_students()})
@app.post("/api/attendance/enroll-folder")
def attendance_enroll_folder():
"""Enroll a student from a local folder of videos/images (no upload needed)."""
data = request.get_json(silent=True) or {}
student_name = data.get("student_name", "").strip()
folder_path = data.get("folder_path", "").strip()
if not student_name:
return jsonify({"ok": False, "error": "Enter a student name."}), 400
if not folder_path:
return jsonify({"ok": False, "error": "Enter a folder path."}), 400
folder = Path(folder_path).expanduser()
if not folder.exists() or not folder.is_dir():
return jsonify({"ok": False, "error": f"Folder not found: {folder_path}"}), 400
media_paths = sorted([
p for p in folder.iterdir()
if p.is_file() and p.suffix.lower() in ALLOWED_EXTENSIONS | ALLOWED_IMAGE_EXTENSIONS
])
if not media_paths:
return jsonify({"ok": False, "error": "No video or image files found in that folder."}), 400
service = get_attendance_service()
try:
result = service.enroll_student(student_name=student_name, media_paths=media_paths)
except Exception as exc:
return jsonify({"ok": False, "error": str(exc)}), 500
return jsonify({"ok": True, **result,
"students": service.list_students(),
"files_used": len(media_paths)})
@app.post("/api/attendance/mark")
def attendance_mark():
uploaded_file = request.files.get("photo")
if uploaded_file is None or not uploaded_file.filename:
return jsonify({"ok": False, "error": "Upload a classroom photo first."}), 400
if not allowed_media(uploaded_file.filename):
return jsonify({"ok": False, "error": "Use an image or video file for attendance marking."}), 400
service = get_attendance_service()
photo_name = secure_filename(uploaded_file.filename)
photo_path = ATTENDANCE_DIR / "uploads" / f"{uuid.uuid4().hex[:12]}_{photo_name}"
photo_path.parent.mkdir(parents=True, exist_ok=True)
uploaded_file.save(photo_path)
try:
result = service.mark_attendance(photo_path)
except Exception as exc:
traceback.print_exc()
return jsonify({"ok": False, "error": str(exc)}), 500
result["marked_url"] = attendance_artifact_url(Path(result["marked_url"]).name)
return jsonify({"ok": True, **result})
@app.post("/api/process")
def process_video():
ensure_runtime_dirs()
uploaded_file = request.files.get("video")
if uploaded_file is None or not uploaded_file.filename:
return jsonify({"ok": False, "error": "Upload a video file first."}), 400
if not allowed_video(uploaded_file.filename):
return jsonify({"ok": False, "error": "Use an .mp4, .mov, .avi, .mkv, or .webm file."}), 400
job_id = uuid.uuid4().hex[:12]
filename = secure_filename(uploaded_file.filename)
input_path = UPLOAD_DIR / f"{job_id}_{filename}"
output_path = OUTPUT_DIR / job_id
uploaded_file.save(input_path)
try:
pipeline = get_pipeline()
result = pipeline.process_video(video_path=input_path, output_dir=output_path, annotate=True)
except Exception as exc:
traceback.print_exc()
return jsonify({"ok": False, "error": str(exc)}), 500
summary = result["summary"]
job_output = OUTPUT_DIR / job_id
clip_entries = []
for clip in summary.get("clips", []):
clip_path_value = clip.get("clip_path")
clip_relative_path = None
if clip_path_value:
try:
clip_relative_path = str(Path(clip_path_value).resolve().relative_to(job_output.resolve()))
except Exception:
clip_relative_path = None
clip_entries.append(
{
**clip,
"clip_relative_path": clip_relative_path,
"clip_url": clip_url(job_id, clip_relative_path) if clip_relative_path else None,
}
)
response = {
"ok": True,
"job_id": job_id,
"summary": summary,
"paths": {
"summary_json": result["summary_path"],
"csv": result["csv_path"],
"clip_dir": result["clip_dir"],
"annotated_video": result["annotated_video"],
},
"download_urls": {
"summary_json": artifact_url(job_id, "summary"),
"csv": artifact_url(job_id, "csv"),
"annotated_video": artifact_url(job_id, "annotated"),
},
"clips": clip_entries,
}
return jsonify(response)
@app.post("/api/engagement/process")
def process_engagement():
ensure_runtime_dirs()
uploaded_file = request.files.get("video")
if uploaded_file is None or not uploaded_file.filename:
return jsonify({"ok": False, "error": "Upload a video file first."}), 400
if not allowed_video(uploaded_file.filename):
return jsonify({"ok": False, "error": "Use an .mp4, .mov, .avi, .mkv, or .webm file."}), 400
job_id = uuid.uuid4().hex[:12]
filename = secure_filename(uploaded_file.filename)
input_path = UPLOAD_DIR / f"{job_id}_{filename}"
output_path = OUTPUT_DIR / f"eng_{job_id}"
uploaded_file.save(input_path)
try:
pipeline = get_engagement_pipeline()
result = pipeline.process(video_path=input_path, output_dir=output_path)
except Exception as exc:
traceback.print_exc()
return jsonify({"ok": False, "error": str(exc)}), 500
# attach clip URLs to each student entry
students = result["students"]
for s in students:
s["clip_urls"] = [
f"/api/engagement/jobs/{job_id}/clips/{fname}"
for fname in (s.get("clips") or [])
]
return jsonify({
"ok": True,
"job_id": job_id,
"summary": result["summary"],
"students": students,
"timeline": result["timeline"],
"download_urls": {
"summary_json": f"/api/engagement/jobs/{job_id}/summary",
"csv": f"/api/engagement/jobs/{job_id}/csv",
},
})
@app.get("/api/engagement/jobs/<job_id>/clips/<filename>")
def serve_engagement_clip(job_id: str, filename: str):
clips_dir = (OUTPUT_DIR / f"eng_{job_id}" / "clips").resolve()
clip_path = (clips_dir / secure_filename(filename)).resolve()
try:
clip_path.relative_to(clips_dir)
except Exception:
return jsonify({"ok": False, "error": "Not found"}), 404
if not clip_path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(clip_path, mimetype="video/mp4")
@app.get("/api/engagement/jobs/<job_id>/summary")
def download_engagement_summary(job_id: str):
path = OUTPUT_DIR / f"eng_{job_id}" / "engagement_summary.json"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="engagement_summary.json")
@app.get("/api/engagement/jobs/<job_id>/csv")
def download_engagement_csv(job_id: str):
path = OUTPUT_DIR / f"eng_{job_id}" / "engagement_students.csv"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="engagement_students.csv")
@app.get("/api/jobs/<job_id>/download/<kind>")
def download_artifact(job_id: str, kind: str):
job_output = OUTPUT_DIR / job_id
summary_path = next(job_output.glob("*_summary.json"), None)
csv_path = next(job_output.glob("*_per_student_predictions.csv"), None)
annotated_path = next(job_output.glob("*_sampled_annotated.mp4"), None)
if kind == "summary" and summary_path is not None and summary_path.exists():
return send_file(summary_path, as_attachment=True, download_name=summary_path.name)
if kind == "csv" and csv_path is not None and csv_path.exists():
return send_file(csv_path, as_attachment=True, download_name=csv_path.name)
if kind == "annotated" and annotated_path is not None and annotated_path.exists():
return send_file(annotated_path, as_attachment=True, download_name=annotated_path.name)
return jsonify({"ok": False, "error": "Artifact not found."}), 404
@app.get("/api/jobs/<job_id>/clips/<path:relative_path>")
def serve_clip(job_id: str, relative_path: str):
job_output = OUTPUT_DIR / job_id
clip_path = (job_output / relative_path).resolve()
try:
clip_path.relative_to(job_output.resolve())
except Exception:
return jsonify({"ok": False, "error": "Clip not found."}), 404
if not clip_path.exists() or not clip_path.is_file():
return jsonify({"ok": False, "error": "Clip not found."}), 404
return send_file(clip_path, as_attachment=False, download_name=clip_path.name)
@app.get("/api/attendance/artifacts/<path:filename>")
def serve_attendance_artifact(filename: str):
artifact_path = (ATTENDANCE_DIR / "marked" / filename).resolve()
marked_dir = (ATTENDANCE_DIR / "marked").resolve()
try:
artifact_path.relative_to(marked_dir)
except Exception:
return jsonify({"ok": False, "error": "Artifact not found."}), 404
if not artifact_path.exists() or not artifact_path.is_file():
return jsonify({"ok": False, "error": "Artifact not found."}), 404
return send_file(artifact_path, as_attachment=False, download_name=artifact_path.name)
@app.post("/api/combined/process")
def process_combined():
ensure_runtime_dirs()
uploaded_file = request.files.get("video")
if uploaded_file is None or not uploaded_file.filename:
return jsonify({"ok": False, "error": "Upload a video file first."}), 400
if not allowed_video(uploaded_file.filename):
return jsonify({"ok": False, "error": "Use an .mp4, .mov, .avi, .mkv, or .webm file."}), 400
job_id = uuid.uuid4().hex[:12]
filename = secure_filename(uploaded_file.filename)
input_path = UPLOAD_DIR / f"{job_id}_{filename}"
output_path = OUTPUT_DIR / f"comb_{job_id}"
uploaded_file.save(input_path)
try:
pipeline = get_combined_pipeline()
result = pipeline.process(video_path=input_path, output_dir=output_path)
except Exception as exc:
traceback.print_exc()
return jsonify({"ok": False, "error": str(exc)}), 500
students = result["students"]
for s in students:
clip_fname = s.get("clip")
s["clip_url"] = f"/api/combined/jobs/{job_id}/clips/{clip_fname}" if clip_fname else None
return jsonify({
"ok": True,
"job_id": job_id,
"summary": result["summary"],
"students": students,
"download_urls": {
"summary_json": f"/api/combined/jobs/{job_id}/summary",
"csv": f"/api/combined/jobs/{job_id}/csv",
},
})
@app.get("/api/combined/jobs/<job_id>/clips/<filename>")
def serve_combined_clip(job_id: str, filename: str):
clips_dir = (OUTPUT_DIR / f"comb_{job_id}" / "clips").resolve()
clip_path = (clips_dir / secure_filename(filename)).resolve()
try:
clip_path.relative_to(clips_dir)
except Exception:
return jsonify({"ok": False, "error": "Not found"}), 404
if not clip_path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(clip_path, mimetype="video/mp4")
@app.get("/api/combined/jobs/<job_id>/summary")
def download_combined_summary(job_id: str):
path = OUTPUT_DIR / f"comb_{job_id}" / "combined_summary.json"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="combined_summary.json")
@app.get("/api/combined/jobs/<job_id>/csv")
def download_combined_csv(job_id: str):
path = OUTPUT_DIR / f"comb_{job_id}" / "combined_students.csv"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="combined_students.csv")
@app.post("/api/cognitive/process")
def process_cognitive():
ensure_runtime_dirs()
uploaded_file = request.files.get("video")
if uploaded_file is None or not uploaded_file.filename:
return jsonify({"ok": False, "error": "Upload a video file first."}), 400
if not allowed_video(uploaded_file.filename):
return jsonify({"ok": False, "error": "Use an .mp4, .mov, .avi, .mkv, or .webm file."}), 400
job_id = uuid.uuid4().hex[:12]
filename = secure_filename(uploaded_file.filename)
input_path = UPLOAD_DIR / f"{job_id}_{filename}"
output_path = OUTPUT_DIR / f"cog_{job_id}"
uploaded_file.save(input_path)
try:
pipeline = get_cognitive_pipeline()
result = pipeline.process(video_path=input_path, output_dir=output_path)
except Exception as exc:
traceback.print_exc()
return jsonify({"ok": False, "error": str(exc)}), 500
students = result["students"]
for s in students:
clip_fname = s.get("clip")
s["clip_url"] = f"/api/cognitive/jobs/{job_id}/clips/{clip_fname}" if clip_fname else None
return jsonify({
"ok": True,
"job_id": job_id,
"summary": result["summary"],
"students": students,
"download_urls": {
"summary_json": f"/api/cognitive/jobs/{job_id}/summary",
"csv": f"/api/cognitive/jobs/{job_id}/csv",
},
})
@app.get("/api/cognitive/jobs/<job_id>/clips/<filename>")
def serve_cognitive_clip(job_id: str, filename: str):
clips_dir = (OUTPUT_DIR / f"cog_{job_id}" / "clips").resolve()
clip_path = (clips_dir / secure_filename(filename)).resolve()
try:
clip_path.relative_to(clips_dir)
except Exception:
return jsonify({"ok": False, "error": "Not found"}), 404
if not clip_path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(clip_path, mimetype="video/mp4")
@app.get("/api/cognitive/jobs/<job_id>/summary")
def download_cognitive_summary(job_id: str):
path = OUTPUT_DIR / f"cog_{job_id}" / "cognitive_summary.json"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="cognitive_summary.json")
@app.get("/api/cognitive/jobs/<job_id>/csv")
def download_cognitive_csv(job_id: str):
path = OUTPUT_DIR / f"cog_{job_id}" / "cognitive_students.csv"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="cognitive_students.csv")
@app.post("/api/classroom/process")
def process_classroom():
ensure_runtime_dirs()
uploaded_file = request.files.get("video")
if uploaded_file is None or not uploaded_file.filename:
return jsonify({"ok": False, "error": "Upload a video file first."}), 400
if not allowed_video(uploaded_file.filename):
return jsonify({"ok": False, "error": "Use an .mp4, .mov, .avi, .mkv, or .webm file."}), 400
job_id = uuid.uuid4().hex[:12]
filename = secure_filename(uploaded_file.filename)
input_path = UPLOAD_DIR / f"{job_id}_{filename}"
output_path = OUTPUT_DIR / f"cls_{job_id}"
uploaded_file.save(input_path)
try:
pipeline = get_classroom_pipeline()
result = pipeline.process(video_path=input_path, output_dir=output_path)
except Exception as exc:
traceback.print_exc()
return jsonify({"ok": False, "error": str(exc)}), 500
summary = result["summary"]
# attach clip URLs to each window record in each student's timeline
for student in summary.get("students", []):
for window in student.get("timeline", []):
clip_rel = window.get("clip")
window["clip_url"] = (
f"/api/classroom/jobs/{job_id}/clips/{clip_rel}" if clip_rel else None
)
return jsonify({
"ok": True,
"job_id": job_id,
"summary": summary,
"download_urls": {
"summary_json": f"/api/classroom/jobs/{job_id}/summary",
"csv": f"/api/classroom/jobs/{job_id}/csv",
},
})
@app.get("/api/classroom/jobs/<job_id>/clips/<path:rel_path>")
def serve_classroom_clip(job_id: str, rel_path: str):
clips_dir = (OUTPUT_DIR / f"cls_{job_id}" / "clips").resolve()
clip_path = (clips_dir / rel_path).resolve()
try:
clip_path.relative_to(clips_dir)
except Exception:
return jsonify({"ok": False, "error": "Not found"}), 404
if not clip_path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(clip_path, mimetype="video/mp4")
@app.get("/api/classroom/jobs/<job_id>/summary")
def download_classroom_summary(job_id: str):
path = OUTPUT_DIR / f"cls_{job_id}" / "classroom_summary.json"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="classroom_summary.json")
@app.get("/api/classroom/jobs/<job_id>/csv")
def download_classroom_csv(job_id: str):
path = OUTPUT_DIR / f"cls_{job_id}" / "classroom_timeline.csv"
if not path.exists():
return jsonify({"ok": False, "error": "Not found"}), 404
return send_file(path, as_attachment=True, download_name="classroom_timeline.csv")
@app.errorhandler(Exception)
def handle_unhandled_exception(exc):
return jsonify({"ok": False, "error": str(exc)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080, debug=True, use_reloader=False)
|