黑屏

时间:2020-03-28 11:58:01

标签: swift xcode

所以,我是一个初学者,我一直在关注在线教程。我已经复制了代码,但没有收到任何错误,但是我的程序似乎没有执行任何操作。这是代码:

import UIKit
import AVKit
import Vision

class viewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

override func viewDidLoad(){
    super.viewDidLoad()

    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)

   guard  let model  = try?VNCoreMLModel(for: SqueezeNet().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 firstObservation = results.first else { return }
        print (firstObservation.identifier, firstObservation.confidence)


    }
    //VNImageRequestHandler(cgImage: <#T##CGImage#>, options: [:]).perform(requests: [VNRequest])


func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
     //print("Camera was able to capture a frame:", Date())
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)else { return }
   try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])

}

} }

1 个答案:

答案 0 :(得分:0)

您忘记的一件事是请求允许使用相机。

类似的东西:

package main

func isDestinationSIEM(json_msg string, json_obj *jason.Object, siem_keys []string) (bool) {
    if json_obj != nil {
        dest, err := json_obj.GetString("destination")
        if err == nil {
            if strings.Contains(dest,"SIEM") {
                return true
            }
        }

        for _, key :=  range siem_keys {
            if strings.Contains(json_msg, key) {
                return true
            }
        }
    }
    return false
}

func sendToSIEM(siem_dst string, json_msg string) (error) {
    // Create connection to syslog server
    roots := x509.NewCertPool()
    ok := roots.AppendCertsFromPEM([]byte(rootPEM))
    if !ok {
        fmt.Println("failed to parse root certificate")
    }
    config := &tls.Config{RootCAs: roots, InsecureSkipVerify: true}
    conn, err := tls.Dial("tcp", siem_dst, config)
    if err != nil {
        fmt.Println("Error connecting SIEM")
        fmt.Println(err.Error())
    } else {
        // Send log message
        _, err = fmt.Fprintf(conn, json_msg)
        if err != nil {
            fmt.Println("Error sending SIEM message: ", json_msg)
            fmt.Println(err.Error())
        }
    }
    defer conn.Close()

    return err
}



func main() {

    // simplified code otherwise there would have been too much
    // but the 'devil' is this for loop
    for _, obj := range objects {

        // first check
        isSIEM := isDestinationSIEM(obj, siem_keys)
        if isSIEM {
           err := sendToSIEM(obj)
           if err != nil {
             // print error
           }

        isAUDIT:= isDestinationSIEM(obj)
        if isAUDIT {
           err := sendToAUDIT(obj)
           if err != nil {
             // print error
           }




    } // end of for


}

此外,您将AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in if response { // Access granted. You can continue. } else { // Tell the user. } } 作为本地/自动变量。它将被释放,离开AVCaptureSession。您必须具有一个实例变量。 iOS documentation甚至显示了这一点。

至少已经犯了两个简单的错误,我建议您遵循有关如何做基础的教程。然后,一旦确定正确,然后在屏幕上看到一些内容,就添加ML内容。

祝你好运,这似乎是一个很好的话题!