1+ // Copyright (C) 2018 Cristobal Valenzuela
2+ //
3+ // This file is part of RunwayML.
4+ //
5+ // RunwayML is free software: you can redistribute it and/or modify
6+ // it under the terms of the GNU General Public License as published by
7+ // the Free Software Foundation, either version 3 of the License, or
8+ // (at your option) any later version.
9+ //
10+ // RunwayML is distributed in the hope that it will be useful,
11+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+ // GNU General Public License for more details.
14+ //
15+ // You should have received a copy of the GNU General Public License
16+ // along with RunwayML. If not, see <http://www.gnu.org/licenses/>.
17+ //
18+ // ===============================================================
19+ //
20+ // Runway: Gaze Demo
21+ // This example receives receives incoming data from Runway.
22+ // p5.js is used to draw the human data sent from Runway
23+ // You should select Camera from the Input Panel
24+ //
25+ // Cristóbal Valenzuela
26+ // Dan Oved
27+ //
28+ // ===============================================================
29+
30+ var capture ;
31+ var w = 432 ;
32+ var h = 368 ;
33+ var colors ;
34+
35+ // This array will get updated with all the gazes detected in the image
36+ var detectedGazes = [ ] ;
37+
38+ // Create a connection with Runway
39+ // *You should update this address to match the URL provided by the app
40+ var socket = io . connect ( 'http://127.0.0.1:3333' ) ;
41+
42+ // Wait until the page is loaded
43+ document . addEventListener ( "DOMContentLoaded" , function ( event ) {
44+ // A variable to hold the status of the connection
45+ var status = document . getElementById ( 'status' ) ;
46+
47+ // When a connection is established
48+ socket . on ( 'connect' , function ( ) {
49+ status . innerHTML = 'Connected' ;
50+ console . log ( 'connected' ) ;
51+ } ) ;
52+
53+ // When there is a data event, update the humans array
54+ socket . on ( 'data' , function ( data ) {
55+ detectedGazes = data . results . estimated_gazes ;
56+ } ) ;
57+ } ) ;
58+
59+ // p5 setup function
60+ function setup ( ) {
61+ // Create a canvas
62+ createCanvas ( w , h ) ;
63+ // Create a video element.
64+ // Although we are getting the images from Runway, this is just to show the video behind
65+ capture = createCapture ( VIDEO ) ;
66+ capture . size ( w , h ) ;
67+ // Hide the video element. We will be showing only the canvas
68+ capture . hide ( ) ;
69+ noStroke ( ) ;
70+ fill ( 255 , 0 , 0 ) ;
71+ }
72+
73+ // p5 draw function
74+ function draw ( ) {
75+ // Every frame, draw the current video frame
76+ image ( capture , 0 , 0 , w , h ) ;
77+ // Draw the current detected gazes
78+ detectedGazes . forEach ( gaze => drawGaze ( gaze ) ) ;
79+ }
80+
81+ // A function that connects joints based on data coming from OpenPose
82+ function drawGaze ( gaze ) {
83+ const x = constrain ( map ( gaze [ 0 ] , - 10 , 10 , 0 , width ) , 0 , width ) ;
84+ const y = constrain ( map ( gaze [ 1 ] , - 2.5 , - 20 , 0 , height ) , 0 , height ) ;
85+ ellipse ( x , y , 15 , 15 ) ;
86+ }
0 commit comments