Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,79 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, ViTForImageClassification, ViTImageProcessor
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2 as cv
|
| 6 |
+
import dlib
|
| 7 |
+
import logging
|
| 8 |
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
|
| 11 |
+
def grab_faces(img: np.ndarray) -> Optional[np.ndarray]:
|
| 12 |
+
cascades = [
|
| 13 |
+
"haarcascade_frontalface_default.xml",
|
| 14 |
+
"haarcascade_frontalface_alt.xml",
|
| 15 |
+
"haarcascade_frontalface_alt2.xml",
|
| 16 |
+
"haarcascade_frontalface_alt_tree.xml"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
detector = dlib.get_frontal_face_detector() # load face detector
|
| 20 |
+
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks_GTX.dat") # load face predictor
|
| 21 |
+
mmod = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat") # load face detector
|
| 22 |
+
|
| 23 |
+
paddingBy = 0.15 # padding by 15%
|
| 24 |
+
|
| 25 |
+
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # convert to grayscale
|
| 26 |
+
|
| 27 |
+
detected = None
|
| 28 |
+
|
| 29 |
+
for cascade in cascades:
|
| 30 |
+
cascadeClassifier = cv.CascadeClassifier(cv.data.haarcascades + cascade)
|
| 31 |
+
faces = cascadeClassifier.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) # detect faces
|
| 32 |
+
if len(faces) > 0:
|
| 33 |
+
detected = faces[0]
|
| 34 |
+
logging.info(f"Face detected by {cascade}")
|
| 35 |
+
break
|
| 36 |
+
|
| 37 |
+
if detected is None:
|
| 38 |
+
faces = detector(gray) # detect faces
|
| 39 |
+
if len(faces) > 0:
|
| 40 |
+
detected = faces[0]
|
| 41 |
+
detected = (detected.left(), detected.top(), detected.width(), detected.height())
|
| 42 |
+
logging.info("Face detected by dlib")
|
| 43 |
+
|
| 44 |
+
if detected is None:
|
| 45 |
+
faces = mmod(img)
|
| 46 |
+
if len(faces) > 0:
|
| 47 |
+
detected = faces[0]
|
| 48 |
+
detected = (detected.rect.left(), detected.rect.top(), detected.rect.width(), detected.rect.height())
|
| 49 |
+
logging.info("Face detected by mmod")
|
| 50 |
+
|
| 51 |
+
if detected is not None: # if face detected
|
| 52 |
+
x, y, w, h = detected # grab first face
|
| 53 |
+
padW = int(paddingBy * w) # get padding width
|
| 54 |
+
padH = int(paddingBy * h) # get padding height
|
| 55 |
+
imgH, imgW, _ = img.shape # get image dims
|
| 56 |
+
x = max(0, x - padW)
|
| 57 |
+
y = max(0, y - padH)
|
| 58 |
+
w = min(imgW - x, w + 2 * padW)
|
| 59 |
+
h = min(imgH - y, h + 2 * padH)
|
| 60 |
+
x = max(0, x - (w - detected[2]) // 2) # center the face horizontally
|
| 61 |
+
y = max(0, y - (h - detected[3]) // 2) # center the face vertically
|
| 62 |
+
face = img[y:y+h, x:x+w] # crop face
|
| 63 |
+
return face
|
| 64 |
+
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
model = ViTForImageClassification.from_pretrained("ongkn/attraction-classifier")
|
| 68 |
+
processor = ViTImageProcessor.from_pretrained("ongkn/attraction-classifier")
|
| 69 |
+
|
| 70 |
+
pipe = pipeline("image-classification", model=model, feature_extractor=processor)
|
| 71 |
+
|
| 72 |
+
def classify_image(inp):
|
| 73 |
+
face = grab_faces(np.array(inp))
|
| 74 |
+
face = Image.fromarray(face)
|
| 75 |
+
result = pipe(face)
|
| 76 |
+
return result[0]["label"], result[0]["score"]
|
| 77 |
+
|
| 78 |
+
iface = gr.Interface(fn=classify_image, inputs="image", outputs=["text", "number"])
|
| 79 |
+
iface.launch()
|