Swift: Accessing the 1220 face vertices and save the vertices from AR face

旧时模样 提交于 2020-06-16 18:12:33

问题


I am trying to get the 1220 vertices data and save them to the iPhone file. The problem I am having is I could not get the captured data and write them into the JSON structure correctly.

I have:

struct CaptureData {
    var vertices: [SIMD3<Float>]


      var verticesFormatted : String {
            let v = "<" + vertices.map{ "\($0.x):\($0.y):\($0.z)" }.joined(separator: "~") + "~t:\(String(Double(Date().timeIntervalSince1970)))>"
            return "\(v)"
        }

    var jsonDict:Dictionary<String, Any> = [
           "facetracking_data" : "1",
           "info" : [
               "frame" : "0",
               "timestamp" : 12345,
               "vertices" : [
                   "x" : "0.1",
                   "y" : "0.2",
                   "z" : "0.3"
               ]
           ]
       ]

    var jsonData : Data {
        let data = try! JSONSerialization.data(withJSONObject: jsonDict, options: .prettyPrinted)
        return data
    }

}

I was able to print some data using print(data), like following:

SIMD3(0.018364782, -0.043627884, 0.0480636), SIMD3(-0.01794959, -0.04421088, 0.04891427), SIMD3(-0.019180747, -0.045720913, 0.049276996)], jsonDict: ["facetracking_data": "1", "info": ["frame": "0", "timestamp": 1590426140.3919969, "vertices": ["x": "0.1", "y": "0.2", "z": "0.3"]]])

I am trying to write the captured data and save them into the JSON format by clicking one button:

@IBAction func saveButtonTapped(_ sender: UIButton) {
     guard let data = getFrameData() else {return}

                      let jsonDataString = String(decoding: data.jsonData, as: UTF8.self)
                      let file = "\(UUID().uuidString).txt"
                      let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                      let fileURL = dir.appendingPathComponent(file)
                      do {

                          try data.write(to: fileURL, atomically: false, encoding: .utf8)
                        print(data)
                      }
                      catch {
                          print("Error: \(error)")
                      }
    }

I could not get try data.write(to: fileURL, atomically: false, encoding: .utf8) this to work as it return error ('data' have no member of 'write') and I am not too sure how to get the captured data and write them into my defined JSON structure...

来源:https://stackoverflow.com/questions/62007364/swift-accessing-the-1220-face-vertices-and-save-the-vertices-from-ar-face

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!