Debian Codegraph @

online/static/blocks/MP.js

raw symbols SCIP
1 var camCallBack= null;
2 var mycam='front';
3
4
5 Blockly.Blocks['ai_isHandAvailable'] = {
6 init: function () {
7 this.jsonInit({
8 "type": "ai_isHandAvailable",
9 "lastDummyAlign0": "CENTRE",
10 "message0": "Is hand Visible?",
11 "output": "Boolean",
12 "colour": 75,
13 "tooltip": "Is hand Visible?",
14 "helpUrl": "",
15 });
16 }
17 };
18
19 Blockly.JavaScript['ai_isHandAvailable'] = function (block) {
20 return ['isHandAvailable()', Blockly.JavaScript.ORDER_NONE];
21 };
22
23
24 Blockly.Blocks['ai_isIndexExtendend'] = {
25 init: function () {
26 this.jsonInit({
27 "type": "ai_isIndexExtendend",
28 "lastDummyAlign0": "CENTRE",
29 "message0": "Is Index Finger Extended?",
30 "output": "Boolean",
31 "colour": 75,
32 "tooltip": "Is Index Finger Extended?",
33 "helpUrl": "",
34 });
35 }
36 };
37
38 Blockly.JavaScript['ai_isIndexExtendend'] = function (block) {
39 return ['isIndexFingerExtended()', Blockly.JavaScript.ORDER_NONE];
40 };
41
42 Blockly.Blocks['ai_isMiddleExtendend'] = {
43 init: function () {
44 this.jsonInit({
45 "type": "ai_isMiddleExtendend",
46 "lastDummyAlign0": "CENTRE",
47 "message0": "Is Middle Finger Extended?",
48 "output": "Boolean",
49 "colour": 75,
50 "tooltip": "Is Middle Finger Extended?",
51 "helpUrl": "",
52 });
53 }
54 };
55
56 Blockly.JavaScript['ai_isMiddleExtendend'] = function (block) {
57 return ['isMiddleFingerExtended()', Blockly.JavaScript.ORDER_NONE];
58 };
59
60 Blockly.Blocks['ai_isRingExtendend'] = {
61 init: function () {
62 this.jsonInit({
63 "type": "ai_isRingExtendend",
64 "lastDummyAlign0": "CENTRE",
65 "message0": "Is Ring Finger Extended?",
66 "output": "Boolean",
67 "colour": 75,
68 "tooltip": "Is Ring Finger Extended?",
69 "helpUrl": "",
70 });
71 }
72 };
73
74 Blockly.JavaScript['ai_isRingExtendend'] = function (block) {
75 return ['isRingFingerExtended()', Blockly.JavaScript.ORDER_NONE];
76 };
77
78 Blockly.Blocks['ai_isLittleExtendend'] = {
79 init: function () {
80 this.jsonInit({
81 "type": "ai_isLittleExtendend",
82 "lastDummyAlign0": "CENTRE",
83 "message0": "Is Little Finger Extended?",
84 "output": "Boolean",
85 "colour": 75,
86 "tooltip": "Is Little Finger Extended?",
87 "helpUrl": "",
88 });
89 }
90 };
91
92 Blockly.JavaScript['ai_isLittleExtendend'] = function (block) {
93 return ['isLittleFingerExtended()', Blockly.JavaScript.ORDER_NONE];
94 };
95
96 Blockly.Blocks['ai_isHandOpen'] = {
97 init: function () {
98 this.jsonInit({
99 "type": "ai_isHandOpen",
100 "lastDummyAlign0": "CENTRE",
101 "message0": "Is Hand Open?",
102 "output": "Boolean",
103 "colour": 75,
104 "tooltip": "Is Hand Open?",
105 "helpUrl": "",
106 });
107 }
108 };
109
110 Blockly.JavaScript['ai_isHandOpen'] = function (block) {
111 return ['isHandExtended()', Blockly.JavaScript.ORDER_NONE];
112 };
113
114 Blockly.Blocks['ai_vertices'] = {
115 init: function () {
116 this.jsonInit({
117 "type": "ai_vertices",
118 "lastDummyAlign0": "CENTRE",
119 "message0": "Get Coordinate Array",
120 "output": "Array",
121 "colour": 75,
122 "tooltip": "Get Coordinates",
123 "helpUrl": "",
124 });
125 }
126 };
127
128 Blockly.JavaScript['ai_vertices'] = function (block) {
129 return ['getCoordinates()', Blockly.JavaScript.ORDER_NONE];
130 };
131
132
133 Blockly.Blocks['ai_getcoord'] = {
134 init: function () {
135 this.jsonInit({
136 "type": "ai_getcoord",
137 "message0": "Save X value into %1 and save Y value into %2 from finger joint %3",
138 "args0": [
139 {
140 "type": "field_variable",
141 "name": "X_VAR",
142 "variable": "finger_x"
143 },
144 {
145 "type": "field_variable",
146 "name": "Y_VAR",
147 "variable": "finger_y"
148 },
149 {
150 "type": "field_dropdown",
151 "name": "finger_joint",
152 "options": [
153 [
154 "0",
155 "0"
156 ],
157 [
158 "1",
159 "1"
160 ],
161 [
162 "2",
163 "2"
164 ],
165 [
166 "3",
167 "3"
168 ],
169 [
170 "4",
171 "4"
172 ],
173 [
174 "5",
175 "5"
176 ],
177 [
178 "6",
179 "6"
180 ],
181 [
182 "7",
183 "7"
184 ],
185 [
186 "8",
187 "8"
188 ],
189 [
190 "9",
191 "9"
192 ],
193 [
194 "10",
195 "10"
196 ],
197 [
198 "11",
199 "11"
200 ],
201 [
202 "12",
203 "12"
204 ],
205 [
206 "13",
207 "13"
208 ],
209 [
210 "14",
211 "14"
212 ],
213 [
214 "15",
215 "15"
216 ],
217 [
218 "16",
219 "16"
220 ],
221 [
222 "17",
223 "17"
224 ],
225 [
226 "18",
227 "18"
228 ],
229 [
230 "19",
231 "19"
232 ],
233 [
234 "20",
235 "20"
236 ]
237 ]
238 }
239 ],
240 "previousStatement": null,
241 "nextStatement": null,
242 "colour": 180,
243 "tooltip": "",
244 "helpUrl": ""
245 });
246 }
247 };
248
249 Blockly.JavaScript['ai_getcoord'] = function(block) {
250 var variable_x_var = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('X_VAR'), Blockly.Variables.NAME_TYPE);
251 var variable_y_var = Blockly.JavaScript.nameDB_.getName(block.getFieldValue('Y_VAR'), Blockly.Variables.NAME_TYPE);
252 var dropdown_finger_joint = block.getFieldValue('finger_joint');
253
254 var code = `${variable_x_var} = getCoordinateX(${dropdown_finger_joint});\n`;
255 code += `${variable_y_var} = getCoordinateY(${dropdown_finger_joint});\n`;
256
257 return code;
258 };
259
260
261 Blockly.Blocks['ai_get'] = {
262 init: function () {
263 this.jsonInit({
264
265 "output": "Number",
266
267 "type": "ai_get",
268 "message0": "Get %1 coordinate from finger joint %2",
269 "args0": [
270 {
271 "type": "field_dropdown",
272 "name": "axis",
273 "options": [
274 [
275 "X",
276 "X"
277 ],
278 [
279 "Y",
280 "Y"
281 ],
282 [
283 "Z",
284 "Z"
285 ]
286 ]
287 },
288 {
289 "type": "field_dropdown",
290 "name": "finger_joint",
291 "options": [
292 [
293 "0",
294 "0"
295 ],
296 [
297 "1",
298 "1"
299 ],
300 [
301 "2",
302 "2"
303 ],
304 [
305 "3",
306 "3"
307 ],
308 [
309 "4",
310 "4"
311 ],
312 [
313 "5",
314 "5"
315 ],
316 [
317 "6",
318 "6"
319 ],
320 [
321 "7",
322 "7"
323 ],
324 [
325 "8",
326 "8"
327 ],
328 [
329 "9",
330 "9"
331 ],
332 [
333 "10",
334 "10"
335 ],
336 [
337 "11",
338 "11"
339 ],
340 [
341 "12",
342 "12"
343 ],
344 [
345 "13",
346 "13"
347 ],
348 [
349 "14",
350 "14"
351 ],
352 [
353 "15",
354 "15"
355 ],
356 [
357 "16",
358 "16"
359 ],
360 [
361 "17",
362 "17"
363 ],
364 [
365 "18",
366 "18"
367 ],
368 [
369 "19",
370 "19"
371 ],
372 [
373 "20",
374 "20"
375 ]
376 ]
377 }
378 ],
379 "colour": 180,
380 "tooltip": "",
381 "helpUrl": ""
382 });
383 }
384 };
385
386 Blockly.JavaScript['ai_get'] = function(block) {
387 var coordinate = block.getFieldValue('axis');
388 var dropdown_finger_joint = block.getFieldValue('finger_joint');
389
390 return [`getCoord(\'${coordinate}\',${dropdown_finger_joint})`, Blockly.JavaScript.ORDER_NONE];
391
392 return code;
393 };
394
395
396
397 Blockly.Blocks['ai_distance'] = {
398 init: function () {
399 this.jsonInit({
400
401 "output": "Number",
402
403 "type": "ai_distance",
404 "message0": "Distance from joint %1 to %2",
405 "args0": [
406 {
407 "type": "field_dropdown",
408 "name": "finger_joint_1",
409 "options": [
410 [
411 "0",
412 "0"
413 ],
414 [
415 "1",
416 "1"
417 ],
418 [
419 "2",
420 "2"
421 ],
422 [
423 "3",
424 "3"
425 ],
426 [
427 "4",
428 "4"
429 ],
430 [
431 "5",
432 "5"
433 ],
434 [
435 "6",
436 "6"
437 ],
438 [
439 "7",
440 "7"
441 ],
442 [
443 "8",
444 "8"
445 ],
446 [
447 "9",
448 "9"
449 ],
450 [
451 "10",
452 "10"
453 ],
454 [
455 "11",
456 "11"
457 ],
458 [
459 "12",
460 "12"
461 ],
462 [
463 "13",
464 "13"
465 ],
466 [
467 "14",
468 "14"
469 ],
470 [
471 "15",
472 "15"
473 ],
474 [
475 "16",
476 "16"
477 ],
478 [
479 "17",
480 "17"
481 ],
482 [
483 "18",
484 "18"
485 ],
486 [
487 "19",
488 "19"
489 ],
490 [
491 "20",
492 "20"
493 ]
494 ]
495 },
496 {
497 "type": "field_dropdown",
498 "name": "finger_joint_2",
499 "options": [
500 [
501 "0",
502 "0"
503 ],
504 [
505 "1",
506 "1"
507 ],
508 [
509 "2",
510 "2"
511 ],
512 [
513 "3",
514 "3"
515 ],
516 [
517 "4",
518 "4"
519 ],
520 [
521 "5",
522 "5"
523 ],
524 [
525 "6",
526 "6"
527 ],
528 [
529 "7",
530 "7"
531 ],
532 [
533 "8",
534 "8"
535 ],
536 [
537 "9",
538 "9"
539 ],
540 [
541 "10",
542 "10"
543 ],
544 [
545 "11",
546 "11"
547 ],
548 [
549 "12",
550 "12"
551 ],
552 [
553 "13",
554 "13"
555 ],
556 [
557 "14",
558 "14"
559 ],
560 [
561 "15",
562 "15"
563 ],
564 [
565 "16",
566 "16"
567 ],
568 [
569 "17",
570 "17"
571 ],
572 [
573 "18",
574 "18"
575 ],
576 [
577 "19",
578 "19"
579 ],
580 [
581 "20",
582 "20"
583 ]
584 ]
585 }
586
587 ],
588 "colour": 180,
589 "tooltip": "",
590 "helpUrl": ""
591 });
592 }
593 };
594
595 Blockly.JavaScript['ai_distance'] = function(block) {
596 var dropdown_finger_joint_1 = block.getFieldValue('finger_joint_1');
597 var dropdown_finger_joint_2 = block.getFieldValue('finger_joint_2');
598
599 return [`getDistance(${dropdown_finger_joint_1},${dropdown_finger_joint_2})`, Blockly.JavaScript.ORDER_NONE];
600
601 return code;
602 };
603
604
605
606 Blockly.Blocks['ai_gesture_available'] = {
607 init: function () {
608 this.jsonInit({
609 "type": "ai_gesture_available",
610 "lastDummyAlign0": "CENTRE",
611 "message0": "New Gesture Available?",
612 "output": "Boolean",
613 "colour": 75,
614 "tooltip": "Is a new gesture available?",
615 "helpUrl": "",
616 });
617 }
618 };
619
620 Blockly.JavaScript['ai_gesture_available'] = function (block) {
621 return ['gestureAvailable()', Blockly.JavaScript.ORDER_NONE];
622 };
623
624
625 Blockly.Blocks['ai_get_gesture'] = {
626 init: function () {
627 this.jsonInit({
628 "type": "ai_get_gesture",
629 "lastDummyAlign0": "CENTRE",
630 "message0": "Get new gesture",
631 "output": "Number",
632 "colour": 75,
633 "tooltip": "Get the latest gesture",
634 "helpUrl": "",
635 });
636 }
637 };
638
639 Blockly.JavaScript['ai_get_gesture'] = function (block) {
640 return ['getGesture()', Blockly.JavaScript.ORDER_NONE];
641 };
642
643
644
645
646
647 Blockly.Blocks['ai_check_gesture'] = {
648 init: function () {
649 this.jsonInit({
650
651 "output": "Boolean",
652
653 "type": "ai_check_gesture",
654 "message0": "Is Gesture %2 %1",
655 "args0": [
656 {
657 "type": "field_dropdown",
658 "name": "gesturetype",
659 "options": [
660 [
661 "INDEXFOLDED",
662 "INDEXFOLDED"
663 ],
664 [
665 "MIDDLEFOLDED",
666 "MIDDLEFOLDED"
667 ],
668 [
669 "RINGFOLDED",
670 "RINGFOLDED"
671 ],
672 [
673 "LITTLEFOLDED",
674 "LITTLEFOLDED"
675 ],
676 [
677 "INDEXEXTENDED",
678 "INDEXEXTENDED"
679 ],
680 [
681 "MIDDLEEXTENDED",
682 "MIDDLEEXTENDED"
683 ],
684 [
685 "RINGEXTENDED",
686 "RINGEXTENDED"
687 ],
688 [
689 "LITTLEEXTENDED",
690 "LITTLEEXTENDED"
691 ],
692 [
693 "CLOSED_FIST",
694 "CLOSED_FIST"
695 ],
696 [
697 "VICTORY",
698 "VICTORY"
699 ],
700 [
701 "POINTING_UP",
702 "POINTING_UP"
703 ],
704 [
705 "THUMB_UP",
706 "THUMB_UP"
707 ],
708 [
709 "THUMB_DOWN",
710 "THUMB_DOWN"
711 ],
712 [
713 "OPEN_PALM",
714 "OPEN_PALM"
715 ]
716 ]
717 },
718 {
719 "type": "input_value",
720 "name": "gesture",
721 "check": "Number"
722 }
723
724 ],
725 "colour": 180,
726 "tooltip": "",
727 "helpUrl": ""
728 });
729 }
730 };
731
732 Blockly.JavaScript['ai_check_gesture'] = function(block) {
733 var gesture = Blockly.JavaScript.valueToCode(block, 'gesture', Blockly.JavaScript.ORDER_NONE);
734 var gesturetype = block.getFieldValue('gesturetype');
735
736 return [`checkGesture(\'${gesturetype}\', ${gesture})`, Blockly.JavaScript.ORDER_NONE];
737
738 return code;
739 };
740
741
742
743
744
745 //---------GAME
746
747 Blockly.Blocks['add_mp_hands'] = {
748 init: function() {
749 this.appendDummyInput()
750 .appendField("Add Hand Detector")
751 .appendField(new Blockly.FieldDropdown([["FRONT","front"], ["BACK","back"] ]), "CAM");
752 this.setColour(135);
753 this.setTooltip("Add AI hand detector");
754 this.setInputsInline(false);
755 this.setPreviousStatement(true, null);
756 this.setNextStatement(true, null);
757 this.setHelpUrl("");
758 }
759 };
760
761 Blockly.JavaScript['add_mp_hands'] = function(block) {
762 var cam = block.getFieldValue('CAM');
763 var code = 'addMP(\'' + cam +'\');';
764 return code;
765 };
766
767
768 Blockly.Python['add_mp_hands'] = function(block) {
769 var cam = block.getFieldValue('CAM');
770 var code = 'addMP(\'' + cam +'\')';
771 return code;
772 };
773
774
775 Blockly.Blocks['del_mp_hands'] = {
776 init: function() {
777 this.appendDummyInput()
778 .appendField("Stop Hand Detector");
779 this.setColour(135);
780 this.setTooltip("Stop AI hand detector");
781 this.setInputsInline(false);
782 this.setPreviousStatement(true, null);
783 this.setNextStatement(true, null);
784 this.setHelpUrl("");
785 }
786 };
787
788 Blockly.JavaScript['del_mp_hands'] = function(block) {
789 var code = 'delMP();';
790 return code;
791 };
792
793
794 Blockly.Python['del_mp_hands'] = function(block) {
795 var code = 'delMP()\n';
796 return code;
797 };
798
799
800 //-------------------- API ------------------------
801
802
803 function initMP(interpreter, scope) {
804 // kuttypy API CALLS
805 interpreter.setProperty(scope, 'addMP', interpreter.createAsyncFunction(
806 function(callback) {
807 fetch(`/addMP`)
808 .then(response => response.json())
809 .then(data => {
810 console.log('camera initialized');
811 callback();
812 })
813 .catch(error => {
814 console.error('Error opening camera:', error);
815 callback(0);
816 });
817 }
818 ));
819
820 interpreter.setProperty(scope, 'delMP', interpreter.createAsyncFunction(
821 function(callback) {
822 fetch(`/delMP`)
823 .then(response => response.json())
824 .then(data => {
825 console.log('camera closed');
826 callback();
827 })
828 .catch(error => {
829 console.error('Error closing camera:', error);
830 callback(0);
831 });
832 }
833 ));
834
835 interpreter.setProperty(scope, 'isHandAvailable', interpreter.createAsyncFunction(
836 function(callback) {
837 fetch(`/isHandVisible`)
838 .then(response => response.json())
839 .then(data => {
840 callback(data.response);
841 })
842 .catch(error => {
843 console.error('Error closing camera:', error);
844 callback(0);
845 });
846 }
847 ));
848
849
850 // kuttypy API CALLS
851 interpreter.setProperty(scope, 'getDistance', interpreter.createAsyncFunction(
852 function(p1,p2, callback) {
853 fetch('/getMPDistance', {
854 method: 'POST',
855 headers: {
856 'Content-Type': 'application/json'
857 },
858 body: JSON.stringify({
859 p1: p1,
860 p2: p2
861 })
862 })
863 .then(response => response.json())
864 .then(data => {
865 callback(data.distance);
866 })
867 .catch(error => {
868 console.error('Error reading landmrks:', error);
869 callback(0);
870 });
871 }
872 ));
873
874 interpreter.setProperty(scope, 'gestureAvailable', interpreter.createNativeFunction(
875 function() {
876 return JSBridge.gestureAvailable();
877 }));
878
879 interpreter.setProperty(scope, 'getGesture', interpreter.createNativeFunction(
880 function() {
881 return JSBridge.getGesture();
882 }));
883
884 interpreter.setProperty(scope, 'checkGesture', interpreter.createNativeFunction(
885 function(g , v) {
886 //TODO . convert to list access for comparison
887 if(v==0 && g == 'INDEXFOLDED') return true;
888 else if(v==1 && g == 'MIDDLEFOLDED') return true;
889 else if(v==2 && g == 'RINGFOLDED') return true;
890 else if(v==3 && g == 'LITTLEFOLDED') return true;
891 else if(v==4 && g == 'INDEXEXTENDED') return true;
892 else if(v==5 && g == 'MIDDLEXTENDED') return true;
893 else if(v==6 && g == 'RINGEXTENDED') return true;
894 else if(v==7 && g == 'LITTLEEXTENDED') return true;
895 else if(v==8 && g == 'CLOSED_FIST') return true;
896 else if(v==9 && g == 'VICTORY') return true;
897 else if(v==10 && g == 'POINTING_UP') return true;
898 else if(v==11 && g == 'THUMB_UP') return true;
899 else if(v==12 && g == 'THUMB_DOWN') return true;
900 else if(v==13 && g == 'OPEN_PALM') return true;
901
902
903 return false;
904 }));
905
906
907
908
909
910
911
912 }
913
914
915