Coder Social home page Coder Social logo

tensorflowjs_tutorial's Introduction

TensorFlowJS_Tutorial

tensorflowjs_tutorial's People

Contributors

adf-telkomuniv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tensorflowjs_tutorial's Issues

Trying to modify 'part_3' of tutorial with code from 'part_4 handsign example'. Images not being read correctly.

After completing the the tutorial for '[Part 3: MNIST Classification]', I tried to apply code to the script to allow the option to import an image of a handwritten digit from the PC and have the trained model predict the value of the digit in the image.

I tried to use the code in the 'handsign.js' and 'handsign.html' files given in the 'part_4' github folder, but the images I'm using all give the same result despite all being different digits.

I think it might be reading the data from the canvas incorrectly?

This is the code I added to the 'index.js' file of Part 3 from the 'handsign.js' file of Part 4:
`initCanvas('predict-canvas1')
$('#upload-btn').click(async() => {
$('#img-upload').click()
})

var imgTensor
$('#img-upload').change(async(evt) => {
var canvas = $('#predict-canvas1')[0]
var context = canvas.getContext("2d")

var img = new Image() 

var file = evt.target.files[0]
if(file.type.match('image.*')) {
    var reader = new FileReader()
    reader.readAsDataURL(file)
    reader.onload = function(evt){
        if( evt.target.readyState == FileReader.DONE) {
            img.src = evt.target.result
            img.onload = () => {                    
                context.clearRect(0, 0, canvas.width, canvas.height)
                context.drawImage(img, 0, 0)
                

                
            }
        }
    }        
    $('#predict-btn1').prop('disabled', false)
    // $('#prediction1').text( 'Predicted: ')    
} else {
    alert("not an image")
}

})

$('#predict-btn1').click(async() => {
// try{
// $('#predict-btn1').prop('disabled', true)
// const img = imgTensor.div(tf.scalar(255))

//     var x_data = tf.cast(resized.reshape([1, 28, 28, 3]), 'float32')
//     var y_pred = await model.predict(x_data)
//     var predictions = Array.from(y_pred.argMax(1).dataSync())

//     $('#prediction1').text( 'Predicted: '+ predictions)    
// } catch (e) {
//     console.log(e)
//     alert('failed to predict image')
// }
var canvas = $('#predict-canvas1')[0]
var preview = $('#preview-canvas1')[0]

var img1 = tf.browser.fromPixels(imgTensor, 4)
var resized = util.cropImage(img1, canvas.width)        
tf.browser.toPixels(resized, preview)    
var x_data = tf.cast(resized.reshape([1, 28, 28, 1]), 'float32')
var y_pred = model.predict(x_data)

var prediction = Array.from(y_pred.argMax(1).dataSync())    
$('#prediction1').text( 'Predicted: '+ prediction)    

const barchartData = Array.from(y_pred.dataSync()).map((d, i) => {
    return { index: i, value: d }
})
tfvis.render.barchart($('#predict-graph1')[0], barchartData,  { width: 400, height: 140 })    

})`

Is there a way for this part of 'handsign.js' to work with the 'index.js' file?

If not, is there an alternative way for the trained model to predict the digit contained in an image?

All the code used in this project, including the images I'm trying to use, can be found at this repository: https://github.com/Miller144/verbose-octo-carnival

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.