Spaces:
Sleeping
Sleeping
Create model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torchvision.transforms import functional as F
|
| 6 |
+
from torchvision.models.detection import (
|
| 7 |
+
fasterrcnn_resnet50_fpn_v2,
|
| 8 |
+
keypointrcnn_resnet50_fpn,
|
| 9 |
+
)
|
| 10 |
+
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# BUILD MODELS
|
| 18 |
+
# -----------------------------
|
| 19 |
+
def build_face_detector(num_classes=2):
|
| 20 |
+
model = fasterrcnn_resnet50_fpn_v2(weights=None)
|
| 21 |
+
in_features = model.roi_heads.box_predictor.cls_score.in_features
|
| 22 |
+
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes)
|
| 23 |
+
return model
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def build_landmark_model(num_classes=2, num_keypoints=13):
|
| 27 |
+
model = keypointrcnn_resnet50_fpn(
|
| 28 |
+
weights=None,
|
| 29 |
+
weights_backbone=None,
|
| 30 |
+
num_classes=num_classes,
|
| 31 |
+
num_keypoints=num_keypoints,
|
| 32 |
+
)
|
| 33 |
+
return model
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# -----------------------------
|
| 37 |
+
# LOAD WEIGHTS
|
| 38 |
+
# -----------------------------
|
| 39 |
+
def load_models(face_path, landmark_path, num_keypoints):
|
| 40 |
+
face_model = build_face_detector()
|
| 41 |
+
landmark_model = build_landmark_model(num_keypoints=num_keypoints)
|
| 42 |
+
|
| 43 |
+
face_model.load_state_dict(torch.load(face_path, map_location=DEVICE))
|
| 44 |
+
landmark_model.load_state_dict(torch.load(landmark_path, map_location=DEVICE))
|
| 45 |
+
|
| 46 |
+
face_model.to(DEVICE).eval()
|
| 47 |
+
landmark_model.to(DEVICE).eval()
|
| 48 |
+
|
| 49 |
+
return face_model, landmark_model
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# -----------------------------
|
| 53 |
+
# CASCADE INFERENCE
|
| 54 |
+
# -----------------------------
|
| 55 |
+
def run_inference(image, face_model, landmark_model,
|
| 56 |
+
face_score_thr=0.5,
|
| 57 |
+
kpt_score_thr=0.2):
|
| 58 |
+
|
| 59 |
+
image_pil = Image.fromarray(image).convert("RGB")
|
| 60 |
+
img_rgb = np.array(image_pil)
|
| 61 |
+
img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
|
| 62 |
+
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
det_out = face_model([F.to_tensor(image_pil).to(DEVICE)])[0]
|
| 65 |
+
|
| 66 |
+
boxes = det_out["boxes"].cpu().numpy()
|
| 67 |
+
scores = det_out["scores"].cpu().numpy()
|
| 68 |
+
|
| 69 |
+
keep = np.where(scores >= face_score_thr)[0]
|
| 70 |
+
|
| 71 |
+
for i in keep:
|
| 72 |
+
x1, y1, x2, y2 = boxes[i].astype(int)
|
| 73 |
+
|
| 74 |
+
cv2.rectangle(img_bgr, (x1, y1), (x2, y2), (255, 180, 0), 2)
|
| 75 |
+
|
| 76 |
+
crop = img_rgb[y1:y2, x1:x2]
|
| 77 |
+
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
kp_out = landmark_model([F.to_tensor(crop).to(DEVICE)])[0]
|
| 80 |
+
|
| 81 |
+
if len(kp_out["scores"]) == 0:
|
| 82 |
+
continue
|
| 83 |
+
|
| 84 |
+
best = torch.argmax(kp_out["scores"]).item()
|
| 85 |
+
if kp_out["scores"][best] < kpt_score_thr:
|
| 86 |
+
continue
|
| 87 |
+
|
| 88 |
+
keypoints = kp_out["keypoints"][best].cpu().numpy()
|
| 89 |
+
|
| 90 |
+
for kx, ky, kv in keypoints:
|
| 91 |
+
if kv > 0:
|
| 92 |
+
cv2.circle(img_bgr,
|
| 93 |
+
(int(kx + x1), int(ky + y1)),
|
| 94 |
+
2,
|
| 95 |
+
(0, 255, 0),
|
| 96 |
+
-1)
|
| 97 |
+
|
| 98 |
+
return cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
|