visionkit.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating Python for colour blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Python.visionkit');
  26. goog.require('Blockly.Python');
  27. Blockly.Python["visionkit_import"] = function(block) {
  28. var vision_import = Blockly.Python.valueToCode(block,"IMPORT",Blockly.Python.ORDER_NONE) || 0;
  29. var vision_from = block.getFieldValue("FROM");
  30. var code = "from " + vision_from + " " + vision_import+ "\n";
  31. return code;
  32. }
  33. Blockly.Python["visionkit_import_annotator"] = function(block) {
  34. var code ="import " + block.getFieldValue("IMPORT") || "Annotator"
  35. return [code, Blockly.Python.ORDER_ATOMIC];
  36. }
  37. Blockly.Python["visionkit_import_models"] = function(block) {
  38. var code ="import "+ block.getFieldValue("IMPORT") || "face_detection"
  39. return [code, Blockly.Python.ORDER_ATOMIC];
  40. }
  41. Blockly.Python["visionkit_import_inference"] = function(block) {
  42. var code ="import "+ block.getFieldValue("IMPORT") || "CameraInference"
  43. return [code, Blockly.Python.ORDER_ATOMIC];
  44. }
  45. Blockly.Python["visionkit_cameraInference"] = function(block) {
  46. var inference = block.getFieldValue("INFERENCE");
  47. var usemodel = block.getFieldValue("USEMODEL");
  48. var statement_input = Blockly.Python.statementToCode(block, "STACK");
  49. var code = "with CameraInference(" + usemodel + ") as " +inference +":\n"+
  50. statement_input+"\n";
  51. return code;
  52. }
  53. Blockly.Python["visionkit_runInference"] = function(block) {
  54. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || "";
  55. var code = "inference.run(" + input + ")";
  56. return [code, Blockly.Python.ORDER_ATOMIC];
  57. }
  58. Blockly.Python["visionkit_GetInference"] = function(block) {
  59. var inference = block.getFieldValue("INFERENCE");
  60. var code = "inference."+ inference;
  61. return [code, Blockly.Python.ORDER_ATOMIC];
  62. }
  63. Blockly.Python["visionkit_faceDetection_operation"] = function(block) {
  64. var model = block.getFieldValue("MODEL");
  65. var operation = block.getFieldValue("OPERATION");
  66. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || "";
  67. var code = model+"."+operation+"("+input+")";
  68. return [code, Blockly.Python.ORDER_ATOMIC];
  69. }
  70. Blockly.Python["visionkit_model_get"] = function(block) {
  71. var model = block.getFieldValue("MODEL");
  72. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || "";
  73. var code = input + "." +model;
  74. return [code, Blockly.Python.ORDER_ATOMIC];
  75. }
  76. Blockly.Python['visionkit_Use_Annotator'] = function(block) {
  77. var annotator = block.getFieldValue("ANNOTATOR");
  78. var dimen_x = block.getFieldValue("X-DIMEN");
  79. var dimen_y = block.getFieldValue("Y-DIMEN");
  80. var code = "Annotator(" + annotator + ", dimensions = ("+ dimen_x + "," + dimen_y +"))\n";
  81. return code;
  82. }
  83. Blockly.Python['visionkit_Use_Annotator_output'] = function(block) {
  84. var annotator = block.getFieldValue("ANNOTATOR");
  85. var dimen_x = block.getFieldValue("X-DIMEN");
  86. var dimen_y = block.getFieldValue("Y-DIMEN");
  87. var code = "Annotator(" + annotator + ", dimensions = ("+ dimen_x + "," + dimen_y +"))";
  88. return [code, Blockly.Python.ORDER_ATOMIC];
  89. }
  90. Blockly.Python['visionkit_Annotator_operation'] = function(block) {
  91. var operation = block.getFieldValue("OPERATION");
  92. var code = "annotator." + operation + "()\n";
  93. return code
  94. }
  95. Blockly.Python['visionkit_Annotator_Bounding'] = function(block) {
  96. var fill = block.getFieldValue("FILL")
  97. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || "";
  98. var code = "annotator.bounding_box(transform(" + input + "),fill="+ fill +")\n";
  99. return code
  100. }
  101. Blockly.Python['visionkit_myAssistant'] = function(block) {
  102. var code = "MyAssistant()";
  103. return [code, Blockly.Python.ORDER_ATOMIC]
  104. }
  105. /************************************
  106. * Simplify *
  107. *************************************/
  108. Blockly.Python['visionkit_import_aiy'] = function(block) {
  109. var code = "from aiy.vision.inference import CameraInference\n"+
  110. "from aiy.vision.models import face_detection\n"+
  111. "from aiy.vision.annotator import Annotator\n";
  112. return code;
  113. }
  114. Blockly.Python['visionkit_avg_joy_score'] = function(block) {
  115. // var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || "";
  116. var code = "def avg_joy_score(faces):\n"+
  117. " if faces:\n"+
  118. " return sum(face.joy_score for face in faces) / len(faces)\n"+
  119. " return 0\n";
  120. return code
  121. }
  122. Blockly.Python['visionkit_annotator_camera'] = function(block) {
  123. var x = block.getFieldValue("X");
  124. var y = block.getFieldValue("Y")
  125. var scale_x = block.getFieldValue("SCALE_X")
  126. var scale_y = block.getFieldValue("SCALE_Y")
  127. var code = "annotator = Annotator(camera, dimensions=("+x+", "+y+"))\n"+
  128. "scale_x = "+x+" / "+scale_x+"\n"+
  129. "scale_y = "+y+" / "+scale_y+"\n"
  130. return code;
  131. }
  132. Blockly.Python['visionkit_transform_boundingBox'] = function(block) {
  133. var code = "def transform(bounding_box):\n"+
  134. " x, y, width, height = bounding_box\n"+
  135. " return (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height))\n"
  136. return code;
  137. }
  138. Blockly.Python['visionkit_CameraInference'] = function(block) {
  139. var setting = block.getFieldValue("SETTING")
  140. var statement_input = Blockly.Python.statementToCode(block,"STACK")
  141. var input_a = Blockly.Python.prefixLines(/** @type {string} */ (statement_input), Blockly.Python.INDENT);
  142. var code = "with CameraInference(face_detection.model()) as inference:\n"+
  143. " for result in inference.run(num_frames):\n"+
  144. " faces = face_detection.get_faces(result)\n"+
  145. " annotator.clear()\n"+
  146. " for face in faces:\n"+
  147. " annotator.bounding_box(transform(face.bounding_box), fill=0)\n"+
  148. " annotator.update()\n"+
  149. input_a;
  150. return code;
  151. }
  152. Blockly.Python['visionkit_avg_joy_score_num'] = function(block) {
  153. var face = block.getFieldValue("FACE")
  154. var code = "avg_joy_score("+face+")"
  155. return [code, Blockly.Python.ORDER_ATOMIC]
  156. }
  157. Blockly.Python['visionkit_face'] = function(block) {
  158. var code = "faces"
  159. return [code, Blockly.Python.ORDER_ATOMIC]
  160. }