Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. <canvas id="myCanvas" width="280" height="280" style="border:8px solid; float: left; margin: 70px; margin-top:160px; border-radius: 5px; cursor: crosshair;"></canvas>
  2.  
  3.  
  4.  
  5. <script type="text/javascript">
  6. var mousePressed = false;
  7. var lastX, lastY;
  8. var ctx;
  9.  
  10. function InitThis() {
  11. ctx = document.getElementById('myCanvas').getContext("2d");
  12.  
  13. $('#myCanvas').mousedown(function (e) {
  14. mousePressed = true;
  15. Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, false);
  16. });
  17.  
  18. $('#myCanvas').mousemove(function (e) {
  19. if (mousePressed) {
  20. Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, true);
  21. }
  22. });
  23.  
  24. $('#myCanvas').mouseup(function (e) {
  25. mousePressed = false;
  26. });
  27.  
  28. $('#myCanvas').mouseleave(function (e) {
  29. mousePressed = false;
  30. });
  31. }
  32.  
  33. function Draw(x, y, isDown) {
  34. if (isDown) {
  35. ctx.beginPath();
  36. ctx.strokeStyle = 'black';
  37. ctx.lineWidth = 5;
  38. ctx.lineJoin = "round";
  39. ctx.moveTo(lastX, lastY);
  40. ctx.lineTo(x, y);
  41. ctx.closePath();
  42. ctx.stroke();
  43. }
  44. lastX = x;
  45. lastY = y;
  46. }
  47.  
  48. function clearArea() {
  49. // Use the identity matrix while clearing the canvas
  50. ctx.setTransform(1, 0, 0, 1, 0, 0);
  51. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  52. }
  53.  
  54. </script>
  55.  
  56.  
  57.  
  58.  
  59. def convertImage(imgData1):
  60. imgstr = re.search(b'base64,(.*)',imgData1).group(1)
  61. with open('output.png','wb') as output:
  62. output.write(base64.b64decode(imgstr))
  63.  
  64.  
  65. def predict():
  66. imgData = request.get_data()
  67.  
  68. convertImage(imgData)
  69. imgArr = cv2.imread("output.png", 0)
  70. imgArr = cv2.resize(imgArr, dsize=(28, 28), interpolation=cv2.INTER_CUBIC)
  71. #print(imgArr)
  72. imgArr = np.expand_dims(imgArr, axis=0)
  73. new_model = tf.keras.models.load_model('my-model.model')
  74. prediction = new_model.predict(imgArr)
  75. return np.array_str(prediction[0])
  76.  
  77. Predicted Output: [0.01503093 0.09207787 0.08750388 0.0216067 0.02371381 0.5564784 0.01984628 0.16058393 0.00308483 0.02007343]
  78.  
  79. expecting an output like [0 0 0 1 0 0 0 0 0 0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement