-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
98 lines (86 loc) · 2.5 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const SHOW_NOT_FOUND = false;
const labels = [];
let parser = null;
async function convertBase64ToArrayBuffer(base64) {
return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
}
async function loadHtml2Llm() {
return html2llm.OmniParser.load({
yoloModelBytes: convertBase64ToArrayBuffer('__YOLO_MODEL__'),
nmsModelBytes: convertBase64ToArrayBuffer('__NMS_MODEL__'),
onnxWasmBytes: convertBase64ToArrayBuffer('__ONNX_WASM__'),
iouThreshold: 0.1,
boxThreshold: 0.05,
progressCallback: (stage, timeMs) => {
console.log('OmniParser', stage, timeMs);
}
});
}
function clearLabels() {
for (const label of labels) {
document.body.removeChild(label);
}
labels.length = 0;
}
function addLabel(d, color, text) {
const label = document.createElement('div');
label.style.position = 'absolute';
label.style.top = Math.floor(d.absY) + 'px';
label.style.left = Math.floor(d.absX) + 'px';
label.style.width = d.w + 'px';
label.style.height = d.h + 'px';
label.style.color = `rgb(${color})`;
label.style.backgroundColor = `rgba(${color}, 0.25)`;
label.style.fontWeight = 'bold';
label.style.border = `2px solid rgb(${color})`;
label.style.zIndex = '999999999';
label.style.overflow = 'hidden';
label.title = text;
label.innerText = text;
labels.push(label);
document.body.appendChild(label);
label.addEventListener(
'click',
() => {
document.body.removeChild(label);
},
false
);
}
function showLabels(extractedElements) {
for (const fe of extractedElements.foundElements) {
addLabel(fe, '255, 0, 0', `${fe.label} (${html2llm.elementTypeToString(fe.type)})`);
}
if (SHOW_NOT_FOUND) {
for (const b of extractedElements.notFoundBoxes) {
addLabel(b, '0, 0, 255', '?');
}
}
}
function loadBase64Image(base64) {
return new Promise(resolve => {
const img = new Image();
img.onload = () => resolve(img);
img.src = 'data:image/png;base64,' + base64;
});
}
window.runHtml2Llm = async screenshot => {
const image = await loadBase64Image(screenshot);
const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
const context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
if (!parser) {
parser = await loadHtml2Llm();
}
const result = await parser.process(canvas);
console.log('result', result);
const extractedElements = html2llm.ElementExtractor.extract(result, {
scrollX: scroll.x,
scrollY: scroll.y,
customDevicePixelRatio: 1
});
showLabels(extractedElements);
console.log('extractedElements', extractedElements);
};