I'm new to Swift
and have been finding Stackoverflow tremendously helpful in my troubleshooting so far. In my current project I'm trying to create a confidence label similar to the attached screen shot in addition to a already existing capture functionality:
When I run my code the camera view and capture functionality works but the object recognition (inception V3) doesn't seem to receive the data from AVCaptureOutput
(it doesn't print the result to the console either).
I'm not getting any error message so I can't tell what I'm doing wrong. Any feedback would be greatly appreciated
Thanks!
func setupCaptureSession() {
captureSession.sessionPreset = AVCaptureSession.Preset.photo
}
func setupDevice() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
let device = deviceDiscoverySession.devices
for device in device {
if device.position == AVCaptureDevice.Position.back {
backCamera = device
}else if device.position == AVCaptureDevice.Position.front {
frontCamera = device
}
}
currentCamera = backCamera
}
func setupInputOutput() {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
captureSession.addInput(captureDeviceInput)
photoOutput = AVCapturePhotoOutput()
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
captureSession.addOutput(photoOutput!)
} catch {
print(error)
}
}
func setupPreviewLayer() {
camerapreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
camerapreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
camerapreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
camerapreviewLayer?.frame = self.view.frame
self.view.layer.insertSublayer(camerapreviewLayer!, at: 0)
}
func setupRunningCaptureSession() {
captureSession.startRunning()
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
// print("Camera was able to capture a frame:", Date())
guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
guard let model = try? VNCoreMLModel(for: Inceptionv3().model) else { return }
let request = VNCoreMLRequest(model: model) { (finishedReq, err) in
//perhaps check the err
// print(finishedReq.results)
guard let results = finishedReq.results as? [VNClassificationObservation] else { return }
guard let firstObservation = results.first else { return }
print(firstObservation.identifier, firstObservation.confidence)
DispatchQueue.main.async {
self.confidenceLabel.text = "\(firstObservation.identifier) \(firstObservation.confidence * 100)"
}
}
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}