Document local setup and OCR workflow
This commit is contained in:
+705
@@ -0,0 +1,705 @@
|
||||
import "./style.css";
|
||||
import {
|
||||
isCalibrationReady,
|
||||
isRectInside,
|
||||
rectFromPoints,
|
||||
resolveFieldRect,
|
||||
} from "./parser.js";
|
||||
import {
|
||||
findAnchor,
|
||||
onOcrProgress,
|
||||
recognizeField,
|
||||
terminateVision,
|
||||
} from "./vision.js";
|
||||
|
||||
const STORAGE_KEY = "l2-market-parser.calibration.v1";
|
||||
const fieldNames = {
|
||||
item: "Название",
|
||||
price: "Цена",
|
||||
quantity: "Количество",
|
||||
text: "Текст",
|
||||
};
|
||||
|
||||
function emptyCalibration() {
|
||||
return { anchor: null, fields: [], threshold: 0.8 };
|
||||
}
|
||||
|
||||
function loadCalibration() {
|
||||
try {
|
||||
const saved = JSON.parse(localStorage.getItem(STORAGE_KEY));
|
||||
return saved?.anchor && Array.isArray(saved.fields)
|
||||
? { ...emptyCalibration(), ...saved }
|
||||
: emptyCalibration();
|
||||
} catch {
|
||||
return emptyCalibration();
|
||||
}
|
||||
}
|
||||
|
||||
const state = {
|
||||
sources: [],
|
||||
selectedSourceId: null,
|
||||
referenceCanvas: null,
|
||||
calibration: loadCalibration(),
|
||||
drawing: null,
|
||||
results: [],
|
||||
busy: false,
|
||||
};
|
||||
|
||||
const elements = {
|
||||
status: document.querySelector("#global-status"),
|
||||
nav: [...document.querySelectorAll("[data-view]")],
|
||||
views: [...document.querySelectorAll(".view")],
|
||||
addSource: document.querySelector("#add-source"),
|
||||
sourceList: document.querySelector("#source-list"),
|
||||
calibrationSource: document.querySelector("#calibration-source"),
|
||||
captureReference: document.querySelector("#capture-reference"),
|
||||
referenceFile: document.querySelector("#reference-file"),
|
||||
canvasEmpty: document.querySelector("#canvas-empty"),
|
||||
canvas: document.querySelector("#calibration-canvas"),
|
||||
regionType: document.querySelector("#region-type"),
|
||||
regionLabel: document.querySelector("#region-label"),
|
||||
drawHint: document.querySelector("#draw-hint"),
|
||||
threshold: document.querySelector("#match-threshold"),
|
||||
thresholdValue: document.querySelector("#threshold-value"),
|
||||
regionCount: document.querySelector("#region-count"),
|
||||
regionList: document.querySelector("#region-list"),
|
||||
saveCalibration: document.querySelector("#save-calibration"),
|
||||
clearCalibration: document.querySelector("#clear-calibration"),
|
||||
recognizeAll: document.querySelector("#recognize-all"),
|
||||
copyResults: document.querySelector("#copy-results"),
|
||||
resultsList: document.querySelector("#results-list"),
|
||||
};
|
||||
|
||||
function setStatus(message, tone = "neutral") {
|
||||
elements.status.textContent = message;
|
||||
elements.status.dataset.tone = tone;
|
||||
}
|
||||
|
||||
function showView(name) {
|
||||
elements.nav.forEach((button) => {
|
||||
const active = button.dataset.view === name;
|
||||
button.classList.toggle("is-active", active);
|
||||
active
|
||||
? button.setAttribute("aria-current", "page")
|
||||
: button.removeAttribute("aria-current");
|
||||
});
|
||||
|
||||
elements.views.forEach((view) => {
|
||||
const active = view.id === `view-${name}`;
|
||||
view.classList.toggle("is-active", active);
|
||||
view.hidden = !active;
|
||||
});
|
||||
}
|
||||
|
||||
function selectedSource() {
|
||||
return state.sources.find((source) => source.id === state.selectedSourceId);
|
||||
}
|
||||
|
||||
async function waitForVideo(video) {
|
||||
if (video.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && video.videoWidth) return;
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
const timeout = window.setTimeout(() => reject(new Error("Нет кадров от выбранного окна")), 5000);
|
||||
video.addEventListener(
|
||||
"loadeddata",
|
||||
() => {
|
||||
window.clearTimeout(timeout);
|
||||
resolve();
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async function addSource() {
|
||||
if (!navigator.mediaDevices?.getDisplayMedia) {
|
||||
setStatus("Браузер не поддерживает захват экрана", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setStatus("Выбери окно игры в системном окне", "working");
|
||||
const stream = await navigator.mediaDevices.getDisplayMedia({
|
||||
video: { frameRate: { ideal: 5, max: 10 } },
|
||||
audio: false,
|
||||
});
|
||||
const track = stream.getVideoTracks()[0];
|
||||
const video = document.createElement("video");
|
||||
video.autoplay = true;
|
||||
video.muted = true;
|
||||
video.playsInline = true;
|
||||
video.srcObject = stream;
|
||||
await video.play();
|
||||
await waitForVideo(video);
|
||||
|
||||
const source = {
|
||||
id: crypto.randomUUID(),
|
||||
name: track.label || `Окно ${state.sources.length + 1}`,
|
||||
stream,
|
||||
video,
|
||||
active: true,
|
||||
};
|
||||
|
||||
track.addEventListener("ended", () => {
|
||||
source.active = false;
|
||||
renderSources();
|
||||
setStatus(`Захват «${source.name}» остановлен`, "neutral");
|
||||
});
|
||||
|
||||
state.sources.push(source);
|
||||
state.selectedSourceId = source.id;
|
||||
renderSources();
|
||||
setStatus(`Добавлено: ${source.name}`, "success");
|
||||
} catch (error) {
|
||||
if (error.name === "NotAllowedError") {
|
||||
setStatus("Захват отменён или не разрешён", "error");
|
||||
} else {
|
||||
setStatus(error.message || "Не удалось добавить окно", "error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeSource(sourceId) {
|
||||
const source = state.sources.find((item) => item.id === sourceId);
|
||||
source?.stream.getTracks().forEach((track) => track.stop());
|
||||
state.sources = state.sources.filter((item) => item.id !== sourceId);
|
||||
state.selectedSourceId = state.sources.find((item) => item.active)?.id ?? null;
|
||||
renderSources();
|
||||
}
|
||||
|
||||
function chooseSource(sourceId) {
|
||||
state.selectedSourceId = sourceId;
|
||||
renderSources();
|
||||
}
|
||||
|
||||
function sourceEmptyState() {
|
||||
const empty = document.createElement("div");
|
||||
empty.className = "empty-state empty-state--wide";
|
||||
const title = document.createElement("strong");
|
||||
title.textContent = "Нет подключённых окон";
|
||||
const text = document.createElement("span");
|
||||
text.textContent = "Нажми «Добавить окно» и выбери первое окно Lineage II.";
|
||||
empty.append(title, text);
|
||||
return empty;
|
||||
}
|
||||
|
||||
function renderSources() {
|
||||
elements.sourceList.replaceChildren();
|
||||
|
||||
if (!state.sources.length) {
|
||||
elements.sourceList.append(sourceEmptyState());
|
||||
}
|
||||
|
||||
state.sources.forEach((source, index) => {
|
||||
const row = document.createElement("article");
|
||||
row.className = "source-row";
|
||||
row.classList.toggle("is-selected", source.id === state.selectedSourceId);
|
||||
|
||||
const preview = document.createElement("div");
|
||||
preview.className = "source-row__preview";
|
||||
const video = document.createElement("video");
|
||||
video.autoplay = true;
|
||||
video.muted = true;
|
||||
video.playsInline = true;
|
||||
video.srcObject = source.stream;
|
||||
preview.append(video);
|
||||
|
||||
const info = document.createElement("div");
|
||||
info.className = "source-row__info";
|
||||
const kicker = document.createElement("span");
|
||||
kicker.textContent = `Источник ${index + 1}`;
|
||||
const name = document.createElement("h3");
|
||||
name.textContent = source.name;
|
||||
const stateText = document.createElement("p");
|
||||
stateText.textContent = source.active
|
||||
? `${source.video.videoWidth} × ${source.video.videoHeight}, захват активен`
|
||||
: "Захват остановлен";
|
||||
info.append(kicker, name, stateText);
|
||||
|
||||
const actions = document.createElement("div");
|
||||
actions.className = "source-row__actions";
|
||||
const calibrate = document.createElement("button");
|
||||
calibrate.className = "button";
|
||||
calibrate.type = "button";
|
||||
calibrate.textContent = "Калибровать";
|
||||
calibrate.disabled = !source.active;
|
||||
calibrate.addEventListener("click", () => {
|
||||
chooseSource(source.id);
|
||||
showView("calibration");
|
||||
});
|
||||
const select = document.createElement("button");
|
||||
select.className = "button button--quiet";
|
||||
select.type = "button";
|
||||
select.textContent = source.id === state.selectedSourceId ? "Выбрано" : "Выбрать";
|
||||
select.disabled = !source.active || source.id === state.selectedSourceId;
|
||||
select.addEventListener("click", () => chooseSource(source.id));
|
||||
const remove = document.createElement("button");
|
||||
remove.className = "button button--quiet";
|
||||
remove.type = "button";
|
||||
remove.textContent = "Убрать";
|
||||
remove.addEventListener("click", () => removeSource(source.id));
|
||||
actions.append(calibrate, select, remove);
|
||||
|
||||
row.append(preview, info, actions);
|
||||
elements.sourceList.append(row);
|
||||
});
|
||||
|
||||
elements.calibrationSource.replaceChildren();
|
||||
if (!state.sources.some((source) => source.active)) {
|
||||
const option = document.createElement("option");
|
||||
option.value = "";
|
||||
option.textContent = "Нет активных окон";
|
||||
elements.calibrationSource.append(option);
|
||||
} else {
|
||||
state.sources
|
||||
.filter((source) => source.active)
|
||||
.forEach((source) => {
|
||||
const option = document.createElement("option");
|
||||
option.value = source.id;
|
||||
option.textContent = source.name;
|
||||
option.selected = source.id === state.selectedSourceId;
|
||||
elements.calibrationSource.append(option);
|
||||
});
|
||||
}
|
||||
|
||||
elements.captureReference.disabled = !selectedSource()?.active;
|
||||
}
|
||||
|
||||
async function captureSource(source) {
|
||||
if (!source?.active) throw new Error("Сначала выбери активное окно");
|
||||
await waitForVideo(source.video);
|
||||
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = source.video.videoWidth;
|
||||
canvas.height = source.video.videoHeight;
|
||||
canvas.getContext("2d", { willReadFrequently: true }).drawImage(source.video, 0, 0);
|
||||
return canvas;
|
||||
}
|
||||
|
||||
function setReference(canvas) {
|
||||
state.referenceCanvas = canvas;
|
||||
elements.canvas.width = canvas.width;
|
||||
elements.canvas.height = canvas.height;
|
||||
elements.canvas.hidden = false;
|
||||
elements.canvasEmpty.hidden = true;
|
||||
drawCalibration();
|
||||
setStatus(`Снимок готов: ${canvas.width} × ${canvas.height}`, "success");
|
||||
}
|
||||
|
||||
async function captureReference() {
|
||||
try {
|
||||
setStatus("Делаю снимок выбранного окна", "working");
|
||||
setReference(await captureSource(selectedSource()));
|
||||
} catch (error) {
|
||||
setStatus(error.message, "error");
|
||||
}
|
||||
}
|
||||
|
||||
async function loadReferenceFile(file) {
|
||||
if (!file) return;
|
||||
|
||||
try {
|
||||
const bitmap = await createImageBitmap(file);
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = bitmap.width;
|
||||
canvas.height = bitmap.height;
|
||||
canvas.getContext("2d", { willReadFrequently: true }).drawImage(bitmap, 0, 0);
|
||||
bitmap.close();
|
||||
setReference(canvas);
|
||||
} catch {
|
||||
setStatus("Не удалось открыть изображение", "error");
|
||||
}
|
||||
}
|
||||
|
||||
function canvasPoint(event) {
|
||||
const bounds = elements.canvas.getBoundingClientRect();
|
||||
return {
|
||||
x: Math.max(
|
||||
0,
|
||||
Math.min(elements.canvas.width, ((event.clientX - bounds.left) / bounds.width) * elements.canvas.width),
|
||||
),
|
||||
y: Math.max(
|
||||
0,
|
||||
Math.min(elements.canvas.height, ((event.clientY - bounds.top) / bounds.height) * elements.canvas.height),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
function cropCanvas(source, rect) {
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = rect.width;
|
||||
canvas.height = rect.height;
|
||||
canvas
|
||||
.getContext("2d", { willReadFrequently: true })
|
||||
.drawImage(source, rect.x, rect.y, rect.width, rect.height, 0, 0, rect.width, rect.height);
|
||||
return canvas;
|
||||
}
|
||||
|
||||
function addRegion(rect) {
|
||||
const type = elements.regionType.value;
|
||||
|
||||
if (type === "anchor") {
|
||||
state.calibration.anchor = {
|
||||
...rect,
|
||||
image: cropCanvas(state.referenceCanvas, rect).toDataURL("image/png"),
|
||||
};
|
||||
state.calibration.fields = [];
|
||||
elements.regionType.value = "item";
|
||||
updateDrawHint();
|
||||
setStatus("Якорь выбран. Теперь отметь поля", "success");
|
||||
} else {
|
||||
const anchor = state.calibration.anchor;
|
||||
if (!anchor) {
|
||||
setStatus("Сначала выдели якорь", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
const sameTypeCount = state.calibration.fields.filter((field) => field.type === type).length;
|
||||
state.calibration.fields.push({
|
||||
id: crypto.randomUUID(),
|
||||
type,
|
||||
label: elements.regionLabel.value.trim() || `${fieldNames[type]} ${sameTypeCount + 1}`,
|
||||
offsetX: rect.x - anchor.x,
|
||||
offsetY: rect.y - anchor.y,
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
});
|
||||
elements.regionLabel.value = "";
|
||||
setStatus("Область добавлена", "success");
|
||||
}
|
||||
|
||||
renderCalibration();
|
||||
}
|
||||
|
||||
function fieldAbsoluteRect(field) {
|
||||
return resolveFieldRect(
|
||||
{ x: state.calibration.anchor.x, y: state.calibration.anchor.y },
|
||||
field,
|
||||
);
|
||||
}
|
||||
|
||||
function drawBox(context, rect, label, color, dashed = false) {
|
||||
context.save();
|
||||
context.strokeStyle = color;
|
||||
context.fillStyle = color;
|
||||
context.lineWidth = Math.max(2, elements.canvas.width / 900);
|
||||
context.setLineDash(dashed ? [10, 7] : []);
|
||||
context.strokeRect(rect.x, rect.y, rect.width, rect.height);
|
||||
|
||||
if (label) {
|
||||
context.font = `${Math.max(13, elements.canvas.width / 90)}px system-ui`;
|
||||
const width = context.measureText(label).width + 14;
|
||||
const y = Math.max(0, rect.y - 25);
|
||||
context.fillRect(rect.x, y, width, 25);
|
||||
context.fillStyle = "oklch(0.98 0.004 250)";
|
||||
context.fillText(label, rect.x + 7, y + 17);
|
||||
}
|
||||
context.restore();
|
||||
}
|
||||
|
||||
function drawCalibration() {
|
||||
if (!state.referenceCanvas) return;
|
||||
const context = elements.canvas.getContext("2d");
|
||||
context.clearRect(0, 0, elements.canvas.width, elements.canvas.height);
|
||||
context.drawImage(state.referenceCanvas, 0, 0);
|
||||
|
||||
if (state.calibration.anchor) {
|
||||
drawBox(context, state.calibration.anchor, "Якорь", "oklch(0.72 0.16 65)");
|
||||
state.calibration.fields.forEach((field) => {
|
||||
drawBox(context, fieldAbsoluteRect(field), field.label, "oklch(0.65 0.17 250)");
|
||||
});
|
||||
}
|
||||
|
||||
if (state.drawing) {
|
||||
const rect = rectFromPoints(state.drawing.start, state.drawing.current);
|
||||
drawBox(context, rect, "", "oklch(0.75 0.18 150)", true);
|
||||
}
|
||||
}
|
||||
|
||||
function updateDrawHint() {
|
||||
const hints = {
|
||||
anchor: "Протяни рамку вокруг небольшого постоянного элемента торгового окна.",
|
||||
item: "Выдели только строку с названием предмета.",
|
||||
price: "Выдели цену вместе с разделителями разрядов.",
|
||||
quantity: "Выдели количество предметов.",
|
||||
text: "Выдели одну строку текста.",
|
||||
};
|
||||
elements.drawHint.textContent = hints[elements.regionType.value];
|
||||
}
|
||||
|
||||
function renderCalibration() {
|
||||
elements.threshold.value = String(state.calibration.threshold);
|
||||
elements.thresholdValue.value = state.calibration.threshold.toFixed(2);
|
||||
elements.regionCount.textContent = String(
|
||||
state.calibration.fields.length + (state.calibration.anchor ? 1 : 0),
|
||||
);
|
||||
elements.regionList.replaceChildren();
|
||||
|
||||
if (!state.calibration.anchor) {
|
||||
const note = document.createElement("p");
|
||||
note.className = "muted-copy";
|
||||
note.textContent = "Якорь ещё не выбран.";
|
||||
elements.regionList.append(note);
|
||||
} else {
|
||||
const anchor = document.createElement("div");
|
||||
anchor.className = "region-row";
|
||||
anchor.innerHTML = "<span><strong>Якорь</strong><small>Шаблон поиска</small></span>";
|
||||
elements.regionList.append(anchor);
|
||||
}
|
||||
|
||||
state.calibration.fields.forEach((field) => {
|
||||
const row = document.createElement("div");
|
||||
row.className = "region-row";
|
||||
const copy = document.createElement("span");
|
||||
const name = document.createElement("strong");
|
||||
name.textContent = field.label;
|
||||
const type = document.createElement("small");
|
||||
type.textContent = fieldNames[field.type];
|
||||
copy.append(name, type);
|
||||
const remove = document.createElement("button");
|
||||
remove.className = "icon-button";
|
||||
remove.type = "button";
|
||||
remove.textContent = "×";
|
||||
remove.setAttribute("aria-label", `Удалить ${field.label}`);
|
||||
remove.addEventListener("click", () => {
|
||||
state.calibration.fields = state.calibration.fields.filter((item) => item.id !== field.id);
|
||||
renderCalibration();
|
||||
drawCalibration();
|
||||
});
|
||||
row.append(copy, remove);
|
||||
elements.regionList.append(row);
|
||||
});
|
||||
|
||||
drawCalibration();
|
||||
}
|
||||
|
||||
function saveCalibration() {
|
||||
if (!isCalibrationReady(state.calibration)) {
|
||||
setStatus("Для сохранения нужны якорь и хотя бы одно поле", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(state.calibration));
|
||||
setStatus("Калибровка сохранена в браузере", "success");
|
||||
} catch {
|
||||
setStatus("Браузер не смог сохранить калибровку", "error");
|
||||
}
|
||||
}
|
||||
|
||||
function clearCalibration() {
|
||||
state.calibration = emptyCalibration();
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
renderCalibration();
|
||||
setStatus("Калибровка сброшена", "neutral");
|
||||
}
|
||||
|
||||
function imageToCanvas(dataUrl) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const image = new Image();
|
||||
image.onload = () => {
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = image.naturalWidth;
|
||||
canvas.height = image.naturalHeight;
|
||||
canvas.getContext("2d").drawImage(image, 0, 0);
|
||||
resolve(canvas);
|
||||
};
|
||||
image.onerror = reject;
|
||||
image.src = dataUrl;
|
||||
});
|
||||
}
|
||||
|
||||
function renderResults() {
|
||||
elements.resultsList.replaceChildren();
|
||||
elements.copyResults.disabled = !state.results.length;
|
||||
|
||||
if (!state.results.length) {
|
||||
const empty = document.createElement("div");
|
||||
empty.className = "empty-state empty-state--wide";
|
||||
const title = document.createElement("strong");
|
||||
title.textContent = "Результатов пока нет";
|
||||
const text = document.createElement("span");
|
||||
text.textContent = "Подключи окна, сохрани калибровку и запусти распознавание.";
|
||||
empty.append(title, text);
|
||||
elements.resultsList.append(empty);
|
||||
return;
|
||||
}
|
||||
|
||||
state.results.forEach((result) => {
|
||||
const section = document.createElement("section");
|
||||
section.className = "result-group";
|
||||
const header = document.createElement("header");
|
||||
const heading = document.createElement("div");
|
||||
const title = document.createElement("h3");
|
||||
title.textContent = result.source;
|
||||
const meta = document.createElement("p");
|
||||
meta.textContent = result.found
|
||||
? `Якорь найден, совпадение ${(result.score * 100).toFixed(1)}%`
|
||||
: `Якорь не найден, лучшее совпадение ${(result.score * 100).toFixed(1)}%`;
|
||||
heading.append(title, meta);
|
||||
const badge = document.createElement("span");
|
||||
badge.className = "result-badge";
|
||||
badge.dataset.tone = result.found ? "success" : "error";
|
||||
badge.textContent = result.found ? "Распознано" : "Не найдено";
|
||||
header.append(heading, badge);
|
||||
section.append(header);
|
||||
|
||||
if (result.error) {
|
||||
const error = document.createElement("p");
|
||||
error.className = "result-error";
|
||||
error.textContent = result.error;
|
||||
section.append(error);
|
||||
} else if (result.fields?.length) {
|
||||
const table = document.createElement("table");
|
||||
table.innerHTML = "<thead><tr><th>Поле</th><th>Значение</th><th>OCR</th><th>Уверенность</th></tr></thead>";
|
||||
const body = document.createElement("tbody");
|
||||
result.fields.forEach((field) => {
|
||||
const row = document.createElement("tr");
|
||||
const label = document.createElement("td");
|
||||
label.textContent = field.label;
|
||||
const value = document.createElement("td");
|
||||
value.textContent =
|
||||
typeof field.value === "number" ? field.value.toLocaleString("ru-RU") : field.value || "Не распознано";
|
||||
const raw = document.createElement("td");
|
||||
raw.textContent = field.text || "пусто";
|
||||
const confidence = document.createElement("td");
|
||||
confidence.textContent = `${field.confidence}%`;
|
||||
row.append(label, value, raw, confidence);
|
||||
body.append(row);
|
||||
});
|
||||
table.append(body);
|
||||
section.append(table);
|
||||
}
|
||||
|
||||
elements.resultsList.append(section);
|
||||
});
|
||||
}
|
||||
|
||||
async function recognizeAll() {
|
||||
const activeSources = state.sources.filter((source) => source.active);
|
||||
if (!activeSources.length) {
|
||||
setStatus("Сначала добавь хотя бы одно окно", "error");
|
||||
return;
|
||||
}
|
||||
if (!isCalibrationReady(state.calibration)) {
|
||||
setStatus("Сначала сохрани якорь и области распознавания", "error");
|
||||
return;
|
||||
}
|
||||
if (state.busy) return;
|
||||
|
||||
state.busy = true;
|
||||
elements.recognizeAll.disabled = true;
|
||||
elements.recognizeAll.textContent = "Распознаю…";
|
||||
state.results = [];
|
||||
renderResults();
|
||||
|
||||
onOcrProgress((progress) => {
|
||||
if (progress.status === "recognizing text") {
|
||||
setStatus(`OCR: ${Math.round((progress.progress || 0) * 100)}%`, "working");
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const template = await imageToCanvas(state.calibration.anchor.image);
|
||||
|
||||
for (const source of activeSources) {
|
||||
setStatus(`Ищу торговое окно: ${source.name}`, "working");
|
||||
const frame = await captureSource(source);
|
||||
const match = await findAnchor(frame, template);
|
||||
const result = {
|
||||
source: source.name,
|
||||
capturedAt: new Date().toISOString(),
|
||||
score: match.score,
|
||||
found: match.score >= state.calibration.threshold,
|
||||
fields: [],
|
||||
};
|
||||
|
||||
if (!result.found) {
|
||||
result.error = "Проверь масштаб интерфейса, выбранный якорь или уменьши порог совпадения.";
|
||||
} else {
|
||||
for (const field of state.calibration.fields) {
|
||||
const rect = resolveFieldRect(match, field);
|
||||
if (!isRectInside(rect, frame.width, frame.height)) {
|
||||
result.fields.push({
|
||||
...field,
|
||||
text: "",
|
||||
value: null,
|
||||
confidence: 0,
|
||||
error: "Область вышла за границы кадра",
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
setStatus(`${source.name}: ${field.label}`, "working");
|
||||
result.fields.push({ ...field, ...(await recognizeField(frame, rect, field.type)) });
|
||||
}
|
||||
}
|
||||
|
||||
state.results.push(result);
|
||||
renderResults();
|
||||
}
|
||||
|
||||
setStatus("Распознавание завершено", "success");
|
||||
} catch (error) {
|
||||
setStatus(error.message || "Ошибка распознавания", "error");
|
||||
} finally {
|
||||
state.busy = false;
|
||||
elements.recognizeAll.disabled = false;
|
||||
elements.recognizeAll.textContent = "Распознать все";
|
||||
}
|
||||
}
|
||||
|
||||
async function copyResults() {
|
||||
try {
|
||||
await navigator.clipboard.writeText(JSON.stringify(state.results, null, 2));
|
||||
setStatus("JSON скопирован", "success");
|
||||
} catch {
|
||||
setStatus("Не удалось скопировать JSON", "error");
|
||||
}
|
||||
}
|
||||
|
||||
elements.nav.forEach((button) => button.addEventListener("click", () => showView(button.dataset.view)));
|
||||
elements.addSource.addEventListener("click", addSource);
|
||||
elements.calibrationSource.addEventListener("change", (event) => chooseSource(event.target.value));
|
||||
elements.captureReference.addEventListener("click", captureReference);
|
||||
elements.referenceFile.addEventListener("change", (event) => loadReferenceFile(event.target.files[0]));
|
||||
elements.regionType.addEventListener("change", updateDrawHint);
|
||||
elements.threshold.addEventListener("input", (event) => {
|
||||
state.calibration.threshold = Number(event.target.value);
|
||||
elements.thresholdValue.value = state.calibration.threshold.toFixed(2);
|
||||
});
|
||||
elements.saveCalibration.addEventListener("click", saveCalibration);
|
||||
elements.clearCalibration.addEventListener("click", clearCalibration);
|
||||
elements.recognizeAll.addEventListener("click", recognizeAll);
|
||||
elements.copyResults.addEventListener("click", copyResults);
|
||||
|
||||
elements.canvas.addEventListener("pointerdown", (event) => {
|
||||
if (!state.referenceCanvas) return;
|
||||
elements.canvas.setPointerCapture(event.pointerId);
|
||||
const point = canvasPoint(event);
|
||||
state.drawing = { start: point, current: point };
|
||||
drawCalibration();
|
||||
});
|
||||
elements.canvas.addEventListener("pointermove", (event) => {
|
||||
if (!state.drawing) return;
|
||||
state.drawing.current = canvasPoint(event);
|
||||
drawCalibration();
|
||||
});
|
||||
elements.canvas.addEventListener("pointerup", (event) => {
|
||||
if (!state.drawing) return;
|
||||
state.drawing.current = canvasPoint(event);
|
||||
const rect = rectFromPoints(state.drawing.start, state.drawing.current);
|
||||
state.drawing = null;
|
||||
if (rect.width >= 4 && rect.height >= 4) addRegion(rect);
|
||||
drawCalibration();
|
||||
});
|
||||
|
||||
window.addEventListener("beforeunload", () => {
|
||||
state.sources.forEach((source) => source.stream.getTracks().forEach((track) => track.stop()));
|
||||
terminateVision();
|
||||
});
|
||||
|
||||
renderSources();
|
||||
renderCalibration();
|
||||
renderResults();
|
||||
updateDrawHint();
|
||||
@@ -0,0 +1,52 @@
|
||||
export function rectFromPoints(start, end) {
|
||||
return {
|
||||
x: Math.round(Math.min(start.x, end.x)),
|
||||
y: Math.round(Math.min(start.y, end.y)),
|
||||
width: Math.round(Math.abs(end.x - start.x)),
|
||||
height: Math.round(Math.abs(end.y - start.y)),
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveFieldRect(anchorPosition, field) {
|
||||
return {
|
||||
x: Math.round(anchorPosition.x + field.offsetX),
|
||||
y: Math.round(anchorPosition.y + field.offsetY),
|
||||
width: Math.round(field.width),
|
||||
height: Math.round(field.height),
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeOcrText(text) {
|
||||
return text.replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
export function parseFieldValue(type, text) {
|
||||
const normalized = normalizeOcrText(text);
|
||||
|
||||
if (type !== "price" && type !== "quantity") {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const digits = normalized.replace(/\D/g, "");
|
||||
return digits ? Number(digits) : null;
|
||||
}
|
||||
|
||||
export function isCalibrationReady(calibration) {
|
||||
return Boolean(
|
||||
calibration?.anchor?.image &&
|
||||
calibration.anchor.width >= 4 &&
|
||||
calibration.anchor.height >= 4 &&
|
||||
calibration.fields?.length,
|
||||
);
|
||||
}
|
||||
|
||||
export function isRectInside(rect, width, height) {
|
||||
return (
|
||||
rect.x >= 0 &&
|
||||
rect.y >= 0 &&
|
||||
rect.width > 0 &&
|
||||
rect.height > 0 &&
|
||||
rect.x + rect.width <= width &&
|
||||
rect.y + rect.height <= height
|
||||
);
|
||||
}
|
||||
+754
@@ -0,0 +1,754 @@
|
||||
:root {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
|
||||
color: oklch(0.24 0.012 255);
|
||||
background: oklch(0.965 0.006 250);
|
||||
font-synthesis: none;
|
||||
--background: oklch(0.965 0.006 250);
|
||||
--surface: oklch(0.99 0.004 250);
|
||||
--surface-raised: oklch(1 0.003 250);
|
||||
--surface-muted: oklch(0.935 0.008 250);
|
||||
--text: oklch(0.24 0.012 255);
|
||||
--text-muted: oklch(0.52 0.018 255);
|
||||
--border: oklch(0.86 0.012 250);
|
||||
--border-strong: oklch(0.74 0.02 250);
|
||||
--accent: oklch(0.58 0.16 250);
|
||||
--accent-hover: oklch(0.52 0.17 250);
|
||||
--accent-soft: oklch(0.93 0.035 250);
|
||||
--success: oklch(0.54 0.13 155);
|
||||
--success-soft: oklch(0.93 0.04 155);
|
||||
--error: oklch(0.56 0.18 25);
|
||||
--error-soft: oklch(0.94 0.04 25);
|
||||
--warning: oklch(0.68 0.15 75);
|
||||
--focus: oklch(0.66 0.18 250);
|
||||
--shadow: 0 14px 40px oklch(0.32 0.025 250 / 0.08);
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
min-width: 320px;
|
||||
min-height: 100vh;
|
||||
background: var(--background);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
button,
|
||||
input,
|
||||
select {
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
button,
|
||||
select,
|
||||
input[type="range"],
|
||||
.file-button {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:focus-visible,
|
||||
input:focus-visible,
|
||||
select:focus-visible,
|
||||
.file-button:has(input:focus-visible) {
|
||||
outline: 3px solid color-mix(in oklch, var(--focus), transparent 50%);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.52;
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
h3,
|
||||
p {
|
||||
margin-block-start: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-block-end: 0;
|
||||
font-size: 1.45rem;
|
||||
line-height: 1.15;
|
||||
letter-spacing: -0.025em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin-block-end: 0.45rem;
|
||||
font-size: 1.75rem;
|
||||
line-height: 1.15;
|
||||
letter-spacing: -0.035em;
|
||||
}
|
||||
|
||||
h3 {
|
||||
margin-block-end: 0.25rem;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.app-shell {
|
||||
width: min(1480px, 100%);
|
||||
min-height: 100vh;
|
||||
margin-inline: auto;
|
||||
padding: 28px clamp(18px, 3vw, 48px) 56px;
|
||||
}
|
||||
|
||||
.app-header {
|
||||
display: flex;
|
||||
min-height: 62px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
margin-block-end: 5px;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.72rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.1em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.status {
|
||||
display: grid;
|
||||
width: 270px;
|
||||
min-height: 38px;
|
||||
place-items: center;
|
||||
padding: 8px 14px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 999px;
|
||||
background: var(--surface);
|
||||
color: var(--text-muted);
|
||||
font-size: 0.84rem;
|
||||
text-align: center;
|
||||
transition: background-color 180ms cubic-bezier(0.22, 1, 0.36, 1),
|
||||
border-color 180ms cubic-bezier(0.22, 1, 0.36, 1);
|
||||
}
|
||||
|
||||
.status[data-tone="working"] {
|
||||
border-color: color-mix(in oklch, var(--accent), transparent 55%);
|
||||
background: var(--accent-soft);
|
||||
color: var(--accent-hover);
|
||||
}
|
||||
|
||||
.status[data-tone="success"] {
|
||||
border-color: color-mix(in oklch, var(--success), transparent 55%);
|
||||
background: var(--success-soft);
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.status[data-tone="error"] {
|
||||
border-color: color-mix(in oklch, var(--error), transparent 55%);
|
||||
background: var(--error-soft);
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.step-nav {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
margin-block: 30px 42px;
|
||||
border-block-end: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.step-nav__item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 10px;
|
||||
min-height: 54px;
|
||||
border: 0;
|
||||
border-block-end: 2px solid transparent;
|
||||
background: transparent;
|
||||
color: var(--text-muted);
|
||||
font-weight: 650;
|
||||
}
|
||||
|
||||
.step-nav__item span {
|
||||
display: grid;
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
place-items: center;
|
||||
border: 1px solid var(--border-strong);
|
||||
border-radius: 50%;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.step-nav__item:hover {
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.step-nav__item.is-active {
|
||||
border-block-end-color: var(--accent);
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.step-nav__item.is-active span {
|
||||
border-color: var(--accent);
|
||||
background: var(--accent);
|
||||
color: oklch(0.98 0.004 250);
|
||||
}
|
||||
|
||||
.view[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.section-heading {
|
||||
display: flex;
|
||||
align-items: end;
|
||||
justify-content: space-between;
|
||||
gap: 32px;
|
||||
margin-block-end: 28px;
|
||||
}
|
||||
|
||||
.section-heading > div:first-child {
|
||||
max-width: 70ch;
|
||||
}
|
||||
|
||||
.section-heading p:last-child {
|
||||
margin-block-end: 0;
|
||||
color: var(--text-muted);
|
||||
line-height: 1.55;
|
||||
}
|
||||
|
||||
.section-heading--compact {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.button-row {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
gap: 9px;
|
||||
}
|
||||
|
||||
.button,
|
||||
select,
|
||||
input[type="text"] {
|
||||
min-height: 40px;
|
||||
border: 1px solid var(--border-strong);
|
||||
border-radius: 9px;
|
||||
background: var(--surface-raised);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 8px 14px;
|
||||
font-weight: 650;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.button:hover:not(:disabled) {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.button--primary {
|
||||
border-color: var(--accent);
|
||||
background: var(--accent);
|
||||
color: oklch(0.98 0.004 250);
|
||||
}
|
||||
|
||||
.button--primary:hover:not(:disabled) {
|
||||
border-color: var(--accent-hover);
|
||||
background: var(--accent-hover);
|
||||
}
|
||||
|
||||
.button--quiet {
|
||||
border-color: transparent;
|
||||
background: transparent;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.button--quiet:hover:not(:disabled) {
|
||||
border-color: var(--border);
|
||||
background: var(--surface-muted);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
select,
|
||||
input[type="text"] {
|
||||
padding-inline: 11px;
|
||||
}
|
||||
|
||||
.file-button input {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
overflow: hidden;
|
||||
clip-path: inset(50%);
|
||||
}
|
||||
|
||||
.source-list {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.source-row {
|
||||
display: grid;
|
||||
grid-template-columns: 220px minmax(220px, 1fr) auto;
|
||||
align-items: center;
|
||||
gap: 22px;
|
||||
padding: 14px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 14px;
|
||||
background: var(--surface);
|
||||
transition: border-color 180ms cubic-bezier(0.22, 1, 0.36, 1),
|
||||
box-shadow 180ms cubic-bezier(0.22, 1, 0.36, 1);
|
||||
}
|
||||
|
||||
.source-row.is-selected {
|
||||
border-color: color-mix(in oklch, var(--accent), transparent 35%);
|
||||
box-shadow: 0 0 0 3px color-mix(in oklch, var(--accent), transparent 88%);
|
||||
}
|
||||
|
||||
.source-row__preview {
|
||||
aspect-ratio: 16 / 9;
|
||||
overflow: hidden;
|
||||
border-radius: 8px;
|
||||
background: oklch(0.18 0.01 255);
|
||||
}
|
||||
|
||||
.source-row__preview video {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.source-row__info span {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.72rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.08em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.source-row__info h3 {
|
||||
margin-block: 5px;
|
||||
}
|
||||
|
||||
.source-row__info p {
|
||||
margin: 0;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.source-row__actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
gap: 8px;
|
||||
min-height: 260px;
|
||||
padding: 32px;
|
||||
color: var(--text-muted);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.empty-state strong {
|
||||
color: var(--text);
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.empty-state--wide {
|
||||
min-height: 300px;
|
||||
border: 1px dashed var(--border-strong);
|
||||
border-radius: 14px;
|
||||
background: color-mix(in oklch, var(--surface), transparent 30%);
|
||||
}
|
||||
|
||||
.calibration-layout {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) 310px;
|
||||
min-height: 620px;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 16px;
|
||||
background: var(--surface);
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
|
||||
.canvas-stage {
|
||||
display: grid;
|
||||
min-width: 0;
|
||||
min-height: 620px;
|
||||
max-height: calc(100vh - 260px);
|
||||
place-items: center;
|
||||
overflow: auto;
|
||||
padding: 24px;
|
||||
background-color: oklch(0.91 0.008 250);
|
||||
background-image: linear-gradient(45deg, oklch(0.88 0.008 250) 25%, transparent 25%),
|
||||
linear-gradient(-45deg, oklch(0.88 0.008 250) 25%, transparent 25%),
|
||||
linear-gradient(45deg, transparent 75%, oklch(0.88 0.008 250) 75%),
|
||||
linear-gradient(-45deg, transparent 75%, oklch(0.88 0.008 250) 75%);
|
||||
background-position: 0 0, 0 8px, 8px -8px, -8px 0;
|
||||
background-size: 16px 16px;
|
||||
}
|
||||
|
||||
#calibration-canvas {
|
||||
display: block;
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
border-radius: 4px;
|
||||
box-shadow: 0 8px 30px oklch(0.2 0.02 255 / 0.2);
|
||||
cursor: crosshair;
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
.inspector {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 20px;
|
||||
padding: 24px;
|
||||
border-inline-start: 1px solid var(--border);
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.field-stack {
|
||||
display: grid;
|
||||
gap: 7px;
|
||||
}
|
||||
|
||||
.field-stack label,
|
||||
.range-label label {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.78rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.hint {
|
||||
min-height: 58px;
|
||||
margin: 0;
|
||||
padding: 11px 12px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 9px;
|
||||
background: var(--surface-muted);
|
||||
color: var(--text-muted);
|
||||
font-size: 0.82rem;
|
||||
line-height: 1.45;
|
||||
}
|
||||
|
||||
.range-label {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.range-label output {
|
||||
color: var(--text);
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
input[type="range"] {
|
||||
width: 100%;
|
||||
accent-color: var(--accent);
|
||||
}
|
||||
|
||||
.inspector__section {
|
||||
min-height: 0;
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
border-block-start: 1px solid var(--border);
|
||||
padding-block-start: 18px;
|
||||
}
|
||||
|
||||
.inspector__title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-block-end: 10px;
|
||||
}
|
||||
|
||||
.inspector__title h3 {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.inspector__title span {
|
||||
display: grid;
|
||||
min-width: 24px;
|
||||
height: 24px;
|
||||
place-items: center;
|
||||
border-radius: 999px;
|
||||
background: var(--surface-muted);
|
||||
color: var(--text-muted);
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.region-list {
|
||||
display: grid;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.region-row {
|
||||
display: flex;
|
||||
min-height: 48px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 10px;
|
||||
padding: 8px 9px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.region-row:hover {
|
||||
background: var(--surface-muted);
|
||||
}
|
||||
|
||||
.region-row span {
|
||||
display: grid;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.region-row strong {
|
||||
font-size: 0.84rem;
|
||||
}
|
||||
|
||||
.region-row small {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.72rem;
|
||||
}
|
||||
|
||||
.icon-button {
|
||||
display: grid;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
place-items: center;
|
||||
border: 0;
|
||||
border-radius: 7px;
|
||||
background: transparent;
|
||||
color: var(--text-muted);
|
||||
font-size: 1.2rem;
|
||||
}
|
||||
|
||||
.icon-button:hover {
|
||||
background: var(--error-soft);
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.muted-copy {
|
||||
color: var(--text-muted);
|
||||
font-size: 0.82rem;
|
||||
}
|
||||
|
||||
.inspector__actions {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr auto;
|
||||
gap: 8px;
|
||||
padding-block-start: 2px;
|
||||
}
|
||||
|
||||
.results-list {
|
||||
display: grid;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
.result-group {
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 14px;
|
||||
background: var(--surface);
|
||||
}
|
||||
|
||||
.result-group > header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 20px;
|
||||
padding: 18px 20px;
|
||||
border-block-end: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.result-group header h3 {
|
||||
margin-block-end: 3px;
|
||||
}
|
||||
|
||||
.result-group header p {
|
||||
margin: 0;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.82rem;
|
||||
}
|
||||
|
||||
.result-badge {
|
||||
padding: 6px 9px;
|
||||
border-radius: 999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.result-badge[data-tone="success"] {
|
||||
background: var(--success-soft);
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.result-badge[data-tone="error"] {
|
||||
background: var(--error-soft);
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.result-error {
|
||||
margin: 0;
|
||||
padding: 22px 20px;
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: 0.86rem;
|
||||
}
|
||||
|
||||
th,
|
||||
td {
|
||||
padding: 12px 20px;
|
||||
border-block-end: 1px solid var(--border);
|
||||
text-align: start;
|
||||
}
|
||||
|
||||
th {
|
||||
background: var(--surface-muted);
|
||||
color: var(--text-muted);
|
||||
font-size: 0.72rem;
|
||||
letter-spacing: 0.05em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
tbody tr:last-child td {
|
||||
border-block-end: 0;
|
||||
}
|
||||
|
||||
td:nth-child(2),
|
||||
td:nth-child(4) {
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-weight: 650;
|
||||
}
|
||||
|
||||
@media (max-width: 960px) {
|
||||
.section-heading {
|
||||
align-items: start;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.button-row {
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.source-row {
|
||||
grid-template-columns: 180px 1fr;
|
||||
}
|
||||
|
||||
.source-row__actions {
|
||||
grid-column: 1 / -1;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.calibration-layout {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.inspector {
|
||||
border-block-start: 1px solid var(--border);
|
||||
border-inline-start: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.app-shell {
|
||||
padding-inline: 14px;
|
||||
}
|
||||
|
||||
.app-header {
|
||||
align-items: start;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.status {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.step-nav {
|
||||
margin-block: 22px 30px;
|
||||
}
|
||||
|
||||
.step-nav__item {
|
||||
flex-direction: column;
|
||||
gap: 5px;
|
||||
font-size: 0.76rem;
|
||||
}
|
||||
|
||||
.source-row {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.source-row__actions {
|
||||
grid-column: auto;
|
||||
flex-wrap: wrap;
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.button-row,
|
||||
.button-row > * {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.canvas-stage {
|
||||
min-height: 360px;
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.result-group {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
table {
|
||||
min-width: 680px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
color: oklch(0.9 0.009 250);
|
||||
background: oklch(0.18 0.012 255);
|
||||
--background: oklch(0.18 0.012 255);
|
||||
--surface: oklch(0.225 0.014 255);
|
||||
--surface-raised: oklch(0.255 0.015 255);
|
||||
--surface-muted: oklch(0.28 0.016 255);
|
||||
--text: oklch(0.9 0.009 250);
|
||||
--text-muted: oklch(0.68 0.018 250);
|
||||
--border: oklch(0.34 0.018 255);
|
||||
--border-strong: oklch(0.43 0.022 255);
|
||||
--accent: oklch(0.7 0.15 250);
|
||||
--accent-hover: oklch(0.75 0.13 250);
|
||||
--accent-soft: oklch(0.3 0.05 250);
|
||||
--success: oklch(0.72 0.12 155);
|
||||
--success-soft: oklch(0.29 0.045 155);
|
||||
--error: oklch(0.72 0.15 25);
|
||||
--error-soft: oklch(0.3 0.05 25);
|
||||
--warning: oklch(0.78 0.13 75);
|
||||
--shadow: 0 18px 48px oklch(0.08 0.015 255 / 0.28);
|
||||
}
|
||||
|
||||
.canvas-stage {
|
||||
background-color: oklch(0.16 0.01 255);
|
||||
background-image: linear-gradient(45deg, oklch(0.2 0.012 255) 25%, transparent 25%),
|
||||
linear-gradient(-45deg, oklch(0.2 0.012 255) 25%, transparent 25%),
|
||||
linear-gradient(45deg, transparent 75%, oklch(0.2 0.012 255) 75%),
|
||||
linear-gradient(-45deg, transparent 75%, oklch(0.2 0.012 255) 75%);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
*,
|
||||
*::before,
|
||||
*::after {
|
||||
scroll-behavior: auto !important;
|
||||
transition-duration: 0.01ms !important;
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
import { createWorker, PSM } from "tesseract.js";
|
||||
import { normalizeOcrText, parseFieldValue } from "./parser.js";
|
||||
|
||||
let cvPromise;
|
||||
let workerPromise;
|
||||
let progressListener = () => {};
|
||||
|
||||
export function onOcrProgress(listener) {
|
||||
progressListener = listener;
|
||||
}
|
||||
|
||||
async function getOpenCv() {
|
||||
if (!cvPromise) {
|
||||
cvPromise = import("@techstark/opencv-js").then(async ({ default: module }) => {
|
||||
const cv = module instanceof Promise ? await module : module;
|
||||
|
||||
if (cv.Mat) return cv;
|
||||
|
||||
await new Promise((resolve) => {
|
||||
cv.onRuntimeInitialized = resolve;
|
||||
});
|
||||
return cv;
|
||||
});
|
||||
}
|
||||
|
||||
return cvPromise;
|
||||
}
|
||||
|
||||
async function getOcrWorker() {
|
||||
if (!workerPromise) {
|
||||
workerPromise = createWorker("eng", 1, {
|
||||
logger: (message) => progressListener(message),
|
||||
});
|
||||
}
|
||||
|
||||
return workerPromise;
|
||||
}
|
||||
|
||||
export async function findAnchor(sourceCanvas, templateCanvas) {
|
||||
if (
|
||||
templateCanvas.width > sourceCanvas.width ||
|
||||
templateCanvas.height > sourceCanvas.height
|
||||
) {
|
||||
return { x: 0, y: 0, score: 0 };
|
||||
}
|
||||
|
||||
const cv = await getOpenCv();
|
||||
const source = cv.imread(sourceCanvas);
|
||||
const template = cv.imread(templateCanvas);
|
||||
const sourceGray = new cv.Mat();
|
||||
const templateGray = new cv.Mat();
|
||||
const result = new cv.Mat();
|
||||
|
||||
try {
|
||||
cv.cvtColor(source, sourceGray, cv.COLOR_RGBA2GRAY);
|
||||
cv.cvtColor(template, templateGray, cv.COLOR_RGBA2GRAY);
|
||||
cv.matchTemplate(sourceGray, templateGray, result, cv.TM_CCOEFF_NORMED);
|
||||
const match = cv.minMaxLoc(result);
|
||||
|
||||
return {
|
||||
x: match.maxLoc.x,
|
||||
y: match.maxLoc.y,
|
||||
score: Number.isFinite(match.maxVal) ? match.maxVal : 0,
|
||||
};
|
||||
} finally {
|
||||
source.delete();
|
||||
template.delete();
|
||||
sourceGray.delete();
|
||||
templateGray.delete();
|
||||
result.delete();
|
||||
}
|
||||
}
|
||||
|
||||
function prepareOcrCrop(sourceCanvas, rect) {
|
||||
const scale = 3;
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = rect.width * scale;
|
||||
canvas.height = rect.height * scale;
|
||||
|
||||
const context = canvas.getContext("2d", { willReadFrequently: true });
|
||||
context.imageSmoothingEnabled = false;
|
||||
context.filter = "grayscale(1) contrast(2)";
|
||||
context.drawImage(
|
||||
sourceCanvas,
|
||||
rect.x,
|
||||
rect.y,
|
||||
rect.width,
|
||||
rect.height,
|
||||
0,
|
||||
0,
|
||||
canvas.width,
|
||||
canvas.height,
|
||||
);
|
||||
|
||||
return canvas;
|
||||
}
|
||||
|
||||
export async function recognizeField(sourceCanvas, rect, type) {
|
||||
const worker = await getOcrWorker();
|
||||
const numbersOnly = type === "price" || type === "quantity";
|
||||
|
||||
await worker.setParameters({
|
||||
tessedit_pageseg_mode: PSM.SINGLE_LINE,
|
||||
tessedit_char_whitelist: numbersOnly ? "0123456789,. " : "",
|
||||
preserve_interword_spaces: "1",
|
||||
});
|
||||
|
||||
const image = prepareOcrCrop(sourceCanvas, rect);
|
||||
const { data } = await worker.recognize(image);
|
||||
const text = normalizeOcrText(data.text);
|
||||
|
||||
return {
|
||||
text,
|
||||
value: parseFieldValue(type, text),
|
||||
confidence: Math.round(data.confidence ?? 0),
|
||||
};
|
||||
}
|
||||
|
||||
export async function terminateVision() {
|
||||
if (!workerPromise) return;
|
||||
const worker = await workerPromise;
|
||||
await worker.terminate();
|
||||
workerPromise = null;
|
||||
}
|
||||
Reference in New Issue
Block a user