Match EXIF lens names by brand and share team calibrations
EXIF folds the brand into the focal range ("EF75-300mm"), so split letters
from digits before scoring, keep the score a pure name score that a caller can
trust, and rank a same-mount-family lens higher without filtering. Auto-match
now declines below 0.75 rather than applying the best of a bad list.
Lenses the bundled database lacks can be measured in-house: /api/lens-profiles
stores calibrations in SQLite, the Optics panel edits them and marks them
"Team", and search treats them as ordinary entries.
🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
+53
-8
@@ -1,3 +1,4 @@
|
||||
import { TEAM_PREFIX, loadTeamProfiles, teamProfileToEntry } from './lensProfiles';
|
||||
import type { LensProfile, PhotoMeta } from '../types';
|
||||
|
||||
/**
|
||||
@@ -35,6 +36,8 @@ export interface LensEntry {
|
||||
d: DistortionSample[];
|
||||
t: TcaSample[];
|
||||
v: VignettingSample[];
|
||||
/** Set for a calibration the team stored on the server, not the bundle. */
|
||||
team?: boolean;
|
||||
}
|
||||
|
||||
export interface CameraEntry {
|
||||
@@ -68,6 +71,8 @@ export interface LensSearchResult {
|
||||
score: number;
|
||||
/** Bit mask of the corrections the calibration supports. */
|
||||
available: number;
|
||||
/** True when the profile came from the team's own calibrations. */
|
||||
team?: boolean;
|
||||
}
|
||||
|
||||
export const FLAG_TCA = 1;
|
||||
@@ -76,6 +81,19 @@ export const FLAG_DISTORTION = 8;
|
||||
|
||||
let databasePromise: Promise<LensDatabase> | null = null;
|
||||
|
||||
/**
|
||||
* Team calibrations are additive and optional: a signed-out or offline browser
|
||||
* still gets the bundled database, so a failed request is not an error.
|
||||
*/
|
||||
async function loadTeamEntries(): Promise<LensEntry[]> {
|
||||
try {
|
||||
const profiles = await loadTeamProfiles();
|
||||
return profiles.map(teamProfileToEntry);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function loadLensDatabase(): Promise<LensDatabase> {
|
||||
if (!databasePromise) {
|
||||
databasePromise = fetch('/lens-db/profiles.json')
|
||||
@@ -108,6 +126,7 @@ export function toResult(entry: LensEntry, score = 0): LensSearchResult {
|
||||
crop: entry.crop,
|
||||
score,
|
||||
available: availableFlags(entry),
|
||||
team: entry.team,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -157,7 +176,18 @@ function queryVariants(query: string): string[] {
|
||||
const raw = query.trim();
|
||||
if (raw.length < 2) return [];
|
||||
const compact = raw.replace(/(\d)\s*mm\b/gi, '$1mm').replace(/\s+/g, ' ');
|
||||
return [...new Set([raw, compact, compact.replace(/\s+/g, '')])].filter((text) => text.length >= 2);
|
||||
// EXIF folds the brand into the focal range ("EF75-300mm", "SEL2870", "RF24-105mm");
|
||||
// splitting letters from digits lets "ef 75" meet "EF 75-300mm".
|
||||
const split = compact.replace(/([a-z])(\d)/gi, '$1 $2');
|
||||
return [...new Set([raw, compact, compact.replace(/\s+/g, ''), split])].filter(
|
||||
(text) => text.length >= 2,
|
||||
);
|
||||
}
|
||||
|
||||
/** "Canon EF" and "Canon EF-S" are one family when ranking fits. */
|
||||
function sameMountFamily(a: string, b: string) {
|
||||
const family = (mount: string) => mount.trim().split(/\s+/)[0] ?? '';
|
||||
return family(a) !== '' && family(a) === family(b);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------- queries */
|
||||
@@ -171,17 +201,26 @@ export async function searchLens(
|
||||
const db = await loadLensDatabase();
|
||||
const camera = await findCamera(meta?.camera);
|
||||
const mount = camera?.mount ?? '';
|
||||
const scored: { entry: LensEntry; score: number }[] = [];
|
||||
for (const entry of db.lenses) {
|
||||
const scored: { entry: LensEntry; score: number; rank: number }[] = [];
|
||||
for (const entry of [...db.lenses, ...(await loadTeamEntries())]) {
|
||||
const score = scoreEntry(entry, variants);
|
||||
if (score < 0.5) continue;
|
||||
// A mount the camera actually uses is a tie-break, never a filter: a
|
||||
// profile that is in the database but filed under another mount must still
|
||||
// be findable.
|
||||
const fits = mount && entry.mounts.includes(mount) ? 0.3 : 0;
|
||||
scored.push({ entry, score: score + fits });
|
||||
// be findable. A crop Canon body lists Canon EF-S, so EF glass (and the
|
||||
// third-party lenses filed under it) only gets the family bonus.
|
||||
const fits = !mount
|
||||
? 0
|
||||
: entry.mounts.includes(mount)
|
||||
? 0.3
|
||||
: entry.mounts.some((other) => sameMountFamily(other, mount))
|
||||
? 0.1
|
||||
: 0;
|
||||
// `score` stays a pure name score so callers can trust a threshold;
|
||||
// `rank` is what orders the list.
|
||||
scored.push({ entry, score, rank: score + fits });
|
||||
}
|
||||
scored.sort((a, b) => b.score - a.score || a.entry.model.length - b.entry.model.length);
|
||||
scored.sort((a, b) => b.rank - a.rank || a.entry.model.length - b.entry.model.length);
|
||||
return scored.slice(0, 20).map(({ entry, score }) => toResult(entry, Math.min(3, score)));
|
||||
}
|
||||
|
||||
@@ -190,7 +229,9 @@ export async function findLensForMeta(
|
||||
): Promise<LensSearchResult | null> {
|
||||
if (!meta.lens) return null;
|
||||
const results = await searchLens(meta.lens, meta);
|
||||
return results.find((result) => result.score >= 0.75) ?? results[0] ?? null;
|
||||
// Applying the wrong profile is worse than applying none: the closest match
|
||||
// must share most of the EXIF name, not merely outrank the alternatives.
|
||||
return results.find((result) => result.score >= 0.75) ?? null;
|
||||
}
|
||||
|
||||
const cameraCache = new Map<string, CameraMatch | null>();
|
||||
@@ -228,6 +269,10 @@ export async function findCamera(camera?: string): Promise<CameraMatch | null> {
|
||||
export async function getLensEntry(profile: LensProfile): Promise<LensEntry | null> {
|
||||
const db = await loadLensDatabase();
|
||||
if (profile.id) {
|
||||
if (profile.id.startsWith(TEAM_PREFIX)) {
|
||||
const team = (await loadTeamEntries()).find((entry) => entry.id === profile.id);
|
||||
if (team) return team;
|
||||
}
|
||||
const byId = db.lenses.find((entry) => entry.id === profile.id);
|
||||
if (byId) return byId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user