变量显示为 nil - swift 4 IOS
Variables show up as nil - swift 4 IOS
出于某种原因,我的变量 stringy 和 stringy 可以很好地打印到控制台,但是当我尝试将它们设置为标签时,它们显示为 nil。
我的目标是将字符串和浮点数打印到应用程序视图控制器,但这不起作用。
我觉得跟viewdidload有关系,好像隐藏了全局变量。但是,如果我尝试在 viewdidload 之外设置标签,则会出现声明错误。
// ViewController.swift
// Intellicam
//
import UIKit
import AVKit
import Vision
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
var stringy:String!
var stringie:Float!
override func viewDidLoad() {
super.viewDidLoad()
//here we start the camera
let captureSession = AVCaptureSession()
captureSession.sessionPreset = .photo
guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else {return}
captureSession.addInput(input)
captureSession.startRunning()
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)
previewLayer.frame = view.frame
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureSession.addOutput(dataOutput)
// let request = VNCoreMLModel(model: VNCoreMLModel, completionHandler: VNRequestCompletionHandler)
// VNImageRequestHandler(cgImage: <#T##CGImage#>, options: <#T##[VNImageOption : Any]#>)
self.Labele.text = "Guess: \(stringy) + Certainty: \(stringie)"
}
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: Resnet50().model) else {return}
let request = VNCoreMLRequest(model: model){
(finishedReq, err) in
//print(finishedReq.results)
guard let results = finishedReq.results as? [VNClassificationObservation] else {return}
guard let firstObservastion = results.first else {return}
//print("Guess: \(firstObservastion.identifier) Certainty: \(firstObservastion.confidence)%")
self.stringy = firstObservastion.identifier
self.stringie = firstObservastion.confidence
print(self.stringy)
print(self.stringie)
}
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}
@IBOutlet weak var Labele: UILabel!
}
首先,在确定价值之前,切勿强行解包。在您的情况下, VNCoreModelRequest 可能会失败并且您的 both 变量将被取消分配,因此它会挑衅地使您的应用程序崩溃。
还有一件事确保您对标签使用正确的命名约定。
你的问题是你没有根据你得到的结果设置标签值。
解决这个问题
var stringy:String? {
didSet {
DispatchQueue.main.async {
self.Labele.text = self.stringy
}
}
}
或
self.stringy = firstObservastion.identifier
self.stringie = firstObservastion.confidence
DispatchQueue.main.async {
self.Labele.text = "Guess: \(stringy) + Certainty: \(stringie)"
}
出于某种原因,我的变量 stringy 和 stringy 可以很好地打印到控制台,但是当我尝试将它们设置为标签时,它们显示为 nil。
我的目标是将字符串和浮点数打印到应用程序视图控制器,但这不起作用。
我觉得跟viewdidload有关系,好像隐藏了全局变量。但是,如果我尝试在 viewdidload 之外设置标签,则会出现声明错误。
// ViewController.swift
// Intellicam
//
import UIKit
import AVKit
import Vision
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
var stringy:String!
var stringie:Float!
override func viewDidLoad() {
super.viewDidLoad()
//here we start the camera
let captureSession = AVCaptureSession()
captureSession.sessionPreset = .photo
guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else {return}
captureSession.addInput(input)
captureSession.startRunning()
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)
previewLayer.frame = view.frame
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
captureSession.addOutput(dataOutput)
// let request = VNCoreMLModel(model: VNCoreMLModel, completionHandler: VNRequestCompletionHandler)
// VNImageRequestHandler(cgImage: <#T##CGImage#>, options: <#T##[VNImageOption : Any]#>)
self.Labele.text = "Guess: \(stringy) + Certainty: \(stringie)"
}
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: Resnet50().model) else {return}
let request = VNCoreMLRequest(model: model){
(finishedReq, err) in
//print(finishedReq.results)
guard let results = finishedReq.results as? [VNClassificationObservation] else {return}
guard let firstObservastion = results.first else {return}
//print("Guess: \(firstObservastion.identifier) Certainty: \(firstObservastion.confidence)%")
self.stringy = firstObservastion.identifier
self.stringie = firstObservastion.confidence
print(self.stringy)
print(self.stringie)
}
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}
@IBOutlet weak var Labele: UILabel!
}
首先,在确定价值之前,切勿强行解包。在您的情况下, VNCoreModelRequest 可能会失败并且您的 both 变量将被取消分配,因此它会挑衅地使您的应用程序崩溃。 还有一件事确保您对标签使用正确的命名约定。
你的问题是你没有根据你得到的结果设置标签值。
解决这个问题
var stringy:String? {
didSet {
DispatchQueue.main.async {
self.Labele.text = self.stringy
}
}
}
或
self.stringy = firstObservastion.identifier
self.stringie = firstObservastion.confidence
DispatchQueue.main.async {
self.Labele.text = "Guess: \(stringy) + Certainty: \(stringie)"
}