forked from runwayml/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
posenet.pde
142 lines (120 loc) · 3.74 KB
/
posenet.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (C) 2018 Runway AI Examples
//
// This file is part of Runway AI Examples.
//
// Runway-Examples is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Runway-Examples is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RunwayML. If not, see <http://www.gnu.org/licenses/>.
//
// ===========================================================================
// RUNWAY
// www.runwayapp.ai
// PoseNet Demo:
// Receive OSC messages from Runway
// Running PoseNet model
// Import OSC
import oscP5.*;
import netP5.*;
// Runway Host
String runwayHost = "127.0.0.1";
// Runway Port
int runwayPort = 57100;
OscP5 oscP5;
NetAddress myBroadcastLocation;
// This array will hold all the humans detected
JSONObject data;
JSONArray humans;
// This are the pair of body connections we want to form.
// Try creating new ones!
String[][] connections = {
{"nose", "leftEye"},
{"leftEye", "leftEar"},
{"nose", "rightEye"},
{"rightEye", "rightEar"},
{"rightShoulder", "rightElbow"},
{"rightElbow", "rightWrist"},
{"leftShoulder", "leftElbow"},
{"leftElbow", "leftWrist"},
{"rightHip", "rightKnee"},
{"rightKnee", "rightAnkle"},
{"leftHip", "leftKnee"},
{"leftKnee", "leftAnkle"}
};
void setup() {
size(400, 350);
frameRate(25);
OscProperties properties = new OscProperties();
properties.setRemoteAddress("127.0.0.1", 57200);
properties.setListeningPort(57200);
properties.setDatagramSize(99999999);
properties.setSRSP(OscProperties.ON);
oscP5 = new OscP5(this, properties);
// Use the localhost and the port 57100 that we define in Runway
myBroadcastLocation = new NetAddress(runwayHost, runwayPort);
connect();
fill(255);
stroke(255);
}
void draw() {
background(0);
// Choose between drawing just an ellipse
// over the body parts or drawing connections
// between them. or both!
drawParts();
}
// A function to draw humans body parts as circles
void drawParts() {
// Only if there are any humans detected
if (data != null) {
humans = data.getJSONArray("poses");
for(int h = 0; h < humans.size(); h++) {
JSONObject human = humans.getJSONObject(h);
JSONArray keypoints = human.getJSONArray("keypoints");
// Now that we have one human, let's draw its body parts
for (int k = 0; k < keypoints.size(); k++) {
JSONObject body_part = keypoints.getJSONObject(k);
JSONObject positions = body_part.getJSONObject("position");
// Body parts are relative to width and weight of the input
float x = positions.getFloat("x");
float y = positions.getFloat("y");
ellipse(x, y, 10, 10);
}
}
}
}
void connect() {
OscMessage m = new OscMessage("/server/connect");
oscP5.send(m, myBroadcastLocation);
}
void disconnect() {
OscMessage m = new OscMessage("/server/disconnect");
oscP5.send(m, myBroadcastLocation);
}
void keyPressed() {
switch(key) {
case('c'):
/* connect to Runway */
connect();
break;
case('d'):
/* disconnect from Runway */
disconnect();
break;
}
}
// OSC Event: listens to data coming from Runway
void oscEvent(OscMessage theOscMessage) {
// The data is in a JSON string, so first we get the string value
String dataString = theOscMessage.get(0).stringValue();
// We then parse it as a JSONObject
data = parseJSONObject(dataString);
}