Spaces:
Sleeping
Sleeping
Thiago Hersan
commited on
Commit
·
8a29fe8
0
Parent(s):
initial commit
Browse files- .gitattributes +4 -0
- .github/workflows/deploy-hf.yml +25 -0
- .gitignore +4 -0
- README.md +10 -0
- app.py +94 -0
- requirements.txt +2 -0
.gitattributes
ADDED
@@ -0,0 +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
|
.github/workflows/deploy-hf.yml
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Deploy to Hugging Face spaces
|
2 |
+
|
3 |
+
on:
|
4 |
+
push:
|
5 |
+
branches:
|
6 |
+
- main
|
7 |
+
|
8 |
+
jobs:
|
9 |
+
build:
|
10 |
+
runs-on: ubuntu-latest
|
11 |
+
|
12 |
+
steps:
|
13 |
+
- name: Checkout Dev Repo
|
14 |
+
uses: actions/checkout@v3
|
15 |
+
with:
|
16 |
+
fetch-depth: 0
|
17 |
+
lfs: true
|
18 |
+
|
19 |
+
- name: Push to HF
|
20 |
+
env:
|
21 |
+
HFTOKEN: ${{ secrets.HFTOKEN }}
|
22 |
+
|
23 |
+
run: |
|
24 |
+
git remote add hf https://thiagohersan:[email protected]/spaces/5020A/5020-FaceAlign-Gradio
|
25 |
+
git push -f hf main
|
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.DS_S*
|
2 |
+
__pycache__/
|
3 |
+
gradio_cached_examples/
|
4 |
+
.ipynb_checkpoints/
|
README.md
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Face Align
|
3 |
+
emoji: 🤨📐
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: gray
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.42.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
12 |
+
OUT_EYE_SPACE = 64
|
13 |
+
OUT_NOSE_TOP = 72
|
14 |
+
|
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()
|
23 |
+
landmark_detector.loadModel(LBFmodel)
|
24 |
+
|
25 |
+
NUM_OUTS = 16
|
26 |
+
all_outputs = [gr.Image(format="jpeg", visible=False) for _ in range(NUM_OUTS)]
|
27 |
+
|
28 |
+
def face(img_in):
|
29 |
+
out_pad = NUM_OUTS * [gr.Image(visible=False)]
|
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
|
49 |
+
|
50 |
+
out_images = []
|
51 |
+
for landmark in landmarks:
|
52 |
+
eye0 = np.array(landmark[0][EYE_0_IDX])
|
53 |
+
eye1 = np.array(landmark[0][EYE_1_IDX])
|
54 |
+
mid = np.mean([eye0, eye1], axis=0)
|
55 |
+
|
56 |
+
eye_line = eye1 - eye0
|
57 |
+
tilt = atan2(eye_line[1], eye_line[0])
|
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]
|
65 |
+
crop_box = (new_mid[0] - (OUT_W // 2),
|
66 |
+
new_mid[1] - OUT_NOSE_TOP,
|
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
|
74 |
+
return out_images[:NUM_OUTS]
|
75 |
+
|
76 |
+
|
77 |
+
with gr.Blocks() as demo:
|
78 |
+
gr.Markdown("""
|
79 |
+
# 9103H 2024F Face Alignment Tool.
|
80 |
+
## Interface for face detection, alignment, cropping\
|
81 |
+
to help create dataset for [HW10](https://github.com/DM-GY-9103-2024F-H/HW10).
|
82 |
+
""")
|
83 |
+
|
84 |
+
gr.Interface(
|
85 |
+
face,
|
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 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
opencv-contrib-python
|
2 |
+
opencv-python
|