前提
私はswiftを学び始めて3ヶ月の初心者です。
swiftでリアルタイムで画像認識するアプリを作っています。
写真を撮ってその画像を認識することはできたのですが、
リアルタイムで作り直すと、数秒の間は目的のリアルタイムで画像認識をしてくれましたが、クラッシュしてしまい、以下のエラーメッセージが発生しました。
Terminated due to memory issue
改善方法のご教授をよろしくお願いします。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- 途中で落ちないようにしたい
発生している問題・エラーメッセージ
Terminated due to memory issue
該当のソースコード
swift
1 2import UIKit 3import AVKit 4import Vision 5 6class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { 7 8 let identifierLabel: UILabel = { 9 let label = UILabel() 10 label.backgroundColor = .white 11 label.textAlignment = .center 12 label.translatesAutoresizingMaskIntoConstraints = false 13 return label 14 }() 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 19 let captureSession = AVCaptureSession() 20 captureSession.sessionPreset = .photo 21 22 guard let captureDevice = AVCaptureDevice.default(for: .video) else { return } 23 guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return } 24 captureSession.addInput(input) 25 26 captureSession.startRunning() 27 28 let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 29 view.layer.addSublayer(previewLayer) 30 previewLayer.frame = view.frame 31 32 let dataOutput = AVCaptureVideoDataOutput() 33 dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue")) 34 captureSession.addOutput(dataOutput) 35 36 setupIdentifierConfidenceLabel() 37 } 38 39 fileprivate func setupIdentifierConfidenceLabel() { 40 view.addSubview(identifierLabel) 41 identifierLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -32).isActive = true 42 identifierLabel.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 43 identifierLabel.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true 44 identifierLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true 45 } 46 47 func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 48 49 guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } 50 51 guard let model = try? VNCoreMLModel(for: Resnet50().model) else { return } 52 let request = VNCoreMLRequest(model: model) { (finishedReq, err) in 53 54 guard let results = finishedReq.results as? [VNClassificationObservation] else { return } 55 56 guard let firstObservation = results.first else { return } 57 58 print(firstObservation.identifier, firstObservation.confidence) 59 60 DispatchQueue.main.async { 61 self.identifierLabel.text = "\(firstObservation.identifier) \(firstObservation.confidence * 100)" 62 } 63 64 } 65 66 try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request]) 67 } 68 69} 70
試したこと
起動してからエラーが起こるので、どこでエラーが起きているのかがわからない状態です。
補足情報(FW/ツールのバージョンなど)
Visionフレームワークは使ったことがないのですが、まだ回答がないようですのでコメントしてみます・・
質問欄のコードはこれで再現可能な全てのコードでしょうか?
再現できないコードだけですと回答がなかなかつかないのかなと思いました。
captureOutputの中だけを見ますと、ネットの記事と比べてもおかしい感じはしませんでしたので、
それ以外の部分に原因がありそうかなと思いました。
ご指摘ありがとうございます。
自分でコピペして確かめてみたところ、
ソースコードの中に余計な文が含まれていました。
この文を除いた上で、問題が起きている状態です。
問題のソースコードに修正は終えました。
よろしくお願いします。
不要な文字列があったので実際のコードとは違うのかなと思いましたが、
でも必要なコードが記載されていなかったわけではなくこれで全てなのですね。
> ImagePredictor.imageClassifier
> Share one ``VNCoreMLModel`` instance --- for each Core ML model file --- across the app, since each can be expensive in time and resources.
> (機械翻訳)アプリ全体で1つのVNCoreMLModelインスタンスを共有します。各インスタンスは時間とリソースがかかるためです。
> https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml
Visonフレームワークのサンプルコードをダウンロードして、中のコードに引用のようなコメントがありました。
関係しているかわからないですが、VNCoreMLModelはメモリを圧迫しているのかもしれません。
初期化時にVNCoreMLModelのインスタンスを生成して、それを使い回す感じにしてみたら何か変わるでしょうか?
回答1件
あなたの回答
tips
プレビュー