// The phone's analysis layer (AlbumsAnalysis.swift, AlbumsFaces.swift, compiled unchanged with ALBUMS_HARNESS) run on // the Mac over a synthetic library's files: what Vision and the face model find on real hardware, for // scripts/albums-score.py. A simulator's Vision cannot say (its networks answer garbage); a phone is not ours to use. // // scripts/albums-vision-mac.sh [] // // Each file is read at 360 px on the long edge (what the phone's pass asks for), as the phone would get it. import CoreGraphics import Foundation import ImageIO struct LibraryAsset: Sendable { let id: String let width: Int let height: Int var longEdge: Int { max(width, height) } } enum Log { static func write(_ line: String) { FileHandle.standardError.write(Data((line + "\n").utf8)) } } struct Row: Encodable { let id: String, file: String, created: Double?, w: Int, h: Int, flags: UInt32, burst: String? let bytes: Int64, exact: Bool, state: Int let side: Int?, print: String?, aesthetic: Float?, utility: Bool?, sharpness: Float?, light: Float?, clipped: Float? let dhash: String?, document: Bool?, text: Float?, millis: Double? let faces: [Face] struct Face: Encodable { let box: [Float]; let quality: Float?; let pixels: Float; let points: [Float]; let vector: [Float]?; let model: String? } } let args = CommandLine.arguments guard args.count >= 3 else { Log.write("usage: albums-vision-mac "); exit(2) } let dir = URL(fileURLWithPath: args[1]) let files = try FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: [.fileSizeKey]) .filter { ["jpg", "png", "mov"].contains($0.pathExtension.lowercased()) } .sorted { $0.lastPathComponent < $1.lastPathComponent } let model = AlbumsFaces.model Log.write("albums-vision-mac: \(files.count) files, model \(model?.name ?? "none")") let exif = DateFormatter() exif.dateFormat = "yyyy:MM:dd HH:mm:ss" exif.timeZone = TimeZone(identifier: "UTC") FileManager.default.createFile(atPath: args[2], contents: nil) let out = try FileHandle(forWritingTo: URL(fileURLWithPath: args[2])) let encoder = JSONEncoder() let started = Date() var done = 0 var closerLooks = 0 for url in files { let name = url.lastPathComponent let bytes = Int64((try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0) if url.pathExtension.lowercased() == "mov" { let row = Row(id: name, file: name, created: nil, w: 0, h: 0, flags: 0, burst: nil, bytes: bytes, exact: true, state: 4, side: nil, print: nil, aesthetic: nil, utility: nil, sharpness: nil, light: nil, clipped: nil, dhash: nil, document: nil, text: nil, millis: nil, faces: []) out.write(try encoder.encode(row) + Data([0x0A])) continue } guard let source = CGImageSourceCreateWithURL(url as CFURL, nil), let props = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any] else { continue } let w = props[kCGImagePropertyPixelWidth] as? Int ?? 0, h = props[kCGImagePropertyPixelHeight] as? Int ?? 0 let taken = ((props[kCGImagePropertyExifDictionary] as? [CFString: Any])?[kCGImagePropertyExifDateTimeOriginal] as? String).flatMap { exif.date(from: $0) } let options: [CFString: Any] = [kCGImageSourceCreateThumbnailFromImageAlways: true, kCGImageSourceThumbnailMaxPixelSize: Int(ProcessInfo.processInfo.environment["ALBUMS_SIDE"] ?? "360") ?? 360, kCGImageSourceCreateThumbnailWithTransform: true] guard let image = CGImageSourceCreateThumbnailAtIndex(source, 0, options as CFDictionary) else { continue } let asset = LibraryAsset(id: name, width: w, height: h) var l = await AlbumsAnalysis.look(image, degraded: false, asset: asset, faces: true, model: model) // As the pass does: faces too small at 360 px but large enough in the original, once more from a larger picture. if let side = AlbumsAnalysis.closerSide(l, asset: asset) { let closer: [CFString: Any] = [kCGImageSourceCreateThumbnailFromImageAlways: true, kCGImageSourceThumbnailMaxPixelSize: Int(side), kCGImageSourceCreateThumbnailWithTransform: true] if let near = CGImageSourceCreateThumbnailAtIndex(source, 0, closer as CFDictionary), max(near.width, near.height) > l.side { let t = Date() let faces = await AlbumsAnalysis.closer(near, asset: asset, model: model) if faces.count >= l.faces.count { l.faces = faces } l.millis += Date().timeIntervalSince(t) * 1000 closerLooks += 1 } } let row = Row( id: name, file: name, created: taken?.timeIntervalSince1970, w: w, h: h, flags: name.hasPrefix("Screenshot_") ? 1 : 0, burst: nil, bytes: bytes, exact: true, state: 1, side: l.side, print: l.print?.base64EncodedString(), aesthetic: l.aesthetic, utility: l.utility, sharpness: l.sharpness, light: l.light, clipped: l.clipped, dhash: String(l.dhash, radix: 16), document: l.document, text: l.text, millis: l.millis, faces: l.faces.map { .init(box: [$0.x, $0.y, $0.w, $0.h], quality: $0.quality, pixels: $0.pixels, points: $0.points, vector: $0.vector, model: $0.model) }) out.write(try encoder.encode(row) + Data([0x0A])) done += 1 if done % 500 == 0 { Log.write("albums-vision-mac: \(done) looked, \(Int(Double(done) / Date().timeIntervalSince(started) * 60)) a minute") } } try out.close() Log.write("albums-vision-mac: \(done) looked in \(Int(Date().timeIntervalSince(started))) s, \(closerLooks) looked at closer for faces")