Switch to pure Swift training script

This commit is contained in:
Christoph Hagen
2023-01-15 11:21:47 +01:00
parent 299dba0da4
commit d516c1acd6
7 changed files with 403 additions and 200 deletions

View File

@ -22,6 +22,8 @@ final class CapServer: ServerOwner {
private let fm = FileManager.default
private let changedImageEntryDateFormatter: DateFormatter
// MARK: Caps
private var writers: Set<String>
@ -77,6 +79,8 @@ final class CapServer: ServerOwner {
self.classifierFile = folder.appendingPathComponent("classifier.mlmodel")
self.changedImagesFile = folder.appendingPathComponent("changes.txt")
self.writers = Set(writers)
self.changedImageEntryDateFormatter = DateFormatter()
changedImageEntryDateFormatter.dateFormat = "yy-MM-dd-HH-mm-ss"
}
func loadData() throws {
@ -303,9 +307,7 @@ final class CapServer: ServerOwner {
unwrittenImageChanges = entries
try? handle.close()
}
let df = DateFormatter()
df.dateFormat = "yy-MM-dd-HH-mm-ss"
let dateString = df.string(from: Date())
let dateString = changedImageEntryDateFormatter.string(from: Date())
while let entry = entries.popLast() {
let content = "\(dateString):\(entry.cap):\(entry.image)\n".data(using: .utf8)!
try handle.write(contentsOf: content)
@ -333,11 +335,25 @@ final class CapServer: ServerOwner {
}
}
func emptyChangedImageListFile() {
func removeAllEntriesInImageChangeList(before date: Date) {
do {
try Data().write(to: changedImagesFile)
try String(contentsOf: changedImagesFile)
.components(separatedBy: "\n")
.filter { $0 != "" }
.compactMap { line -> String? in
guard let entryDate = changedImageEntryDateFormatter.date(from: line.components(separatedBy: ":").first!) else {
return nil
}
guard entryDate > date else {
return nil
}
return line
}
.joined(separator: "\n")
.data(using: .utf8)!
.write(to: changedImagesFile)
} catch {
log("Failed to empty changed images file: \(error)")
log("Failed to update changed images file: \(error)")
}
}

View File

@ -4,6 +4,13 @@ import Foundation
/// The decoder to extract caps from JSON payloads given to the `cap` route.
private let decoder = JSONDecoder()
/// The date formatter to decode dates in requests
private let dateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "yy-MM-dd-HH-mm-ss"
return df
}()
private func authorize(_ request: Request) throws {
guard let key = request.headers.first(name: "key") else {
throw Abort(.badRequest) // 400
@ -65,7 +72,16 @@ func routes(_ app: Application) {
}
// Update the trained classes
app.postCatching("classes") { request in
app.postCatching("classes", ":date") { request in
guard let dateString = request.parameters.get("date") else {
log("Invalid parameter for date")
throw Abort(.badRequest)
}
guard let date = dateFormatter.date(from: dateString) else {
log("Invalid date specification")
throw Abort(.badRequest)
}
try authorize(request)
guard let buffer = request.body.data else {
log("Missing body data: \(request.body.description)")
@ -77,6 +93,6 @@ func routes(_ app: Application) {
throw CapError.invalidBody
}
server.updateTrainedClasses(content: content)
server.emptyChangedImageListFile()
server.removeAllEntriesInImageChangeList(before: date)
}
}