Модель CreateML, работающая совершенно по -разному на macOS против iPadosIOS

Программируем под IOS
Ответить
Anonymous
 Модель CreateML, работающая совершенно по -разному на macOS против iPados

Сообщение Anonymous »

Я обучил модель Coreml Corml с использованием приложения CreateMl. При использовании живого предварительного просмотра в приложении CreateML я получаю модель, которая примерно работает (то есть изменения в выражении лица приводит к изменениям в прогнозировании). < /P>

Однако, когда я использую модель в моем приложении (iPad + Mac) он работает только в версии Mac, а не в версии iPad. Версия MAC ведет себя аналогично живым предварительному просмотру в приложении CreateML, но версия iPad дает положительное настроение в 98% случаев. Ниже приведен мой код для реализации модели, которая в основном следует примеру приложения на веб-сайте Apple.

Код: Выделить всё

class CameraViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
var bufferSize: CGSize = .zero

private let session = AVCaptureSession()
private var previewLayer: AVCaptureVideoPreviewLayer! = nil
private let videoDataOutput = AVCaptureVideoDataOutput()

private let videoDataOutputQueue = DispatchQueue(label: "VideoDataOutput", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem)

let frameSize: CGSize = .zero

private var requests = [VNRequest]()
@Binding var sentiment: String

init(sentiment: Binding) {
self._sentiment = sentiment
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
setupAVCapture()
setupVision()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func setupVision() {
do {
let config = MLModelConfiguration()
let faceModel = try FaceEmotion(configuration: config)
let visionModel = try VNCoreMLModel(for: faceModel.model)
let objectRecognition = VNCoreMLRequest(model: visionModel) { (request, error) in
guard let results = request.results as? [VNClassificationObservation], let topResult = results.first else {
return
}
DispatchQueue.main.async {
self.sentiment = "\(topResult.identifier) \(topResult.confidence)"
}
}
self.requests = [objectRecognition]
} catch let error as NSError {
print("Model loading went wrong: \(error)")
}
}

func setupAVCapture() {
var deviceInput: AVCaptureDeviceInput!

// Select a video device, make an input
let videoDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .front).devices.first
do {
deviceInput = try AVCaptureDeviceInput(device: videoDevice!)
} catch {
print("Could not create video device input: \(error)")
return
}

session.beginConfiguration()
//            session.sessionPreset = .cif352x288 // Model image size is smaller.

// Add a video input
guard session.canAddInput(deviceInput) else {
print("Could not add video device input to the session")
session.commitConfiguration()
return
}
session.addInput(deviceInput)
if session.canAddOutput(videoDataOutput) {
session.addOutput(videoDataOutput)
// Add a video data output
videoDataOutput.alwaysDiscardsLateVideoFrames = true
videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
videoDataOutput.setSampleBufferDelegate(self, queue: videoDataOutputQueue)
} else {
print("Could not add video data output to the session")
session.commitConfiguration()
return
}
let captureConnection = videoDataOutput.connection(with: .video)
// Always process the frames
captureConnection?.isEnabled = true
do {
try  videoDevice!.lockForConfiguration()
let dimensions = CMVideoFormatDescriptionGetDimensions((videoDevice?.activeFormat.formatDescription)!)
bufferSize.width = CGFloat(dimensions.width)
bufferSize.height = CGFloat(dimensions.height)
videoDevice!.unlockForConfiguration()
} catch {
print(error)
}
session.commitConfiguration()
previewLayer = AVCaptureVideoPreviewLayer(session:  session)
//            previewLayer.ro
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.connection?.videoRotationAngle = 0
previewLayer.frame = view.bounds
view.layer.addSublayer(previewLayer)
session.startRunning()
}

// Clean up capture setup
func teardownAVCapture() {
previewLayer.removeFromSuperlayer()
previewLayer = nil
}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
return
}

let exifOrientation = exifOrientationFromDeviceOrientation()

let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: exifOrientation, options: [:])
do {
try imageRequestHandler.perform(self.requests)
} catch {
print(error)
}
}

public func exifOrientationFromDeviceOrientation() -> CGImagePropertyOrientation {
let curDeviceOrientation = UIDevice.current.orientation
let exifOrientation: CGImagePropertyOrientation

switch curDeviceOrientation {
case UIDeviceOrientation.portraitUpsideDown:  // Device oriented vertically, home button on the top
exifOrientation = .left
case UIDeviceOrientation.landscapeLeft:       // Device oriented horizontally, home button on the right
exifOrientation = .upMirrored
case UIDeviceOrientation.landscapeRight:      // Device oriented horizontally, home button on the left
exifOrientation = .down
case UIDeviceOrientation.portrait:            // Device oriented vertically, home button on the bottom
exifOrientation = .up
default:
exifOrientation = .up
}
return exifOrientation
}
}
Любая помощь будет очень оценена.

Подробнее здесь: https://stackoverflow.com/questions/794 ... -vs-ipados
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «IOS»