Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import {
|
| 2 |
|
| 3 |
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
|
| 4 |
env.allowLocalModels = false;
|
|
@@ -9,13 +9,16 @@ const fileUpload = document.getElementById('upload');
|
|
| 9 |
const imageContainer = document.getElementById('container');
|
| 10 |
const example = document.getElementById('example');
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
// Create a new object detection pipeline
|
| 15 |
status.textContent = 'Loading model...';
|
| 16 |
-
const
|
|
|
|
|
|
|
| 17 |
status.textContent = 'Ready';
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
example.addEventListener('click', (e) => {
|
| 20 |
e.preventDefault();
|
| 21 |
detect(EXAMPLE_URL);
|
|
@@ -42,38 +45,15 @@ async function detect(img) {
|
|
| 42 |
imageContainer.style.backgroundImage = `url(${img})`;
|
| 43 |
|
| 44 |
status.textContent = 'Analysing...';
|
| 45 |
-
const
|
| 46 |
-
threshold: 0.5,
|
| 47 |
-
percentage: true,
|
| 48 |
-
});
|
| 49 |
-
status.textContent = '';
|
| 50 |
-
output.forEach(renderBox);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
// Render a bounding box and label on the image
|
| 54 |
-
function renderBox({ box, label }) {
|
| 55 |
-
const { xmax, xmin, ymax, ymin } = box;
|
| 56 |
-
|
| 57 |
-
// Generate a random color for the box
|
| 58 |
-
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 59 |
|
| 60 |
-
//
|
| 61 |
-
const
|
| 62 |
-
boxElement.className = 'bounding-box';
|
| 63 |
-
Object.assign(boxElement.style, {
|
| 64 |
-
borderColor: color,
|
| 65 |
-
left: 100 * xmin + '%',
|
| 66 |
-
top: 100 * ymin + '%',
|
| 67 |
-
width: 100 * (xmax - xmin) + '%',
|
| 68 |
-
height: 100 * (ymax - ymin) + '%',
|
| 69 |
-
})
|
| 70 |
|
| 71 |
-
//
|
| 72 |
-
const
|
| 73 |
-
labelElement.textContent = label;
|
| 74 |
-
labelElement.className = 'bounding-box-label';
|
| 75 |
-
labelElement.style.backgroundColor = color;
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
| 1 |
+
import { MgpstrForSceneTextRecognition, MgpstrProcessor, RawImage } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@3.1.0';
|
| 2 |
|
| 3 |
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
|
| 4 |
env.allowLocalModels = false;
|
|
|
|
| 9 |
const imageContainer = document.getElementById('container');
|
| 10 |
const example = document.getElementById('example');
|
| 11 |
|
| 12 |
+
// Load Model
|
|
|
|
|
|
|
| 13 |
status.textContent = 'Loading model...';
|
| 14 |
+
const model_id = 'onnx-community/mgp-str-base';
|
| 15 |
+
const model = await MgpstrForSceneTextRecognition.from_pretrained(model_id);
|
| 16 |
+
const processor = await MgpstrProcessor.from_pretrained(model_id);
|
| 17 |
status.textContent = 'Ready';
|
| 18 |
|
| 19 |
+
// Load image from the IIIT-5k dataset
|
| 20 |
+
const EXAMPLE_URL = "https://i.postimg.cc/ZKwLg2Gw/367-14.png";
|
| 21 |
+
|
| 22 |
example.addEventListener('click', (e) => {
|
| 23 |
e.preventDefault();
|
| 24 |
detect(EXAMPLE_URL);
|
|
|
|
| 45 |
imageContainer.style.backgroundImage = `url(${img})`;
|
| 46 |
|
| 47 |
status.textContent = 'Analysing...';
|
| 48 |
+
const image = await RawImage.read(img)-;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
// Preprocess the image
|
| 51 |
+
const result = await processor(image);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
// Perform inference
|
| 54 |
+
const outputs = await model(result);
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
// Decode the model outputs
|
| 57 |
+
const generated_text = processor.batch_decode(outputs.logits).generated_text;
|
| 58 |
+
status.textContent = generated_text;
|
| 59 |
+
}
|