Skip to content

Commit

Permalink
Add delegate to opject detector example (#354)
Browse files Browse the repository at this point in the history
* add delegate to opject detector example

* remove development team
  • Loading branch information
st-tuanmai authored Mar 25, 2024
1 parent 644b31e commit 2a712f8
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
9E41A7603AB8BE3B86C06DB9 /* Pods_ObjectDetector.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3FBCABF9DABD56A270978F4 /* Pods_ObjectDetector.framework */; };
AAF981DB2A80D27500C7121A /* CameraViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAF981DA2A80D27500C7121A /* CameraViewController.swift */; };
AAF981DD2A80D28B00C7121A /* MediaLibraryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAF981DC2A80D28B00C7121A /* MediaLibraryViewController.swift */; };
AAF981DF2A81330700C7121A /* InferenceConfigManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAF981DE2A81330700C7121A /* InferenceConfigManager.swift */; };
AAF981DF2A81330700C7121A /* InferenceConfigurationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAF981DE2A81330700C7121A /* InferenceConfigurationManager.swift */; };
AAF981E52A85156500C7121A /* DefaultConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAF981E42A85156500C7121A /* DefaultConstants.swift */; };
BF2B2D2F2A3C09E200589A11 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF2B2D2E2A3C09E200589A11 /* AppDelegate.swift */; };
BF2B2D332A3C09E200589A11 /* RootViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF2B2D322A3C09E200589A11 /* RootViewController.swift */; };
Expand Down Expand Up @@ -48,7 +48,7 @@
7FEC31BE2AA97EF20074D3D3 /* SceneDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
AAF981DA2A80D27500C7121A /* CameraViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraViewController.swift; sourceTree = "<group>"; };
AAF981DC2A80D28B00C7121A /* MediaLibraryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaLibraryViewController.swift; sourceTree = "<group>"; };
AAF981DE2A81330700C7121A /* InferenceConfigManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InferenceConfigManager.swift; sourceTree = "<group>"; };
AAF981DE2A81330700C7121A /* InferenceConfigurationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InferenceConfigurationManager.swift; sourceTree = "<group>"; };
AAF981E42A85156500C7121A /* DefaultConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultConstants.swift; sourceTree = "<group>"; };
BF2B2D2B2A3C09E200589A11 /* ObjectDetector.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ObjectDetector.app; sourceTree = BUILT_PRODUCTS_DIR; };
BF2B2D2E2A3C09E200589A11 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -146,7 +146,7 @@
isa = PBXGroup;
children = (
AAF981E42A85156500C7121A /* DefaultConstants.swift */,
AAF981DE2A81330700C7121A /* InferenceConfigManager.swift */,
AAF981DE2A81330700C7121A /* InferenceConfigurationManager.swift */,
);
path = Configs;
sourceTree = "<group>";
Expand Down Expand Up @@ -399,7 +399,7 @@
BF2B2D332A3C09E200589A11 /* RootViewController.swift in Sources */,
AAF981E52A85156500C7121A /* DefaultConstants.swift in Sources */,
BF2B2D6B2A3C0C3F00589A11 /* BottomSheetViewController.swift in Sources */,
AAF981DF2A81330700C7121A /* InferenceConfigManager.swift in Sources */,
AAF981DF2A81330700C7121A /* InferenceConfigurationManager.swift in Sources */,
BF2B2D642A3C0C2900589A11 /* CameraFeedService.swift in Sources */,
BF2B2D2F2A3C09E200589A11 /* AppDelegate.swift in Sources */,
7FEC31BF2AA97EF20074D3D3 /* SceneDelegate.swift in Sources */,
Expand Down Expand Up @@ -570,7 +570,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = H83UK2M7VU;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ObjectDetector/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "This app uses camera to detection the objects that appear in the camera feed.";
Expand Down Expand Up @@ -603,7 +603,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = H83UK2M7VU;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ObjectDetector/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "This app uses camera to detection the objects that appear in the camera feed.";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="l9g-3L-U89">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="l9g-3L-U89">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21679"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22684"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="UIMenu" message="Requires Xcode 11 or later." minToolsVersion="11.0" requiredIntegratedClassName="UICommandDiff"/>
Expand Down Expand Up @@ -125,6 +126,44 @@
<constraint firstAttribute="height" constant="1" id="Xlb-hr-NyK"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NzK-5c-MAF">
<rect key="frame" x="0.0" y="244" width="393" height="32"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Delegate" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="oms-G0-MyQ">
<rect key="frame" x="16" y="7.6666666666666572" width="58" height="17"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" showsMenuAsPrimaryAction="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="aFq-ri-Hr7">
<rect key="frame" x="353" y="0.0" width="24" height="32"/>
<color key="backgroundColor" red="0.0" green="0.49803921569999998" blue="0.5450980392" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="32" id="6Cg-ze-Ehv"/>
</constraints>
<color key="tintColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<menu key="menu" id="XtT-Ia-qgq">
<children>
<command title="item1" id="iou-2Y-r5k"/>
<command title="item2" id="yL3-YM-jDD"/>
</children>
</menu>
<buttonConfiguration key="configuration" style="plain"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="layer.cornerRadius" value="8"/>
</userDefinedRuntimeAttributes>
</button>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="aFq-ri-Hr7" secondAttribute="trailing" constant="16" id="5tr-dh-hhY"/>
<constraint firstItem="oms-G0-MyQ" firstAttribute="leading" secondItem="NzK-5c-MAF" secondAttribute="leading" constant="16" id="6o6-RK-RAx"/>
<constraint firstItem="aFq-ri-Hr7" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="oms-G0-MyQ" secondAttribute="trailing" id="eyT-Np-DPU"/>
<constraint firstItem="oms-G0-MyQ" firstAttribute="centerY" secondItem="aFq-ri-Hr7" secondAttribute="centerY" id="h06-XT-8Og"/>
<constraint firstAttribute="bottom" secondItem="aFq-ri-Hr7" secondAttribute="bottom" id="sO6-it-f3L"/>
<constraint firstItem="aFq-ri-Hr7" firstAttribute="top" secondItem="NzK-5c-MAF" secondAttribute="top" id="wKh-kV-Da9"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" red="0.99999600649999998" green="1" blue="1" alpha="0.79932968277945615" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
Expand All @@ -150,9 +189,12 @@
<constraint firstItem="s05-yK-gXh" firstAttribute="centerY" secondItem="0nH-Zx-p69" secondAttribute="centerY" id="i6B-0T-nRV"/>
<constraint firstItem="7MT-s0-Gcl" firstAttribute="leading" secondItem="WmQ-cb-lxE" secondAttribute="trailing" constant="4" id="mUj-pi-LYj"/>
<constraint firstItem="d2C-9O-82c" firstAttribute="top" secondItem="0Hl-aV-l7w" secondAttribute="bottom" constant="32" id="mcI-24-pk6"/>
<constraint firstItem="NzK-5c-MAF" firstAttribute="leading" secondItem="AOI-Gh-SLK" secondAttribute="leading" id="mts-qE-zDM"/>
<constraint firstItem="iaZ-s2-My0" firstAttribute="centerY" secondItem="0nH-Zx-p69" secondAttribute="centerY" id="o81-hG-imF"/>
<constraint firstItem="4Av-AR-vvS" firstAttribute="trailing" secondItem="7MT-s0-Gcl" secondAttribute="trailing" id="qti-tf-eCr"/>
<constraint firstAttribute="trailing" secondItem="NzK-5c-MAF" secondAttribute="trailing" id="r2s-3B-VDA"/>
<constraint firstItem="0Hl-aV-l7w" firstAttribute="top" secondItem="AOI-Gh-SLK" secondAttribute="top" constant="10" id="rl8-eu-Q06"/>
<constraint firstItem="NzK-5c-MAF" firstAttribute="top" secondItem="4Av-AR-vvS" secondAttribute="bottom" constant="12" id="uwB-Vx-sb2"/>
<constraint firstItem="w11-Wg-LB1" firstAttribute="leading" secondItem="r1c-bf-rQA" secondAttribute="leading" id="vOd-lp-h4v"/>
<constraint firstAttribute="trailing" secondItem="tZo-oH-ToE" secondAttribute="trailing" id="xSe-Ss-8WB"/>
<constraint firstItem="tZo-oH-ToE" firstAttribute="top" secondItem="AOI-Gh-SLK" secondAttribute="top" id="yoR-Hn-w9J"/>
Expand All @@ -173,6 +215,7 @@
<navigationItem key="navigationItem" id="BIS-1q-gQH"/>
<connections>
<outlet property="choseModelButton" destination="4Av-AR-vvS" id="lVc-y6-jA6"/>
<outlet property="delegateButton" destination="aFq-ri-Hr7" id="HhJ-dd-M00"/>
<outlet property="inferenceTimeLabel" destination="cRO-ho-FXh" id="tV1-AJ-pi9"/>
<outlet property="inferenceTimeNameLabel" destination="d2C-9O-82c" id="ZdG-9v-GaP"/>
<outlet property="maxResultLabel" destination="WmQ-cb-lxE" id="PIG-Gi-kpV"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Foundation
import UIKit
import MediaPipeTasksVision

