/** * @license * Visual Blocks Language * * Copyright 2012 Google Inc. * https://developers.google.com/blockly/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @fileoverview Generating Python for colour blocks. * @author fraser@google.com (Neil Fraser) */ 'use strict'; goog.provide('Blockly.Python.visionkit'); goog.require('Blockly.Python'); Blockly.Python["visionkit_import"] = function(block) { var vision_import = Blockly.Python.valueToCode(block,"IMPORT",Blockly.Python.ORDER_NONE) || 0; var vision_from = block.getFieldValue("FROM"); var code = "from " + vision_from + " " + vision_import+ "\n"; return code; } Blockly.Python["visionkit_import_annotator"] = function(block) { var code ="import " + block.getFieldValue("IMPORT") || "Annotator" return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python["visionkit_import_models"] = function(block) { var code ="import "+ block.getFieldValue("IMPORT") || "face_detection" return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python["visionkit_import_inference"] = function(block) { var code ="import "+ block.getFieldValue("IMPORT") || "CameraInference" return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python["visionkit_cameraInference"] = function(block) { var inference = block.getFieldValue("INFERENCE"); var usemodel = block.getFieldValue("USEMODEL"); var statement_input = Blockly.Python.statementToCode(block, "STACK"); var code = "with CameraInference(" + usemodel + ") as " +inference +":\n"+ statement_input+"\n"; return code; } Blockly.Python["visionkit_runInference"] = function(block) { var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || ""; var code = "inference.run(" + input + ")"; return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python["visionkit_GetInference"] = function(block) { var inference = block.getFieldValue("INFERENCE"); var code = "inference."+ inference; return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python["visionkit_faceDetection_operation"] = function(block) { var model = block.getFieldValue("MODEL"); var operation = block.getFieldValue("OPERATION"); var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || ""; var code = model+"."+operation+"("+input+")"; return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python["visionkit_model_get"] = function(block) { var model = block.getFieldValue("MODEL"); var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || ""; var code = input + "." +model; return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python['visionkit_Use_Annotator'] = function(block) { var annotator = block.getFieldValue("ANNOTATOR"); var dimen_x = block.getFieldValue("X-DIMEN"); var dimen_y = block.getFieldValue("Y-DIMEN"); var code = "Annotator(" + annotator + ", dimensions = ("+ dimen_x + "," + dimen_y +"))\n"; return code; } Blockly.Python['visionkit_Use_Annotator_output'] = function(block) { var annotator = block.getFieldValue("ANNOTATOR"); var dimen_x = block.getFieldValue("X-DIMEN"); var dimen_y = block.getFieldValue("Y-DIMEN"); var code = "Annotator(" + annotator + ", dimensions = ("+ dimen_x + "," + dimen_y +"))"; return [code, Blockly.Python.ORDER_ATOMIC]; } Blockly.Python['visionkit_Annotator_operation'] = function(block) { var operation = block.getFieldValue("OPERATION"); var code = "annotator." + operation + "()\n"; return code } Blockly.Python['visionkit_Annotator_Bounding'] = function(block) { var fill = block.getFieldValue("FILL") var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || ""; var code = "annotator.bounding_box(transform(" + input + "),fill="+ fill +")\n"; return code } Blockly.Python['visionkit_myAssistant'] = function(block) { var code = "MyAssistant()"; return [code, Blockly.Python.ORDER_ATOMIC] } /************************************ * Simplify * *************************************/ Blockly.Python['visionkit_import_aiy'] = function(block) { var code = "from aiy.vision.inference import CameraInference\n"+ "from aiy.vision.models import face_detection\n"+ "from aiy.vision.annotator import Annotator\n"; return code; } Blockly.Python['visionkit_avg_joy_score'] = function(block) { // var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_NONE) || ""; var code = "def avg_joy_score(faces):\n"+ " if faces:\n"+ " return sum(face.joy_score for face in faces) / len(faces)\n"+ " return 0\n"; return code } Blockly.Python['visionkit_annotator_camera'] = function(block) { var x = block.getFieldValue("X"); var y = block.getFieldValue("Y") var scale_x = block.getFieldValue("SCALE_X") var scale_y = block.getFieldValue("SCALE_Y") var code = "annotator = Annotator(camera, dimensions=("+x+", "+y+"))\n"+ "scale_x = "+x+" / "+scale_x+"\n"+ "scale_y = "+y+" / "+scale_y+"\n" return code; } Blockly.Python['visionkit_transform_boundingBox'] = function(block) { var code = "def transform(bounding_box):\n"+ " x, y, width, height = bounding_box\n"+ " return (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height))\n" return code; } Blockly.Python['visionkit_CameraInference'] = function(block) { var setting = block.getFieldValue("SETTING") var statement_input = Blockly.Python.statementToCode(block,"STACK") var input_a = Blockly.Python.prefixLines(/** @type {string} */ (statement_input), Blockly.Python.INDENT); var code = "with CameraInference(face_detection.model()) as inference:\n"+ " for result in inference.run(num_frames):\n"+ " faces = face_detection.get_faces(result)\n"+ " annotator.clear()\n"+ " for face in faces:\n"+ " annotator.bounding_box(transform(face.bounding_box), fill=0)\n"+ " annotator.update()\n"+ input_a; return code; } Blockly.Python['visionkit_avg_joy_score_num'] = function(block) { var face = block.getFieldValue("FACE") var code = "avg_joy_score("+face+")" return [code, Blockly.Python.ORDER_ATOMIC] } Blockly.Python['visionkit_face'] = function(block) { var code = "faces" return [code, Blockly.Python.ORDER_ATOMIC] }