docker 1
Browse files- Dockerfile +5 -8
- app/main.py +22 -23
Dockerfile
CHANGED
|
@@ -14,13 +14,13 @@ RUN apt-get update && apt-get install -y \
|
|
| 14 |
libxrender-dev \
|
| 15 |
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
-
#
|
| 18 |
RUN pip install --upgrade pip setuptools wheel
|
| 19 |
|
| 20 |
-
# Install dlib separately to avoid
|
| 21 |
RUN pip install dlib --use-pep517 --no-build-isolation
|
| 22 |
|
| 23 |
-
# Install
|
| 24 |
COPY requirements.txt .
|
| 25 |
RUN pip install -r requirements.txt
|
| 26 |
|
|
@@ -28,8 +28,5 @@ RUN pip install -r requirements.txt
|
|
| 28 |
COPY app/ /app/
|
| 29 |
WORKDIR /app
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Run the app
|
| 35 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 14 |
libxrender-dev \
|
| 15 |
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
|
| 17 |
+
# Upgrade pip
|
| 18 |
RUN pip install --upgrade pip setuptools wheel
|
| 19 |
|
| 20 |
+
# Install dlib separately to avoid issues
|
| 21 |
RUN pip install dlib --use-pep517 --no-build-isolation
|
| 22 |
|
| 23 |
+
# Install Python dependencies
|
| 24 |
COPY requirements.txt .
|
| 25 |
RUN pip install -r requirements.txt
|
| 26 |
|
|
|
|
| 28 |
COPY app/ /app/
|
| 29 |
WORKDIR /app
|
| 30 |
|
| 31 |
+
# Run the app with uvicorn (no need to expose ports for Hugging Face)
|
| 32 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
|
|
|
|
|
app/main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import numpy as np
|
|
@@ -6,28 +6,24 @@ 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]] = {}
|
| 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"
|
| 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)
|
|
@@ -37,7 +33,6 @@ def load_student_encodings(folder_path: str):
|
|
| 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"}
|
|
@@ -53,8 +48,11 @@ def process_image(image: np.ndarray, students: dict):
|
|
| 53 |
return {"missing_students": list(missing_students)}
|
| 54 |
|
| 55 |
@app.post("/create_folder/")
|
| 56 |
-
async def create_folder(
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -64,36 +62,37 @@ async def create_folder(org_name: str = Form(...), folder_name: str = Form(...),
|
|
| 64 |
with open(file_path, "wb") as buffer:
|
| 65 |
shutil.copyfileobj(file.file, buffer)
|
| 66 |
|
| 67 |
-
return {
|
|
|
|
|
|
|
| 68 |
|
| 69 |
@app.post("/check_attendance/")
|
| 70 |
-
async def check_attendance(
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
folder_path = get_org_folder_path(org_name, folder_name)
|
| 73 |
-
|
| 74 |
-
# Validate faculty access
|
| 75 |
folder_key = (org_name, folder_name)
|
|
|
|
| 76 |
if faculty_id not in faculty_assignments.get(folder_key, set()):
|
| 77 |
return {"error": "Unauthorized access. Only assigned faculty members can check attendance."}
|
| 78 |
|
| 79 |
-
# Load student encodings from the specified folder
|
| 80 |
students = load_student_encodings(folder_path)
|
| 81 |
if "error" in students:
|
| 82 |
return students
|
| 83 |
|
| 84 |
try:
|
| 85 |
-
# Read and process uploaded image
|
| 86 |
image_bytes = await file.read()
|
| 87 |
-
image = Image.open(io.BytesIO(image_bytes))
|
| 88 |
-
image = image.convert("RGB")
|
| 89 |
image_array = np.array(image)
|
| 90 |
-
|
| 91 |
return process_image(image_array, students)
|
| 92 |
-
|
| 93 |
except UnidentifiedImageError:
|
| 94 |
-
return {"error": "Invalid image format. Please upload a valid
|
| 95 |
except Exception as e:
|
| 96 |
return {"error": f"Internal server error: {str(e)}"}
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import numpy as np
|
|
|
|
| 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)
|
|
|
|
| 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"}
|
|
|
|
| 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)
|
|
|
|
| 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)
|