feat(image-to-text): clean json export to image_file and prompt only, sort queue sequentially by filename, and auto-resize images >720px on upload

This commit is contained in:
2026-10-29 13:27:57 +05:30
parent eb7b549785
commit 47c35da17e
4 changed files with 280 additions and 54 deletions
+183 -30
View File
@@ -237,38 +237,172 @@
// ── Unified Intake & Queue Management ──
function addFilesToQueue(files) {
const validExts = [".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tiff", ".tif", ".avif"];
let addedCount = 0;
// Helper: Naturally sort queue items sequentially by file name
function sortQueueByFilename() {
state.queue.sort((a, b) =>
a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: "base" })
);
}
for (const file of files) {
// Helper: Resize image to max 720px right when uploaded if greater than 720px
function prepareAndResizeImage(file, maxDimension = 720) {
return new Promise((resolve) => {
const validExts = [".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tiff", ".tif", ".avif"];
const ext = "." + file.name.split(".").pop().toLowerCase();
if (!validExts.includes(ext) && !file.type.startsWith("image/")) continue;
if (!validExts.includes(ext) && !file.type.startsWith("image/")) {
resolve(null);
return;
}
const previewUrl = URL.createObjectURL(file);
state.queue.push({
id: "q_" + Date.now() + "_" + Math.random().toString(36).slice(2, 7),
file: file,
serverPath: null,
name: file.name,
size: file.size,
previewUrl: previewUrl,
status: "pending", // "pending" | "processing" | "done" | "failed"
result: "",
elapsed: 0,
width: 0,
height: 0,
error: ""
});
addedCount++;
const img = new Image();
const objectUrl = URL.createObjectURL(file);
img.onload = () => {
const origW = img.naturalWidth || img.width;
const origH = img.naturalHeight || img.height;
// If within 720 on both sides, keep original file untouched
if (origW <= maxDimension && origH <= maxDimension) {
resolve({
file: file,
previewUrl: objectUrl,
width: origW,
height: origH,
name: file.name,
size: file.size,
resized: false
});
return;
}
// If greater than 720, downscale preserving aspect ratio
let targetW = origW;
let targetH = origH;
if (origW >= origH) {
targetW = maxDimension;
targetH = Math.max(1, Math.round((origH * maxDimension) / origW));
} else {
targetH = maxDimension;
targetW = Math.max(1, Math.round((origW * maxDimension) / origH));
}
const canvas = document.createElement("canvas");
canvas.width = targetW;
canvas.height = targetH;
const ctx = canvas.getContext("2d", { alpha: false });
ctx.imageSmoothingEnabled = true;
ctx.imageSmoothingQuality = "high";
ctx.drawImage(img, 0, 0, targetW, targetH);
let mimeType = file.type;
if (!mimeType || mimeType === "" || mimeType.includes("bmp") || mimeType.includes("tiff")) {
mimeType = "image/jpeg";
}
canvas.toBlob(
(blob) => {
URL.revokeObjectURL(objectUrl);
if (!blob) {
resolve({
file: file,
previewUrl: URL.createObjectURL(file),
width: origW,
height: origH,
name: file.name,
size: file.size,
resized: false
});
return;
}
const resizedFile = new File([blob], file.name, {
type: blob.type || mimeType,
lastModified: Date.now()
});
const previewUrl = URL.createObjectURL(resizedFile);
resolve({
file: resizedFile,
previewUrl: previewUrl,
width: targetW,
height: targetH,
name: file.name,
size: resizedFile.size,
resized: true,
origW: origW,
origH: origH
});
},
mimeType,
0.92
);
};
img.onerror = () => {
URL.revokeObjectURL(objectUrl);
resolve(null);
};
img.src = objectUrl;
});
}
async function addFilesToQueue(files) {
if (!files || files.length === 0) return;
const totalFiles = files.length;
if (totalFiles > 12) {
showToast(`Uploading & optimizing ${totalFiles} images (auto-resizing >720px)...`, "info");
}
let addedCount = 0;
let resizedCount = 0;
// Process in concurrent batches of 6 for high performance & responsive UI
const batchSize = 6;
for (let i = 0; i < files.length; i += batchSize) {
const batch = files.slice(i, i + batchSize);
const preparedBatch = await Promise.all(batch.map((f) => prepareAndResizeImage(f, 720)));
for (const prep of preparedBatch) {
if (!prep) continue;
if (prep.resized) resizedCount++;
state.queue.push({
id: "q_" + Date.now() + "_" + Math.random().toString(36).slice(2, 7),
file: prep.file,
serverPath: null,
name: prep.name,
size: prep.size,
previewUrl: prep.previewUrl,
status: "pending", // "pending" | "processing" | "done" | "failed"
result: "",
elapsed: 0,
width: prep.width,
height: prep.height,
resized: prep.resized,
error: ""
});
addedCount++;
}
}
if (addedCount > 0) {
showToast(`Loaded ${addedCount} image(s)`, "info");
// Sort queue sequentially by filename
sortQueueByFilename();
if (resizedCount > 0) {
showToast(`Loaded ${addedCount} images (${resizedCount} resized to max 720px)`, "info");
} else {
showToast(`Loaded ${addedCount} image(s)`, "info");
}
updateQueueUI();
}
}
window.addFilesToStudio = addFilesToQueue;
window.getStudioQueue = () => state.queue;
function addServerFilesToQueue(filesList) {
let addedCount = 0;
for (const f of filesList) {
@@ -284,11 +418,14 @@
elapsed: 0,
width: 0,
height: 0,
resized: false,
error: ""
});
addedCount++;
}
if (addedCount > 0) {
// Sort queue sequentially by filename
sortQueueByFilename();
showToast(`Loaded ${addedCount} image(s) from server path`, "info");
updateQueueUI();
}
@@ -797,6 +934,10 @@
const saveSidecar = saveSidecarCb.checked;
const skipExisting = skipExistingCb.checked;
// Ensure queue is strictly sorted sequentially by filename before processing
sortQueueByFilename();
updateQueueUI();
const total = state.queue.length;
// Process queue items sequentially so user sees immediate results per item
@@ -946,12 +1087,18 @@
};
downloadTxtBtn.addEventListener("click", () => downloadSingleTxt());
// 3. Single JSON
downloadJsonBtn.addEventListener("click", () => {
const item = state.queue[state.selectedIndex];
// 3. Single JSON (Strictly image_file and prompt only)
window.downloadSingleJson = function (itemId) {
const item = state.queue.find((x) => x.id === itemId) || state.queue[state.selectedIndex];
if (!item || !item.result) return;
downloadBlob(JSON.stringify(item, null, 2), `${item.name.replace(/\.[^/.]+$/, "")}_output.json`, "application/json");
});
const stem = item.name.replace(/\.[^/.]+$/, "");
const cleanJson = {
image_file: item.name,
prompt: item.result
};
downloadBlob(JSON.stringify(cleanJson, null, 2), `${stem}.json`, "application/json");
};
downloadJsonBtn.addEventListener("click", () => downloadSingleJson());
// 4. Batch ZIP of All Sidecars (Direct Rock-solid Server Zip Generation)
downloadAllZipBtn.addEventListener("click", async () => {
@@ -1008,14 +1155,20 @@
downloadBlob(csv, `batch_results_${Date.now()}.csv`, "text/csv;charset=utf-8");
});
// 6. Batch JSON Export
// 6. Batch JSON Export (Strictly image_file and prompt only)
exportJsonBtn.addEventListener("click", () => {
const doneItems = state.queue.filter((x) => x.status === "done" || x.status === "failed");
const doneItems = state.queue.filter((x) => x.status === "done" && x.result);
if (doneItems.length === 0) {
showToast("No processed results to export", "error");
return;
}
downloadBlob(JSON.stringify(doneItems, null, 2), `batch_results_${Date.now()}.json`, "application/json");
const cleanJsonList = doneItems.map((it) => ({
image_file: it.name,
prompt: it.result
}));
downloadBlob(JSON.stringify(cleanJsonList, null, 2), `dataset_prompts_${Date.now()}.json`, "application/json");
});
// 7. Copy All Prompts