Skip to content

Instantly share code, notes, and snippets.

@motoishmz
Last active October 13, 2021 03:33
Show Gist options
  • Save motoishmz/98d1f83b37acf371af06fa898624e253 to your computer and use it in GitHub Desktop.
Save motoishmz/98d1f83b37acf371af06fa898624e253 to your computer and use it in GitHub Desktop.
Paste it to ScriptCHOP
# me - this DAT
# scriptOp - the OP which is cooking
import mediapipe as mp
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
"""
https://google.github.io/mediapipe/solutions/face_detection.html
MODEL_SELECTION
An integer index 0 or 1. Use 0 to select a short-range model that works best for faces within 2 meters from the camera, and 1 for a full-range model best for faces within 5 meters. For the full-range option, a sparse model is used for its improved inference speed. Please refer to the model cards for details. Default to 0 if not specified.
MIN_DETECTION_CONFIDENCE
Minimum confidence value ([0.0, 1.0]) from the face detection model for the detection to be considered successful. Default to 0.5.
"""
face_detection = mp_face_detection.FaceDetection(
model_selection=0,
min_detection_confidence=0.5)
NUM_FACES_SLOTS = 2
def onCook(scriptOp):
scriptOp.clear()
nA = op('src').numpyArray()
nA = nA[:,:,:3] * 255 # rgba float --> rgb uint
nA = nA.astype('uint8')
results = face_detection.process(nA)
"""
The channel name and coordinate should be same to FaceTrackCHOP
https://docs.derivative.ca/Face_Track_CHOP
"""
for i in range(0, NUM_FACES_SLOTS):
index = i + 1
valid = scriptOp.appendChan('face{}:valid'.format(index))
width = scriptOp.appendChan('face{}/bbox:width'.format(index))
height = scriptOp.appendChan('face{}/bbox:height'.format(index))
u = scriptOp.appendChan('face{}/bbox:u'.format(index))
v = scriptOp.appendChan('face{}/bbox:v'.format(index))
if results.detections and i < len(results.detections):
detection = results.detections[i]
valid[0] = 1 # detection.score
width[0] = detection.location_data.relative_bounding_box.width
height[0] = detection.location_data.relative_bounding_box.height
u[0] = detection.location_data.relative_bounding_box.xmin + width[0]/2
v[0] = detection.location_data.relative_bounding_box.ymin + height[0]/2
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment