【问题标题】:How can I crop the image given to the model in Teachable Machines?如何在 Teachable Machines 中裁剪提供给模型的图像?
【发布时间】:2020-02-01 13:29:03
【问题描述】:

所以我想要的是在将图像输入可教学机器模型之前裁剪图像。因为我对模型进行了重点训练,以识别图像的一小部分。因此,在 Teachable Machine 提供的默认代码中,整个图像都被赋予了 TM 模型。

我已经在这里添加了完整的代码。

<div>Teachable Machine Image Model</div>
<button type="button" onclick="init()">Start</button>
<div id="webcam-container"></div>
<div id="label-container"></div>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8/dist/teachablemachine-image.min.js"></script>
<script type="text/javascript">
    // More API functions here:
    // https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image

    // the link to your model provided by Teachable Machine export panel
    const URL = ""

    let model, webcam, labelContainer, maxPredictions;

    // Load the image model and setup the webcam
    async function init() {
        const modelURL = URL + "model.json";
        const metadataURL = URL + "metadata.json";

        // load the model and metadata
        // Refer to tmImage.loadFromFiles() in the API to support files from a file picker
        // or files from your local hard drive
        // Note: the pose library adds "tmImage" object to your window (window.tmImage)
        model = await tmImage.load(modelURL, metadataURL);
        maxPredictions = model.getTotalClasses();

        // Convenience function to setup a webcam
        const flip = true; // whether to flip the webcam
        webcam = new tmImage.Webcam(400, 400, flip); // width, height, flip
        await webcam.setup(); // request access to the webcam
        await webcam.play();
        window.requestAnimationFrame(loop);

        // append elements to the DOM
        document.getElementById("webcam-container").appendChild(webcam.canvas);
        labelContainer = document.getElementById("label-container");
        for (let i = 0; i < maxPredictions; i++) { // and class labels
            labelContainer.appendChild(document.createElement("div"));
        }
    }

    async function loop() {
        webcam.update(); // update the webcam frame
        await predict();
        window.requestAnimationFrame(loop);
    }

    // run the webcam image through the image model
    async function predict() {
        // predict can take in an image, video or canvas html element
        const prediction = await model.predict(webcam.canvas);
        for (let i = 0; i < maxPredictions; i++) {
            const classPrediction =
                prediction[i].className + ": " + prediction[i].probability.toFixed(2);
            labelContainer.childNodes[i].innerHTML = classPrediction;
        }
    }
</script>

我最感兴趣的是这条线

const prediction = await model.predict(webcam.canvas);

这里 webcam.canvas 为模型提供了整个网络摄像头画布作为模型的输入。但我想在此之前裁剪图像。我该怎么做?

【问题讨论】:

    标签: javascript html webcam


    【解决方案1】:

    你只需要换行

    webcam = new tmImage.Webcam(400, 400, flip); // width, height, flip
    

    第一个 400 是宽度,第二个 400 是给模型的网络摄像头的高度。根据您的意愿更改该值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2019-07-19
      • 1970-01-01
      • 2017-05-22
      • 2012-11-16
      • 2013-08-19
      • 2019-03-14
      相关资源
      最近更新 更多