Skip to content

Instantly share code, notes, and snippets.

@juliuscanute
Last active September 21, 2020 22:45
Show Gist options
  • Save juliuscanute/98e734f1e64461d6d4c42232981580d1 to your computer and use it in GitHub Desktop.
Save juliuscanute/98e734f1e64461d6d4c42232981580d1 to your computer and use it in GitHub Desktop.
[Data Collection for Sequence Classification] #data #collection #sequence #classification #machine #learning

Building a dataset

  • Google Dataset Search
  • Kaggle

Define activities to record using Motion API

enum ActivityType: Int {
    case none, driveIt, shakeIt, chopIt
}

Accessing sensor data from Core Motion

import CoreMotion

Declare Core Motion properties

let motionManager = CMMotionManager()
let queue = OperationQueue()
var activityData: [String] = []

Define sampling interval for sensors

enum Config {
    static let samplesPerSecond = 25.0
  }

Write sensor data to a file

  private func confirmSavingActivityData(_ action: UIAlertAction) {
    let dataURL = FileManager.documentDirectoryURL
      .appendingPathComponent("u\(self.userIdField.text!)-\(self.selectedActivityName)-data")
      .appendingPathExtension("csv")
    
    do {
      try self.activityData.appendLinesToURL(fileURL: dataURL)
      print("Data appended to \(dataURL)")
    } catch {
      print("Error appending data: \(error)")
    }
  }

  func enableMotionUpdates() {
    // 1
    motionManager.deviceMotionUpdateInterval = 1 / Config.samplesPerSecond
    // 2
    activityData = []
    // 3
    motionManager.startDeviceMotionUpdates(
      using: .xArbitraryZVertical,
      to: queue,
      withHandler: { [weak self] motionData, error in
        // 4
        guard let self = self, let motionData = motionData else
        {
          let errorText = error?.localizedDescription ?? "Unknown"
          print("Device motion update error: \(errorText)")
          return
        }
        // 5
        self.process(data: motionData)
        
      }
    )
  }

  func process(data motionData: CMDeviceMotion) {
    let activity = isRecording ? currendActivity : .none
    let sample = """
    \(sessionId!)-\(numberOfActionsRecorded),\
    \(activity.rawValue),\
    \(motionData.attitude.roll),\
    \(motionData.attitude.pitch),\
    \(motionData.attitude.yaw),\
    \(motionData.rotationRate.x),\
    \(motionData.rotationRate.y),\
    \(motionData.rotationRate.z),\
    \(motionData.gravity.x),\
    \(motionData.gravity.y),\
    \(motionData.gravity.z),\
    \(motionData.userAcceleration.x),\
    \(motionData.userAcceleration.y),\
    \(motionData.userAcceleration.z)
    """
    activityData.append(sample)
    
  }

Start & Stop data collection session

    case Utterances.sessionStart:
      // TODO: enable Core Motion
      enableMotionUpdates()
      queueNextActivity()
    case Utterances.sessionComplete:
      disableMotionUpdates()



 func disableMotionUpdates() {
   motionManager.stopDeviceMotionUpdates() 
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment