Skip to content

Commit

Permalink
feat(example): enable to switch face detection models
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Jul 11, 2021
1 parent e31b79d commit 58084e8
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ MonoBehaviour:
gpuConfig: {fileID: 4900000, guid: ea3b02438eeb2743c8ca34c733a1ab8a, type: 3}
cpuConfig: {fileID: 4900000, guid: 51972370b918e54f78553d2d36c8e40a, type: 3}
androidConfig: {fileID: 4900000, guid: 754b23d93f14744ee82f1115e5afdb37, type: 3}
modelType: 0
--- !u!114 &5680418270291803702
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,21 @@ node: {
node {
calculator: "FaceDetectionShortRangeGpu"
input_stream: "IMAGE:transformed_input_video"
output_stream: "DETECTIONS:face_detections"
output_stream: "DETECTIONS:short_range_detections"
}

node {
calculator: "FaceDetectionFullRangeGpu"
input_stream: "IMAGE:transformed_input_video"
output_stream: "DETECTIONS:full_range_detections"
}

node {
calculator: "SwitchMuxCalculator"
input_side_packet: "SELECT:model_type"
input_stream: "C0__FUNC_INPUT:short_range_detections"
input_stream: "C1__FUNC_INPUT:full_range_detections"
output_stream: "FUNC_INPUT:face_detections"
}

node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#
# CHANGES:
# - Add ImageTransformationCalculator and rotate the input
# - Switch models
# - Remove AnnotationOverlayCalculator
# - Add PacketPresenceCalculator

Expand Down Expand Up @@ -62,7 +63,21 @@ node: {
node {
calculator: "FaceDetectionShortRangeCpu"
input_stream: "IMAGE:transformed_input_video"
output_stream: "DETECTIONS:face_detections"
output_stream: "DETECTIONS:short_range_detections"
}

node {
calculator: "FaceDetectionFullRangeCpu"
input_stream: "IMAGE:transformed_input_video"
output_stream: "DETECTIONS:full_range_detections"
}

node {
calculator: "SwitchMuxCalculator"
input_side_packet: "SELECT:model_type"
input_stream: "C0__FUNC_INPUT:short_range_detections"
input_stream: "C1__FUNC_INPUT:full_range_detections"
output_stream: "FUNC_INPUT:face_detections"
}

node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# CHANGES:
# - `input_video` is ImageFrame (ImageFrameToGpuBufferCalculator converts it into GpuBuffer)
# - Add ImageTransformationCalculator and rotate the input
# - Switch models
# - Remove AnnotationOverlayCalculator
# - Add PacketPresenceCalculator

Expand Down Expand Up @@ -69,7 +70,21 @@ node: {
node {
calculator: "FaceDetectionShortRangeGpu"
input_stream: "IMAGE:transformed_input_video"
output_stream: "DETECTIONS:face_detections"
output_stream: "DETECTIONS:short_range_detections"
}

node {
calculator: "FaceDetectionFullRangeGpu"
input_stream: "IMAGE:transformed_input_video"
output_stream: "DETECTIONS:full_range_detections"
}

node {
calculator: "SwitchMuxCalculator"
input_side_packet: "SELECT:model_type"
input_stream: "C0__FUNC_INPUT:short_range_detections"
input_stream: "C1__FUNC_INPUT:full_range_detections"
output_stream: "FUNC_INPUT:face_detections"
}

node {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
using Mediapipe;
using System.Collections.Generic;
using UnityEngine;

public class FaceDetectionGraph : DemoGraph {
enum ModelType {
ShortRange = 0,
FullRangeSparse = 1,
}

[SerializeField] ModelType modelType = ModelType.ShortRange;

private const string faceDetectionsStream = "face_detections";
private OutputStreamPoller<List<Detection>> faceDetectionsStreamPoller;
private DetectionVectorPacket faceDetectionsPacket;
Expand All @@ -10,14 +18,19 @@ public class FaceDetectionGraph : DemoGraph {
private OutputStreamPoller<bool> faceDetectionsPresenceStreamPoller;
private BoolPacket faceDetectionsPresencePacket;

private SidePacket sidePacket;

public override Status StartRun() {
faceDetectionsStreamPoller = graph.AddOutputStreamPoller<List<Detection>>(faceDetectionsStream).Value();
faceDetectionsPacket = new DetectionVectorPacket();

faceDetectionsPresenceStreamPoller = graph.AddOutputStreamPoller<bool>(faceDetectionsPresenceStream).Value();
faceDetectionsPresencePacket = new BoolPacket();

return graph.StartRun();
sidePacket = new SidePacket();
sidePacket.Emplace("model_type", new IntPacket((int)modelType));

return graph.StartRun(sidePacket);
}

public override void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame) {
Expand All @@ -42,5 +55,6 @@ public class FaceDetectionGraph : DemoGraph {

protected override void PrepareDependentAssets() {
PrepareDependentAsset("face_detection_short_range.bytes");
PrepareDependentAsset("face_detection_full_range_sparse.bytes");
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion mediapipe_api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ cc_library(
"@com_google_mediapipe//mediapipe/gpu:disable_gpu": [
"//mediapipe_api/modules/iris_landmark:iris_landmarks_from_face_landmarks_cpu",
"@com_google_mediapipe//mediapipe/graphs/face_detection:desktop_live_calculators",
"@com_google_mediapipe//mediapipe/graphs/face_detection:face_detection_full_range_desktop_live_deps",
"@com_google_mediapipe//mediapipe/graphs/face_mesh:desktop_live_calculators",
"@com_google_mediapipe//mediapipe/graphs/hand_tracking:desktop_tflite_calculators",
"@com_google_mediapipe//mediapipe/graphs/holistic_tracking:holistic_tracking_cpu_graph_deps",
Expand All @@ -106,6 +107,7 @@ cc_library(
"@com_google_mediapipe//mediapipe/gpu:gl_scaler_calculator",
"@com_google_mediapipe//mediapipe/gpu:image_frame_to_gpu_buffer_calculator",
"@com_google_mediapipe//mediapipe/graphs/face_detection:desktop_live_gpu_calculators",
"@com_google_mediapipe//mediapipe/graphs/face_detection:face_detection_full_range_mobile_gpu_deps",
"@com_google_mediapipe//mediapipe/graphs/face_mesh:desktop_live_gpu_calculators",
"@com_google_mediapipe//mediapipe/graphs/hair_segmentation:mobile_calculators",
"@com_google_mediapipe//mediapipe/graphs/hand_tracking:mobile_calculators",
Expand Down Expand Up @@ -144,7 +146,7 @@ pkg_asset(
"@com_google_mediapipe//mediapipe/models:hair_segmentation.tflite",
"@com_google_mediapipe//mediapipe/models:ssdlite_object_detection.tflite",
"@com_google_mediapipe//mediapipe/models:ssdlite_object_detection_labelmap.txt",
# "@com_google_mediapipe//mediapipe/modules/face_detection:face_detection_full_range.tflite",
"@com_google_mediapipe//mediapipe/modules/face_detection:face_detection_full_range.tflite",
"@com_google_mediapipe//mediapipe/modules/face_detection:face_detection_full_range_sparse.tflite",
"@com_google_mediapipe//mediapipe/modules/face_detection:face_detection_short_range.tflite",
"@com_google_mediapipe//mediapipe/modules/face_landmark:face_landmark.tflite",
Expand Down

0 comments on commit 58084e8

Please sign in to comment.