Update README.md
Browse files
README.md
CHANGED
|
@@ -1,47 +1,51 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
const
|
| 22 |
-
const
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
const
|
| 26 |
-
const
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
const
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
```
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
library_name: transformers.js
|
| 4 |
+
---
|
| 5 |
+
Converted from [wybxc/DocLayout-YOLO-DocStructBench-onnx](https://huggingface.co/wybxc/DocLayout-YOLO-DocStructBench-onnx)
|
| 6 |
+
|
| 7 |
+
```ts
|
| 8 |
+
import {
|
| 9 |
+
AutoProcessor,
|
| 10 |
+
RawImage,
|
| 11 |
+
AutoModel,
|
| 12 |
+
env,
|
| 13 |
+
} from '@huggingface/transformers';
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
const model = await AutoModel.from_pretrained('darknoah99/DocLayout-YOLO-DocStructBench-onnx');
|
| 17 |
+
const processor = await AutoProcessor.from_pretrained('darknoah99/DocLayout-YOLO-DocStructBench-onnx', {});
|
| 18 |
+
let image;
|
| 19 |
+
if (isUrl(fileOrUrl)) image = await RawImage.fromURL(fileOrUrl);
|
| 20 |
+
else if (fs.statSync(fileOrUrl).isFile()) {
|
| 21 |
+
const data = fs.readFileSync(fileOrUrl);
|
| 22 |
+
const blob = new Blob([data]);
|
| 23 |
+
image = await RawImage.fromBlob(blob);
|
| 24 |
+
}
|
| 25 |
+
const { pixel_values } = await processor(image);
|
| 26 |
+
const output = await model({ images: pixel_values });
|
| 27 |
+
const permuted = output.output0[0];
|
| 28 |
+
const result = [];
|
| 29 |
+
const threshold = 0.3;
|
| 30 |
+
const [scaledHeight, scaledWidth] = pixel_values.dims.slice(-2);
|
| 31 |
+
for (const [xc, yc, w, h, ...scores] of permuted.tolist()) {
|
| 32 |
+
const x1 = (xc / scaledWidth) * image.width;
|
| 33 |
+
const y1 = (yc / scaledHeight) * image.height;
|
| 34 |
+
const x2 = (w / scaledWidth) * image.width;
|
| 35 |
+
const y2 = (h / scaledHeight) * image.height;
|
| 36 |
+
const score = scores[0];
|
| 37 |
+
if (score > threshold) {
|
| 38 |
+
const label = model.config.id2label[scores[1]];
|
| 39 |
+
result.push({
|
| 40 |
+
x1,
|
| 41 |
+
x2,
|
| 42 |
+
y1,
|
| 43 |
+
y2,
|
| 44 |
+
score,
|
| 45 |
+
label,
|
| 46 |
+
index: scores[1],
|
| 47 |
+
});
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
console.log(result)
|
| 51 |
```
|