PachecoT commited on
Commit
fc754ef
verified
1 Parent(s): b78b643

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -12,28 +12,43 @@ app = FastAPI()
12
 
13
  # Initialize MediaPipe Face Detection
14
  mp_face_detection = mp.solutions.face_detection
15
- face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.2)
16
 
17
  def buscar_existe(image):
18
- existe = "no"
19
- print("resultado: ", image.shape)
20
-
21
- # Convert the image to RGB
22
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
 
23
  results = face_detection.process(image_rgb)
24
 
 
25
  if results.detections:
26
- existe = "si"
27
-
28
- return existe
29
-
30
  # Ruta de predicci贸n
31
  @app.post('/predict/')
32
  async def predict(file: UploadFile = File(...)):
33
  try:
34
- image = Image.open(BytesIO(await file.read()))
35
- image = np.asarray(image)
36
- prediction = buscar_existe(image)
 
 
 
 
 
 
 
 
 
 
 
37
  return {"prediction": prediction}
38
  except Exception as e:
39
  raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
12
 
13
  # Initialize MediaPipe Face Detection
14
  mp_face_detection = mp.solutions.face_detection
15
+ face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)
16
 
17
  def buscar_existe(image):
18
+ # Convert the image to RGB (MediaPipe requires RGB input)
 
 
 
19
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
20
+
21
+ # Process the image
22
  results = face_detection.process(image_rgb)
23
 
24
+ # Check if any faces were detected
25
  if results.detections:
26
+ return "si"
27
+ else:
28
+ return "no"
29
+
30
  # Ruta de predicci贸n
31
  @app.post('/predict/')
32
  async def predict(file: UploadFile = File(...)):
33
  try:
34
+ # Read the file
35
+ contents = await file.read()
36
+ image = Image.open(BytesIO(contents))
37
+
38
+ # Convert PIL Image to numpy array
39
+ image_np = np.array(image)
40
+
41
+ # If the image is RGB, convert to BGR (OpenCV uses BGR)
42
+ if image_np.shape[-1] == 3:
43
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
44
+
45
+ # Perform face detection
46
+ prediction = buscar_existe(image_np)
47
+
48
  return {"prediction": prediction}
49
  except Exception as e:
50
  raise HTTPException(status_code=500, detail=str(e))
51
+
52
+ @app.get("/")
53
+ async def root():
54
+ return {"message": "Face Detection API is running"}