Thiago Hersan commited on
Commit
6472e3a
·
1 Parent(s): 37b6eef

using YOLO for face detection

Browse files
.gitattributes CHANGED
@@ -1,4 +1,4 @@
1
- models/haarcascade_frontalface_alt2.xml filter=lfs diff=lfs merge=lfs -text
2
  models/lbfmodel.yaml filter=lfs diff=lfs merge=lfs -text
3
  imgs/03.webp filter=lfs diff=lfs merge=lfs -text
4
  imgs/11.jpg filter=lfs diff=lfs merge=lfs -text
 
 
 
1
  models/lbfmodel.yaml filter=lfs diff=lfs merge=lfs -text
2
  imgs/03.webp filter=lfs diff=lfs merge=lfs -text
3
  imgs/11.jpg filter=lfs diff=lfs merge=lfs -text
4
+ imgs/people.jpg filter=lfs diff=lfs merge=lfs -text
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import cv2
2
  import gradio as gr
3
  import numpy as np
4
- import matplotlib.pyplot as plt
5
 
 
6
  from math import atan2
7
- from os import listdir, path
8
  from PIL import Image as PImage
 
9
 
10
  OUT_W = 130
11
  OUT_H = 170
@@ -15,8 +15,8 @@ OUT_NOSE_TOP = 72
15
  EYE_0_IDX = 36
16
  EYE_1_IDX = 45
17
 
18
- haarcascade = "./models/haarcascade_frontalface_alt2.xml"
19
- face_detector = cv2.CascadeClassifier(haarcascade)
20
 
21
  LBFmodel = "./models/lbfmodel.yaml"
22
  landmark_detector = cv2.face.createFacemarkLBF()
@@ -30,19 +30,22 @@ def face(img_in):
30
  if img_in is None:
31
  return out_pad
32
 
33
- pimg = img_in.convert("L")
34
- pimg.thumbnail((1000,1000))
35
- imgg = np.array(pimg).copy()
36
 
37
- iw,ih = pimg.size
38
-
39
- faces = face_detector.detectMultiScale(imgg)
40
 
41
- if len(faces) < 1:
 
 
42
  return out_pad
43
 
 
 
 
44
  biggest_faces = faces[np.argsort(-faces[:,2])]
45
- _, landmarks = landmark_detector.fit(imgg, biggest_faces)
46
 
47
  if len(landmarks) < 1:
48
  return out_pad
@@ -58,7 +61,7 @@ def face(img_in):
58
  tilt_deg = 180 * tilt / np.pi
59
 
60
  scale = OUT_EYE_SPACE / abs(eye0[0] - eye1[0])
61
- pimgs = pimg.resize((int(iw * scale), int(ih * scale)), resample=PImage.Resampling.LANCZOS)
62
 
63
  # rotate around nose
64
  new_mid = [int(c * scale) for c in mid]
@@ -67,7 +70,7 @@ def face(img_in):
67
  new_mid[0] + (OUT_W // 2),
68
  new_mid[1] + (OUT_H - OUT_NOSE_TOP))
69
 
70
- img_out = pimgs.rotate(tilt_deg, center=new_mid, resample=PImage.Resampling.BICUBIC).crop(crop_box)
71
  out_images.append(gr.Image(img_out, visible=True))
72
 
73
  out_images += out_pad
@@ -86,7 +89,7 @@ with gr.Blocks() as demo:
86
  inputs=gr.Image(type="pil"),
87
  outputs=all_outputs,
88
  cache_examples=True,
89
- examples=[["./imgs/03.webp"], ["./imgs/11.jpg"]],
90
  allow_flagging="never",
91
  )
92
 
 
1
  import cv2
2
  import gradio as gr
3
  import numpy as np
 
4
 
5
+ from huggingface_hub import hf_hub_download
6
  from math import atan2
 
7
  from PIL import Image as PImage
8
+ from ultralytics import YOLO
9
 
10
  OUT_W = 130
11
  OUT_H = 170
 
15
  EYE_0_IDX = 36
16
  EYE_1_IDX = 45
17
 
18
+ yolo_model_path = hf_hub_download(repo_id="AdamCodd/YOLOv11n-face-detection", filename="model.pt")
19
+ face_detector = YOLO(yolo_model_path)
20
 
21
  LBFmodel = "./models/lbfmodel.yaml"
22
  landmark_detector = cv2.face.createFacemarkLBF()
 
30
  if img_in is None:
31
  return out_pad
32
 
33
+ img = img_in.copy()
34
+ img.thumbnail((1000,1000))
35
+ img_np = np.array(img).copy()
36
 
37
+ iw,ih = img.size
 
 
38
 
39
+ output = face_detector.predict(img, verbose=False)
40
+
41
+ if len(output) < 1 or len(output[0]) < 1:
42
  return out_pad
43
 
44
+ faces_xyxy = output[0].boxes.xyxy.numpy()
45
+ faces = np.array([[x0, y0, (x1 - x0), (y1 - y0)] for x0,y0,x1,y1 in faces_xyxy])
46
+
47
  biggest_faces = faces[np.argsort(-faces[:,2])]
48
+ _, landmarks = landmark_detector.fit(img_np, biggest_faces)
49
 
50
  if len(landmarks) < 1:
51
  return out_pad
 
61
  tilt_deg = 180 * tilt / np.pi
62
 
63
  scale = OUT_EYE_SPACE / abs(eye0[0] - eye1[0])
64
+ img_s = img.resize((int(iw * scale), int(ih * scale)))
65
 
66
  # rotate around nose
67
  new_mid = [int(c * scale) for c in mid]
 
70
  new_mid[0] + (OUT_W // 2),
71
  new_mid[1] + (OUT_H - OUT_NOSE_TOP))
72
 
73
+ img_out = img_s.rotate(tilt_deg, center=new_mid, resample=PImage.Resampling.BICUBIC).crop(crop_box).convert("L")
74
  out_images.append(gr.Image(img_out, visible=True))
75
 
76
  out_images += out_pad
 
89
  inputs=gr.Image(type="pil"),
90
  outputs=all_outputs,
91
  cache_examples=True,
92
+ examples=[["./imgs/03.webp"], ["./imgs/11.jpg"], ["./imgs/people.jpg"]],
93
  allow_flagging="never",
94
  )
95
 
models/haarcascade_frontalface_alt2.xml → imgs/people.jpg RENAMED
File without changes
requirements.txt CHANGED
@@ -146,6 +146,7 @@ typer-slim==0.12.5
146
  typing_extensions==4.11.0
147
  tzdata==2024.1
148
  uc-micro-py==1.0.1
 
149
  unicodedata2==15.1.0
150
  urllib3==2.2.2
151
  uvicorn==0.30.6
 
146
  typing_extensions==4.11.0
147
  tzdata==2024.1
148
  uc-micro-py==1.0.1
149
+ ultralytics==8.3.102
150
  unicodedata2==15.1.0
151
  urllib3==2.2.2
152
  uvicorn==0.30.6