Skip to content
This repository was archived by the owner on Mar 6, 2025. It is now read-only.

Commit f8409aa

Browse files
authored
Merge pull request #1 from orgicus/master
openpose sendWebcam example + socket local IP
2 parents 0bf79ba + 6eda061 commit f8409aa

4 files changed

Lines changed: 184 additions & 2 deletions

File tree

im2txt/sendWebcam/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// Create a connection to the Runway HTTP Server
3131
// You should select HTTP from the INPUT Panel
3232
// *You should update this address to match the URL provided by the app
33-
var socket = io.connect('http://10.0.1.5:33100/query');
33+
var socket = io.connect('http://127.0.0.1:33100/query');
3434

3535
// Wait until the page is loaded
3636
document.addEventListener("DOMContentLoaded", function(event) {

openpose/sendImage/script.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,20 @@ document.addEventListener("DOMContentLoaded", function(event) {
6464

6565
// Create a connection with Runway
6666
// *You should update this address to match the URL provided by the app
67-
socket = io.connect('http://10.0.1.5:33200/query');
67+
socket = io.connect('http://127.0.0.1:33200/query');
6868

6969
// When a connection is established
7070
socket.on('connect', function() {
7171
status.innerHTML = 'Connected';
7272
});
73+
// Handle connection error (in case something is wrong and we can't connect to Runway)
74+
socket.on('connect_error', (error) => {
75+
console.error(error);
76+
});
77+
// Handle connection timeout (in case something is wrong and it's taking ages connecting to Runway)
78+
socket.on('connect_timeout', (timeout) => {
79+
console.warn(socket.io.uri,"connect_timeout",timeout);
80+
});
7381

7482
// When there is new data coming in, update the log element
7583
socket.on('update_response', function(data) {
@@ -79,6 +87,7 @@ document.addEventListener("DOMContentLoaded", function(event) {
7987
}
8088
});
8189

90+
8291
});
8392

8493
// p5 setup function

openpose/sendWebcam/index.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,50 @@
1818
1919
-->
2020

21+
<!--
22+
OpenPose Demo: This example creates a video stream and send images to Runway
23+
24+
// Cristóbal Valenzuela, George Profenza (merging sendImage and receivesOnly examples)
25+
26+
-->
27+
28+
<!DOCTYPE html>
29+
<html lang="en">
30+
31+
<head>
32+
<meta charset="UTF-8">
33+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
34+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
35+
<title>Runway: OpenPose Demo</title>
36+
<!-- We are using socket.io to establish a socket connection to Runway -->
37+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
38+
<!-- We are using p5.js to draw in the canvas -->
39+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.min.js"></script>
40+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/addons/p5.dom.js"></script>
41+
42+
<style>
43+
* {
44+
font-family: Arial, Helvetica, sans-serif;
45+
font-weight: lighter;
46+
}
47+
48+
</style>
49+
</head>
50+
51+
<body>
52+
<!-- Information about the model -->
53+
<div>
54+
<h2>OpenPose Demo: sending webcam pixels and receiving data coming from Runway</h2>
55+
<p>About the model: Real-time multi-person keypoint detection library for body, face, and hands estimation.</p>
56+
<p>More information: <a href="https://runwayml.com/tutorials/openpose/">https://runwayml.com/tutorials/openpose/</a></p>
57+
<p>This example uses <a href="https://p5js.org/">p5.js</a> to draw body connections</p>
58+
<hr />
59+
</div>
60+
61+
<!-- A log element to show the connection status -->
62+
<div id="status">Not Connected</div>
63+
64+
<script src="script.js"></script>
65+
</body>
66+
67+
</html>

openpose/sendWebcam/script.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,130 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with RunwayML. If not, see <http://www.gnu.org/licenses/>.
1717
//
18+
// ===============================================================
19+
//
20+
// Runway: OpenPose Send Webcam Demo
21+
// This example sends one image to Runway and draw all humans detected in the image
22+
// p5.js is used to draw the human data sent from Runway
23+
// You should select HTTP from the Input Panel
24+
//
25+
// Cristóbal Valenzuela, George Profenza (merging sendImage and receviesOnly examples)
26+
// cris@runwayml.com
27+
//
28+
// ===============================================================
29+
30+
var capture;
31+
var w = 400;
32+
var h = 225;
33+
var colors;
34+
35+
// This are all the body connections we want to draw
36+
var bodyConnections = [
37+
['Nose', 'Left_Eye'],
38+
['Left_Eye', 'Left_Ear'],
39+
['Nose', 'Right_Eye'],
40+
['Right_Eye', 'Right_Ear'],
41+
['Nose', 'Neck'],
42+
['Neck', 'Right_Shoulder'],
43+
['Right_Shoulder', 'Right_Elbow'],
44+
['Right_Elbow', 'Right_Wrist'],
45+
['Neck', 'Left_Shoulder'],
46+
['Left_Shoulder', 'Left_Elbow'],
47+
['Left_Elbow', 'Left_Wrist'],
48+
['Neck', 'Right_Hip'],
49+
['Right_Hip', 'Right_Knee'],
50+
['Right_Knee', 'Right_Ankle'],
51+
['Neck', 'Left_Hip'],
52+
['Left_Hip', 'Left_Knee'],
53+
['Left_Knee', 'Left_Ankle'],
54+
]
55+
56+
// This array will get updated with all the humans detected in the image
57+
var humans = [];
58+
59+
// This connection (to be later initialized)
60+
var socket;
61+
62+
// Wait until the page is loaded
63+
document.addEventListener("DOMContentLoaded", function(event) {
64+
// A variable to hold the status of the connection
65+
var status = document.getElementById('status');
66+
67+
// Create a connection with Runway
68+
// *You should update this address to match the URL provided by the app
69+
socket = io.connect('http://127.0.0.1:33200/query');
70+
71+
// When a connection is established
72+
socket.on('connect', function() {
73+
status.innerHTML = 'Connected';
74+
setInterval(sendImgToRunway,500,capture);
75+
});
76+
77+
// When there is a data event, update the humans array
78+
socket.on('update_response', function(data) {
79+
humans = data.results.humans;
80+
});
81+
});
82+
83+
// p5 setup function
84+
function setup() {
85+
// Create a canvas
86+
createCanvas(w, h);
87+
// Create a video element.
88+
// Although we are getting the images from Runway, this is just to show the video behind
89+
capture = createCapture(VIDEO);
90+
capture.size(w, h);
91+
// Set some style and colors
92+
strokeWeight(2);
93+
colors = [color('#00ff00'), color('#ffff00'), color('#ff0000'), color('#00ffff'), color('#ffffff'), color('#f4f'), color('#00ff'), color('#ffaf00'), color('#aff'), color('#aaf'), color('#33a'), color('#55f'), color('#771'), color('#15f'), color('#ff0000'), color('#00ff00'), color('#ffff00'), color('#ff0000')];
94+
95+
}
96+
97+
function sendImgToRunway(img){
98+
if(img){
99+
// update camera pixels
100+
img.loadPixels();
101+
// send data
102+
socket.emit('update_request', {
103+
data: img.canvas.toDataURL('image/jpeg'),
104+
model: "mobilenet_thin"
105+
});
106+
}else{
107+
console.warn("invalid pixels, skipping request to Runway");
108+
}
109+
}
110+
111+
// p5 draw function
112+
function draw() {
113+
// Every frame, draw the current video frame
114+
image(capture, 0, 0, w, h);
115+
// If there are humans detected, draw them in top of video frames
116+
if (humans.length > 0) {
117+
humans.forEach(human => drawHuman(human));
118+
}
119+
}
18120

121+
// A function that connects joints based on data coming from OpenPose
122+
function drawHuman(human) {
123+
bodyConnections.forEach((connection, i) => {
124+
let start = null;
125+
let endA = null;
126+
let endB = null;
127+
human.forEach(bodyPart => {
128+
const name = bodyPart[0];
129+
if (name === connection[0]) {
130+
start = bodyPart;
131+
} else if (name === connection[1]) {
132+
endA = bodyPart;
133+
} else if (connection[2] && name === connection[2]) {
134+
endB = bodyPart;
135+
}
136+
});
137+
stroke(colors[i]);
138+
if (start && endA && !endB) {
139+
line(start[1] * w, start[2] * h, endA[1] * w, endA[2] * h);
140+
} else if (start && endA && endB) {
141+
line(start[1] * w, start[2] * h, (endA[1] + endB[1]) / 2 * w, (endB[2] + endB[2]) / 2 * h);
142+
}
143+
});
144+
}

0 commit comments

Comments
 (0)