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:
Lichtkasten DevandClaude Opus 5.5 committed 2026-11-04 01:16:47 +00:00
1 parent 9dcef7bd88
commit 3aa5bdef0e
6 files changed
+72 -40

No files matched your search

+3 -3
View File
@@ -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) })
}
}