How can I convert a tensor into bounding box coordinates and labels in Javascript?

a 夏天 提交于 2021-01-29 07:13:47

问题


I've trained a .pb object detection model in python using Colab and converted it to the model.json format using the TensorFlow converter. I need to load this model inside the browser (no Node.js!) and run inference there.

This is my complete code so far (images added in HTML using PHP):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../style.css">
</head>

<body>
<img id="img1" class="hide_img" src="image.png" alt="Scan" width="100" height="100" draggable="true" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.8.3/dist/tf.min.js"></script>

    <script>

    let username = "user"

    // Load the image model and setup the webcam
    async function predictImages() { // async
        console.log("loading model");
        // Load the model.
        const model = await tf.loadGraphModel('ROI_js/model.json');
        console.log("model loaded.");
        
        // predict for all images
        for (let i = 0; i <= 4; i++) {
            const img = document.getElementById('img' + i); // check if image exists
            if (img != null) {
                console.log("doc exists: " + 'img' + i);
                const tensor = tf.browser.fromPixels(img);
                model.executeAsync(tensor.expandDims(0)).then(predictions => {
                    console.log('Predictions: ');
                    console.log(predictions);
                    console.log("----");
                    console.log(predictions[0].arraySync()); // index: 0 or 1
                });
                
            } else {
                break;
            }
        }
    }
    predictImages();
    </script>
</body>

</html>

This code outputs the following:

[Array(1917)]0: Array(1917)[0 … 99][100 … 199][200 … 299][300 … 399][400 … 499][500 … 599][600 … 699][700 … 799][800 … 899][900 … 999][1000 … 1099][1100 … 1199][1200 … 1299][1300 … 1399][1400 … 1499][1500 … 1599][1600 … 1699][1700 … 1799][1800 … 1899][1900 … 1916]length: 1917__proto__: Array(0)length: 1__proto__: Array(0)

The model finds text regions on documents and should output their coordinates as well as their label (paragraph, header, ...) and the confidence instead of such arrays, how can I convert them?

来源:https://stackoverflow.com/questions/65652335/how-can-i-convert-a-tensor-into-bounding-box-coordinates-and-labels-in-javascrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!