3a67070063
Adds optional LLM-based OCR as an alternative to Tesseract for reading machine IDs from photos. Backend (server.py): - New run_ocr_llm() function calls OpenCode Go API (mimo-v2-omni model) - Auto-falls back to Tesseract if API key missing or call fails - Endpoints /api/analyze and /api/bulk-process accept ?ocr_engine=llm query param (default: tesseract) and ?ocr_model for model override - Configurable via env vars: OPENCODE_GO_API_KEY, LLM_OCR_MODEL - Requires User-Agent: Hermes-Agent/1.0 header for OpenCode Go API Frontend (static/index.html): - Toggle checkbox 'Use LLM OCR' in the UI - OCR engine badge shown in results (llm vs tesseract + model name) - getOcrParams() helper appends ?ocr_engine=llm to API calls Infrastructure: - .gitignore for uploads/ directory Closes: #2
790 lines
30 KiB
HTML
790 lines
30 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover">
|
|
<title>EXIF Scanner — Find GPS Photos</title>
|
|
<script src="https://cdn.jsdelivr.net/npm/exifr@7/dist/lite.umd.js"></script>
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
|
|
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
|
<style>
|
|
:root {
|
|
--bg: #0f172a; --card: #1e293b; --card2: #334155;
|
|
--text: #f1f5f9; --text2: #94a3b8; --text3: #64748b;
|
|
--accent: #3b82f6; --accent2: #2563eb; --green: #22c55e;
|
|
--amber: #f59e0b; --red: #ef4444; --border: #334155;
|
|
--radius: 12px; --radius-sm: 8px;
|
|
}
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background: var(--bg); color: var(--text);
|
|
max-width: 480px; margin: 0 auto; padding: 16px;
|
|
min-height: 100dvh; -webkit-tap-highlight-color: transparent;
|
|
overflow-x: hidden;
|
|
}
|
|
h1 { font-size: 20px; margin-bottom: 4px; }
|
|
.subtitle { font-size: 12px; color: var(--text2); margin-bottom: 16px; }
|
|
|
|
/* Upload area */
|
|
.upload-area {
|
|
border: 2px dashed var(--border); border-radius: var(--radius);
|
|
padding: 28px 20px; text-align: center; cursor: pointer;
|
|
transition: border-color 0.2s; background: var(--card);
|
|
margin-bottom: 16px;
|
|
}
|
|
.upload-area:active { border-color: var(--accent); background: var(--card2); }
|
|
.upload-area .icon { font-size: 36px; display: block; margin-bottom: 6px; }
|
|
.upload-area .label { font-size: 15px; font-weight: 600; }
|
|
.upload-area .hint { font-size: 11px; color: var(--text2); margin-top: 4px; }
|
|
|
|
/* Summary bar */
|
|
#summary {
|
|
display: none; background: var(--card); border-radius: var(--radius);
|
|
padding: 12px 14px; margin-bottom: 12px;
|
|
}
|
|
.summary-grid {
|
|
display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px;
|
|
text-align: center;
|
|
}
|
|
.summary-num { font-size: 24px; font-weight: 800; }
|
|
.summary-label { font-size: 10px; color: var(--text2); text-transform: uppercase; letter-spacing: 0.05em; }
|
|
.summary-gps { color: var(--green); }
|
|
.summary-nogps { color: var(--amber); }
|
|
.summary-total { color: var(--text); }
|
|
|
|
/* Photo grid */
|
|
#gallery {
|
|
display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
|
|
margin-bottom: 12px;
|
|
}
|
|
.photo-card {
|
|
position: relative; border-radius: var(--radius-sm); overflow: hidden;
|
|
aspect-ratio: 1; background: var(--card); cursor: pointer;
|
|
border: 2px solid var(--border); transition: border-color 0.15s;
|
|
}
|
|
.photo-card.selected { border-color: var(--accent); }
|
|
.photo-card img {
|
|
width: 100%; height: 100%; object-fit: cover; display: block;
|
|
}
|
|
.photo-card .overlay {
|
|
position: absolute; bottom: 0; left: 0; right: 0;
|
|
padding: 4px 6px; font-size: 10px; font-weight: 700;
|
|
display: flex; align-items: center; gap: 3px;
|
|
}
|
|
.overlay-gps { background: rgba(34,197,94,0.85); color: #000; }
|
|
.overlay-nogps { background: rgba(245,158,11,0.85); color: #000; }
|
|
.overlay-scanning { background: rgba(59,130,246,0.85); color: #fff; }
|
|
.photo-card .check {
|
|
position: absolute; top: 4px; right: 4px;
|
|
width: 22px; height: 22px; border-radius: 50%;
|
|
background: var(--accent); color: #fff;
|
|
display: none; align-items: center; justify-content: center;
|
|
font-size: 12px; font-weight: 700;
|
|
}
|
|
.photo-card.selected .check { display: flex; }
|
|
|
|
/* Detail card */
|
|
#detail {
|
|
display: none; background: var(--card); border-radius: var(--radius);
|
|
padding: 14px; margin-bottom: 12px;
|
|
}
|
|
#detailPreview { width: 100%; border-radius: var(--radius-sm); margin-bottom: 10px; }
|
|
.detail-section {
|
|
font-size: 13px; font-weight: 700; text-transform: uppercase;
|
|
letter-spacing: 0.05em; color: var(--text2); margin-bottom: 8px;
|
|
display: flex; align-items: center; gap: 6px;
|
|
}
|
|
.badge {
|
|
font-size: 10px; padding: 2px 8px; border-radius: 10px; font-weight: 600;
|
|
}
|
|
.badge-ok { background: var(--green); color: #000; }
|
|
.badge-warn { background: var(--amber); color: #000; }
|
|
.badge-fail { background: var(--red); color: #fff; }
|
|
.badge-info { background: var(--accent); color: #fff; }
|
|
|
|
.exif-row {
|
|
display: flex; justify-content: space-between; padding: 6px 0;
|
|
border-bottom: 1px solid var(--border); font-size: 13px;
|
|
}
|
|
.exif-row:last-child { border-bottom: none; }
|
|
.exif-key { color: var(--text2); flex-shrink: 0; margin-right: 12px; }
|
|
.exif-val { color: var(--text); text-align: right; word-break: break-all; }
|
|
|
|
.gps-coords {
|
|
font-family: 'SF Mono', 'Fira Code', monospace;
|
|
font-size: 16px; font-weight: 700; color: var(--green);
|
|
text-align: center; padding: 12px; background: var(--card2);
|
|
border-radius: var(--radius-sm); margin: 8px 0;
|
|
}
|
|
|
|
/* Buttons */
|
|
.btn {
|
|
display: block; width: 100%; padding: 14px; font-size: 15px;
|
|
font-weight: 600; border: none; border-radius: var(--radius-sm);
|
|
cursor: pointer; transition: opacity 0.15s;
|
|
-webkit-tap-highlight-color: transparent;
|
|
}
|
|
.btn:active { opacity: 0.8; }
|
|
.btn-primary { background: var(--accent); color: #fff; }
|
|
.btn-outline { background: transparent; border: 2px solid var(--border); color: var(--text); }
|
|
.btn-danger { background: transparent; border: 2px solid var(--red); color: var(--red); }
|
|
.btn:disabled { opacity: 0.4; pointer-events: none; }
|
|
.btn-row { display: flex; gap: 8px; margin-top: 8px; }
|
|
.btn-sm { padding: 8px 14px; font-size: 12px; width: auto; }
|
|
|
|
.spinner {
|
|
display: inline-block; width: 16px; height: 16px;
|
|
border: 2px solid var(--text3); border-top-color: var(--text);
|
|
border-radius: 50%; animation: spin 0.6s linear infinite;
|
|
vertical-align: middle; margin-right: 6px;
|
|
}
|
|
@keyframes spin { to { transform: rotate(360deg); } }
|
|
|
|
.empty { color: var(--text3); font-size: 12px; text-align: center; padding: 12px; font-style: italic; }
|
|
|
|
#fileInput { display: none; }
|
|
|
|
/* Filter bar */
|
|
#filterBar {
|
|
display: none; align-items: center; gap: 6px; margin-bottom: 10px;
|
|
}
|
|
.filter-chip {
|
|
font-size: 11px; font-weight: 600; padding: 5px 10px;
|
|
border-radius: 14px; border: 1px solid var(--border);
|
|
background: var(--card2); color: var(--text2);
|
|
cursor: pointer; -webkit-tap-highlight-color: transparent;
|
|
}
|
|
.filter-chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
|
|
|
|
/* Asset match card */
|
|
.asset-card {
|
|
background: var(--card2); border-radius: var(--radius-sm);
|
|
padding: 12px; margin-top: 10px; border-left: 3px solid var(--green);
|
|
}
|
|
.asset-card .asset-name {
|
|
font-size: 15px; font-weight: 700; margin-bottom: 4px;
|
|
}
|
|
.asset-card .asset-meta {
|
|
font-size: 12px; color: var(--text2);
|
|
display: flex; flex-wrap: wrap; gap: 8px;
|
|
}
|
|
.asset-card .asset-meta span {
|
|
background: var(--card); padding: 2px 8px; border-radius: 10px;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
/* Server section */
|
|
.section {
|
|
background: var(--card); border-radius: var(--radius);
|
|
padding: 14px; margin-bottom: 12px;
|
|
}
|
|
.ocr-text {
|
|
background: var(--card2); border-radius: var(--radius-sm);
|
|
padding: 10px; font-family: 'SF Mono', 'Fira Code', monospace;
|
|
font-size: 12px; white-space: pre-wrap; word-break: break-all;
|
|
max-height: 120px; overflow-y: auto; color: var(--text2);
|
|
margin-top: 6px;
|
|
}
|
|
|
|
/* OCR engine toggle */
|
|
.ocr-toggle {
|
|
display: flex; align-items: center; gap: 8px; margin-bottom: 8px;
|
|
font-size: 12px; color: var(--text2);
|
|
}
|
|
.ocr-toggle label { display: flex; align-items: center; gap: 4px; cursor: pointer; }
|
|
.ocr-toggle input[type="checkbox"] { accent-color: var(--accent); }
|
|
.engine-badge {
|
|
font-size: 10px; padding: 2px 8px; border-radius: 10px; font-weight: 600;
|
|
background: var(--card2); color: var(--text2); display: inline-block; margin-left: 4px;
|
|
}
|
|
.engine-badge.llm { background: var(--accent); color: #fff; }
|
|
.engine-badge.tesseract { background: var(--card2); color: var(--text2); }
|
|
|
|
/* Bulk results */
|
|
#mapContainer { height: 250px; border-radius: var(--radius-sm); margin-bottom: 10px; display: none; }
|
|
.bulk-card {
|
|
background: var(--card); border-radius: var(--radius-sm); padding: 10px;
|
|
margin-bottom: 8px; display: flex; align-items: center; gap: 10px;
|
|
border-left: 3px solid var(--border);
|
|
}
|
|
.bulk-card.gps-ready { border-left-color: var(--green); }
|
|
.bulk-card .bulk-thumb { width: 48px; height: 48px; border-radius: 6px; object-fit: cover; flex-shrink: 0; }
|
|
.bulk-card .bulk-info { flex: 1; min-width: 0; }
|
|
.bulk-card .bulk-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
.bulk-card .bulk-meta { font-size: 11px; color: var(--text2); }
|
|
.bulk-card .bulk-action { flex-shrink: 0; }
|
|
.btn-push {
|
|
font-size: 11px; font-weight: 700; padding: 6px 12px; border-radius: 14px;
|
|
border: none; cursor: pointer; -webkit-tap-highlight-color: transparent;
|
|
background: var(--green); color: #000;
|
|
}
|
|
.btn-push:disabled { opacity: 0.4; }
|
|
.btn-push.done { background: var(--card2); color: var(--text3); }
|
|
|
|
.match-badge {
|
|
display: inline-block; font-size: 10px; padding: 2px 8px; border-radius: 10px; font-weight: 600;
|
|
}
|
|
.match-badge.matched { background: var(--green); color: #000; }
|
|
.match-badge.no-match { background: var(--border); color: var(--text2); }
|
|
.match-badge.no-gps { background: var(--amber); color: #000; }
|
|
.match-badge.has-gps { background: var(--card2); color: var(--text2); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<h1>📸 EXIF Scanner</h1>
|
|
<p class="subtitle">Select multiple photos — instantly see which have GPS</p>
|
|
|
|
<div class="upload-area" id="uploadArea" onclick="document.getElementById('fileInput').click()">
|
|
<span class="icon">📱</span>
|
|
<span class="label">Tap to Select Photos</span>
|
|
<span class="hint">Pick from gallery — multi-select enabled</span>
|
|
</div>
|
|
<input type="file" id="fileInput" accept="image/*" multiple>
|
|
|
|
<!-- Summary stats -->
|
|
<div id="summary">
|
|
<div class="summary-grid">
|
|
<div><div class="summary-num summary-total" id="sumTotal">0</div><div class="summary-label">Total</div></div>
|
|
<div><div class="summary-num summary-gps" id="sumGps">0</div><div class="summary-label">📍 Have GPS</div></div>
|
|
<div><div class="summary-num summary-nogps" id="sumNoGps">0</div><div class="summary-label">⚠️ No GPS</div></div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Filter chips -->
|
|
<div id="filterBar">
|
|
<span class="filter-chip active" onclick="setFilter('all')">All</span>
|
|
<span class="filter-chip" onclick="setFilter('gps')">📍 GPS</span>
|
|
<span class="filter-chip" onclick="setFilter('nogps')">⚠️ No GPS</span>
|
|
</div>
|
|
|
|
<!-- Photo grid -->
|
|
<div id="gallery"></div>
|
|
|
|
<!-- Detail card (tap a photo to see) -->
|
|
<div id="detail">
|
|
<img id="detailPreview" alt="">
|
|
<div id="detailExif"></div>
|
|
<div style="margin-top:10px;">
|
|
<button class="btn btn-primary btn-sm" onclick="uploadSelected()">🔍 Upload & Run OCR</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Server results for single upload -->
|
|
<div class="section" id="serverSection" style="display:none;">
|
|
<div class="detail-section">🖥️ Server Results <span class="badge badge-info">PIL + OCR</span></div>
|
|
<div id="serverResults"></div>
|
|
</div>
|
|
|
|
<!-- OCR engine toggle -->
|
|
<div class="ocr-toggle" style="margin-bottom:4px;">
|
|
<label>
|
|
<input type="checkbox" id="ocrLlmToggle" onchange="onOcrToggle()">
|
|
🧠 Use LLM OCR (<code id="ocrModelLabel">mimo-v2-omni</code>)
|
|
</label>
|
|
</div>
|
|
|
|
<!-- Bulk process button -->
|
|
<button class="btn btn-primary" id="bulkBtn" style="display:none;margin-top:12px;" onclick="startBulkProcess()">
|
|
🔍 Bulk Process GPS Photos
|
|
</button>
|
|
|
|
<!-- Bulk results -->
|
|
<div class="section" id="bulkSection" style="display:none;">
|
|
<div class="detail-section">📊 Bulk Results
|
|
<span class="match-badge" id="bulkSummary" style="margin-left:auto;"></span>
|
|
</div>
|
|
<div id="mapContainer"></div>
|
|
<div id="bulkResults"></div>
|
|
</div>
|
|
|
|
<div class="btn-row" style="margin-top:8px;">
|
|
<button class="btn btn-outline btn-sm" id="clearBtn" style="display:none;" onclick="resetAll()">🔄 Clear All & Re-select</button>
|
|
</div>
|
|
|
|
<script>
|
|
let allPhotos = []; // {file, exif, hasGps, lat, lng, thumb}
|
|
let selectedIdx = -1;
|
|
let currentFilter = 'all';
|
|
|
|
document.getElementById('fileInput').addEventListener('change', async function(e) {
|
|
const files = Array.from(e.target.files || []);
|
|
if (!files.length) return;
|
|
|
|
document.getElementById('uploadArea').style.display = 'none';
|
|
document.getElementById('filterBar').style.display = 'flex';
|
|
document.getElementById('clearBtn').style.display = 'block';
|
|
|
|
// Show scanning state
|
|
document.getElementById('gallery').innerHTML = files.map((_, i) =>
|
|
'<div class="photo-card" id="card' + i + '">' +
|
|
'<div style="display:flex;align-items:center;justify-content:center;height:100%;color:var(--text3);font-size:24px;">' +
|
|
'<span class="spinner" style="width:24px;height:24px;"></span>' +
|
|
'</div>' +
|
|
'<div class="overlay overlay-scanning">Scanning...</div>' +
|
|
'</div>'
|
|
).join('');
|
|
|
|
// Scan in small batches — iOS Safari can't handle many concurrent file reads
|
|
const results = [];
|
|
const batchSize = 2;
|
|
for (let i = 0; i < files.length; i += batchSize) {
|
|
const batch = files.slice(i, i + batchSize);
|
|
const batchResults = await Promise.all(batch.map(f => scanPhoto(f)));
|
|
results.push(...batchResults);
|
|
}
|
|
|
|
allPhotos = results;
|
|
updateSummary();
|
|
renderGallery();
|
|
});
|
|
|
|
async function scanPhoto(file) {
|
|
const result = { file, exif: null, hasGps: false, lat: null, lng: null, thumb: null };
|
|
|
|
// Use object URL instead of readAsDataURL — avoids loading the entire file
|
|
// into memory as a base64 string (the main cause of iOS hangs with many photos)
|
|
result.thumb = URL.createObjectURL(file);
|
|
|
|
// Read EXIF
|
|
try {
|
|
const exif = await exifr.parse(file);
|
|
if (exif && Object.keys(exif).length > 0) {
|
|
result.exif = {};
|
|
for (const [k, v] of Object.entries(exif)) {
|
|
if (v !== undefined && v !== null) result.exif[k] = v;
|
|
}
|
|
if (exif.latitude !== undefined && exif.longitude !== undefined) {
|
|
result.hasGps = true;
|
|
result.lat = exif.latitude;
|
|
result.lng = exif.longitude;
|
|
}
|
|
}
|
|
} catch (_) { /* EXIF is best-effort */ }
|
|
|
|
return result;
|
|
}
|
|
|
|
function updateSummary() {
|
|
const gps = allPhotos.filter(p => p.hasGps).length;
|
|
const nogps = allPhotos.length - gps;
|
|
document.getElementById('sumTotal').textContent = allPhotos.length;
|
|
document.getElementById('sumGps').textContent = gps;
|
|
document.getElementById('sumNoGps').textContent = nogps;
|
|
document.getElementById('summary').style.display = 'block';
|
|
// Show bulk button if we have GPS photos
|
|
document.getElementById('bulkBtn').style.display = gps > 0 ? 'block' : 'none';
|
|
}
|
|
|
|
function setFilter(f) {
|
|
currentFilter = f;
|
|
document.querySelectorAll('.filter-chip').forEach(c => c.classList.remove('active'));
|
|
event.target.classList.add('active');
|
|
renderGallery();
|
|
}
|
|
|
|
function renderGallery() {
|
|
let filtered = allPhotos;
|
|
if (currentFilter === 'gps') filtered = allPhotos.filter(p => p.hasGps);
|
|
else if (currentFilter === 'nogps') filtered = allPhotos.filter(p => !p.hasGps);
|
|
|
|
const gallery = document.getElementById('gallery');
|
|
gallery.innerHTML = filtered.map(p => {
|
|
const idx = allPhotos.indexOf(p);
|
|
const selClass = idx === selectedIdx ? ' selected' : '';
|
|
const overlayClass = p.hasGps ? 'overlay-gps' : 'overlay-nogps';
|
|
const overlayText = p.hasGps ? '📍 ' + p.lat.toFixed(4) + ', ' + p.lng.toFixed(4) : '⚠️ No GPS';
|
|
return '<div class="photo-card' + selClass + '" id="card' + idx + '" onclick="showDetail(' + idx + ')">' +
|
|
'<img src="' + p.thumb + '" alt="">' +
|
|
'<div class="check">✓</div>' +
|
|
'<div class="overlay ' + overlayClass + '">' + overlayText + '</div>' +
|
|
'</div>';
|
|
}).join('');
|
|
|
|
if (filtered.length === 0) {
|
|
gallery.innerHTML = '<div class="empty" style="grid-column:1/-1;">No photos match filter</div>';
|
|
}
|
|
}
|
|
|
|
function showDetail(idx) {
|
|
selectedIdx = idx;
|
|
const p = allPhotos[idx];
|
|
if (!p) return;
|
|
|
|
renderGallery(); // update selection highlights
|
|
|
|
const detail = document.getElementById('detail');
|
|
detail.style.display = 'block';
|
|
document.getElementById('detailPreview').src = p.thumb;
|
|
document.getElementById('serverSection').style.display = 'none';
|
|
|
|
let html = '';
|
|
html += '<div class="exif-row"><span class="exif-key">Name</span><span class="exif-val">' + esc(p.file.name) + '</span></div>';
|
|
html += '<div class="exif-row"><span class="exif-key">Size</span><span class="exif-val">' + formatSize(p.file.size) + '</span></div>';
|
|
|
|
if (p.exif) {
|
|
const priority = ['Make', 'Model', 'DateTimeOriginal', 'Orientation'];
|
|
const keys = Object.keys(p.exif);
|
|
const sorted = [...new Set([...priority.filter(k => k in p.exif), ...keys.filter(k => !priority.includes(k))])];
|
|
|
|
for (const k of sorted) {
|
|
const v = p.exif[k];
|
|
const label = k.replace(/([A-Z])/g, ' $1').trim();
|
|
const display = Array.isArray(v) ? v.map(x => typeof x === 'number' ? x.toFixed(3) : x).join(', ') : String(v);
|
|
html += '<div class="exif-row"><span class="exif-key">' + esc(label) + '</span><span class="exif-val">' + esc(display) + '</span></div>';
|
|
}
|
|
|
|
if (p.hasGps) {
|
|
html = '<div class="gps-coords">📍 ' + p.lat.toFixed(6) + ', ' + p.lng.toFixed(6) + '</div>' + html;
|
|
} else {
|
|
html = '<div class="gps-coords" style="color:var(--amber);">⚠️ No GPS in EXIF</div>' + html;
|
|
}
|
|
} else {
|
|
html += '<div class="empty" style="margin-top:8px;">No EXIF data at all — file may have been stripped</div>';
|
|
}
|
|
|
|
document.getElementById('detailExif').innerHTML = html;
|
|
detail.scrollIntoView({ behavior: 'smooth' });
|
|
}
|
|
|
|
async function uploadSelected() {
|
|
if (selectedIdx < 0 || !allPhotos[selectedIdx]) return;
|
|
const file = allPhotos[selectedIdx].file;
|
|
|
|
const section = document.getElementById('serverSection');
|
|
const div = document.getElementById('serverResults');
|
|
section.style.display = 'block';
|
|
div.innerHTML = '<div style="text-align:center;padding:12px;"><span class="spinner"></span> Uploading & analyzing...</div>';
|
|
section.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
const fd = new FormData();
|
|
fd.append('file', file, file.name || 'photo.jpg');
|
|
|
|
try {
|
|
const resp = await fetch('/api/analyze' + getOcrParams(), { method: 'POST', body: fd });
|
|
const data = await resp.json();
|
|
|
|
let html = '';
|
|
html += '<div class="exif-row"><span class="exif-key">Saved as</span><span class="exif-val">' + esc(data.saved_as) + '</span></div>';
|
|
html += '<div class="exif-row"><span class="exif-key">File size</span><span class="exif-val">' + data.file_size_kb + ' KB</span></div>';
|
|
|
|
const exif = data.exif;
|
|
if (exif.gps) {
|
|
html = '<div class="gps-coords" style="margin:8px 0;">📍 Server GPS: ' + exif.gps.lat + ', ' + exif.gps.lng + '</div>' + html;
|
|
} else if (exif.has_exif) {
|
|
html = '<div class="gps-coords" style="color:var(--amber);margin:8px 0;">⚠️ Server found EXIF but no GPS</div>' + html;
|
|
} else {
|
|
html = '<div class="gps-coords" style="color:var(--red);margin:8px 0;">❌ Server found no EXIF — stripped during upload</div>' + html;
|
|
}
|
|
|
|
// EXIF tags
|
|
if (exif.has_exif) {
|
|
for (const [k, v] of Object.entries(exif.tags).slice(0, 8)) {
|
|
html += '<div class="exif-row"><span class="exif-key">' + esc(k) + '</span><span class="exif-val">' + esc(v) + '</span></div>';
|
|
}
|
|
}
|
|
|
|
// OCR
|
|
const ocr = data.ocr;
|
|
const engineCls = ocr.engine === 'llm' ? 'llm' : 'tesseract';
|
|
html += '<div style="margin-top:10px;font-weight:600;">🔤 OCR <span class="engine-badge ' + engineCls + '">' + esc(ocr.engine || 'tesseract') + (ocr.llm_model ? ' ' + ocr.llm_model : '') + '</span></div>';
|
|
if (ocr.raw_text) {
|
|
html += '<div class="ocr-text">' + esc(ocr.raw_text) + '</div>';
|
|
}
|
|
if (ocr.match_5dash6) {
|
|
const mid = ocr.match_5dash6.replace(/[^0-9]/g,'').slice(-5);
|
|
html += '<div style="margin-top:4px;color:var(--green);">✅ Matched: <strong>' + esc(ocr.match_5dash6) + '</strong> → machine ID: ' + mid + '</div>';
|
|
// Auto-lookup
|
|
lookupAsset(mid);
|
|
} else if (ocr.match_5plus) {
|
|
html += '<div style="margin-top:4px;color:var(--amber);">⚠️ Digits: <strong>' + esc(ocr.match_5plus) + '</strong> (no 5-6 pattern)</div>';
|
|
} else {
|
|
html += '<div style="margin-top:4px;color:var(--text3);">No machine ID found in image</div>';
|
|
}
|
|
|
|
div.innerHTML = html;
|
|
} catch (e) {
|
|
div.innerHTML = '<div class="empty">❌ Upload failed: ' + esc(e.message) + '</div>';
|
|
}
|
|
}
|
|
|
|
async function lookupAsset(machineId) {
|
|
const div = document.getElementById('serverResults');
|
|
try {
|
|
const resp = await fetch('/api/lookup?machine_id=' + encodeURIComponent(machineId));
|
|
const data = await resp.json();
|
|
if (data.found) {
|
|
const a = data.asset;
|
|
let extra = '';
|
|
if (a.latitude && a.longitude) {
|
|
extra += '<span>📍 ' + Number(a.latitude).toFixed(5) + ', ' + Number(a.longitude).toFixed(5) + '</span>';
|
|
}
|
|
if (a.address) extra += '<span>🏠 ' + esc(a.address) + '</span>';
|
|
if (a.building_name) extra += '<span>🏢 ' + esc(a.building_name) + '</span>';
|
|
if (a.floor) extra += '<span>📶 Floor ' + esc(a.floor) + '</span>';
|
|
if (a.room) extra += '<span>🚪 ' + esc(a.room) + '</span>';
|
|
|
|
div.innerHTML +=
|
|
'<div class="asset-card">' +
|
|
'<div class="asset-name">' + esc(a.name) + '</div>' +
|
|
'<div class="asset-meta">' +
|
|
'<span>🆔 ' + esc(a.machine_id) + '</span>' +
|
|
'<span>📦 ' + esc(a.category) + '</span>' +
|
|
'<span>' + (a.status === 'active' ? '🟢 Active' : '⚪ ' + esc(a.status)) + '</span>' +
|
|
extra +
|
|
'</div>' +
|
|
'</div>';
|
|
} else {
|
|
div.innerHTML +=
|
|
'<div style="margin-top:6px;color:var(--amber);font-size:12px;">⚠️ No asset found for machine ID <strong>' + esc(machineId) + '</strong></div>';
|
|
}
|
|
} catch (e) {
|
|
div.innerHTML +=
|
|
'<div style="margin-top:6px;color:var(--red);font-size:12px;">❌ Lookup error: ' + esc(e.message) + '</div>';
|
|
}
|
|
}
|
|
|
|
let bulkMap = null;
|
|
let bulkData = [];
|
|
|
|
async function startBulkProcess() {
|
|
const gpsPhotos = allPhotos.filter(p => p.hasGps);
|
|
if (!gpsPhotos.length) return;
|
|
|
|
const btn = document.getElementById('bulkBtn');
|
|
btn.disabled = true;
|
|
btn.innerHTML = '<span class="spinner"></span> Processing ' + gpsPhotos.length + ' photos...';
|
|
|
|
// Build FormData with all GPS photos
|
|
const fd = new FormData();
|
|
gpsPhotos.forEach(p => fd.append('files', p.file, p.file.name || 'photo.jpg'));
|
|
|
|
try {
|
|
const resp = await fetch('/api/bulk-process' + getOcrParams(), { method: 'POST', body: fd });
|
|
const data = await resp.json();
|
|
|
|
// Merge thumbnails back into results
|
|
bulkData = data.results.map((r, i) => ({
|
|
...r,
|
|
thumb: gpsPhotos[i]?.thumb || null,
|
|
clientLat: gpsPhotos[i]?.lat || null,
|
|
clientLng: gpsPhotos[i]?.lng || null,
|
|
}));
|
|
|
|
renderBulkResults(data.summary);
|
|
} catch (e) {
|
|
document.getElementById('bulkResults').innerHTML =
|
|
'<div class="empty">❌ Bulk process failed: ' + esc(e.message) + '</div>';
|
|
} finally {
|
|
btn.disabled = false;
|
|
btn.textContent = '🔍 Bulk Process GPS Photos';
|
|
}
|
|
}
|
|
|
|
function renderBulkResults(summary) {
|
|
const section = document.getElementById('bulkSection');
|
|
section.style.display = 'block';
|
|
|
|
// Summary badge
|
|
document.getElementById('bulkSummary').textContent =
|
|
summary.matched + ' matched, ' + summary.needs_gps + ' need GPS';
|
|
document.getElementById('bulkSummary').className =
|
|
'match-badge ' + (summary.matched > 0 ? 'matched' : 'no-match');
|
|
|
|
// Init map
|
|
const mapDiv = document.getElementById('mapContainer');
|
|
mapDiv.style.display = 'block';
|
|
if (bulkMap) bulkMap.remove();
|
|
bulkMap = L.map('mapContainer').setView([28.35, -81.55], 8);
|
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
attribution: '© OSM', maxZoom: 19
|
|
}).addTo(bulkMap);
|
|
|
|
const markers = [];
|
|
let html = '';
|
|
|
|
bulkData.forEach((item, i) => {
|
|
const asset = item.asset;
|
|
const hasGps = item.exif?.gps;
|
|
const gpsLat = hasGps ? item.exif.gps.lat : null;
|
|
const gpsLng = hasGps ? item.exif.gps.lng : null;
|
|
|
|
// Map marker for every GPS photo
|
|
if (gpsLat && gpsLng) {
|
|
const marker = L.marker([gpsLat, gpsLng])
|
|
.addTo(bulkMap)
|
|
.bindPopup(
|
|
(asset ? esc(asset.name) : 'Unknown machine') +
|
|
'<br>📸 ' + esc(item.filename || 'photo') +
|
|
'<br>📍 ' + gpsLat.toFixed(5) + ', ' + gpsLng.toFixed(5)
|
|
);
|
|
markers.push([gpsLat, gpsLng]);
|
|
}
|
|
|
|
// Asset marker if it already has GPS in DB
|
|
if (asset && asset.latitude && asset.longitude) {
|
|
L.marker([asset.latitude, asset.longitude], {
|
|
icon: L.divIcon({
|
|
className: 'cat-pin-icon',
|
|
html: '<div style="width:22px;height:22px;border-radius:50%;background:#3b82f6;' +
|
|
'display:flex;align-items:center;justify-content:center;font-size:11px;' +
|
|
'box-shadow:0 2px 5px rgba(0,0,0,0.5);border:2px solid #fff;">🏠</div>',
|
|
iconSize: [24, 24], iconAnchor: [12, 12]
|
|
})
|
|
}).addTo(bulkMap)
|
|
.bindPopup('<b>' + esc(asset.name) + '</b><br>Existing location');
|
|
}
|
|
|
|
// Card
|
|
const isGpsReady = item.needs_gps;
|
|
html += '<div class="bulk-card' + (isGpsReady ? ' gps-ready' : '') + '">';
|
|
|
|
if (item.thumb) {
|
|
html += '<img class="bulk-thumb" src="' + item.thumb + '" alt="">';
|
|
}
|
|
|
|
html += '<div class="bulk-info">';
|
|
|
|
if (asset) {
|
|
html += '<div class="bulk-name">' + esc(asset.name) + '</div>';
|
|
html += '<div class="bulk-meta">';
|
|
html += '<span class="match-badge matched">🆔 ' + esc(asset.machine_id) + '</span> ';
|
|
html += '<span class="match-badge ' + (isGpsReady ? 'no-gps' : 'has-gps') + '">';
|
|
if (asset.latitude && asset.longitude) {
|
|
html += '📍 Has GPS in DB';
|
|
} else if (isGpsReady) {
|
|
html += '📍 Photo GPS: ' + gpsLat.toFixed(4) + ', ' + gpsLng.toFixed(4);
|
|
} else {
|
|
html += '⚠️ No photo GPS';
|
|
}
|
|
html += '</span>';
|
|
html += '</div>';
|
|
} else if (item.machine_id) {
|
|
html += '<div class="bulk-name">Machine #' + esc(item.machine_id) + '</div>';
|
|
html += '<div class="bulk-meta"><span class="match-badge no-match">No DB match</span></div>';
|
|
} else {
|
|
html += '<div class="bulk-name">' + esc(item.filename || 'Photo') + '</div>';
|
|
html += '<div class="bulk-meta"><span class="match-badge no-match">No OCR match</span></div>';
|
|
}
|
|
|
|
html += '</div>'; // bulk-info
|
|
|
|
// Push button
|
|
html += '<div class="bulk-action">';
|
|
if (isGpsReady) {
|
|
html += '<button class="btn-push" id="pushBtn' + i + '" onclick="pushGpsToAsset(' + i + ')">📤 Push GPS</button>';
|
|
} else if (asset && asset.latitude && asset.longitude) {
|
|
html += '<button class="btn-push done">✓ In DB</button>';
|
|
}
|
|
html += '</div>';
|
|
|
|
html += '</div>'; // bulk-card
|
|
});
|
|
|
|
document.getElementById('bulkResults').innerHTML = html;
|
|
|
|
// Fit map to markers
|
|
if (markers.length) {
|
|
const bounds = L.latLngBounds(markers);
|
|
bulkMap.fitBounds(bounds, { padding: [30, 30], maxZoom: 15 });
|
|
}
|
|
|
|
// Invalidate map size after display
|
|
setTimeout(() => { if (bulkMap) bulkMap.invalidateSize(); }, 200);
|
|
|
|
section.scrollIntoView({ behavior: 'smooth' });
|
|
}
|
|
|
|
async function pushGpsToAsset(idx) {
|
|
const item = bulkData[idx];
|
|
if (!item || !item.asset || !item.needs_gps) return;
|
|
|
|
const btn = document.getElementById('pushBtn' + idx);
|
|
btn.disabled = true;
|
|
btn.textContent = '⏳ Pushing...';
|
|
|
|
const gps = item.exif.gps;
|
|
try {
|
|
const resp = await fetch('/api/push-gps', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
asset_id: item.asset.id,
|
|
latitude: gps.lat,
|
|
longitude: gps.lng,
|
|
}),
|
|
});
|
|
const data = await resp.json();
|
|
if (data.updated) {
|
|
btn.className = 'btn-push done';
|
|
btn.textContent = '✓ Pushed!';
|
|
// Add marker to map for pushed location
|
|
if (bulkMap) {
|
|
L.marker([gps.lat, gps.lng], {
|
|
icon: L.divIcon({
|
|
className: 'cat-pin-icon',
|
|
html: '<div style="width:22px;height:22px;border-radius:50%;background:#22c55e;' +
|
|
'display:flex;align-items:center;justify-content:center;font-size:11px;' +
|
|
'box-shadow:0 2px 5px rgba(0,0,0,0.5);border:2px solid #fff;">✓</div>',
|
|
iconSize: [24, 24], iconAnchor: [12, 12]
|
|
})
|
|
}).addTo(bulkMap)
|
|
.bindPopup('<b>' + esc(item.asset.name) + '</b><br>📍 Pushed: ' + gps.lat.toFixed(5) + ', ' + gps.lng.toFixed(5))
|
|
.openPopup();
|
|
}
|
|
} else {
|
|
btn.textContent = data.reason || 'Failed';
|
|
btn.style.background = 'var(--red)';
|
|
}
|
|
} catch (e) {
|
|
btn.textContent = 'Error';
|
|
btn.style.background = 'var(--red)';
|
|
}
|
|
}
|
|
|
|
function resetAll() {
|
|
// Revoke all blob URLs to prevent memory leaks
|
|
allPhotos.forEach(p => {
|
|
if (p.thumb && p.thumb.startsWith('blob:')) {
|
|
URL.revokeObjectURL(p.thumb);
|
|
}
|
|
});
|
|
allPhotos = [];
|
|
selectedIdx = -1;
|
|
currentFilter = 'all';
|
|
bulkData = [];
|
|
if (bulkMap) { bulkMap.remove(); bulkMap = null; }
|
|
document.getElementById('fileInput').value = '';
|
|
document.getElementById('uploadArea').style.display = '';
|
|
document.getElementById('gallery').innerHTML = '';
|
|
document.getElementById('summary').style.display = 'none';
|
|
document.getElementById('filterBar').style.display = 'none';
|
|
document.getElementById('detail').style.display = 'none';
|
|
document.getElementById('serverSection').style.display = 'none';
|
|
document.getElementById('bulkSection').style.display = 'none';
|
|
document.getElementById('bulkBtn').style.display = 'none';
|
|
document.getElementById('mapContainer').style.display = 'none';
|
|
document.getElementById('clearBtn').style.display = 'none';
|
|
}
|
|
|
|
function esc(s) {
|
|
const d = document.createElement('div');
|
|
d.textContent = String(s);
|
|
return d.innerHTML;
|
|
}
|
|
|
|
function getOcrParams() {
|
|
const useLlm = document.getElementById('ocrLlmToggle').checked;
|
|
if (!useLlm) return '';
|
|
return '?ocr_engine=llm';
|
|
}
|
|
|
|
function formatSize(bytes) {
|
|
if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' KB';
|
|
return (bytes / 1048576).toFixed(1) + ' MB';
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|