added change model complexity support
This commit is contained in:
parent
003df4fe47
commit
80815435ce
|
@ -13,10 +13,6 @@ swift_library(
|
||||||
name = "lindera_app_lib",
|
name = "lindera_app_lib",
|
||||||
srcs = glob(["**/*.swift"]),
|
srcs = glob(["**/*.swift"]),
|
||||||
data =[
|
data =[
|
||||||
"//mediapipe/graphs/pose_tracking:pose_tracking_gpu.binarypb",
|
|
||||||
"//mediapipe/modules/pose_detection:pose_detection.tflite",
|
|
||||||
"//mediapipe/modules/pose_landmark:pose_landmark_full.tflite",
|
|
||||||
] + [
|
|
||||||
"Base.lproj/LaunchScreen.storyboard",
|
"Base.lproj/LaunchScreen.storyboard",
|
||||||
"Base.lproj/Main.storyboard",
|
"Base.lproj/Main.storyboard",
|
||||||
],
|
],
|
||||||
|
|
|
@ -34,6 +34,9 @@
|
||||||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
|
||||||
<state key="normal" title="Button"/>
|
<state key="normal" title="Button"/>
|
||||||
<buttonConfiguration key="configuration" style="plain" title="MODEL (LITE)"/>
|
<buttonConfiguration key="configuration" style="plain" title="MODEL (LITE)"/>
|
||||||
|
<connections>
|
||||||
|
<action selector="setModelComplexity" destination="BYZ-38-t0r" eventType="touchDown" id="cVM-E4-dua"/>
|
||||||
|
</connections>
|
||||||
</button>
|
</button>
|
||||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text=" Copper Labs" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="UGR-Ho-hUQ">
|
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text=" Copper Labs" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="UGR-Ho-hUQ">
|
||||||
<rect key="frame" x="8" y="41" width="398" height="37"/>
|
<rect key="frame" x="8" y="41" width="398" height="37"/>
|
||||||
|
|
|
@ -24,8 +24,60 @@ class ViewController: UIViewController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateModelButtonText(){
|
|
||||||
|
|
||||||
|
func updateModelButtonText(){
|
||||||
|
var text = "MODEL "
|
||||||
|
switch(lindera.getModelComplexity()){
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
text += "(LITE)"
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
text += "(FULL)"
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
text += "(HEAVY)"
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
text += "(Unknown)"
|
||||||
|
}
|
||||||
|
chooseModelButton.setTitle(text, for: UIControl.State.normal)
|
||||||
|
}
|
||||||
|
|
||||||
|
@IBAction func setModelComplexity(){
|
||||||
|
let alert = UIAlertController(
|
||||||
|
title: nil,
|
||||||
|
message: nil,
|
||||||
|
preferredStyle: .actionSheet
|
||||||
|
)
|
||||||
|
|
||||||
|
alert.addAction(
|
||||||
|
.init(title: "MODEL (LITE)", style: .default) {[weak self] _ in
|
||||||
|
self?.lindera.setModelComplexityNow(complexity: 0)
|
||||||
|
self?.updateModelButtonText()
|
||||||
|
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
alert.addAction(
|
||||||
|
.init(title: "MODEL (FULL)", style: .default) { [weak self] _ in
|
||||||
|
self?.lindera.setModelComplexityNow(complexity: 1)
|
||||||
|
self?.updateModelButtonText()
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
)
|
||||||
|
alert.addAction(
|
||||||
|
.init(title: "MODEL (HEAVY)", style: .default) { [weak self] _ in
|
||||||
|
self?.lindera.setModelComplexityNow(complexity: 2)
|
||||||
|
self?.updateModelButtonText()
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
present(alert, animated: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func showLandmarksButtonTouch(sender: UIButton){
|
@IBAction func showLandmarksButtonTouch(sender: UIButton){
|
||||||
|
@ -33,25 +85,7 @@ class ViewController: UIViewController {
|
||||||
lindera.showLandmarks(value: !lindera.areLandmarksShown());
|
lindera.showLandmarks(value: !lindera.areLandmarksShown());
|
||||||
updateLandmarksButtonText()
|
updateLandmarksButtonText()
|
||||||
|
|
||||||
// let alert = UIAlertController(
|
|
||||||
// title: nil,
|
|
||||||
// message: nil,
|
|
||||||
// preferredStyle: .actionSheet
|
|
||||||
// )
|
|
||||||
//
|
|
||||||
// alert.addAction(
|
|
||||||
// .init(title: "Action 1", style: .default) { _ in
|
|
||||||
// print("Action1")
|
|
||||||
// }
|
|
||||||
// )
|
|
||||||
//
|
|
||||||
// alert.addAction(
|
|
||||||
// .init(title: "Action 2", style: .default) { _ in
|
|
||||||
// print("Action 2")
|
|
||||||
// }
|
|
||||||
// )
|
|
||||||
//
|
|
||||||
// present(alert, animated: true)
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -100,7 +134,7 @@ class ViewController: UIViewController {
|
||||||
|
|
||||||
self.liveView.bringSubviewToFront(titleview)
|
self.liveView.bringSubviewToFront(titleview)
|
||||||
updateLandmarksButtonText()
|
updateLandmarksButtonText()
|
||||||
// self.liveView.bringSubviewToFront(chooseModelButton)
|
updateModelButtonText()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,8 @@ node {
|
||||||
node {
|
node {
|
||||||
calculator: "PoseLandmarkGpu"
|
calculator: "PoseLandmarkGpu"
|
||||||
input_side_packet: "ENABLE_SEGMENTATION:enable_segmentation"
|
input_side_packet: "ENABLE_SEGMENTATION:enable_segmentation"
|
||||||
|
input_side_packet: "MODEL_COMPLEXITY:model_complexity"
|
||||||
|
|
||||||
input_stream: "IMAGE:throttled_input_video"
|
input_stream: "IMAGE:throttled_input_video"
|
||||||
output_stream: "LANDMARKS:pose_landmarks"
|
output_stream: "LANDMARKS:pose_landmarks"
|
||||||
output_stream: "SEGMENTATION_MASK:segmentation_mask"
|
output_stream: "SEGMENTATION_MASK:segmentation_mask"
|
||||||
|
|
|
@ -13,6 +13,12 @@ objc_library(
|
||||||
copts = [
|
copts = [
|
||||||
"-Wno-shorten-64-to-32",
|
"-Wno-shorten-64-to-32",
|
||||||
],
|
],
|
||||||
|
data = [
|
||||||
|
"//mediapipe/graphs/pose_tracking:pose_tracking_gpu.binarypb",
|
||||||
|
"//mediapipe/modules/pose_detection:pose_detection.tflite",
|
||||||
|
"//mediapipe/modules/pose_landmark:pose_landmark_heavy.tflite",
|
||||||
|
"//mediapipe/modules/pose_landmark:pose_landmark_full.tflite",
|
||||||
|
"//mediapipe/modules/pose_landmark:pose_landmark_lite.tflite", ] ,
|
||||||
sdk_frameworks = ["Accelerate"],
|
sdk_frameworks = ["Accelerate"],
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
|
|
|
@ -47,6 +47,7 @@
|
||||||
- (void) startWithCamera: (MPPCameraInputSource*) cameraSource;
|
- (void) startWithCamera: (MPPCameraInputSource*) cameraSource;
|
||||||
- (void)showLandmarks: (BOOL) value;
|
- (void)showLandmarks: (BOOL) value;
|
||||||
- (BOOL) areLandmarksShown;
|
- (BOOL) areLandmarksShown;
|
||||||
|
- (void) stopGraph;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#include "mediapipe/framework/formats/landmark.pb.h"
|
#include "mediapipe/framework/formats/landmark.pb.h"
|
||||||
#import "mediapipe/objc/MPPGraph.h"
|
#import "mediapipe/objc/MPPGraph.h"
|
||||||
#import "mediapipe/objc/MPPTimestampConverter.h"
|
#import "mediapipe/objc/MPPTimestampConverter.h"
|
||||||
|
#include "mediapipe/framework/packet.h"
|
||||||
|
|
||||||
static const char* kVideoQueueLabel = "com.google.mediapipe.example.videoQueue";
|
static const char* kVideoQueueLabel = "com.google.mediapipe.example.videoQueue";
|
||||||
static const char* kLandmarksOutputStream = "pose_landmarks";
|
static const char* kLandmarksOutputStream = "pose_landmarks";
|
||||||
|
@ -162,6 +162,10 @@ static const char* kLandmarksOutputStream = "pose_landmarks";
|
||||||
|
|
||||||
weakSelf.poseTrackingResultsListener(results);
|
weakSelf.poseTrackingResultsListener(results);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
self->mediapipeGraph.delegate = self->poseTrackingGraphDelegate;
|
self->mediapipeGraph.delegate = self->poseTrackingGraphDelegate;
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,6 +188,7 @@ static const char* kLandmarksOutputStream = "pose_landmarks";
|
||||||
|
|
||||||
- (void)startGraph {
|
- (void)startGraph {
|
||||||
// Start running self.mediapipeGraph.
|
// Start running self.mediapipeGraph.
|
||||||
|
[self->mediapipeGraph setSidePacket:mediapipe::MakePacket<int>(self.poseTrackingOptions.modelComplexity) named:"model_complexity"];
|
||||||
NSError* error;
|
NSError* error;
|
||||||
if (![self->mediapipeGraph startWithError:&error]) {
|
if (![self->mediapipeGraph startWithError:&error]) {
|
||||||
NSLog(@"Failed to start graph: %@", error);
|
NSLog(@"Failed to start graph: %@", error);
|
||||||
|
@ -193,6 +198,20 @@ static const char* kLandmarksOutputStream = "pose_landmarks";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void) stopGraph {
|
||||||
|
[self->mediapipeGraph cancel];
|
||||||
|
NSError* error;
|
||||||
|
if ([self->mediapipeGraph closeAllInputStreamsWithError: &error]){
|
||||||
|
if (![self->mediapipeGraph waitUntilDoneWithError:&error]){
|
||||||
|
NSLog(@"Failed to stop graph: %@", error);
|
||||||
|
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
NSLog(@"Failed to close input streams: %@", error);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
- (void) startWithCamera: (MPPCameraInputSource*) cameraSource {
|
- (void) startWithCamera: (MPPCameraInputSource*) cameraSource {
|
||||||
[cameraSource setDelegate:self queue:self.videoQueue];
|
[cameraSource setDelegate:self queue:self.videoQueue];
|
||||||
|
|
||||||
|
|
|
@ -5,12 +5,16 @@
|
||||||
#ifndef MEDIAPIPE_POSETRACKINGOPTIONS_H
|
#ifndef MEDIAPIPE_POSETRACKINGOPTIONS_H
|
||||||
#define MEDIAPIPE_POSETRACKINGOPTIONS_H
|
#define MEDIAPIPE_POSETRACKINGOPTIONS_H
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
@interface PoseTrackingOptions: NSObject
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@interface PoseTrackingOptions: NSObject
|
||||||
|
@property(nonatomic) int modelComplexity;
|
||||||
@property(nonatomic) bool showLandmarks;
|
@property(nonatomic) bool showLandmarks;
|
||||||
//@property(nonatomic) int cameraRotation;
|
//@property(nonatomic) int cameraRotation;
|
||||||
|
|
||||||
- (instancetype) initWithShowLandmarks : (bool) showLandmarks;
|
|
||||||
|
- (instancetype) initWithShowLandmarks : (bool) showLandmarks modelComplexity: (int) modelComplexity;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
|
|
||||||
@implementation PoseTrackingOptions
|
@implementation PoseTrackingOptions
|
||||||
|
|
||||||
- (instancetype) initWithShowLandmarks : (bool) showLandmarks {
|
- (instancetype) initWithShowLandmarks : (bool) showLandmarks modelComplexity: (int) modelComplexity{
|
||||||
// self.cameraRotation = cameraRotation;
|
// self.cameraRotation = cameraRotation;
|
||||||
self.showLandmarks = showLandmarks;
|
self.showLandmarks = showLandmarks;
|
||||||
|
self.modelComplexity = modelComplexity;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import UIKit
|
||||||
/// TFLite models are also loaded when you initialize this class
|
/// TFLite models are also loaded when you initialize this class
|
||||||
public final class Lindera{
|
public final class Lindera{
|
||||||
// initalize the PoseTracking api and load models
|
// initalize the PoseTracking api and load models
|
||||||
let poseTracking:PoseTracking = PoseTracking(poseTrackingOptions: PoseTrackingOptions(showLandmarks: true))
|
var poseTracking:PoseTracking = PoseTracking(poseTrackingOptions: PoseTrackingOptions(showLandmarks: true,modelComplexity: 1))
|
||||||
// attach Mediapipe camera helper to our class
|
// attach Mediapipe camera helper to our class
|
||||||
let cameraSource = MPPCameraInputSource()
|
let cameraSource = MPPCameraInputSource()
|
||||||
|
|
||||||
|
@ -26,6 +26,24 @@ public final class Lindera{
|
||||||
public func areLandmarksShown() -> Bool{
|
public func areLandmarksShown() -> Bool{
|
||||||
return self.poseTracking.areLandmarksShown()
|
return self.poseTracking.areLandmarksShown()
|
||||||
}
|
}
|
||||||
|
public func getModelComplexity() -> Int {
|
||||||
|
return Int(self.poseTracking.poseTrackingOptions.modelComplexity);
|
||||||
|
}
|
||||||
|
public func setModelComplexityNow(complexity:Int){
|
||||||
|
let poseTrackingOptions = poseTracking.poseTrackingOptions
|
||||||
|
|
||||||
|
poseTrackingOptions?.modelComplexity = Int32(complexity)
|
||||||
|
|
||||||
|
poseTracking = PoseTracking(poseTrackingOptions: poseTrackingOptions)
|
||||||
|
startPoseTracking()
|
||||||
|
startCamera()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// public func getModelComplexity() -> Int{
|
// public func getModelComplexity() -> Int{
|
||||||
// return self.poseTracking
|
// return self.poseTracking
|
||||||
// }
|
// }
|
||||||
|
@ -35,6 +53,14 @@ public final class Lindera{
|
||||||
// this will be the main camera view
|
// this will be the main camera view
|
||||||
let liveView = UIView()
|
let liveView = UIView()
|
||||||
|
|
||||||
|
startPoseTracking()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return liveView
|
||||||
|
|
||||||
|
}()
|
||||||
|
private func startPoseTracking(){
|
||||||
// set camera preferences
|
// set camera preferences
|
||||||
self.cameraSource.sessionPreset = AVCaptureSession.Preset.high.rawValue
|
self.cameraSource.sessionPreset = AVCaptureSession.Preset.high.rawValue
|
||||||
self.cameraSource.cameraPosition = AVCaptureDevice.Position.front
|
self.cameraSource.cameraPosition = AVCaptureDevice.Position.front
|
||||||
|
@ -53,16 +79,11 @@ public final class Lindera{
|
||||||
self.poseTracking.startGraph()
|
self.poseTracking.startGraph()
|
||||||
// attach camera's output with poseTracking object and its videoQueue
|
// attach camera's output with poseTracking object and its videoQueue
|
||||||
self.cameraSource.setDelegate(self.poseTracking, queue: self.poseTracking.videoQueue)
|
self.cameraSource.setDelegate(self.poseTracking, queue: self.poseTracking.videoQueue)
|
||||||
|
}
|
||||||
return liveView
|
|
||||||
|
|
||||||
}()
|
|
||||||
|
|
||||||
public required init(){}
|
public required init(){}
|
||||||
|
|
||||||
|
|
||||||
public func startCamera(_ completion: ((Result<Void, Error>) -> Void)? = nil) {
|
public func startCamera(_ completion: ((Result<Void, Error>) -> Void)? = nil) {
|
||||||
if (!self.cameraSource.isRunning){
|
|
||||||
// set our rendering layer frame according to cameraView boundry
|
// set our rendering layer frame according to cameraView boundry
|
||||||
self.poseTracking.renderer.layer.frame = cameraView.layer.bounds
|
self.poseTracking.renderer.layer.frame = cameraView.layer.bounds
|
||||||
// attach render CALayer on cameraView to render output to
|
// attach render CALayer on cameraView to render output to
|
||||||
|
@ -84,7 +105,6 @@ public final class Lindera{
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,6 +118,7 @@ public final class Lindera{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// switches camera from front to back and vice versa
|
/// switches camera from front to back and vice versa
|
||||||
func switchCamera(_ completion: ((Result<Void, Error>) -> Void)? = nil) {
|
func switchCamera(_ completion: ((Result<Void, Error>) -> Void)? = nil) {
|
||||||
self.poseTracking.videoQueue.async { [weak self] in
|
self.poseTracking.videoQueue.async { [weak self] in
|
||||||
|
|
Loading…
Reference in New Issue
Block a user