Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of 192.168.7.1:workspace/mustache/

  • Loading branch information...
commit daf6d158586c25f54e4ebdeab8c56abc2a1a9722 2 parents 63c5ba8 + 1fcb6f5
Jason Kridner authored
1  .gitignore
View
@@ -0,0 +1 @@
+twitterkeys.js
BIN  beaglestache.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  presentations/BorisStache.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  presentations/DesignEast-GadgetFreak-DIY-Lab-BeagleStache.pdf
View
Binary file not shown
71 stache.cpp
View
@@ -8,11 +8,13 @@
#include <iostream>
#include <stdio.h>
+#include <termios.h>
+#include <unistd.h>
using namespace std;
using namespace cv;
-string copyright = "\
+const char copyright[] = "\
IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. \n\
\n\
By downloading, copying, installing or using the software you agree to this license.\n\
@@ -54,6 +56,9 @@ the use of this software, even if advised of the possibility of such damage.\n\
/** Function Headers */
void detectAndDisplay(Mat frame);
+void saveFrame(Mat frame);
+int inputAvailable();
+void inputSetup(int setup);
/** Global variables */
String face_cascade_name = "lbpcascade_frontalface.xml";
@@ -70,6 +75,8 @@ int camWidth = 0;
int camHeight = 0;
float camFPS = 0;
+int savedFrames = 0;
+
/**
* @function main
*/
@@ -86,14 +93,14 @@ int main(int argc, const char** argv) {
if(argc > 7) camFPS = (float)atof(argv[7]);
//-- 0. Print the copyright
- cout << copyright;
+ fprintf(stderr, "%s", copyright);
//-- 1. Load the cascade
- if( !face_cascade.load(face_cascade_name) ){ printf("--(!)Error loading\n"); return -1; };
+ if( !face_cascade.load(face_cascade_name) ){ fprintf(stderr, "--(!)Error loading\n"); return -1; };
//-- 1a. Load the mustache mask
mask = cvLoadImage(stacheMaskFile);
- if(!mask) { printf("Could not load %s\n", stacheMaskFile); exit(-1); }
+ if(!mask) { fprintf(stderr, "Could not load %s\n", stacheMaskFile); exit(-1); }
//-- 2. Read the video stream
capture = cvCaptureFromCAM(numCamera);
@@ -101,6 +108,7 @@ int main(int argc, const char** argv) {
if(camHeight) cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, camHeight);
if(camFPS) cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, camFPS);
if(capture) {
+ inputSetup(1);
while(true) {
frame = cvQueryFrame(capture);
@@ -109,14 +117,17 @@ int main(int argc, const char** argv) {
if(!frame.empty()) {
detectAndDisplay( frame );
} else {
- printf(" --(!) No captured frame -- Break!"); break;
+ fprintf(stderr, " --(!) No captured frame -- Break!"); break;
}
- int c = waitKey(10);
- if( (char)c == 'c' ) { break; }
+ if(int c = inputAvailable()) {
+ if( (char)c == 'c' ) { break; }
+ else if( (char)c == 's' ) { saveFrame(frame); }
+ }
} catch(cv::Exception e) {
}
}
+ inputSetup(0);
}
return 0;
}
@@ -127,6 +138,7 @@ int main(int argc, const char** argv) {
void detectAndDisplay(Mat frame) {
std::vector<Rect> faces;
Mat frame_gray;
+ int i = 0;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
@@ -134,7 +146,7 @@ void detectAndDisplay(Mat frame) {
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, Size(80, 80));
- for(int i = 0; i < faces.size(); i++) {
+ for(; i < faces.size(); i++) {
//-- Scale and apply mustache mask for each face
Mat faceROI = frame_gray(faces[i]);
IplImage iplFrame = frame;
@@ -151,3 +163,46 @@ void detectAndDisplay(Mat frame) {
flip(frame, frame, 1);
imshow(window_name, frame);
}
+
+
+void saveFrame(Mat frame) {
+ char filename[20];
+ IplImage iplFrame = frame;
+ sprintf(filename, "captured%03d.jpg", savedFrames);
+ cvSaveImage(filename, &iplFrame);
+ fprintf(stdout, "{\"tweet\":\"New BeagleStache captured!\",\"filename\":\"%s\"}\n", filename);
+ fflush(stdout);
+ savedFrames++;
+ if(savedFrames >= 1000) savedFrames = 0;
+}
+
+int inputAvailable()
+{
+ struct timeval tv;
+ int c;
+ fd_set fds;
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ FD_ZERO(&fds);
+ FD_SET(STDIN_FILENO, &fds);
+ select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
+ if(FD_ISSET(0, &fds)) {
+ c = getchar();
+ return c;
+ } else {
+ return 0;
+ }
+}
+
+void inputSetup(int setup)
+{
+ static struct termios oldt, newt;
+ if(setup) {
+ tcgetattr(STDIN_FILENO, &oldt);
+ newt = oldt;
+ newt.c_lflag &= ~(ICANON);
+ tcsetattr( STDIN_FILENO, TCSANOW, &newt);
+ } else {
+ tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
+ }
+}
128 tweetstache.js
View
@@ -0,0 +1,128 @@
+var http = require('http');
+var https = require('https');
+var fs = require('fs');
+var OAuth = require('oauth').OAuth;
+var child_process = require('child_process');
+var keys = require('./twitterkeys');
+
+function sendTweet(tweet, photoName) {
+ var hostname = 'upload.twitter.com';
+ var path = '/1/statuses/update_with_media.json';
+ var port = 443;
+ //var ppath = 'https://' + hostname + path;
+ //var phostname = 'webproxy.ext.ti.com';
+ //var phostname = 'wwwgate.ti.com';
+ //var pport = 80;
+ //var proxy = 'http://' + phostname;
+
+ var data = fs.readFileSync('./' + photoName);
+
+ var oauth = new OAuth(
+ 'https://api.twitter.com/oauth/request_token',
+ 'https://api.twitter.com/oauth/access_token',
+ keys.twitterKey, keys.twitterSecret,
+ '1.0', null, 'HMAC-SHA1');
+
+ var crlf = "\r\n";
+ var boundary = Math.random().toString(16) + Math.random().toString(16);
+
+ var separator = '--' + boundary;
+ var footer = new Buffer(crlf + separator + '--' + crlf);
+ var fileHeader = 'Content-Disposition: file; name="media[]"; filename="' + photoName + '"';
+
+ var contents = new Buffer(separator + crlf
+ + 'Content-Disposition: form-data; name="status"' + crlf
+ + crlf
+ + tweet + crlf
+ + separator + crlf
+ + fileHeader + crlf
+ + 'Content-Type: image/jpeg' + crlf
+ + crlf);
+
+ var multipartBodyLength = contents.length + data.length + footer.length;
+ var multipartBody = new Buffer(multipartBodyLength);
+ console.log('contents.length = ' + contents.length);
+ console.log('' + contents);
+ console.log('data.length = ' + data.length);
+ console.log('footer.length = ' + footer.length);
+ console.log('' + footer);
+ console.log('multipartBody.length = ' + multipartBody.length);
+ var index = 0;
+ console.log('index = ' + index);
+ for(var i = 0; i < contents.length; i++, index++) {
+ multipartBody[index] = contents[i];
+ }
+ console.log('index = ' + index);
+ for(var i = 0; i < data.length; i++, index++) {
+ multipartBody[index] = data[i];
+ }
+ console.log('index = ' + index);
+ for(var i = 0; i < footer.length; i++, index++) {
+ multipartBody[index] = footer[i];
+ }
+
+ var authorization = oauth.authHeader(
+ 'https://' + hostname + path,
+ keys.accessToken, keys.tokenSecret, 'POST');
+
+ var headers = {
+ 'Authorization': authorization,
+ 'Content-Type': 'multipart/form-data; boundary=' + boundary,
+ 'Host': hostname,
+ 'Content-Length': multipartBodyLength,
+ 'Connection': 'Keep-Alive'
+ };
+
+ var options = {
+ host: typeof(phostname) !== 'undefined' ? phostname : hostname,
+ path: typeof(ppath) !== 'undefined' ? ppath : path,
+ port: typeof(pport) !== 'undefined' ? pport : port,
+ method: 'POST',
+ headers: headers
+ };
+
+ console.log('options = ' + JSON.stringify(options));
+
+ function printChunk(chunk) {
+ console.log(chunk.toString());
+ };
+ function onRequestErr(err) {
+ console.log('Error: Something is wrong.\n'+err+JSON.stringify(err)+'\n');
+ };
+ function onRequestResponse(response) {
+ function printStatusCode() {
+ console.log(response.statusCode +'\n');
+ };
+ response.setEncoding('utf8');
+ response.on('data', printChunk);
+ response.on('end', printStatusCode);
+ };
+ var request = typeof(proxy) !== 'undefined' ? http.request(options) : https.request(options);
+ request.on('error', onRequestErr);
+ request.on('response', onRequestResponse);
+ request.write(multipartBody);
+ request.end();
+};
+
+function stacheMessage(data) {
+ //console.log('stacheMessage = ' + data);
+ try {
+ data = JSON.parse(data);
+ if(data.tweet && data.filename) {
+ console.log('stacheMessage = ' + JSON.stringify(data));
+ //sendTweet(data.tweet, data.filename);
+ }
+ } catch(ex) {
+ }
+};
+var stache = child_process.spawn('./stache', [], {stdio:['pipe', 'pipe', process.stderr]});
+stache.stdout.setEncoding('ascii');
+stache.stdout.on('data', stacheMessage);
+
+function requestStache() {
+ console.log('requestStache');
+ stache.stdin.write('s');
+};
+
+setInterval(requestStache, 5000);
+
Please sign in to comment.
Something went wrong with that request. Please try again.