我正在尝试更新类变量(预测),然后将该变量用作segue(AnalyzeSegue)的数据。但是,即使我在函数(detectScene)中更新了class变量,在准备segue导致我的应用崩溃时,class变量也是public class IckeMain {
Scanner inread = new Scanner (System.in);
int num;
//Make sure to declare your array up here, but don't worry about setting a value
int [] array;
int [] nyArr;
public void fillArray () {
System.out.println("You chose: " + num + " with size: " +array.length);
for (int j=0; j<num; j++) {
array[j] = (int)(Math.random() * 99);
System.out.println("Unsorted: " +array[j]);
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
//Now initialize to a new array with size num
array = new int [num];
nyArr = new int [num];
}
值。
大多数代码是通过本教程(仅为我的模型定制的)
https://www.raywenderlich.com/577-core-ml-and-vision-machine-learning-in-ios-11-tutorial
nil
检测功能:
class PreviewViewController: UIViewController {
var prediction: String?
@IBOutlet weak var Photo: UIImageView!
//This button is what starts the segue into the next screen(
@IBAction func analyze(_ sender: Any) {}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "saveSegue"{
"save photo"}
else if segue.identifier == "AnalyzeSegue" #Analyze button
{
let AnalyzeVC = segue.destination as! AnalyzeViewController
let ciImage = CIImage(image:Photo.image!)
self.detectScene(image: ciImage!)
print(self.prediction!) #nil value
if let hasPrediction = self.prediction {
AnalyzeVC.predictionLabel.text = hasPrediction
AnalyzeVC.imagePreview.image = Photo.image}
}
因此,应该使用extension PreviewViewController{
func detectScene(image: CIImage) {
var localPrediction: String?
// Load the ML model through its generated class
guard let model = try? VNCoreMLModel(for: test2().model)
else {
fatalError("can't load Places ML model")
}
let request = VNCoreMLRequest(model: model) { [weak self] request, error in
guard let results = request.results as? [VNCoreMLFeatureValueObservation]
else {
fatalError("unexpected result type from VNCoreMLRequest")
}
let topResult = results.first?.featureValue.multiArrayValue
DispatchQueue.main.async { () in
var max_value : Float32 = 0
for i in 0..<topResult!.count{
if max_value < topResult![i].floatValue{
max_value = topResult![i].floatValue
localPrediction = String(i)
}
} //#end of loop
self?.prediction = localPrediction #update self?.prediction
print(self?.prediction, "Inside Function") #seems to have value
} //#end of dispatch Queue
} //#end of closure
let handler = VNImageRequestHandler(ciImage: image)
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([request])
} catch {print(error)}
}
} //end of function
} //end of extension
函数来更新类变量detectScene
,并且该信息用于pree seugue函数中。
该函数似乎正在更新值,因为我在函数中有一条打印语句正在打印出可选值:
prediction
谢谢您的帮助。