// MARK: Define default constants
struct DefaultConstants {
Expand All @@ -33,6 +34,7 @@ struct DefaultConstants {
]
static let displayFont = UIFont.systemFont(ofSize: 14.0, weight: .medium)
static let model: Model = .efficientdetLite0
static let delegate: Delegate = .CPU
}

// MARK: Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@
// limitations under the License.

import Foundation
import MediaPipeTasksVision

/**
* Singleton storing the configs needed to initialize an MediaPipe Tasks object and run inference.
* Controllers can observe the `InferenceConfigManager.notificationName` for any changes made by the user.
*/
class InferenceConfigManager: NSObject {
class InferenceConfigurationManager: NSObject {
var model: Model = DefaultConstants.model {
didSet { postConfigChangedNotification() }
}

var delegate: Delegate = DefaultConstants.delegate {
didSet { postConfigChangedNotification() }
}

var maxResults: Int = DefaultConstants.maxResults {
didSet { postConfigChangedNotification() }
}

var scoreThreshold: Float = DefaultConstants.scoreThreshold {
didSet { postConfigChangedNotification() }
}
static let sharedInstance = InferenceConfigManager()

static let sharedInstance = InferenceConfigurationManager()

static let notificationName = Notification.Name.init(rawValue: "com.google.mediapipe.inferenceConfigChanged")

private func postConfigChangedNotification() {
NotificationCenter.default
.post(name: InferenceConfigManager.notificationName, object: nil)
.post(name: InferenceConfigurationManager.notificationName, object: nil)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ class ObjectDetectorService: NSObject {
private(set) var runningMode = RunningMode.image
private var maxResults = 3
private var scoreThreshold: Float = 0.5
var modelPath: String
private var modelPath: String
private var delegate: Delegate

// MARK: - Custom Initializer
private init?(model: Model, maxResults: Int, scoreThreshold: Float, runningMode:RunningMode) {
private init?(model: Model, maxResults: Int, scoreThreshold: Float, runningMode:RunningMode, delegate: Delegate) {
guard let modelPath = model.modelPath else {
return nil
}
self.modelPath = modelPath
self.maxResults = maxResults
self.scoreThreshold = scoreThreshold
self.runningMode = runningMode
self.delegate = delegate
super.init()

createObjectDetector()
Expand All @@ -68,6 +70,7 @@ class ObjectDetectorService: NSObject {
objectDetectorOptions.maxResults = self.maxResults
objectDetectorOptions.scoreThreshold = self.scoreThreshold
objectDetectorOptions.baseOptions.modelAssetPath = modelPath
objectDetectorOptions.baseOptions.delegate = delegate
if runningMode == .liveStream {
objectDetectorOptions.objectDetectorLiveStreamDelegate = self
}
Expand All @@ -83,12 +86,14 @@ class ObjectDetectorService: NSObject {
static func videoObjectDetectorService(
model: Model, maxResults: Int,
scoreThreshold: Float,
videoDelegate: ObjectDetectorServiceVideoDelegate?) -> ObjectDetectorService? {
videoDelegate: ObjectDetectorServiceVideoDelegate?,
delegate: Delegate) -> ObjectDetectorService? {
let objectDetectorService = ObjectDetectorService(
model: model,
maxResults: maxResults,
scoreThreshold: scoreThreshold,
runningMode: .video)
runningMode: .video,
delegate: delegate)
objectDetectorService?.videoDelegate = videoDelegate

return objectDetectorService
Expand All @@ -97,26 +102,30 @@ class ObjectDetectorService: NSObject {
static func liveStreamDetectorService(
model: Model, maxResults: Int,
scoreThreshold: Float,
liveStreamDelegate: ObjectDetectorServiceLiveStreamDelegate?) -> ObjectDetectorService? {
liveStreamDelegate: ObjectDetectorServiceLiveStreamDelegate?,
delegate: Delegate) -> ObjectDetectorService? {
let objectDetectorService = ObjectDetectorService(
model: model,
maxResults: maxResults,
scoreThreshold: scoreThreshold,
runningMode: .liveStream)
runningMode: .liveStream,
delegate: delegate)
objectDetectorService?.liveStreamDelegate = liveStreamDelegate

return objectDetectorService
}

static func stillImageDetectorService(
model: Model, maxResults: Int,
scoreThreshold: Float) -> ObjectDetectorService? {
scoreThreshold: Float,
delegate: Delegate) -> ObjectDetectorService? {
let objectDetectorService = ObjectDetectorService(
model: model,
maxResults: maxResults,
scoreThreshold: scoreThreshold,
runningMode: .image)

runningMode: .image,
delegate: delegate)

return objectDetectorService
}

Expand Down
Loading

0 comments on commit 2a712f8

Please sign in to comment.