mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Merge 0dc58f43fa into 8759db0b6a
This commit is contained in:
commit
c460ae5ac6
@ -44,6 +44,12 @@
|
|||||||
"alignment": 16,
|
"alignment": 16,
|
||||||
"optimize": "Performance"
|
"optimize": "Performance"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "tflite",
|
||||||
|
"path": "{TOP}/lib/models/face_landmarks_192.tflite",
|
||||||
|
"alignment": 16,
|
||||||
|
"optimize": "Performance"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "tflite",
|
"type": "tflite",
|
||||||
"path": "{TOP}/lib/models/palm_detection_full_192.tflite",
|
"path": "{TOP}/lib/models/palm_detection_full_192.tflite",
|
||||||
|
|||||||
@ -26,6 +26,12 @@
|
|||||||
"alignment": 32,
|
"alignment": 32,
|
||||||
"profile": "default"
|
"profile": "default"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "tflite",
|
||||||
|
"path": "{TOP}/lib/models/face_landmarks_192.tflite",
|
||||||
|
"alignment": 32,
|
||||||
|
"profile": "default"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "tflite",
|
"type": "tflite",
|
||||||
"path": "{TOP}/lib/models/palm_detection_full_192.tflite",
|
"path": "{TOP}/lib/models/palm_detection_full_192.tflite",
|
||||||
|
|||||||
BIN
lib/models/face_landmarks_192.tflite
Normal file
BIN
lib/models/face_landmarks_192.tflite
Normal file
Binary file not shown.
@ -0,0 +1,57 @@
|
|||||||
|
# This work is licensed under the MIT license.
|
||||||
|
# Copyright (c) 2013-2025 OpenMV LLC. All rights reserved.
|
||||||
|
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||||
|
#
|
||||||
|
# This example shows off Google's MediaPipe Face Landmark Detection model for multiple faces.
|
||||||
|
#
|
||||||
|
# NOTE: This exaxmple requires an OpenMV Cam with an NPU like the AE3 or N6 to run real-time.
|
||||||
|
|
||||||
|
import csi
|
||||||
|
import time
|
||||||
|
import ml
|
||||||
|
from ml.preprocessing import Normalization
|
||||||
|
from ml.postprocessing.mediapipe import BlazeFace
|
||||||
|
from ml.postprocessing.mediapipe import FaceLandmarks
|
||||||
|
|
||||||
|
# Initialize the sensor.
|
||||||
|
csi0 = csi.CSI()
|
||||||
|
csi0.reset()
|
||||||
|
csi0.pixformat(csi.RGB565)
|
||||||
|
csi0.framesize(csi.VGA)
|
||||||
|
csi0.window((400, 400))
|
||||||
|
|
||||||
|
# Load built-in face detection model
|
||||||
|
face_detection = ml.Model("/rom/blazeface_front_128.tflite", postprocess=BlazeFace(threshold=0.4))
|
||||||
|
print(face_detection)
|
||||||
|
|
||||||
|
# Load built-in face landmark model
|
||||||
|
face_landmarks = ml.Model("/rom/face_landmarks_192.tflite", postprocess=FaceLandmarks(threshold=0.4))
|
||||||
|
print(face_landmarks)
|
||||||
|
|
||||||
|
clock = time.clock()
|
||||||
|
while True:
|
||||||
|
clock.tick()
|
||||||
|
img = csi0.snapshot()
|
||||||
|
|
||||||
|
# faces is a list of ((x, y, w, h), score, keypoints) tuples
|
||||||
|
faces = face_detection.predict([img])
|
||||||
|
|
||||||
|
if faces:
|
||||||
|
for r, score, keypoints in faces[0]:
|
||||||
|
# rect is (x, y, w, h) - enlarge by 2x for face landmarks model
|
||||||
|
wider_rect = (r[0] - r[2] // 2, r[1] - r[3] // 2, r[2] * 2, r[3] * 2)
|
||||||
|
# Operate on just the ROI of the detected face
|
||||||
|
n = Normalization(roi=wider_rect)
|
||||||
|
|
||||||
|
# marks is a list of ((x, y, w, h), score, keypoints) tuples
|
||||||
|
marks = face_landmarks.predict([n(img)])
|
||||||
|
|
||||||
|
# Draw bounding boxes around the detected faces and keypoints.
|
||||||
|
for i, detections in enumerate(marks):
|
||||||
|
for r, score, keypoints in detections:
|
||||||
|
ml.utils.draw_predictions(img, [r], ("face",), ((0, 0, 255),), format=None)
|
||||||
|
|
||||||
|
# keypoints is a ndarray of shape (468, 3) where each keypoint is (x, y, z)
|
||||||
|
ml.utils.draw_keypoints(img, keypoints, radius=0, color=(255, 0, 0))
|
||||||
|
|
||||||
|
print(clock.fps(), "fps")
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
# This work is licensed under the MIT license.
|
||||||
|
# Copyright (c) 2013-2025 OpenMV LLC. All rights reserved.
|
||||||
|
# https://github.com/openmv/openmv/blob/master/LICENSE
|
||||||
|
#
|
||||||
|
# This example shows off Google's MediaPipe Face Landmark Detection model for a single face.
|
||||||
|
#
|
||||||
|
# NOTE: This exaxmple requires an OpenMV Cam with an NPU like the AE3 or N6 to run real-time.
|
||||||
|
|
||||||
|
import csi
|
||||||
|
import time
|
||||||
|
import ml
|
||||||
|
from ml.preprocessing import Normalization
|
||||||
|
from ml.postprocessing.mediapipe import BlazeFace
|
||||||
|
from ml.postprocessing.mediapipe import FaceLandmarks
|
||||||
|
|
||||||
|
# Initialize the sensor.
|
||||||
|
csi0 = csi.CSI()
|
||||||
|
csi0.reset()
|
||||||
|
csi0.pixformat(csi.RGB565)
|
||||||
|
csi0.framesize(csi.VGA)
|
||||||
|
csi0.window((400, 400))
|
||||||
|
|
||||||
|
# Load built-in face detection model
|
||||||
|
face_detection = ml.Model("/rom/blazeface_front_128.tflite", postprocess=BlazeFace(threshold=0.4))
|
||||||
|
print(face_detection)
|
||||||
|
|
||||||
|
# Load built-in face landmark model
|
||||||
|
face_landmarks = ml.Model("/rom/face_landmarks_192.tflite", postprocess=FaceLandmarks(threshold=0.4))
|
||||||
|
print(face_landmarks)
|
||||||
|
|
||||||
|
# Tracking vars.
|
||||||
|
n = None
|
||||||
|
|
||||||
|
clock = time.clock()
|
||||||
|
while True:
|
||||||
|
clock.tick()
|
||||||
|
img = csi0.snapshot()
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
# faces is a list of ((x, y, w, h), score, keypoints) tuples
|
||||||
|
faces = face_detection.predict([img])
|
||||||
|
|
||||||
|
if faces:
|
||||||
|
for r, score, keypoints in faces[0]:
|
||||||
|
# rect is (x, y, w, h) - enlarge by 2x for face landmarks model
|
||||||
|
wider_rect = (r[0] - r[2] // 2, r[1] - r[3] // 2, r[2] * 2, r[3] * 2)
|
||||||
|
# Operate on just the ROI of the detected face
|
||||||
|
n = Normalization(roi=wider_rect)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# marks is a list of ((x, y, w, h), score, keypoints) tuples
|
||||||
|
marks = face_landmarks.predict([n(img)])
|
||||||
|
|
||||||
|
# No faces detected, reset the tracker.
|
||||||
|
if not marks:
|
||||||
|
n = None
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Draw bounding boxes around the detected faces and keypoints.
|
||||||
|
for i, detections in enumerate(marks):
|
||||||
|
for r, score, keypoints in detections:
|
||||||
|
ml.utils.draw_predictions(img, [r], ("face",), ((0, 0, 255),), format=None)
|
||||||
|
|
||||||
|
# keypoints is a ndarray of shape (468, 3) where each keypoint is (x, y, z)
|
||||||
|
ml.utils.draw_keypoints(img, keypoints, radius=0, color=(255, 0, 0))
|
||||||
|
|
||||||
|
# Center new_wider_rect on face for tracking
|
||||||
|
new_wider_rect = (r[0] + (r[2] // 2) - (wider_rect[2] // 2),
|
||||||
|
r[1] + (r[3] // 2) - (wider_rect[3] // 2),
|
||||||
|
wider_rect[2],
|
||||||
|
wider_rect[3])
|
||||||
|
# Operate on just the ROI of the detected face
|
||||||
|
n = Normalization(roi=new_wider_rect)
|
||||||
|
|
||||||
|
print(clock.fps(), "fps")
|
||||||
@ -173,3 +173,35 @@ class HandLandmarks:
|
|||||||
|
|
||||||
nms.add_bounding_box(xmin, ymin, xmax, ymax, score, left_right, keypoints=keypoints)
|
nms.add_bounding_box(xmin, ymin, xmax, ymax, score, left_right, keypoints=keypoints)
|
||||||
return nms.get_bounding_boxes(threshold=self.nms_threshold, sigma=self.nms_sigma)
|
return nms.get_bounding_boxes(threshold=self.nms_threshold, sigma=self.nms_sigma)
|
||||||
|
|
||||||
|
|
||||||
|
class FaceLandmarks:
|
||||||
|
def __init__(self, threshold=0.6, nms_threshold=0.1, nms_sigma=0.1):
|
||||||
|
self.threshold = threshold
|
||||||
|
self.nms_threshold = nms_threshold
|
||||||
|
self.nms_sigma = nms_sigma
|
||||||
|
|
||||||
|
def __call__(self, model, inputs, outputs):
|
||||||
|
ib, ih, iw, ic = model.input_shape[0]
|
||||||
|
nms = NMS(iw, ih, inputs[0].roi)
|
||||||
|
|
||||||
|
score = sigmoid(outputs[1][0, 0, 0, 0])
|
||||||
|
if score < self.threshold:
|
||||||
|
return _NO_DETECTION
|
||||||
|
|
||||||
|
cords = outputs[0][0, 0, 0, :]
|
||||||
|
|
||||||
|
# Get the keypoint information
|
||||||
|
keypoints = np.empty((len(cords) // 3, 3))
|
||||||
|
keypoints[:, 0] = cords[0::3]
|
||||||
|
keypoints[:, 1] = cords[1::3]
|
||||||
|
keypoints[:, 2] = cords[2::3]
|
||||||
|
|
||||||
|
# Get bounding box information
|
||||||
|
xmin = np.min(keypoints[:, 0])
|
||||||
|
ymin = np.min(keypoints[:, 1])
|
||||||
|
xmax = np.max(keypoints[:, 0])
|
||||||
|
ymax = np.max(keypoints[:, 1])
|
||||||
|
|
||||||
|
nms.add_bounding_box(xmin, ymin, xmax, ymax, score, 0, keypoints=keypoints)
|
||||||
|
return nms.get_bounding_boxes(threshold=self.nms_threshold, sigma=self.nms_sigma)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user