Update main.py
Browse files
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
from fastapi.responses import StreamingResponse, FileResponse
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
import torch
|
@@ -13,6 +13,7 @@ app = FastAPI()
|
|
13 |
|
14 |
# Load model and necessary components
|
15 |
model = None
|
|
|
16 |
|
17 |
def load_model():
|
18 |
global model
|
@@ -24,7 +25,7 @@ def load_model():
|
|
24 |
logging.basicConfig(level=logging.INFO)
|
25 |
|
26 |
@app.post("/upload/")
|
27 |
-
async def process_image(file: UploadFile = File(...)
|
28 |
global model
|
29 |
if model is None:
|
30 |
load_model()
|
@@ -42,6 +43,18 @@ async def process_image(file: UploadFile = File(...), top: int = Form(...), bott
|
|
42 |
|
43 |
logging.info(f"Uploaded image shape: {frame_bgr.shape}")
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# Save the uploaded image temporarily to pass the file path to the model
|
46 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
47 |
cv2.imwrite(temp_file.name, frame_bgr)
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
from fastapi.responses import StreamingResponse, FileResponse
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
import torch
|
|
|
13 |
|
14 |
# Load model and necessary components
|
15 |
model = None
|
16 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
17 |
|
18 |
def load_model():
|
19 |
global model
|
|
|
25 |
logging.basicConfig(level=logging.INFO)
|
26 |
|
27 |
@app.post("/upload/")
|
28 |
+
async def process_image(file: UploadFile = File(...)):
|
29 |
global model
|
30 |
if model is None:
|
31 |
load_model()
|
|
|
43 |
|
44 |
logging.info(f"Uploaded image shape: {frame_bgr.shape}")
|
45 |
|
46 |
+
# Detect faces in the image
|
47 |
+
gray = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2GRAY)
|
48 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
49 |
+
|
50 |
+
if len(faces) == 0:
|
51 |
+
logging.error("No faces detected in the image.")
|
52 |
+
return {"error": "No faces detected in the image."}
|
53 |
+
|
54 |
+
# Use the first detected face
|
55 |
+
(x, y, w, h) = faces[0]
|
56 |
+
top, bottom, left, right = y, y + h, x, x + w
|
57 |
+
|
58 |
# Save the uploaded image temporarily to pass the file path to the model
|
59 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
60 |
cv2.imwrite(temp_file.name, frame_bgr)
|