Build-Fix: KitColor statt RGBColor (Namenskonflikt mit QuickDraw), öffentlicher JerseySuggestion-Init, k-means vereinfacht, CGFloat→Double; Warnungen (NSLock in async, Captures)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
1 parent
9dcef7bd88
commit
3aa5bdef0e
6 files changed
+72
-40
No files matched your search
@@ -406,7 +406,7 @@ final class AppState: ObservableObject {
|
||||
do {
|
||||
let result = try await IngestService.run(
|
||||
sources: sources, settings: settings, exiftool: et,
|
||||
onFolder: { url in
|
||||
onFolder: { [weak self] url in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
self.tasks.update(taskID, folder: url)
|
||||
@@ -419,7 +419,7 @@ final class AppState: ObservableObject {
|
||||
}
|
||||
}
|
||||
},
|
||||
progress: { p in
|
||||
progress: { [weak self] p in
|
||||
Task { @MainActor [weak self] in
|
||||
self?.tasks.update(taskID, done: p.done, total: p.total, detail: p.currentFile)
|
||||
}
|
||||
@@ -570,7 +570,7 @@ final class AppState: ObservableObject {
|
||||
done.n = snapshot.count - missing.count
|
||||
let total = snapshot.count
|
||||
await pool.readMetadata(groups: groups) { [weak self] meta in
|
||||
await MainActor.run {
|
||||
await MainActor.run { [weak self] in
|
||||
guard let self, self.metadataGeneration == generation else { return }
|
||||
for (path, fileMeta) in meta {
|
||||
guard let item = byPrimary[path], let idx = self.indexByID[item.id] else { continue }
|
||||
|
||||
@@ -55,9 +55,11 @@ struct JerseyRecognizer: Sendable {
|
||||
let team = color.flatMap { JerseyLogic.matchTeam(color: $0, region: region, teams: teams) }
|
||||
for hit in Self.readNumbers(crop) where hit.confidence >= minConfidence {
|
||||
// number box back to whole-image coordinates (top-left origin, normalized)
|
||||
let bx = (r.minX + hit.box.minX * r.width) / W
|
||||
let by = (r.minY + (1 - hit.box.maxY) * r.height) / H
|
||||
let box = [bx, by, hit.box.width * r.width / W, hit.box.height * r.height / H]
|
||||
let bx = (Double(r.minX) + Double(hit.box.minX) * Double(r.width)) / W
|
||||
let by = (Double(r.minY) + Double(1 - hit.box.maxY) * Double(r.height)) / H
|
||||
let bw = Double(hit.box.width) * Double(r.width) / W
|
||||
let bh = Double(hit.box.height) * Double(r.height) / H
|
||||
let box: [Double] = [bx, by, bw, bh]
|
||||
raw += suggestions(number: hit.number, team: team, confidence: hit.confidence,
|
||||
region: region, box: box, personArea: Double(p.width * p.height))
|
||||
}
|
||||
@@ -66,7 +68,7 @@ struct JerseyRecognizer: Sendable {
|
||||
// No person found (tight crop, blur): big numbers anywhere in the frame
|
||||
if people.isEmpty {
|
||||
for hit in Self.readNumbers(cg, minTextHeight: 0.04) where hit.confidence >= minConfidence + 0.1 {
|
||||
let box = [hit.box.minX, 1 - hit.box.maxY, hit.box.width, hit.box.height]
|
||||
let box: [Double] = [Double(hit.box.minX), Double(1 - hit.box.maxY), Double(hit.box.width), Double(hit.box.height)]
|
||||
raw += suggestions(number: hit.number, team: nil, confidence: hit.confidence * 0.8,
|
||||
region: .image, box: box, personArea: 0)
|
||||
}
|
||||
@@ -137,7 +139,7 @@ struct JerseyRecognizer: Sendable {
|
||||
|
||||
/// Dominant color of the kit area: 24×24 thumbnail, simple k-means (k = 3), largest cluster
|
||||
/// that is not grass green or skin-ish.
|
||||
static func dominantColor(_ img: CGImage) -> RGBColor? {
|
||||
static func dominantColor(_ img: CGImage) -> KitColor? {
|
||||
let n = 24
|
||||
var pixels = [UInt8](repeating: 0, count: n * n * 4)
|
||||
guard let ctx = CGContext(data: &pixels, width: n, height: n, bitsPerComponent: 8, bytesPerRow: n * 4,
|
||||
@@ -149,28 +151,42 @@ struct JerseyRecognizer: Sendable {
|
||||
guard let center = img.cropping(to: inner) else { return nil }
|
||||
ctx.interpolationQuality = .medium
|
||||
ctx.draw(center, in: CGRect(x: 0, y: 0, width: n, height: n))
|
||||
var pts: [RGBColor] = []
|
||||
var pts: [KitColor] = []
|
||||
for i in 0..<(n * n) {
|
||||
pts.append(RGBColor(r: Double(pixels[i * 4]) / 255, g: Double(pixels[i * 4 + 1]) / 255, b: Double(pixels[i * 4 + 2]) / 255))
|
||||
pts.append(KitColor(r: Double(pixels[i * 4]) / 255, g: Double(pixels[i * 4 + 1]) / 255, b: Double(pixels[i * 4 + 2]) / 255))
|
||||
}
|
||||
var centers = [pts[0], pts[pts.count / 2], pts[pts.count - 1]]
|
||||
var assign = [Int](repeating: 0, count: pts.count)
|
||||
for _ in 0..<8 {
|
||||
for (i, p) in pts.enumerated() {
|
||||
assign[i] = centers.indices.min { p.distance(to: centers[$0]) < p.distance(to: centers[$1]) } ?? 0
|
||||
var best = 0
|
||||
var bestD = Double.greatestFiniteMagnitude
|
||||
for (ci, c) in centers.enumerated() {
|
||||
let d = p.distance(to: c)
|
||||
if d < bestD { bestD = d; best = ci }
|
||||
}
|
||||
assign[i] = best
|
||||
}
|
||||
for c in centers.indices {
|
||||
let members = pts.indices.filter { assign[$0] == c }.map { pts[$0] }
|
||||
guard !members.isEmpty else { continue }
|
||||
let k = Double(members.count)
|
||||
centers[c] = RGBColor(r: members.reduce(0) { $0 + $1.r } / k, g: members.reduce(0) { $0 + $1.g } / k,
|
||||
b: members.reduce(0) { $0 + $1.b } / k)
|
||||
var sr = 0.0, sg = 0.0, sb = 0.0, count = 0
|
||||
for i in pts.indices where assign[i] == c {
|
||||
sr += pts[i].r; sg += pts[i].g; sb += pts[i].b; count += 1
|
||||
}
|
||||
guard count > 0 else { continue }
|
||||
let k = Double(count)
|
||||
centers[c] = KitColor(r: sr / k, g: sg / k, b: sb / k)
|
||||
}
|
||||
}
|
||||
let sizes = centers.indices.map { c in assign.filter { $0 == c }.count }
|
||||
let ranked = centers.indices.sorted { sizes[$0] > sizes[$1] }
|
||||
func isGrass(_ c: RGBColor) -> Bool { c.g > c.r * 1.25 && c.g > c.b * 1.25 && c.g > 0.25 && c.g < 0.75 && c.r < 0.45 }
|
||||
return ranked.map { centers[$0] }.first { !isGrass($0) } ?? centers[ranked[0]]
|
||||
var sizes = [Int](repeating: 0, count: centers.count)
|
||||
for a in assign { sizes[a] += 1 }
|
||||
let ranked: [Int] = centers.indices.sorted { sizes[$0] > sizes[$1] }
|
||||
for idx in ranked where !isGrass(centers[idx]) { return centers[idx] }
|
||||
return centers[ranked[0]]
|
||||
}
|
||||
|
||||
static func isGrass(_ c: KitColor) -> Bool {
|
||||
let greenish = c.g > c.r * 1.25 && c.g > c.b * 1.25
|
||||
return greenish && c.g > 0.25 && c.g < 0.75 && c.r < 0.45
|
||||
}
|
||||
|
||||
// MARK: Image
|
||||
|
||||
@@ -111,21 +111,21 @@ struct JerseySetupView: View {
|
||||
private func add(_ set: CodeSet) {
|
||||
let prefix = JerseyLogic.detectPrefix(codes: set.entries.map(\.code))
|
||||
// first team light, second dark as a starting point – adjust with the picker
|
||||
let defaults = [RGBColor(r: 0.1, g: 0.45, b: 0.25), RGBColor(r: 0.95, g: 0.95, b: 0.95), RGBColor(r: 0.1, g: 0.2, b: 0.6)]
|
||||
let defaults = [KitColor(r: 0.1, g: 0.45, b: 0.25), KitColor(r: 0.95, g: 0.95, b: 0.95), KitColor(r: 0.1, g: 0.2, b: 0.6)]
|
||||
let color = defaults[min(state.settings.jersey.teams.count, defaults.count - 1)]
|
||||
state.settings.jersey.teams.append(JerseyTeam(name: set.name, codeSetID: set.id, prefix: prefix, shirtColor: color))
|
||||
}
|
||||
|
||||
private func cg(_ c: RGBColor) -> CGColor { CGColor(srgbRed: c.r, green: c.g, blue: c.b, alpha: 1) }
|
||||
private func cg(_ c: KitColor) -> CGColor { CGColor(srgbRed: c.r, green: c.g, blue: c.b, alpha: 1) }
|
||||
|
||||
private func rgb(_ c: CGColor) -> RGBColor {
|
||||
private func rgb(_ c: CGColor) -> KitColor {
|
||||
let conv = c.converted(to: CGColorSpace(name: CGColorSpace.sRGB)!, intent: .defaultIntent, options: nil) ?? c
|
||||
let comps = conv.components ?? [0, 0, 0]
|
||||
if comps.count >= 3 { return RGBColor(r: Double(comps[0]), g: Double(comps[1]), b: Double(comps[2])) }
|
||||
return RGBColor(r: Double(comps[0]), g: Double(comps[0]), b: Double(comps[0])) // grayscale
|
||||
if comps.count >= 3 { return KitColor(r: Double(comps[0]), g: Double(comps[1]), b: Double(comps[2])) }
|
||||
return KitColor(r: Double(comps[0]), g: Double(comps[0]), b: Double(comps[0])) // grayscale
|
||||
}
|
||||
|
||||
private func cgBinding(_ b: Binding<RGBColor>) -> Binding<CGColor> {
|
||||
private func cgBinding(_ b: Binding<KitColor>) -> Binding<CGColor> {
|
||||
Binding(get: { cg(b.wrappedValue) }, set: { b.wrappedValue = rgb($0) })
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
import Foundation
|
||||
|
||||
/// Simple RGB color (0…1), Codable for settings.
|
||||
public struct RGBColor: Codable, Hashable, Sendable {
|
||||
public struct KitColor: Codable, Hashable, Sendable {
|
||||
public var r: Double, g: Double, b: Double
|
||||
public init(r: Double, g: Double, b: Double) { self.r = r; self.g = g; self.b = b }
|
||||
|
||||
/// Perceptual-ish distance (weighted RGB, "redmean"), 0 … ~1.
|
||||
public func distance(to o: RGBColor) -> Double {
|
||||
public func distance(to o: KitColor) -> Double {
|
||||
let rm = (r + o.r) / 2
|
||||
let dr = r - o.r, dg = g - o.g, db = b - o.b
|
||||
return sqrt((2 + rm) * dr * dr + 4 * dg * dg + (3 - rm) * db * db) / 3
|
||||
@@ -20,10 +20,10 @@ public struct JerseyTeam: Codable, Hashable, Identifiable, Sendable {
|
||||
public var codeSetID: UUID?
|
||||
/// Code = prefix + number, e.g. "w" + "10" → "w10"
|
||||
public var prefix: String
|
||||
public var shirtColor: RGBColor
|
||||
public var shortsColor: RGBColor?
|
||||
public var shirtColor: KitColor
|
||||
public var shortsColor: KitColor?
|
||||
|
||||
public init(name: String, codeSetID: UUID? = nil, prefix: String, shirtColor: RGBColor, shortsColor: RGBColor? = nil) {
|
||||
public init(name: String, codeSetID: UUID? = nil, prefix: String, shirtColor: KitColor, shortsColor: KitColor? = nil) {
|
||||
self.name = name; self.codeSetID = codeSetID; self.prefix = prefix
|
||||
self.shirtColor = shirtColor; self.shortsColor = shortsColor
|
||||
}
|
||||
@@ -44,6 +44,12 @@ public struct JerseySuggestion: Codable, Hashable, Identifiable, Sendable {
|
||||
public var box: [Double] // x, y, w, h
|
||||
/// Person size in the frame (main subject first)
|
||||
public var personArea: Double
|
||||
|
||||
public init(number: Int, teamID: UUID?, code: String?, replacement: String?, confidence: Double,
|
||||
regions: [JerseyRegion], box: [Double], personArea: Double) {
|
||||
self.number = number; self.teamID = teamID; self.code = code; self.replacement = replacement
|
||||
self.confidence = confidence; self.regions = regions; self.box = box; self.personArea = personArea
|
||||
}
|
||||
}
|
||||
|
||||
public enum JerseyLogic {
|
||||
@@ -72,7 +78,7 @@ public enum JerseyLogic {
|
||||
}
|
||||
|
||||
/// Team whose kit color is closest (shirt for shirt numbers, shorts or shirt for shorts numbers).
|
||||
public static func matchTeam(color: RGBColor, region: JerseyRegion, teams: [JerseyTeam], threshold: Double = 0.30) -> JerseyTeam? {
|
||||
public static func matchTeam(color: KitColor, region: JerseyRegion, teams: [JerseyTeam], threshold: Double = 0.30) -> JerseyTeam? {
|
||||
var best: (JerseyTeam, Double)?
|
||||
for t in teams {
|
||||
var d = color.distance(to: t.shirtColor)
|
||||
|
||||
@@ -161,19 +161,29 @@ public final class ImmichClient: @unchecked Sendable {
|
||||
|
||||
/// Finds or creates an album and returns its id.
|
||||
public func albumId(named name: String) async throws -> String {
|
||||
lock.lock(); let cached = albumCache[name]; lock.unlock()
|
||||
if let cached { return cached }
|
||||
if let cached = cachedAlbum(name) { return cached }
|
||||
if let list = try await send(try request("albums")) as? [[String: Any]],
|
||||
let existing = list.first(where: { ($0["albumName"] as? String) == name }), let id = existing["id"] as? String {
|
||||
lock.lock(); albumCache[name] = id; lock.unlock()
|
||||
cacheAlbum(name, id)
|
||||
return id
|
||||
}
|
||||
guard let created = try await send(try request("albums", method: "POST", json: ["albumName": name])) as? [String: Any],
|
||||
let id = created["id"] as? String else { throw ImmichError.decoding }
|
||||
lock.lock(); albumCache[name] = id; lock.unlock()
|
||||
cacheAlbum(name, id)
|
||||
return id
|
||||
}
|
||||
|
||||
// NSLock must not be used directly in async functions → small synchronous helpers.
|
||||
private func cachedAlbum(_ name: String) -> String? {
|
||||
lock.lock(); defer { lock.unlock() }
|
||||
return albumCache[name]
|
||||
}
|
||||
|
||||
private func cacheAlbum(_ name: String, _ id: String) {
|
||||
lock.lock(); defer { lock.unlock() }
|
||||
albumCache[name] = id
|
||||
}
|
||||
|
||||
public func add(assetIds: [String], toAlbum albumId: String) async throws {
|
||||
_ = try await send(try request("albums/\(albumId)/assets", method: "PUT", json: ["ids": assetIds]))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user