Skip to content

Commit

Permalink
torch control (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Buzeqq committed Jun 20, 2023
1 parent 5f66111 commit 98a9cf8
Show file tree
Hide file tree
Showing 6 changed files with 670 additions and 3 deletions.
512 changes: 512 additions & 0 deletions iOS/PolishBanknotesApp/PolishBanknotesApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1400"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "356516C0291C51F4006A460B"
BuildableName = "PolishBanknotesApp.app"
BlueprintName = "PolishBanknotesApp"
ReferencedContainer = "container:PolishBanknotesApp.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "356516C0291C51F4006A460B"
BuildableName = "PolishBanknotesApp.app"
BlueprintName = "PolishBanknotesApp"
ReferencedContainer = "container:PolishBanknotesApp.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "356516C0291C51F4006A460B"
BuildableName = "PolishBanknotesApp.app"
BlueprintName = "PolishBanknotesApp"
ReferencedContainer = "container:PolishBanknotesApp.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
44 changes: 44 additions & 0 deletions iOS/PolishBanknotesApp/PolishBanknotesApp/Torch/TorchManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// FlashlightManager.swift
// PolishBanknotesApp
//
// Created by Miłosz Chojnacki on 20/06/2023.
//

import Foundation
import AVFoundation
import UIKit

class TorchManager {
var isOn = false

func activateTorch(interval: TimeInterval) {
isOn = true
DispatchQueue.global().async {
self.toggleTorch(on: true)
Thread.sleep(forTimeInterval: interval)
self.toggleTorch(on: false)
}
isOn = false
}

func toggleTorch(on: Bool) {
guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return }
guard device.hasTorch else { print("Torch isn't available"); return }

do {
try device.lockForConfiguration()
device.torchMode = on ? .on : .off
if on { try device.setTorchModeOn(level: AVCaptureDevice.maxAvailableTorchLevel) }
device.unlockForConfiguration()

if on {
UIAccessibility.post(notification: .announcement, argument: "Flash turned on!")
} else {
UIAccessibility.post(notification: .announcement, argument: "Flash turned off!")
}
} catch {
print("Torch can't be used")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Foundation
import CoreImage
import Vision
import Combine
import AVFoundation
import UIKit

class ContentViewModel: ObservableObject {
@Published var error: Error?
Expand All @@ -19,10 +21,13 @@ class ContentViewModel: ObservableObject {

private let cameraManager = CameraManager.shared
private let frameManager = FrameManager.shared
private let torchManager = TorchManager()
private var frameCount = 0
private let classifier = Classifier.shared

private var framePredictionSubscription: AnyCancellable?;
private var framePredictionSubscription: AnyCancellable?

// fps ~= 30, predictionRate = 2 => ~ 15fps (every 2nd frame)
private let predictionRate = 5
private let predictionSize = 5

Expand All @@ -41,7 +46,6 @@ class ContentViewModel: ObservableObject {

framePredictionSubscription?.cancel()

// fps ~= 30, predictionRate = 2 => ~ 15fps (every 2nd frame)
framePredictionSubscription = predictionSubscription()
}

Expand Down Expand Up @@ -79,8 +83,20 @@ class ContentViewModel: ObservableObject {
.assign(to: &$error)

frameManager.$current
.receive(on: RunLoop.main)
.compactMap { buffer in
if (buffer != nil) {
let rawMetadata = CMCopyDictionaryOfAttachments(allocator: nil, target: buffer!, attachmentMode: CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))
let metadata = CFDictionaryCreateMutableCopy(nil, 0, rawMetadata) as NSMutableDictionary
let directory = metadata.value(forKey: "MetadataDictionary") as? NSMutableDictionary
let luxLevel = directory?.value(forKey: "LuxLevel") as? Int

if luxLevel != 0 && luxLevel ?? 21 < 20 {
if !self.torchManager.isOn {
self.torchManager.activateTorch(interval: 30)
}
}
}

return CGImage.create(from: buffer)
}
.assign(to: &$frame)
Expand All @@ -93,4 +109,6 @@ class ContentViewModel: ObservableObject {

self.framePredictionSubscription = self.predictionSubscription()
}


}

0 comments on commit 98a9cf8

Please sign in to comment.