Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Depends
|
| 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 |
+
import uvicorn
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
BASE_DIR = "uploaded_images"
|
| 14 |
+
|
| 15 |
+
# Ensure base directory exists
|
| 16 |
+
os.makedirs(BASE_DIR, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
faculty_assignments: Dict[tuple, Set[str]] = {} # Dictionary to track faculties assigned to folders
|
| 19 |
+
|
| 20 |
+
def get_org_folder_path(org_name: str, folder_name: str):
|
| 21 |
+
return os.path.join(BASE_DIR, org_name, folder_name)
|
| 22 |
+
|
| 23 |
+
def load_student_encodings(folder_path: str):
|
| 24 |
+
"""Load student images from a given folder and return encodings."""
|
| 25 |
+
students = {}
|
| 26 |
+
if not os.path.exists(folder_path):
|
| 27 |
+
return {"error": f"Folder '{folder_path}' does not exist"}
|
| 28 |
+
|
| 29 |
+
for filename in os.listdir(folder_path):
|
| 30 |
+
if filename.endswith(".jpg") or filename.endswith(".png"):
|
| 31 |
+
image_path = os.path.join(folder_path, filename)
|
| 32 |
+
image = face_recognition.load_image_file(image_path)
|
| 33 |
+
encoding = face_recognition.face_encodings(image)
|
| 34 |
+
if encoding:
|
| 35 |
+
students[filename.split(".")[0]] = encoding[0]
|
| 36 |
+
|
| 37 |
+
return students
|
| 38 |
+
|
| 39 |
+
def process_image(image: np.ndarray, students: dict):
|
| 40 |
+
"""Helper function to compare uploaded image with stored student images."""
|
| 41 |
+
group_face_encodings = face_recognition.face_encodings(image)
|
| 42 |
+
if not group_face_encodings:
|
| 43 |
+
return {"error": "No faces detected in the image"}
|
| 44 |
+
|
| 45 |
+
present_students = set()
|
| 46 |
+
for group_encoding in group_face_encodings:
|
| 47 |
+
for name, student_encoding in students.items():
|
| 48 |
+
match = face_recognition.compare_faces([student_encoding], group_encoding, tolerance=0.6)
|
| 49 |
+
if match[0]:
|
| 50 |
+
present_students.add(name)
|
| 51 |
+
|
| 52 |
+
missing_students = set(students.keys()) - present_students
|
| 53 |
+
return {"missing_students": list(missing_students)}
|
| 54 |
+
|
| 55 |
+
@app.post("/create_folder/")
|
| 56 |
+
async def create_folder(org_name: str = Form(...), folder_name: str = Form(...), files: List[UploadFile] = File(...)):
|
| 57 |
+
"""Creates a new folder under an organization and stores images."""
|
| 58 |
+
org_path = os.path.join(BASE_DIR, org_name)
|
| 59 |
+
folder_path = os.path.join(org_path, folder_name)
|
| 60 |
+
os.makedirs(folder_path, exist_ok=True)
|
| 61 |
+
|
| 62 |
+
for file in files:
|
| 63 |
+
file_path = os.path.join(folder_path, file.filename)
|
| 64 |
+
with open(file_path, "wb") as buffer:
|
| 65 |
+
shutil.copyfileobj(file.file, buffer)
|
| 66 |
+
|
| 67 |
+
return {"message": f"Folder '{folder_name}' created under organization '{org_name}' with {len(files)} images."}
|
| 68 |
+
|
| 69 |
+
@app.post("/check_attendance/")
|
| 70 |
+
async def check_attendance(org_name: str = Form(...), folder_name: str = Form(...), file: UploadFile = File(...)):
|
| 71 |
+
"""Check attendance using an uploaded image, ensuring only assigned faculty can check."""
|
| 72 |
+
folder_path = get_org_folder_path(org_name, folder_name)
|
| 73 |
+
|
| 74 |
+
# Load student encodings from the specified folder
|
| 75 |
+
students = load_student_encodings(folder_path)
|
| 76 |
+
if "error" in students:
|
| 77 |
+
return students
|
| 78 |
+
|
| 79 |
+
try:
|
| 80 |
+
# Read and process uploaded image
|
| 81 |
+
image_bytes = await file.read()
|
| 82 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 83 |
+
image = image.convert("RGB")
|
| 84 |
+
image_array = np.array(image)
|
| 85 |
+
|
| 86 |
+
return process_image(image_array, students)
|
| 87 |
+
|
| 88 |
+
except UnidentifiedImageError:
|
| 89 |
+
return {"error": "Invalid image format. Please upload a valid image file (JPG or PNG)."}
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return {"error": f"Internal server error: {str(e)}"}
|
| 92 |
+
|