forked from younahjeon/apple_facestim_generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
executable file
·367 lines (308 loc) · 12.4 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// ViewController.swift
// FaceDataRecorder
//
// Created by Elisha Hung on 2017/11/12.
// Copyright © 2017 Elisha Hung. All rights reserved.
//
// http://www.elishahung.com/
import UIKit
import ARKit
import SceneKit
import Foundation
class ViewController: UIViewController, ARSessionDelegate {
@IBOutlet weak var sceneView: ARSCNView! // Main view
@IBOutlet weak var captureButton: UIButton! // Start capture process
@IBOutlet weak var switchButton: UISwitch! // Switch between record mode and stream mode
@IBOutlet weak var settingButton: UIButton! // Record fps setting or server ip setting
@IBOutlet weak var infoText: UILabel! // Simple capture information
private let ini = UserDefaults.standard // Store user setting
var session: ARSession {
return sceneView.session
}
var isCapturing = false {
didSet {
switchButton.isEnabled = !isCapturing
settingButton.isEnabled = !isCapturing
}
}
var captureMode = CaptureMode.stream {
didSet {
if captureMode == .record {
if switchButton.isOn { switchButton.isOn = false }
}
refreshInfo()
ini.set(captureMode == .record, forKey: "mode")
}
}
// Stream mode's properties
var host = "192.168.1.101" {
didSet {
ini.set(host, forKey: "host")
}
}
var port = 19977 {
didSet {
ini.set(port, forKey: "port")
}
}
var outputStream: OutputStream!
// Record mode's properties
var fps = 30.0 {
didSet {
fps = min(max(fps, 1.0), 60.0)
ini.set(fps, forKey: "fps")
}
}
var fpsTimer: Timer!
var captureData: [CaptureData]!
var currentCaptureFrame = 0
var folderPath : URL!
// Queue varibales
private let saveQueue = DispatchQueue.init(label: "com.eliWorks.faceCaptureX")
private let dispatchGroup = DispatchGroup()
// Init
override func viewDidLoad() {
super.viewDidLoad()
if let lastHost = ini.string(forKey: "host") {
host = lastHost
}
let lastPort = ini.integer(forKey: "port")
if lastPort != 0 {
port = lastPort
}
let lastFps = ini.double(forKey: "fps")
if lastFps != 0 {
fps = lastFps
}
let lastMode = ini.bool(forKey: "mode")
if lastMode {
captureMode = .record
}else{
captureMode = .stream
}
sceneView.session.delegate = self
sceneView.automaticallyUpdatesLighting = false // for performance
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// View actions and initialize tracking here
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.isIdleTimerDisabled = true
initTracking()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
stopCapture()
session.pause()
}
// AR session delegate
func session(_ session: ARSession, didFailWithError error: Error) {
stopCapture()
DispatchQueue.main.async {
self.initTracking()
}
}
func sessionWasInterrupted(_ session: ARSession) {
return
}
func sessionInterruptionEnded(_ session: ARSession) {
DispatchQueue.main.async {
self.initTracking()
}
}
func session(_ session: ARSession, didUpdate frame: ARFrame) {
// When capture mode is stream, execute streaming here
if captureMode == .stream && isCapturing {
streamData()
}
}
// UI Actions
@IBAction func pressCaptureButton(_ sender: Any) {
// Stop Capture
if isCapturing {
stopCapture()
}else{
// Start Capture
var text: String!
switch captureMode {
case .record:
text = "Recording"
case .stream:
text = "Streaming"
}
captureButton.setTitle(text, for: .normal)
startCapture()
}
}
@IBAction func switchCaptureMode(_ sender: Any) {
captureMode = switchButton.isOn ? .stream : .record
}
@IBAction func settingPressed(_ sender: Any) {
switch captureMode {
case .record:
popRecordSetting()
case .stream:
popStreamSetting()
}
}
func refreshInfo() {
switch captureMode{
case .record:
infoText.text = "Record > \(fps) FPS"
case .stream:
infoText.text = "Stream > \(host):\(port)"
}
}
// Capture Process
func initTracking() {
guard ARFaceTrackingConfiguration.isSupported else { return }
let configuration = ARFaceTrackingConfiguration()
configuration.isLightEstimationEnabled = false
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
}
func startCapture() { // Where capture button pressed, streaming or recording
refreshInfo()
switch captureMode {
case .stream:
// Stream Mode : Create socket, connect to server
if outputStream != nil {
outputStream.close()
}
var out: OutputStream?
Stream.getStreamsToHost(withName: host, port: port, inputStream: nil, outputStream: &out)
outputStream = out!
outputStream.open()
isCapturing = true // This will let didUpdate delegate to stream data
case .record:
// Record Mode : Clean record data, create save folder, use timer to record for stable fps
captureData = []
currentCaptureFrame = 0
let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
folderPath = documentPath.appendingPathComponent(folderName())
try? FileManager.default.createDirectory(atPath: folderPath.path, withIntermediateDirectories: true, attributes: nil)
isCapturing = true
fpsTimer = Timer.scheduledTimer(withTimeInterval: 1/fps, repeats: true, block: {(timer) -> Void in
self.recordData()
})
}
}
func stopCapture() { // Stop Capture Process
isCapturing = false
switch captureMode {
case .stream:
// Stream Mode : Send "z" to server to tell that I'm stop streaming
let dataStr = "z"
let dataBuffer = dataStr.data(using: .utf8)!
_ = dataBuffer.withUnsafeBytes { self.outputStream.write($0, maxLength: dataBuffer.count) }
outputStream.close()
case .record:
// Record Mode : Turn off timer, convert capture data to string and save into documentary
fpsTimer.invalidate()
let fileName = folderPath.appendingPathComponent("faceData.txt")
let data = captureData.map{ $0.str }.joined(separator: "\n")
try? data.write(to: fileName, atomically: false, encoding: String.Encoding.utf8)
dispatchGroup.wait() // Wait until last image saved
}
captureButton.setTitle("Capture", for: .normal)
}
func streamData() { // Every frame's process in stream mode
if outputStream.streamStatus == .error {
infoText.text = "Connection Error!"
stopCapture()
return
}
guard let data = getFrameData() else {return}
dispatchGroup.enter()
saveQueue.async{
autoreleasepool {
let dataStr = data.str + "a" // Let server know where bytes received finished
let dataBuffer = dataStr.data(using: .utf8)!
if self.outputStream.streamStatus == .open {
_ = dataBuffer.withUnsafeBytes { self.outputStream.write($0, maxLength: dataBuffer.count) }
}
}
}
}
func recordData() { // Every frame's process in record mode
guard let data = getFrameData() else {return}
captureData.append(data)
let snap = session.currentFrame!.capturedImage
let num = currentCaptureFrame // Image sequence's filename
dispatchGroup.enter()
saveQueue.async{
autoreleasepool { // Prevent JPEG conversion memory leak
let writePath = self.folderPath.appendingPathComponent( String(format: "%04d", num)+".jpg" )
try? UIImageJPEGRepresentation(UIImage(pixelBuffer: snap), 0.85)?.write(to: writePath)
self.dispatchGroup.leave()
}
}
currentCaptureFrame += 1
}
func getFrameData() -> CaptureData? { // Organize arkit's data
let arFrame = session.currentFrame!
let faceGeometry = ARSCNFaceGeometry(device: sceneView.device!, fillMesh: true)!
guard let anchor = arFrame.anchors[0] as? ARFaceAnchor else {return nil}
guard let shaderURL = Bundle.main.url(forResource: "shader", withExtension: "shader"),
let modifier = try? String(contentsOf: shaderURL)
else { fatalError("Can't load shader modifier from bundle.") }
faceGeometry.shaderModifiers = [ .geometry: modifier]
let vertices = anchor.geometry.vertices
let camTransform = arFrame.camera.transform
let imageResolution_width = arFrame.camera.imageResolution.width
let imageResolution_height = arFrame.camera.imageResolution.height
let projTransform = arFrame.camera.projectionMatrix
let faceTransform = anchor.transform
let data = CaptureData(vertices: vertices, camTransform: arFrame.camera.transform, faceTransform: anchor.transform, projTransform: projTransform,imageResolution_width: arFrame.camera.imageResolution.width, imageResolution_height: arFrame.camera.imageResolution.height, blendShapes: anchor.blendShapes)
return data
}
// utility
func folderName() -> String {
let dateFormatter : DateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMdd_HHmmss"
let date = Date()
let folderStr = dateFormatter.string(from: date)
return folderStr
}
func popRecordSetting() {
let alert = UIAlertController(title: "Record Setting", message: "Set frames per second.", preferredStyle: .alert)
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "\(self.fps)"
textField.keyboardType = .decimalPad
})
let okAction = UIAlertAction(title: "Accept", style: .default, handler: { (action) -> Void in
self.fps = Double(alert.textFields![0].text!)!
self.refreshInfo()
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {(action) -> Void in})
alert.addAction(okAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
func popStreamSetting() {
let alert = UIAlertController(title: "Stream Setting", message: "Set stream server IP address.", preferredStyle: .alert)
alert.addTextField(configurationHandler: { textField in
textField.placeholder = self.host
textField.keyboardType = .decimalPad
})
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "\(self.port)"
textField.keyboardType = .decimalPad
})
let okAction = UIAlertAction(title: "Accept", style: .default, handler: { (action) -> Void in
if alert.textFields![0].text != "" {
self.host = alert.textFields![0].text!
}
if alert.textFields![1].text != "" {
self.port = Int(alert.textFields![1].text!)!
}
self.refreshInfo()
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {(action) -> Void in})
alert.addAction(okAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}