Trikotnummern-Erkennung (Trikot + Hose, Vision on-device), Team über Trikotfarbe, Vorschläge mit Bestätigung im IPTC-Fenster (⌘J), Abzeichen + Filter
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
5505fa1f37
commit
9dcef7bd88
11 files changed
+788
-3
No files matched your search
@@ -0,0 +1,126 @@
|
||||
import Foundation
|
||||
|
||||
/// Simple RGB color (0…1), Codable for settings.
|
||||
public struct RGBColor: 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 {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// A team in the current match: which code team it uses, its code prefix ("w") and kit colors.
|
||||
public struct JerseyTeam: Codable, Hashable, Identifiable, Sendable {
|
||||
public var id = UUID()
|
||||
public var name: String
|
||||
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 init(name: String, codeSetID: UUID? = nil, prefix: String, shirtColor: RGBColor, shortsColor: RGBColor? = nil) {
|
||||
self.name = name; self.codeSetID = codeSetID; self.prefix = prefix
|
||||
self.shirtColor = shirtColor; self.shortsColor = shortsColor
|
||||
}
|
||||
}
|
||||
|
||||
public enum JerseyRegion: String, Codable, Sendable { case shirt = "Trikot", shorts = "Hose", image = "Bild" }
|
||||
|
||||
/// One suggestion for an image: "w10 – Svenja Huth (VfL Wolfsburg, 10)".
|
||||
public struct JerseySuggestion: Codable, Hashable, Identifiable, Sendable {
|
||||
public var id: String { "\(teamID?.uuidString ?? "?")-\(number)" }
|
||||
public var number: Int
|
||||
public var teamID: UUID?
|
||||
public var code: String? // resolved code, nil if team unknown
|
||||
public var replacement: String? // text from the code library
|
||||
public var confidence: Double // 0…1
|
||||
public var regions: [JerseyRegion]
|
||||
/// Normalized box of the number (top-left origin) for the overlay in the preview.
|
||||
public var box: [Double] // x, y, w, h
|
||||
/// Person size in the frame (main subject first)
|
||||
public var personArea: Double
|
||||
}
|
||||
|
||||
public enum JerseyLogic {
|
||||
/// Jersey numbers are 0…99. OCR confusions are fixed ("I0" → 10, "O7" → 07 → 7).
|
||||
public static func parseNumber(_ raw: String) -> Int? {
|
||||
var s = raw.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !s.isEmpty, s.count <= 3 else { return nil }
|
||||
let fixes: [Character: Character] = ["O": "0", "o": "0", "D": "0", "I": "1", "l": "1", "|": "1", "i": "1",
|
||||
"S": "5", "s": "5", "B": "8", "Z": "2", "z": "2", "G": "6", "g": "9", "q": "9", "T": "7"]
|
||||
s = String(s.map { fixes[$0] ?? $0 })
|
||||
s = s.filter { !$0.isWhitespace && $0 != "." && $0 != "," && $0 != "-" }
|
||||
guard (1...2).contains(s.count), s.allSatisfy(\.isNumber), let n = Int(s), (0...99).contains(n) else { return nil }
|
||||
// at least one real digit in the original (avoid "IO" from text)
|
||||
guard raw.contains(where: \.isNumber) else { return nil }
|
||||
return n
|
||||
}
|
||||
|
||||
/// Most common letter prefix of a team's codes ("w10", "w23" → "w").
|
||||
public static func detectPrefix(codes: [String]) -> String {
|
||||
var counts: [String: Int] = [:]
|
||||
for c in codes {
|
||||
let p = String(c.prefix { !$0.isNumber })
|
||||
if !p.isEmpty, c.dropFirst(p.count).allSatisfy(\.isNumber) { counts[p.lowercased(), default: 0] += 1 }
|
||||
}
|
||||
return counts.max { $0.value < $1.value }?.key ?? ""
|
||||
}
|
||||
|
||||
/// 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? {
|
||||
var best: (JerseyTeam, Double)?
|
||||
for t in teams {
|
||||
var d = color.distance(to: t.shirtColor)
|
||||
if region == .shorts, let s = t.shortsColor { d = min(d, color.distance(to: s)) }
|
||||
if best == nil || d < best!.1 { best = (t, d) }
|
||||
}
|
||||
guard let best, best.1 <= threshold else { return nil }
|
||||
// ambiguous if the second-best team is almost as close
|
||||
let others = teams.filter { $0.id != best.0.id }.map { min(color.distance(to: $0.shirtColor), $0.shortsColor.map { color.distance(to: $0) } ?? 9) }
|
||||
if let second = others.min(), second - best.1 < 0.05 { return nil }
|
||||
return best.0
|
||||
}
|
||||
|
||||
/// Pure name for "Persons shown": column 2 if the code file has one, else the replacement without "(…)".
|
||||
public static func personName(replacement: String, columns: [String]) -> String {
|
||||
if columns.count > 1, !columns[1].isEmpty { return columns[1] }
|
||||
if let p = replacement.firstIndex(of: "(") { return replacement[..<p].trimmingCharacters(in: .whitespaces) }
|
||||
return replacement
|
||||
}
|
||||
|
||||
/// Puts the accepted players into the first line of the caption (PM workflow: template starts with an empty line
|
||||
/// for the people, then "; Fußball, DFB-Pokal …"). Existing names in the first line are kept, duplicates skipped.
|
||||
public static func insertIntoCaption(_ caption: String, names: [String], separator: String = ", ") -> String {
|
||||
var lines = caption.components(separatedBy: "\n")
|
||||
if lines.isEmpty { lines = [""] }
|
||||
var first = lines[0]
|
||||
for n in names where !first.contains(n) {
|
||||
first = first.trimmingCharacters(in: .whitespaces).isEmpty ? n : first + separator + n
|
||||
}
|
||||
lines[0] = first
|
||||
return lines.joined(separator: "\n")
|
||||
}
|
||||
|
||||
/// Merges raw detections: same number + team from shirt and shorts → one suggestion with higher confidence.
|
||||
public static func merge(_ raw: [JerseySuggestion]) -> [JerseySuggestion] {
|
||||
var byID: [String: JerseySuggestion] = [:]
|
||||
var order: [String] = []
|
||||
for s in raw {
|
||||
if var e = byID[s.id] {
|
||||
e.confidence = min(1, max(e.confidence, s.confidence) + 0.15 * min(e.confidence, s.confidence))
|
||||
e.regions = Array(Set(e.regions + s.regions)).sorted { $0.rawValue < $1.rawValue }
|
||||
e.personArea = max(e.personArea, s.personArea)
|
||||
byID[s.id] = e
|
||||
} else {
|
||||
byID[s.id] = s
|
||||
order.append(s.id)
|
||||
}
|
||||
}
|
||||
return order.compactMap { byID[$0] }.sorted { ($0.personArea, $0.confidence) > ($1.personArea, $1.confidence) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user