-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewController.swift
670 lines (546 loc) · 30.6 KB
/
MainViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//
// MainViewController.swift
// Obserwator
//
// Created by Kamil Chmielewski on 21/04/2020.
// Copyright © 2020 Kamil Chmielewski. All rights reserved.
//
import AVFoundation
import UIKit
import Vision
class MainViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
private enum DetectorState {
case sleeping, detecting, tracking
}
// MARK: - Application Parameters
private let interfaceCoverViewAnimationDuration = 0.5
private let wasInformationViewShownKey = "wasInformationViewShown"
private let clockDateFormat = "HH:mm"
private let cameraAutoFocusSystem = AVCaptureDevice.Format.AutoFocusSystem.phaseDetection
private let cameraFocusMode = AVCaptureDevice.FocusMode.continuousAutoFocus
private let cameraFormatType = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
private let cameraFrameRate = 60
private let cameraMaximumVideoWidth = 1920
private let cameraStabilizationMode = AVCaptureVideoStabilizationMode.standard
private let objectDetectionConfidenceThreshold: Float = 0.6
private let objectDetectionMaximumAspectRatioDeviation: CGFloat = 0.2
private let objectDetectionMinimumHeight: CGFloat = 0.04
private let observationRectangleLineWidth: CGFloat = 2
private let observationRectangleLineDashPattern: [NSNumber] = [8, 6]
private let observationRectangleStrokeColor = UIColor.green.cgColor
private let observationRectangleFillColor = UIColor.clear.cgColor
private let objectTrackingConfidenceThreshold: Float = 0.6
private let objectTrackingEscapeMargin: CGFloat = 0.02
private let objectTrackingLevel = VNRequestTrackingLevel.fast
private let objectTrackingRevision = VNTrackObjectRequestRevision2
private let textRecognitionConfidenceThreshold: Float = 0.6
private let textRecognitionMinimumTextHeight: Float = 0.1
private let textRecognitionLevel = VNRequestTextRecognitionLevel.fast
private let textRecognitionRevision = VNRecognizeTextRequestRevision1
private let textRecognitionUsesLanguageCorrection = true
private let maximumTextRecognitionAttempts = 10
private let forbiddenTextRecognitionResultCharacters = [",", "m", "t"]
private let possibleTextRecognitionResultCharacterConfusions = [
"0": ["c", "C", "o", "O", "Q", "U", "()", "(", ")"],
"1": ["i", "I", "l", "!"],
"2": ["z", "Z"],
"5": ["s", "S", "$"],
"6": ["b", "G"],
"8": ["B"],
"9": ["q"]
]
private let unambiguousTextRecognitionResultCorrections = [
"4": "40",
"6": "60",
"7": "70",
"8": "80",
"9": "90"
]
private let otherTextRecognitionResultCorrections = [
{ (text: String) -> String in
text.contains("00") ? "100" : text
}
]
private let validTextRecognitionResults = ["5", "10", "15", "20", "25", "30", "40", "50", "60", "70", "80", "90", "100", "110", "120", "130"]
private let sleepDuration = 3
private let speedLimitValidityDuration = 180
// MARK: - Interface Builder Outlets
@IBOutlet private var cameraView: UIView!
@IBOutlet private var interfaceCover: UIView!
@IBOutlet private var cameraAccessDenialView: UIScrollView!
@IBOutlet private var speedLimitSign: SpeedLimitView!
@IBOutlet private var blurEffectView: UIVisualEffectView!
@IBOutlet private var informationBar: UIVisualEffectView!
@IBOutlet private var informationBarLeadingConstraint: NSLayoutConstraint!
@IBOutlet private var clock: UILabel!
// MARK: - Application Variables
private var dateFormatter: DateFormatter!
private var captureSession: AVCaptureSession!
private var cameraPreviewLayer: AVCaptureVideoPreviewLayer!
private var objectObservationLayer: CALayer!
private var pixelBufferSize: CGSize!
private var videoOrientation = AVCaptureVideoOrientation.landscapeRight
private var imageOrientation = CGImagePropertyOrientation.up
private var isCameraReady = false
private var detectorState = DetectorState.sleeping
private var currentPixelBuffer: CVImageBuffer!
private var objectDetectionRequest: VNCoreMLRequest!
private var objectObservation: VNDetectedObjectObservation!
private var objectTrackingRequest: VNTrackObjectRequest!
private var objectTrackingRequestHandler: VNSequenceRequestHandler!
private var textRecognitionRequest: VNRecognizeTextRequest!
private var textRecognitionAttemptCounter = 0
private var upcomingSpeedLimit: String?
private var currentSpeedLimitCancelationTask: DispatchWorkItem?
// MARK: - UIViewController Overrides
override func viewDidLoad() {
super.viewDidLoad()
// Configure the clock.
dateFormatter = DateFormatter()
dateFormatter.dateFormat = clockDateFormat
dateFormatter.timeZone = .autoupdatingCurrent
clock.text = dateFormatter.string(from: Date())
RunLoop.current.add(Timer(timeInterval: 1, repeats: true) { _ in
self.clock.text = self.dateFormatter.string(from: Date())
}, forMode: .common)
// Prevent the screen from sleeping.
UIApplication.shared.isIdleTimerDisabled = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Check the camera access authorization status and either show a denial view or start the main application tasks.
checkCameraAccessAuthorizationStatus()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Handle a device orientation change.
updateOrientationData()
cameraPreviewLayer?.connection?.videoOrientation = videoOrientation
}
// MARK: - Configuration
private func checkCameraAccessAuthorizationStatus() {
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
self.configureCameraFeed()
self.configureObjectDetection()
self.configureTextRecognition()
DispatchQueue.main.async {
self.startCameraFeed()
// Hide an interface cover and begin object detection.
UIView.animate(withDuration: self.interfaceCoverViewAnimationDuration, animations: {
self.interfaceCover.layer.opacity = 0
}, completion: { _ in
self.interfaceCover.isHidden = true
// If it is the first time the application is being launched successfully, show the information view.
if self.presentedViewController == nil {
if !UserDefaults.standard.bool(forKey: self.wasInformationViewShownKey) {
UserDefaults.standard.setValue(true, forKey: self.wasInformationViewShownKey)
self.performSegue(withIdentifier: "showInformationView", sender: nil)
} else {
self.detectorState = .detecting
}
}
})
}
} else {
// If not granted, show a camera access denial view.
DispatchQueue.main.async {
self.cameraAccessDenialView.layer.opacity = 0
self.cameraAccessDenialView.isHidden = false
UIView.animate(withDuration: self.interfaceCoverViewAnimationDuration, animations: {
self.cameraAccessDenialView.layer.opacity = 1
}, completion: nil)
}
}
// After an app launch, unlock both landscape orientations.
DispatchQueue.main.async {
(UIApplication.shared.delegate as! AppDelegate).orientationMask = .landscape
}
}
}
private func configureCameraFeed() {
captureSession = AVCaptureSession()
captureSession.beginConfiguration()
guard let backCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {
showGeneralError()
captureSession.commitConfiguration()
return
}
let captureInput: AVCaptureDeviceInput
do {
captureInput = try AVCaptureDeviceInput(device: backCamera)
} catch {
showGeneralError()
captureSession.commitConfiguration()
return
}
guard captureSession.canAddInput(captureInput) else {
showGeneralError()
captureSession.commitConfiguration()
return
}
captureSession.addInput(captureInput)
// Query available input device formats, filtering them by criteria described in Application Parameters section. Next, sort them by their minimum exposure duration in ascending order and choose the best one.
guard let cameraFormat = backCamera.formats.filter({ $0.formatDescription.mediaSubType.rawValue == cameraFormatType && $0.formatDescription.dimensions.width <= cameraMaximumVideoWidth && $0.videoSupportedFrameRateRanges.contains(where: { $0.maxFrameRate >= Double(cameraFrameRate) }) && $0.isVideoStabilizationModeSupported(cameraStabilizationMode) && $0.autoFocusSystem == cameraAutoFocusSystem }).sorted(by: { $0.minExposureDuration < $1.minExposureDuration }).first else {
showGeneralError()
captureSession.commitConfiguration()
return
}
// Store the selected format's dimensions for future use.
let dimensions = cameraFormat.formatDescription.dimensions
pixelBufferSize = CGSize(width: CGFloat(dimensions.width), height: CGFloat(dimensions.height))
// Configure an input device.
do {
try backCamera.lockForConfiguration()
backCamera.activeFormat = cameraFormat
backCamera.focusMode = cameraFocusMode
backCamera.activeVideoMinFrameDuration = CMTimeMake(value: 1, timescale: Int32(cameraFrameRate))
backCamera.activeVideoMaxFrameDuration = CMTimeMake(value: 1, timescale: Int32(cameraFrameRate))
if backCamera.isLowLightBoostSupported {
backCamera.automaticallyEnablesLowLightBoostWhenAvailable = true
}
if backCamera.isSmoothAutoFocusSupported {
backCamera.isSmoothAutoFocusEnabled = true
}
backCamera.unlockForConfiguration()
} catch {
showGeneralError()
captureSession.commitConfiguration()
return
}
let captureOutput = AVCaptureVideoDataOutput()
guard captureSession.canAddOutput(captureOutput) else {
showGeneralError()
captureSession.commitConfiguration()
return
}
captureSession.addOutput(captureOutput)
// Configure a capture output.
let captureQueue = DispatchQueue(label: "CaptureQueue", qos: .userInteractive, attributes: [], autoreleaseFrequency: .workItem)
captureOutput.alwaysDiscardsLateVideoFrames = true
captureOutput.setSampleBufferDelegate(self, queue: captureQueue)
captureOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(cameraFormatType)]
// Configure a capture connection. If supported, enable two important options.
let captureConnection = captureSession.connections.first!
if captureConnection.isCameraIntrinsicMatrixDeliverySupported {
captureConnection.isCameraIntrinsicMatrixDeliveryEnabled = true
}
if captureConnection.isVideoStabilizationSupported {
captureConnection.preferredVideoStabilizationMode = cameraStabilizationMode
}
captureSession.commitConfiguration()
isCameraReady = true
}
private func configureObjectDetection() {
guard isCameraReady else { return }
do {
let model = try VNCoreMLModel(for: MLModel(contentsOf: SpeedLimitSignDetector.urlOfModelInThisBundle))
objectDetectionRequest = VNCoreMLRequest(model: model) { (request, _) in
self.processObjectDetectionResults(request.results)
}
} catch {
showGeneralError()
}
}
private func configureTextRecognition() {
guard isCameraReady else { return }
textRecognitionRequest = VNRecognizeTextRequest { (request, _) in
self.processTextRecognitionResults(request.results)
}
textRecognitionRequest.customWords = validTextRecognitionResults
textRecognitionRequest.minimumTextHeight = textRecognitionMinimumTextHeight
textRecognitionRequest.recognitionLevel = textRecognitionLevel
textRecognitionRequest.revision = textRecognitionRevision
textRecognitionRequest.usesLanguageCorrection = textRecognitionUsesLanguageCorrection
}
private func startCameraFeed() {
guard isCameraReady else { return }
// Set up a camera preview layer.
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer.connection!.videoOrientation = videoOrientation
cameraPreviewLayer.videoGravity = .resizeAspectFill
cameraPreviewLayer.frame = cameraView.layer.bounds
cameraView.layer.addSublayer(cameraPreviewLayer)
// Set up an object observation layer.
objectObservationLayer = CALayer()
objectObservationLayer.frame = cameraPreviewLayer.bounds
cameraPreviewLayer.addSublayer(objectObservationLayer)
captureSession.startRunning()
}
// MARK: - Computer Vision
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
// If available, prepare an options dictionary with some additional camera information. This supports computer vision tasks.
var options: [VNImageOption: Any] = [:]
if let cameraIntrinsics = pixelBuffer.attachments.propagated[kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix as String] {
options = [VNImageOption.cameraIntrinsics: cameraIntrinsics]
}
switch detectorState {
case .sleeping:
// Relax. 🏖
return
case .detecting:
currentPixelBuffer = pixelBuffer
// Perform an object detection task.
let requestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: imageOrientation, options: options)
do {
try requestHandler.perform([objectDetectionRequest])
} catch {
showGeneralError()
}
case .tracking:
// If needed, perform a text recognition task (one has already been completed right after a successful object detection).
if upcomingSpeedLimit == nil {
if textRecognitionAttemptCounter < maximumTextRecognitionAttempts - 1 {
textRecognitionAttemptCounter += 1
beginTextRecognition(onPixelBuffer: pixelBuffer, croppingTo: objectObservation.boundingBox)
} else {
DispatchQueue.main.async {
self.clearObjectObservationRectangle()
}
returnToObjectDetection()
return
}
}
// Begin another round of object tracking.
if objectTrackingRequest == nil {
objectTrackingRequest = VNTrackObjectRequest(detectedObjectObservation: objectObservation) { (request, _) in
self.processObjectTrackingResults(request.results)
}
objectTrackingRequest.revision = objectTrackingRevision
objectTrackingRequest.trackingLevel = objectTrackingLevel
} else {
objectTrackingRequest.inputObservation = objectObservation
}
do {
try objectTrackingRequestHandler.perform([objectTrackingRequest], on: pixelBuffer, orientation: imageOrientation)
} catch {
showGeneralError()
returnToObjectDetection()
}
}
}
private func processObjectDetectionResults(_ results: [Any]?) {
if let results = results as? [VNDetectedObjectObservation], !results.isEmpty {
// Accept only the objects which pass the confidence threshold, are big enough and close to the target 1:1 aspect ratio. Next, sort them by their confidence in descending order.
let validResults = results.filter { result in
let imageRectangle = VNImageRectForNormalizedRect(result.boundingBox, Int(pixelBufferSize.width), Int(pixelBufferSize.height))
if result.confidence >= objectDetectionConfidenceThreshold && result.boundingBox.height >= objectDetectionMinimumHeight && abs(imageRectangle.width / imageRectangle.height - 1) <= objectDetectionMaximumAspectRatioDeviation {
return true
}
return false
}.sorted { $0.confidence > $1.confidence }
// If there is a valid observation, begin a text recognition task and prepare for tracking the observation.
if let bestResult = validResults.first {
beginTextRecognition(onPixelBuffer: currentPixelBuffer, croppingTo: bestResult.boundingBox)
detectorState = .tracking
objectObservation = bestResult
objectTrackingRequestHandler = VNSequenceRequestHandler()
currentPixelBuffer = nil
}
}
}
private func prepareImageForTextRecognition(_ image: CIImage) -> CIImage {
var preprocessedImage = image
// 1. Desaturate the image - make it black & white.
preprocessedImage = CIFilter(name: "CIColorControls", parameters: [kCIInputImageKey: preprocessedImage, kCIInputSaturationKey: 0])!.outputImage!
// 2. Increase the exposure value.
preprocessedImage = CIFilter(name: "CIExposureAdjust", parameters: [kCIInputImageKey: image, kCIInputEVKey: 2])!.outputImage!
// 3. Remove highlights.
preprocessedImage = CIFilter(name: "CIHighlightShadowAdjust", parameters: [kCIInputImageKey: image, "inputHighlightAmount": 0])!.outputImage!
// 4. Increase brightness and contrast.
preprocessedImage = CIFilter(name: "CIColorControls", parameters: [kCIInputImageKey: image, kCIInputBrightnessKey: 0.45, kCIInputContrastKey: 2])!.outputImage!
// 5. Remove highlights again.
preprocessedImage = CIFilter(name: "CIHighlightShadowAdjust", parameters: [kCIInputImageKey: image, "inputHighlightAmount": 0])!.outputImage!
// 6. Apply a box blur to remove some of noise in the image.
preprocessedImage = CIFilter(name: "CIBoxBlur", parameters: [kCIInputImageKey: image, kCIInputRadiusKey: 4])!.outputImage!
return preprocessedImage
}
private func beginTextRecognition(onPixelBuffer pixelBuffer: CVImageBuffer, croppingTo objectObservationBoundingBox: CGRect) {
// Text recognition is performed on an oriented, cropped and preprocessed part of given pixel buffer. Thus, a camera intrinsics options dictionary does not apply in this case.
let imageBoundingBox = VNImageRectForNormalizedRect(objectObservationBoundingBox, Int(pixelBufferSize.width), Int(pixelBufferSize.height))
let croppedImage = CIImage(cvImageBuffer: pixelBuffer).oriented(imageOrientation).cropped(to: imageBoundingBox)
let preprocessedImage = prepareImageForTextRecognition(croppedImage)
let requestHandler = VNImageRequestHandler(ciImage: preprocessedImage, options: [:])
do {
try requestHandler.perform([textRecognitionRequest])
} catch {
showGeneralError()
returnToObjectDetection()
}
}
private func processTextRecognitionResults(_ results: [Any]?) {
// Choose the best text recognition candidate and make sure it passes the confidence threshold.
if let results = results as? [VNRecognizedTextObservation], let result = results.first?.topCandidates(1).first, result.confidence >= textRecognitionConfidenceThreshold {
var containsForbiddenCharacter = false
for character in forbiddenTextRecognitionResultCharacters {
if result.string.contains(character) {
containsForbiddenCharacter = true
break
}
}
if !containsForbiddenCharacter {
let correctedResult = correctTextRecognition(result: result.string)
// Having applied some limitations and corrections, shall the result be correct, mark it as an upcoming speed limit.
if validTextRecognitionResults.contains(correctedResult) {
upcomingSpeedLimit = correctedResult
}
}
}
}
private func correctTextRecognition(result: String) -> String {
var correctedResult = result
for (character, confusions) in possibleTextRecognitionResultCharacterConfusions {
for confusion in confusions {
correctedResult = correctedResult.replacingOccurrences(of: confusion, with: character)
}
}
for (confusion, correction) in unambiguousTextRecognitionResultCorrections {
if correctedResult == confusion {
correctedResult = correction
break
}
}
for correction in otherTextRecognitionResultCorrections {
correctedResult = correction(correctedResult)
}
return correctedResult
}
private func processObjectTrackingResults(_ results: [Any]?) {
// Remove a previous observation rectangle.
DispatchQueue.main.async {
self.clearObjectObservationRectangle()
}
// Make sure that a new observation passes the confidence threshold or there has been an upcoming speed limit already set.
guard let results = results as? [VNDetectedObjectObservation], let result = results.first, (result.confidence >= objectTrackingConfidenceThreshold || upcomingSpeedLimit != nil) else {
returnToObjectDetection()
return
}
// If the tracked object's confidence drops below the threshold or its bounding box moves beyond an escape margin, finish object tracking and update the speed limit (if it has been successfully read).
if result.confidence < objectTrackingConfidenceThreshold || abs(result.boundingBox.midX - 0.5) > 0.5 - objectTrackingEscapeMargin - result.boundingBox.width / 2 || abs(result.boundingBox.midY - 0.5) > 0.5 - objectTrackingEscapeMargin - result.boundingBox.height / 2 {
if let newSpeedLimit = upcomingSpeedLimit {
// Show the new speed limit.
DispatchQueue.main.async {
self.updateSpeedLimit(newValue: newSpeedLimit)
}
// Go to sleep and schedule a return.
detectorState = .sleeping
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(sleepDuration)) {
self.returnToObjectDetection()
}
// Cancel any pending speed limit cancelation task and schedule a new one.
currentSpeedLimitCancelationTask?.cancel()
currentSpeedLimitCancelationTask = DispatchWorkItem {
self.cancelSpeedLimit()
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(speedLimitValidityDuration), execute: currentSpeedLimitCancelationTask!)
} else {
returnToObjectDetection()
}
return
}
// Pass the result to be used for further object tracking.
objectObservation = result
// Draw a new observation rectangle.
DispatchQueue.main.async {
self.drawObjectObservationRectangle(fromBoundingBox: result.boundingBox)
}
}
// MARK: - Helpers
private func clearObjectObservationData() {
objectObservation = nil
objectTrackingRequest = nil
objectTrackingRequestHandler = nil
textRecognitionAttemptCounter = 0
upcomingSpeedLimit = nil
}
private func returnToObjectDetection() {
clearObjectObservationData()
detectorState = .detecting
}
// MARK: - User Interface
@IBAction func showInformationView() {
detectorState = .sleeping
clearObjectObservationData()
}
@IBAction func hideInformationView(withUnwindSegue unwindSegue: UIStoryboardSegue) {
returnToObjectDetection()
}
@IBAction func handleDoubleTap(_ sender: UITapGestureRecognizer) {
guard sender.state == .ended else { return }
currentSpeedLimitCancelationTask?.cancel()
cancelSpeedLimit()
}
private func updateOrientationData() {
// This function is called before an orientation change. If the current value is .landscapeLeft, the next one will be .landscapeRight.
if UIDevice.current.orientation == .landscapeLeft {
videoOrientation = .landscapeRight
imageOrientation = .up
} else {
videoOrientation = .landscapeLeft
imageOrientation = .down
}
}
private func showGeneralError() {
DispatchQueue.main.async {
guard self.presentedViewController == nil else { return }
self.present(UIAlertController(title: "Wystąpił błąd 🤭", message: "Otwórz przełączanie aplikacji, zamknij aplikację Obserwator i spróbuj uruchomić ją ponownie.", preferredStyle: .alert), animated: true)
}
}
private func drawObjectObservationRectangle(fromBoundingBox boundingBox: CGRect) {
CATransaction.begin()
CATransaction.setValue(true, forKey: kCATransactionDisableActions) // Disable animations.
let objectObservationRectangleLayer = CAShapeLayer()
// Whichever scale is larger, it will determine the way of projecting an original image onto the camera preview layer, using the aspect fill option.
let scaleX = cameraPreviewLayer.bounds.width / pixelBufferSize.width
let scaleY = cameraPreviewLayer.bounds.height / pixelBufferSize.height
let scale = max(scaleX, scaleY)
let shiftX = (scale * pixelBufferSize.width - cameraPreviewLayer.bounds.width) / 2
let shiftY = (scale * pixelBufferSize.height - cameraPreviewLayer.bounds.height) / 2
// The input bounding box has to be projected onto a scaled image space. Next, it must be converted into Core Animation's coordinate system. Finally, some translation is needed because of using the aspect fill option.
let objectObservationRectangle = VNImageRectForNormalizedRect(boundingBox, Int(scale * pixelBufferSize.width), Int(scale * pixelBufferSize.height)).applying(CGAffineTransform(translationX: 0, y: cameraPreviewLayer.bounds.height).scaledBy(x: 1, y: -1).translatedBy(x: shiftX, y: -shiftY))
objectObservationRectangleLayer.path = CGPath(rect: objectObservationRectangle, transform: nil)
objectObservationRectangleLayer.lineWidth = observationRectangleLineWidth
// If an upcoming speed limit has not been determined yet, use a dashed line for drawing.
if upcomingSpeedLimit == nil {
objectObservationRectangleLayer.lineDashPattern = observationRectangleLineDashPattern
}
objectObservationRectangleLayer.strokeColor = observationRectangleStrokeColor
objectObservationRectangleLayer.fillColor = observationRectangleFillColor
objectObservationLayer.addSublayer(objectObservationRectangleLayer)
CATransaction.commit()
}
private func clearObjectObservationRectangle() {
CATransaction.begin()
CATransaction.setValue(true, forKey: kCATransactionDisableActions) // Disable animations.
self.objectObservationLayer.sublayers = nil
CATransaction.commit()
}
private func updateSpeedLimit(newValue value: String) {
speedLimitSign.speedLimit = value
if speedLimitSign.isHidden {
// Update the leading constraint of the information bar.
informationBarLeadingConstraint.isActive = false
informationBarLeadingConstraint = NSLayoutConstraint(item: informationBar!, attribute: .leading, relatedBy: .equal, toItem: speedLimitSign, attribute: .trailing, multiplier: 1, constant: 0)
informationBarLeadingConstraint.isActive = true
// Move the information bar to the right and show the speed limit sign.
UIView.animate(withDuration: SpeedLimitView.showAnimationDuration, delay: 0, options: .curveEaseInOut, animations: {
self.blurEffectView.layoutIfNeeded()
}, completion: { _ in
self.speedLimitSign.show()
})
}
}
private func cancelSpeedLimit() {
guard !speedLimitSign.isHidden else { return }
// Update the leading constraint of the information bar.
informationBarLeadingConstraint.isActive = false
informationBarLeadingConstraint = NSLayoutConstraint(item: informationBar!, attribute: .leading, relatedBy: .equal, toItem: blurEffectView, attribute: .leading, multiplier: 1, constant: 0)
informationBarLeadingConstraint.isActive = true
// Hide the speed limit sign and move the information bar to the center.
speedLimitSign.hide()
UIView.animate(withDuration: SpeedLimitView.hideAnimationDuration, delay: SpeedLimitView.hideAnimationDuration, options: .curveEaseInOut, animations: {
self.blurEffectView.layoutIfNeeded()
}, completion: nil)
}
}