フォルダ内の写真から、中身が同じものと見た目が似ているものを検出して グループにまとめ、残す1枚を選んでほかをゴミ箱へ送れるようにする。 検出は安い判定から順に4段: - ファイルサイズが同じものだけ SHA-256 でバイト一致を確定 - dHash(知覚ハッシュ)のハミング距離で候補ペアを抽出 - 候補に出てくる画像だけ Vision の feature print で精査 - Union-Find で連結成分をグループ化 判定のしきい値は実写画像で距離を実測して決めた(docs/calibration.md)。 tools/release-update.sh で Gitea のリリースを更新する。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
59 lines
2.3 KiB
Swift
59 lines
2.3 KiB
Swift
import Foundation
|
|
import Testing
|
|
|
|
@testable import Photochk
|
|
|
|
@Suite("dHash")
|
|
struct DHashTests {
|
|
@Test("同じ値のハミング距離は 0")
|
|
func identical() {
|
|
#expect(DHash.hammingDistance(0xDEAD_BEEF_1234_5678, 0xDEAD_BEEF_1234_5678) == 0)
|
|
}
|
|
|
|
@Test("違うビットの数がそのまま距離になる")
|
|
func differingBits() {
|
|
#expect(DHash.hammingDistance(0, 0b1011) == 3)
|
|
#expect(DHash.hammingDistance(.max, 0) == 64)
|
|
#expect(DHash.hammingDistance(0b1100, 0b0110) == 2)
|
|
}
|
|
|
|
@Test("生成した画像から 64bit のハッシュが取れる")
|
|
func computesFromImage() throws {
|
|
let directory = try TestImages.makeTemporaryDirectory()
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
|
|
let url = directory.appendingPathComponent("base.jpg")
|
|
try TestImages.writeJPEG(TestImages.pattern(seed: 1), to: url, quality: 0.9)
|
|
|
|
let basics = try #require(DHash.compute(for: url))
|
|
#expect(basics.pixelWidth == TestImages.width)
|
|
#expect(basics.pixelHeight == TestImages.height)
|
|
|
|
// 同じファイルからは必ず同じ値が出る
|
|
let again = try #require(DHash.compute(for: url))
|
|
#expect(basics.dHash == again.dHash)
|
|
}
|
|
|
|
@Test("リサイズしてもハッシュはほとんど変わらず、別の絵柄とは大きく離れる")
|
|
func robustToResize() throws {
|
|
let directory = try TestImages.makeTemporaryDirectory()
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
|
|
let base = directory.appendingPathComponent("base.jpg")
|
|
let small = directory.appendingPathComponent("small.jpg")
|
|
let other = directory.appendingPathComponent("other.jpg")
|
|
|
|
let pattern = TestImages.pattern(seed: 7)
|
|
try TestImages.writeJPEG(pattern, to: base, quality: 0.9)
|
|
try TestImages.writeJPEG(TestImages.scaled(pattern, factor: 0.5), to: small, quality: 0.8)
|
|
try TestImages.writeJPEG(TestImages.pattern(seed: 99), to: other, quality: 0.9)
|
|
|
|
let baseHash = try #require(DHash.compute(for: base)).dHash
|
|
let smallHash = try #require(DHash.compute(for: small)).dHash
|
|
let otherHash = try #require(DHash.compute(for: other)).dHash
|
|
|
|
#expect(DHash.hammingDistance(baseHash, smallHash) <= 12)
|
|
#expect(DHash.hammingDistance(baseHash, otherHash) > 12)
|
|
}
|
|
}
|