aniudupa commited on
Commit
7ca08d4
·
verified ·
1 Parent(s): 1dac835

Delete app

Browse files
Files changed (1) hide show
  1. app/main.py +0 -98
app/main.py DELETED
@@ -1,98 +0,0 @@
1
- from fastapi import FastAPI, UploadFile, File, Form
2
- import os
3
- import shutil
4
- import numpy as np
5
- import face_recognition
6
- from PIL import Image, UnidentifiedImageError
7
- import io
8
- from typing import List, Dict, Set
9
-
10
- app = FastAPI()
11
-
12
- BASE_DIR = "uploaded_images"
13
- os.makedirs(BASE_DIR, exist_ok=True)
14
-
15
- faculty_assignments: Dict[tuple, Set[str]] = {}
16
-
17
- def get_org_folder_path(org_name: str, folder_name: str):
18
- return os.path.join(BASE_DIR, org_name, folder_name)
19
-
20
- def load_student_encodings(folder_path: str):
21
- students = {}
22
- if not os.path.exists(folder_path):
23
- return {"error": f"Folder '{folder_path}' does not exist"}
24
-
25
- for filename in os.listdir(folder_path):
26
- if filename.lower().endswith((".jpg", ".jpeg", ".png")):
27
- image_path = os.path.join(folder_path, filename)
28
- image = face_recognition.load_image_file(image_path)
29
- encoding = face_recognition.face_encodings(image)
30
- if encoding:
31
- students[filename.split(".")[0]] = encoding[0]
32
-
33
- return students
34
-
35
- def process_image(image: np.ndarray, students: dict):
36
- group_face_encodings = face_recognition.face_encodings(image)
37
- if not group_face_encodings:
38
- return {"error": "No faces detected in the image"}
39
-
40
- present_students = set()
41
- for group_encoding in group_face_encodings:
42
- for name, student_encoding in students.items():
43
- match = face_recognition.compare_faces([student_encoding], group_encoding, tolerance=0.6)
44
- if match[0]:
45
- present_students.add(name)
46
-
47
- missing_students = set(students.keys()) - present_students
48
- return {"missing_students": list(missing_students)}
49
-
50
- @app.post("/create_folder/")
51
- async def create_folder(
52
- org_name: str = Form(...),
53
- folder_name: str = Form(...),
54
- files: List[UploadFile] = File(...)
55
- ):
56
- org_path = os.path.join(BASE_DIR, org_name)
57
- folder_path = os.path.join(org_path, folder_name)
58
- os.makedirs(folder_path, exist_ok=True)
59
-
60
- for file in files:
61
- file_path = os.path.join(folder_path, file.filename)
62
- with open(file_path, "wb") as buffer:
63
- shutil.copyfileobj(file.file, buffer)
64
-
65
- return {
66
- "message": f"Folder '{folder_name}' created under organization '{org_name}' with {len(files)} images."
67
- }
68
-
69
- @app.post("/check_attendance/")
70
- async def check_attendance(
71
- org_name: str = Form(...),
72
- folder_name: str = Form(...),
73
- faculty_id: str = Form(...),
74
- file: UploadFile = File(...)
75
- ):
76
- folder_path = get_org_folder_path(org_name, folder_name)
77
- folder_key = (org_name, folder_name)
78
-
79
- if faculty_id not in faculty_assignments.get(folder_key, set()):
80
- return {"error": "Unauthorized access. Only assigned faculty members can check attendance."}
81
-
82
- students = load_student_encodings(folder_path)
83
- if "error" in students:
84
- return students
85
-
86
- try:
87
- image_bytes = await file.read()
88
- image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
89
- image_array = np.array(image)
90
- return process_image(image_array, students)
91
- except UnidentifiedImageError:
92
- return {"error": "Invalid image format. Please upload a valid JPG or PNG image."}
93
- except Exception as e:
94
- return {"error": f"Internal server error: {str(e)}"}
95
-
96
- if __name__ == "__main__":
97
- import uvicorn
98
- uvicorn.run(app, host="0.0.0.0", port=7860)