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
@@ -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
|
||||
|
||||
Reference in new issue
Block a user