since: 2016/05/08
update: 2016/05/08
reference:
1. openframeworks
2. I touchs: openFrameworks: Control The Raspberry Pi Camera Module
A. 功能測試: opencvHaarFinderExample
// 單張照片臉部辨識
$ cd /home/pi/of_v0.9.3/examples/addons/opencvHaarFinderExample
$ make
$ ./bin/opencvHaarFinderExample // 結果
-----------------------------------------------------------------------------------------------
B. 功能測試: example-texture-mode
// Camera Module
$ cd /home/pi/of_v0.9.3/addons/
$ git clone https://github.com/jvcleave/ofxRPiCameraVideoGrabber
$ cd /home/pi/of_v0.9.3/addons/ofxRPiCameraVideoGrabber/example-texture-mode
$ make
$ ./bin/example-texture-mode
-----------------------------------------------------------------------------------------------
C. Face Detection With Camera Module
1. addons.make
ofxOpenCv
ofxRPiCameraVideoGrabber
***********************************************
2. main.cpp
#include "ofMain.h"
#include "ofApp.h"
#include "ofGLProgrammableRenderer.h"
int main()
{
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> _linesOfTheFile;
ofBuffer _buffer = ofBufferFromFile("config.txt");
for (auto line : _buffer.getLines()){
_linesOfTheFile.push_back(line);
}
// local variables
int _debug = ofToBool(_linesOfTheFile[1]);
int _camWidth = ofToInt(_linesOfTheFile[4]);
int _camHeight = ofToInt(_linesOfTheFile[7]);
bool _fullScreen = ofToBool(_linesOfTheFile[10]);
// show debug message
if(_debug) {
ofLog() << "****** main()";
ofLog() << "debug: " << _debug;
ofLog() << "OpenGL width: " << _camWidth;
ofLog() << "OpenGL height: " << _camHeight;
ofLog() << "fullScreen: " << _fullScreen;
}
ofGLESWindowSettings settings;
//settings.width = 320;
//settings.height = 240;
settings.width = _camWidth;
settings.height = _camHeight;
settings.setGLESVersion(2);
ofCreateWindow(settings);
ofRunApp( new ofApp());
}
***********************************************
3. ofApp.h
#pragma once
#include "ofMain.h"
#include "TerminalListener.h"
#include "RPiVideoGrabber.h"
#include "ofxCvHaarFinder.h"
class ofApp : public ofBaseApp, public KeyListener{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void onCharacterReceived(KeyListenerEventData& e);
TerminalListener consoleListener;
//wrapper class for drop-in replacement of ofVideoGrabber
RPiVideoGrabber vidGrabber;
//@add for CvHaarFinder
ofxCvHaarFinder finder;
ofImage grayImg;
//ofImage img;
//@add second record
float countROI;
long readSeconds; // read from totalSeconds record file
long writeSeconds; // write to totalSeconds record file
long latestSeconds; // latestSeconds
// config file
bool debug;
int camWidth;
int camHeight;
bool fullScreen;
int frameRate;
bool concurrentVisit;
float minROI;
float maxROI;
float weightROI;
string recordFile;
};
***********************************************
4. ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup()
{
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> linesOfTheFile;
ofBuffer buffer = ofBufferFromFile("config.txt");
for (auto line : buffer.getLines()){
linesOfTheFile.push_back(line);
}
//for (int i = 0; i < linesOfTheFile.size(); i++) {
// ofLog(OF_LOG_VERBOSE, "cur.width = %s", linesOfTheFile[i].c_str());
//}
debug = ofToBool(linesOfTheFile[1]);
camWidth = ofToInt(linesOfTheFile[4]);
camHeight = ofToInt(linesOfTheFile[7]);
fullScreen = ofToBool(linesOfTheFile[10]);
frameRate = ofToInt(linesOfTheFile[13]);
concurrentVisit = ofToBool(linesOfTheFile[16]);
minROI = ofToFloat(linesOfTheFile[19]);
maxROI = ofToFloat(linesOfTheFile[22]);
weightROI = ofToFloat(linesOfTheFile[25]);
recordFile = linesOfTheFile[28];
if(debug)
{
ofLog() << "****** ofApp::setup()";
ofLog() << "debug: " << debug;
ofLog() << "camWidth: " << camWidth;
ofLog() << "camHeight: " << camHeight;
ofLog() << "fullScreen: " << fullScreen;
ofLog() << "frameRate: " << frameRate;
ofLog() << "concurrentVisit: " << concurrentVisit;
ofLog() << "minROI: " << minROI;
ofLog() << "maxROI: " << maxROI;
ofLog() << "weightROI: " << weightROI;
ofLog() << "recordFile: " << recordFile;
}
// read totalSeconds record file
vector<string> totalSecondsFile;
ofBuffer sbuffer = ofBufferFromFile(recordFile);
for (auto line : sbuffer.getLines()){
totalSecondsFile.push_back(line);
}
readSeconds = ofToFloat(totalSecondsFile[0]);
if(debug)
{
ofLog() << "****** read totalSeconds record file";
ofLog() << "readSeconds: " << readSeconds;
}
//ofSetFrameRate(5);
ofSetFrameRate(frameRate);
//allows keys to be entered via terminal remotely (ssh)
consoleListener.setup(this);
//vidGrabber.setDesiredFrameRate(30);
vidGrabber.setDesiredFrameRate(frameRate);
vidGrabber.initGrabber(camWidth, camHeight);
//@add for CvHaarFinder
//img.load("test.jpg");
finder.setup("haarcascade_frontalface_default.xml");
//finder.findHaarObjects(img);
}
//--------------------------------------------------------------
void ofApp::update()
{
ofBackground(100, 100, 100);
vidGrabber.update();
if(vidGrabber.isFrameNew())
{
//ofPixels & pixels = vidGrabber.getPixels();
//finder.findHaarObjects(pixels);
grayImg.setFromPixels(vidGrabber.getPixels());
grayImg.setImageType(OF_IMAGE_GRAYSCALE);
finder.findHaarObjects(grayImg);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetHexColor(0xffffff);
//ofSetColor(ofColor::white);
vidGrabber.draw(0, 0);
//@add for CvHaarFinder
//img.draw(0, 0);
ofNoFill();
for(unsigned int i = 0; i < finder.blobs.size(); i++) {
ofRectangle cur = finder.blobs[i].boundingRect;
//if(debug){
// ofLog(OF_LOG_VERBOSE, "ROI width = %f", cur.width);
// ofLog(OF_LOG_VERBOSE, "ROI height = %f", cur.height);
//}
//if((cur.width >= 50.0 && cur.width <= 150.0) &&
// (cur.height >= 50.0 && cur.height <= 150.0)) {
if((cur.width >= (minROI * camWidth) && cur.width <= (maxROI * camWidth)) &&
(cur.height >= (minROI * camWidth) && cur.height <= (maxROI * camWidth))) {
countROI++;
ofSetColor(255, 0, 0); // red color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
if(debug) {
ofSetColor(ofColor::yellow);
ofDrawBitmapString("ROI = " + ofToString(cur.width) + " x " + ofToString(cur.height), camWidth/5, camHeight/1.02);
}
// not concurrent Visit
if(!concurrentVisit){
break;
}
}
else {
ofSetColor(0, 0, 255); // blue color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
}
}
// writeSeconds
writeSeconds = readSeconds + (countROI / frameRate * weightROI); // weightROI: for calibrate detect missing
if(writeSeconds > latestSeconds){
// Write data
if(debug) {
// totalSeconds record file
ofLog() << "****** write totalSeconds record file: " << writeSeconds;
}
ofBuffer secondsBuff;
secondsBuff.set(ofToString(writeSeconds));
bool fileWritten = ofBufferToFile(recordFile, secondsBuff);
latestSeconds = writeSeconds;
}
if(debug){
// show message
ofSetColor(ofColor::white);
ofDrawBitmapString("total seconds: " + ofToString(writeSeconds), camWidth/5, camHeight/1.2);
ofDrawBitmapString(ofToString(minROI * camWidth) + " <= ROI <= " + ofToString(maxROI * camWidth), camWidth/5, camHeight/1.1);
}
ofSetColor(100, 100, 100);
}
//--------------------------------------------------------------
void ofApp::keyPressed (int key)
{
ofLog(OF_LOG_VERBOSE, "%c keyPressed", key);
//if (key == 'e')
//{
//}
//@add for VideoGrabber
if(key == 'd' || key == 'D'){
debug = !debug;
}
}
void ofApp::onCharacterReceived(KeyListenerEventData& e)
{
keyPressed((int)e.character);
}
***********************************************
5. bin/data 資料夾
a. haarcascade_frontalface_default.xml // from ofxOpenCv
b. totalSeconds.txt // record second
0
c. config.txt // config file
# debug (true/false)
false
# cam width (int)
320
# cam height (int)
240
# full screen (true/false)
false
# frame rate (int)
5
# concurrent visit (true/false)
false
# minimum width ratio of ROI(region of interest) (float)
0.125
# maximum width ratio of ROI(region of interest) (float)
0.5
# weight of ROI (float)
1.0
# recordFile (string)
totalSeconds.txt
2016年5月8日 星期日
Raspberry Pi: Real-Time Face Detection With USB Webcam & GPIO(I2C)
since: 2016/05/08
update: 2016/08/11
reference:
1. openframeworks
2. GitHub - kashimAstro/ofxGPIO
3. I touchs: Raspberry Pi: Connected Arduino Using I2C
A. 功能測試: opencvHaarFinderExample
// 單張照片臉部辨識
$ cd /home/pi/of_v0.9.3/examples/addons/opencvHaarFinderExample
$ make
$ ./bin/opencvHaarFinderExample // 結果
-----------------------------------------------------------------------------------------------
B. 功能測試: videoGrabberExample
// USB Web Cam
$ cd /home/pi/of_v0.9.3/examples/video/videoGrabberExample
$ make
$ ./bin/videoGrabberExample
// 結果
-----------------------------------------------------------------------------------------------
C. Face Detection With USB Webcam
1. addons.make
ofxOpenCv
***********************************************
2. main.cpp
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> _linesOfTheFile;
ofBuffer _buffer = ofBufferFromFile("config.txt");
for (auto line : _buffer.getLines()){
_linesOfTheFile.push_back(line);
}
// local variables
int _debug = ofToBool(_linesOfTheFile[1]);
int _camWidth = ofToInt(_linesOfTheFile[4]);
int _camHeight = ofToInt(_linesOfTheFile[7]);
bool _fullScreen = ofToBool(_linesOfTheFile[10]);
// show debug message
if(_debug) {
ofLog() << "****** main()";
ofLog() << "debug: " << _debug;
ofLog() << "OpenGL width: " << _camWidth;
ofLog() << "OpenGL height: " << _camHeight;
ofLog() << "fullScreen: " << _fullScreen;
}
// fullScreen
if(_fullScreen) {
// set camWidth & camHeight as window's
ofSetupOpenGL(_camWidth, _camHeight, OF_FULLSCREEN);
}
// window
else {
// set camWidth & camHeight as window's
ofSetupOpenGL(_camWidth, _camHeight, OF_WINDOW);
}
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
***********************************************
3. ofApp.h
#pragma once
#include "ofMain.h"
#include "ofxCvHaarFinder.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
//@add for VideoGrabber
ofVideoGrabber vidGrabber;
//@add for CvHaarFinder
//ofImage img;
ofxCvHaarFinder finder;
//@add second record
float countROI;
long readSeconds; // read from totalSeconds record file
long writeSeconds; // write to totalSeconds record file
long latestSeconds; // latestSeconds
// config file
bool debug;
int camWidth;
int camHeight;
bool fullScreen;
int frameRate;
bool concurrentVisit;
float minROI;
float maxROI;
float weightROI;
string recordFile;
};
***********************************************
4. ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> linesOfTheFile;
ofBuffer buffer = ofBufferFromFile("config.txt");
for (auto line : buffer.getLines()){
linesOfTheFile.push_back(line);
}
//for (int i = 0; i < linesOfTheFile.size(); i++) {
// ofLog(OF_LOG_VERBOSE, "cur.width = %s", linesOfTheFile[i].c_str());
//}
debug = ofToBool(linesOfTheFile[1]);
camWidth = ofToInt(linesOfTheFile[4]);
camHeight = ofToInt(linesOfTheFile[7]);
fullScreen = ofToBool(linesOfTheFile[10]);
frameRate = ofToInt(linesOfTheFile[13]);
concurrentVisit = ofToBool(linesOfTheFile[16]);
minROI = ofToFloat(linesOfTheFile[19]);
maxROI = ofToFloat(linesOfTheFile[22]);
weightROI = ofToFloat(linesOfTheFile[25]);
recordFile = linesOfTheFile[28];
if(debug)
{
ofLog() << "****** ofApp::setup()";
ofLog() << "debug: " << debug;
ofLog() << "camWidth: " << camWidth;
ofLog() << "camHeight: " << camHeight;
ofLog() << "fullScreen: " << fullScreen;
ofLog() << "frameRate: " << frameRate;
ofLog() << "concurrentVisit: " << concurrentVisit;
ofLog() << "minROI: " << minROI;
ofLog() << "maxROI: " << maxROI;
ofLog() << "weightROI: " << weightROI;
ofLog() << "recordFile: " << recordFile;
}
// read totalSeconds record file
vector<string> totalSecondsFile;
ofBuffer sbuffer = ofBufferFromFile(recordFile);
for (auto line : sbuffer.getLines()){
totalSecondsFile.push_back(line);
}
readSeconds = ofToFloat(totalSecondsFile[0]);
if(debug)
{
ofLog() << "****** read totalSeconds record file";
ofLog() << "readSeconds: " << readSeconds;
}
//ofSetFrameRate(5);
ofSetFrameRate(frameRate);
//@add for VideoGrabber
//we can now get back a list of devices.
vector<ofVideoDevice> devices = vidGrabber.listDevices();
for(int i = 0; i < devices.size(); i++){
if(devices[i].bAvailable){
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName;
}else{
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName << " - unavailable ";
}
}
vidGrabber.setDeviceID(0);
//vidGrabber.setDesiredFrameRate(60);
vidGrabber.setDesiredFrameRate(frameRate);
vidGrabber.initGrabber(camWidth, camHeight);
ofSetVerticalSync(true);
//@add for CvHaarFinder
//img.load("test.jpg");
finder.setup("haarcascade_frontalface_default.xml");
//finder.findHaarObjects(img);
}
//--------------------------------------------------------------
void ofApp::update(){
//@add for VideoGrabber
ofBackground(100, 100, 100);
vidGrabber.update();
//@add for CvHaarFinder
if(vidGrabber.isFrameNew()){
ofPixels & pixels = vidGrabber.getPixels();
finder.findHaarObjects(pixels);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
//@add for VideoGrabber
ofSetHexColor(0xffffff);
vidGrabber.draw(0, 0);
//@add for CvHaarFinder
//img.draw(0, 0);
ofNoFill();
for(unsigned int i = 0; i < finder.blobs.size(); i++) {
ofRectangle cur = finder.blobs[i].boundingRect;
//if(debug){
// ofLog(OF_LOG_VERBOSE, "ROI width = %f", cur.width);
// ofLog(OF_LOG_VERBOSE, "ROI height = %f", cur.height);
//}
//if((cur.width >= 50.0 && cur.width <= 150.0) &&
// (cur.height >= 50.0 && cur.height <= 150.0)) {
if((cur.width >= (minROI * camWidth) && cur.width <= (maxROI * camWidth)) &&
(cur.height >= (minROI * camWidth) && cur.height <= (maxROI * camWidth))) {
countROI++;
ofSetColor(255, 0, 0); // red color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
if(debug) {
ofSetColor(ofColor::yellow);
ofDrawBitmapString("ROI = " + ofToString(cur.width) + " x " + ofToString(cur.height), camWidth/5, camHeight/1.02);
}
// not concurrent Visit
if(!concurrentVisit){
break;
}
}
else {
ofSetColor(0, 0, 255); // blue color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
}
}
// writeSeconds
writeSeconds = readSeconds + (countROI / frameRate * weightROI); // weightROI: for calibrate detect missing
if(writeSeconds > latestSeconds){
// Write data
if(debug) {
// totalSeconds record file
ofLog() << "****** write totalSeconds record file: " << writeSeconds;
}
ofBuffer secondsBuff;
secondsBuff.set(ofToString(writeSeconds));
bool fileWritten = ofBufferToFile(recordFile, secondsBuff);
latestSeconds = writeSeconds;
}
if(debug){
// show message
ofSetColor(ofColor::white);
ofDrawBitmapString("total seconds: " + ofToString(writeSeconds), camWidth/5, camHeight/1.2);
ofDrawBitmapString(ofToString(minROI * camWidth) + " <= ROI <= " + ofToString(maxROI * camWidth), camWidth/5, camHeight/1.1);
}
ofSetColor(100, 100, 100);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
//@add for VideoGrabber
if(key == 'd' || key == 'D'){
debug = !debug;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
***********************************************
5. bin/data 資料夾
a. haarcascade_frontalface_default.xml // from ofxOpenCv
b. totalSeconds.txt // record second
0
c. config.txt // config file
# debug (true/false)
false
# cam width (int)
320
# cam height (int)
240
# full screen (true/false)
false
# frame rate (int)
5
# concurrent visit (true/false)
false
# minimum width ratio of ROI(region of interest) (float)
0.125
# maximum width ratio of ROI(region of interest) (float)
0.5
# weight of ROI (float)
1.0
# recordFile (string)
totalSeconds.txt
-----------------------------------------------------------------------------------------------
* 無法正常作用(可能要配合 ofx 0.9.2) *
D. 利用 GPIO( or I²C: Inter-Integrated Circuit) 傳送資料給 Arduino
1. 參考: I touchs: Raspberry Pi: Connected Arduino Using I2C
> 讓 Raspberry Pi 的 I2C 功能啟用, 並且記下以下的資料
$ ls /dev/i2c*
/dev/i2c-1
以及 Arduino 裡的定義:
#define SLAVE_ADDRESS 0x04
2. 安裝 ofxGPIO
$ cd /home/pi/of_v0.9.3/addons
$ git clone https://github.com/kashimAstro/ofxGPIO
3. 修改專案的 addons.make 檔案如下:
/home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB/addons.make
ofxOpenCv
ofxGPIO
4. 修改 ofApp.h 如下:
/home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB/src/ofApp.h
#pragma once
#include "ofMain.h"
#include "ofxCvHaarFinder.h"
//@add for GPIO
#include "ofxGPIO.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
//@add for GPIO
//void exit();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
//@add for VideoGrabber
ofVideoGrabber vidGrabber;
//@add for CvHaarFinder
//ofImage img;
ofxCvHaarFinder finder;
//@add second record
float countROI;
long readSeconds; // read from totalSeconds record file
long writeSeconds; // write to totalSeconds record file
long latestSeconds; // latestSeconds
// config file
bool debug;
int camWidth;
int camHeight;
bool fullScreen;
int frameRate;
bool concurrentVisit;
float minROI;
float maxROI;
float weightROI;
string recordFile;
//@add for I2C
//I2CBus * bus;
//@add for GPIO
//GPIO ofxGPIO;
GPIO* ofxGPIO;
};
5. 修改 ofApp.cpp 如下:
/home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB/src/ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> linesOfTheFile;
ofBuffer buffer = ofBufferFromFile("config.txt");
for (auto line : buffer.getLines()){
linesOfTheFile.push_back(line);
}
//for (int i = 0; i < linesOfTheFile.size(); i++) {
// ofLog(OF_LOG_VERBOSE, "cur.width = %s", linesOfTheFile[i].c_str());
//}
debug = ofToBool(linesOfTheFile[1]);
camWidth = ofToInt(linesOfTheFile[4]);
camHeight = ofToInt(linesOfTheFile[7]);
fullScreen = ofToBool(linesOfTheFile[10]);
frameRate = ofToInt(linesOfTheFile[13]);
concurrentVisit = ofToBool(linesOfTheFile[16]);
minROI = ofToFloat(linesOfTheFile[19]);
maxROI = ofToFloat(linesOfTheFile[22]);
weightROI = ofToFloat(linesOfTheFile[25]);
recordFile = linesOfTheFile[28];
if(debug)
{
ofLog() << "****** ofApp::setup()";
ofLog() << "debug: " << debug;
ofLog() << "camWidth: " << camWidth;
ofLog() << "camHeight: " << camHeight;
ofLog() << "fullScreen: " << fullScreen;
ofLog() << "frameRate: " << frameRate;
ofLog() << "concurrentVisit: " << concurrentVisit;
ofLog() << "minROI: " << minROI;
ofLog() << "maxROI: " << maxROI;
ofLog() << "weightROI: " << weightROI;
ofLog() << "recordFile: " << recordFile;
}
// read totalSeconds record file
vector<string> totalSecondsFile;
ofBuffer sbuffer = ofBufferFromFile(recordFile);
for (auto line : sbuffer.getLines()){
totalSecondsFile.push_back(line);
}
readSeconds = ofToFloat(totalSecondsFile[0]);
if(debug)
{
ofLog() << "****** read totalSeconds record file";
ofLog() << "readSeconds: " << readSeconds;
}
//ofSetFrameRate(5);
ofSetFrameRate(frameRate);
//@add for VideoGrabber
//we can now get back a list of devices.
vector<ofVideoDevice> devices = vidGrabber.listDevices();
for(int i = 0; i < devices.size(); i++){
if(devices[i].bAvailable){
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName;
}else{
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName << " - unavailable ";
}
}
vidGrabber.setDeviceID(0);
//vidGrabber.setDesiredFrameRate(60);
vidGrabber.setDesiredFrameRate(frameRate);
vidGrabber.initGrabber(camWidth, camHeight);
ofSetVerticalSync(true);
//@add for CvHaarFinder
//img.load("test.jpg");
finder.setup("haarcascade_frontalface_default.xml");
//finder.findHaarObjects(img);
//@add for GPIO
//bus = new I2CBus("/dev/i2c-1");
//bus->addressSet(0x04);
//@add for GPIO (pin)
//ofxGPIO.setup("7");
//ofxGPIO.export_gpio();
//ofxGPIO.setdir_gpio("out");
ofxGPIO = new GPIO("7");
ofxGPIO->export_gpio();
ofxGPIO->setdir_gpio("out");
}
//--------------------------------------------------------------
void ofApp::update(){
//@add for VideoGrabber
ofBackground(100, 100, 100);
vidGrabber.update();
//@add for CvHaarFinder
if(vidGrabber.isFrameNew()){
ofPixels & pixels = vidGrabber.getPixels();
finder.findHaarObjects(pixels);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
//@add for VideoGrabber
ofSetHexColor(0xffffff);
vidGrabber.draw(0, 0);
//@add for CvHaarFinder
//img.draw(0, 0);
ofNoFill();
for(unsigned int i = 0; i < finder.blobs.size(); i++) {
ofRectangle cur = finder.blobs[i].boundingRect;
//if(debug){
// ofLog(OF_LOG_VERBOSE, "ROI width = %f", cur.width);
// ofLog(OF_LOG_VERBOSE, "ROI height = %f", cur.height);
//}
//if((cur.width >= 50.0 && cur.width <= 150.0) &&
// (cur.height >= 50.0 && cur.height <= 150.0)) {
if((cur.width >= (minROI * camWidth) && cur.width <= (maxROI * camWidth)) &&
(cur.height >= (minROI * camWidth) && cur.height <= (maxROI * camWidth))) {
countROI++;
ofSetColor(255, 0, 0); // red color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
if(debug) {
ofSetColor(ofColor::yellow);
ofDrawBitmapString("ROI = " + ofToString(cur.width) + " x " + ofToString(cur.height), camWidth/5, camHeight/1.02);
}
// not concurrent Visit
if(!concurrentVisit){
break;
}
}
else {
ofSetColor(0, 0, 255); // blue color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
}
}
// writeSeconds
writeSeconds = readSeconds + (countROI / frameRate * weightROI); // weightROI: for calibrate detect missing
if(writeSeconds > latestSeconds){
// Write data
if(debug) {
// totalSeconds record file
ofLog() << "****** write totalSeconds record file: " << writeSeconds;
}
ofBuffer secondsBuff;
secondsBuff.set(ofToString(writeSeconds));
bool fileWritten = ofBufferToFile(recordFile, secondsBuff);
latestSeconds = writeSeconds;
//@add for GPIO
//bus->writeByte(0x04, writeSeconds);
//usleep(50000); // sleep for 0.05 second
//@add for test
/*
bus->writeByte(0x04,1);
usleep(500000); // sleep for 0.5 second
bus->writeByte(0x04,0);
usleep(500000); // sleep for 0.5 second
*/
//@add for GPIO
//ofxGPIO.setval_gpio("1");
//usleep(50000); // sleep for 0.05 second
//ofxGPIO.setval_gpio("0");
//usleep(50000); // sleep for 0.05 second
ofxGPIO->setval_gpio("1");
usleep(50000); // sleep for 0.05 second
ofxGPIO->setval_gpio("0");
usleep(50000); // sleep for 0.05 second
}
if(debug){
// show message
ofSetColor(ofColor::white);
ofDrawBitmapString("total seconds: " + ofToString(writeSeconds), camWidth/5, camHeight/1.2);
ofDrawBitmapString(ofToString(minROI * camWidth) + " <= ROI <= " + ofToString(maxROI * camWidth), camWidth/5, camHeight/1.1);
}
ofSetColor(100, 100, 100);
}
/*
//--------------------------------------------------------------
void ofApp::exit(){
//@add for GPIO
ofxGPIO.unexport_gpio();
}
*/
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
//@add for VideoGrabber
if(key == 'd' || key == 'D'){
debug = !debug;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
6. 編譯與執行:
$ cd /home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB
$ make
$ ./bin/DPHeadTrackingUSB
7. 開機後自動執行: (console 的訊息會出不來, 建議改成在 .bashrc 裡執行程式)
$ cd
$ touch startHeadTracking.sh
$ chmod 755 startHeadTracking.sh
$ sudo nano startHeadTracking.sh
#!/bin/bash
sleep 5
cd /home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB
./bin/DPHeadTrackingUSB
$ cat startHeadTracking.sh
$ sudo crontab -e
....
@reboot /home/pi/startHeadTracking.sh
$ sudo crontab -l
update: 2016/08/11
reference:
1. openframeworks
2. GitHub - kashimAstro/ofxGPIO
3. I touchs: Raspberry Pi: Connected Arduino Using I2C
A. 功能測試: opencvHaarFinderExample
// 單張照片臉部辨識
$ cd /home/pi/of_v0.9.3/examples/addons/opencvHaarFinderExample
$ make
$ ./bin/opencvHaarFinderExample // 結果
B. 功能測試: videoGrabberExample
// USB Web Cam
$ cd /home/pi/of_v0.9.3/examples/video/videoGrabberExample
$ make
$ ./bin/videoGrabberExample
// 結果
C. Face Detection With USB Webcam
1. addons.make
ofxOpenCv
***********************************************
2. main.cpp
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> _linesOfTheFile;
ofBuffer _buffer = ofBufferFromFile("config.txt");
for (auto line : _buffer.getLines()){
_linesOfTheFile.push_back(line);
}
// local variables
int _debug = ofToBool(_linesOfTheFile[1]);
int _camWidth = ofToInt(_linesOfTheFile[4]);
int _camHeight = ofToInt(_linesOfTheFile[7]);
bool _fullScreen = ofToBool(_linesOfTheFile[10]);
// show debug message
if(_debug) {
ofLog() << "****** main()";
ofLog() << "debug: " << _debug;
ofLog() << "OpenGL width: " << _camWidth;
ofLog() << "OpenGL height: " << _camHeight;
ofLog() << "fullScreen: " << _fullScreen;
}
// fullScreen
if(_fullScreen) {
// set camWidth & camHeight as window's
ofSetupOpenGL(_camWidth, _camHeight, OF_FULLSCREEN);
}
// window
else {
// set camWidth & camHeight as window's
ofSetupOpenGL(_camWidth, _camHeight, OF_WINDOW);
}
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
***********************************************
3. ofApp.h
#pragma once
#include "ofMain.h"
#include "ofxCvHaarFinder.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
//@add for VideoGrabber
ofVideoGrabber vidGrabber;
//@add for CvHaarFinder
//ofImage img;
ofxCvHaarFinder finder;
//@add second record
float countROI;
long readSeconds; // read from totalSeconds record file
long writeSeconds; // write to totalSeconds record file
long latestSeconds; // latestSeconds
// config file
bool debug;
int camWidth;
int camHeight;
bool fullScreen;
int frameRate;
bool concurrentVisit;
float minROI;
float maxROI;
float weightROI;
string recordFile;
};
***********************************************
4. ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> linesOfTheFile;
ofBuffer buffer = ofBufferFromFile("config.txt");
for (auto line : buffer.getLines()){
linesOfTheFile.push_back(line);
}
//for (int i = 0; i < linesOfTheFile.size(); i++) {
// ofLog(OF_LOG_VERBOSE, "cur.width = %s", linesOfTheFile[i].c_str());
//}
debug = ofToBool(linesOfTheFile[1]);
camWidth = ofToInt(linesOfTheFile[4]);
camHeight = ofToInt(linesOfTheFile[7]);
fullScreen = ofToBool(linesOfTheFile[10]);
frameRate = ofToInt(linesOfTheFile[13]);
concurrentVisit = ofToBool(linesOfTheFile[16]);
minROI = ofToFloat(linesOfTheFile[19]);
maxROI = ofToFloat(linesOfTheFile[22]);
weightROI = ofToFloat(linesOfTheFile[25]);
recordFile = linesOfTheFile[28];
if(debug)
{
ofLog() << "****** ofApp::setup()";
ofLog() << "debug: " << debug;
ofLog() << "camWidth: " << camWidth;
ofLog() << "camHeight: " << camHeight;
ofLog() << "fullScreen: " << fullScreen;
ofLog() << "frameRate: " << frameRate;
ofLog() << "concurrentVisit: " << concurrentVisit;
ofLog() << "minROI: " << minROI;
ofLog() << "maxROI: " << maxROI;
ofLog() << "weightROI: " << weightROI;
ofLog() << "recordFile: " << recordFile;
}
// read totalSeconds record file
vector<string> totalSecondsFile;
ofBuffer sbuffer = ofBufferFromFile(recordFile);
for (auto line : sbuffer.getLines()){
totalSecondsFile.push_back(line);
}
readSeconds = ofToFloat(totalSecondsFile[0]);
if(debug)
{
ofLog() << "****** read totalSeconds record file";
ofLog() << "readSeconds: " << readSeconds;
}
//ofSetFrameRate(5);
ofSetFrameRate(frameRate);
//@add for VideoGrabber
//we can now get back a list of devices.
vector<ofVideoDevice> devices = vidGrabber.listDevices();
for(int i = 0; i < devices.size(); i++){
if(devices[i].bAvailable){
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName;
}else{
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName << " - unavailable ";
}
}
vidGrabber.setDeviceID(0);
//vidGrabber.setDesiredFrameRate(60);
vidGrabber.setDesiredFrameRate(frameRate);
vidGrabber.initGrabber(camWidth, camHeight);
ofSetVerticalSync(true);
//@add for CvHaarFinder
//img.load("test.jpg");
finder.setup("haarcascade_frontalface_default.xml");
//finder.findHaarObjects(img);
}
//--------------------------------------------------------------
void ofApp::update(){
//@add for VideoGrabber
ofBackground(100, 100, 100);
vidGrabber.update();
//@add for CvHaarFinder
if(vidGrabber.isFrameNew()){
ofPixels & pixels = vidGrabber.getPixels();
finder.findHaarObjects(pixels);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
//@add for VideoGrabber
ofSetHexColor(0xffffff);
vidGrabber.draw(0, 0);
//@add for CvHaarFinder
//img.draw(0, 0);
ofNoFill();
for(unsigned int i = 0; i < finder.blobs.size(); i++) {
ofRectangle cur = finder.blobs[i].boundingRect;
//if(debug){
// ofLog(OF_LOG_VERBOSE, "ROI width = %f", cur.width);
// ofLog(OF_LOG_VERBOSE, "ROI height = %f", cur.height);
//}
//if((cur.width >= 50.0 && cur.width <= 150.0) &&
// (cur.height >= 50.0 && cur.height <= 150.0)) {
if((cur.width >= (minROI * camWidth) && cur.width <= (maxROI * camWidth)) &&
(cur.height >= (minROI * camWidth) && cur.height <= (maxROI * camWidth))) {
countROI++;
ofSetColor(255, 0, 0); // red color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
if(debug) {
ofSetColor(ofColor::yellow);
ofDrawBitmapString("ROI = " + ofToString(cur.width) + " x " + ofToString(cur.height), camWidth/5, camHeight/1.02);
}
// not concurrent Visit
if(!concurrentVisit){
break;
}
}
else {
ofSetColor(0, 0, 255); // blue color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
}
}
// writeSeconds
writeSeconds = readSeconds + (countROI / frameRate * weightROI); // weightROI: for calibrate detect missing
if(writeSeconds > latestSeconds){
// Write data
if(debug) {
// totalSeconds record file
ofLog() << "****** write totalSeconds record file: " << writeSeconds;
}
ofBuffer secondsBuff;
secondsBuff.set(ofToString(writeSeconds));
bool fileWritten = ofBufferToFile(recordFile, secondsBuff);
latestSeconds = writeSeconds;
}
if(debug){
// show message
ofSetColor(ofColor::white);
ofDrawBitmapString("total seconds: " + ofToString(writeSeconds), camWidth/5, camHeight/1.2);
ofDrawBitmapString(ofToString(minROI * camWidth) + " <= ROI <= " + ofToString(maxROI * camWidth), camWidth/5, camHeight/1.1);
}
ofSetColor(100, 100, 100);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
//@add for VideoGrabber
if(key == 'd' || key == 'D'){
debug = !debug;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
***********************************************
5. bin/data 資料夾
a. haarcascade_frontalface_default.xml // from ofxOpenCv
b. totalSeconds.txt // record second
0
c. config.txt // config file
# debug (true/false)
false
# cam width (int)
320
# cam height (int)
240
# full screen (true/false)
false
# frame rate (int)
5
# concurrent visit (true/false)
false
# minimum width ratio of ROI(region of interest) (float)
0.125
# maximum width ratio of ROI(region of interest) (float)
0.5
# weight of ROI (float)
1.0
# recordFile (string)
totalSeconds.txt
-----------------------------------------------------------------------------------------------
* 無法正常作用(可能要配合 ofx 0.9.2) *
D. 利用 GPIO( or I²C: Inter-Integrated Circuit) 傳送資料給 Arduino
1. 參考: I touchs: Raspberry Pi: Connected Arduino Using I2C
> 讓 Raspberry Pi 的 I2C 功能啟用, 並且記下以下的資料
$ ls /dev/i2c*
/dev/i2c-1
以及 Arduino 裡的定義:
#define SLAVE_ADDRESS 0x04
2. 安裝 ofxGPIO
$ cd /home/pi/of_v0.9.3/addons
$ git clone https://github.com/kashimAstro/ofxGPIO
3. 修改專案的 addons.make 檔案如下:
/home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB/addons.make
ofxOpenCv
ofxGPIO
4. 修改 ofApp.h 如下:
/home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB/src/ofApp.h
#pragma once
#include "ofMain.h"
#include "ofxCvHaarFinder.h"
//@add for GPIO
#include "ofxGPIO.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
//@add for GPIO
//void exit();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
//@add for VideoGrabber
ofVideoGrabber vidGrabber;
//@add for CvHaarFinder
//ofImage img;
ofxCvHaarFinder finder;
//@add second record
float countROI;
long readSeconds; // read from totalSeconds record file
long writeSeconds; // write to totalSeconds record file
long latestSeconds; // latestSeconds
// config file
bool debug;
int camWidth;
int camHeight;
bool fullScreen;
int frameRate;
bool concurrentVisit;
float minROI;
float maxROI;
float weightROI;
string recordFile;
//@add for I2C
//I2CBus * bus;
//@add for GPIO
//GPIO ofxGPIO;
GPIO* ofxGPIO;
};
5. 修改 ofApp.cpp 如下:
/home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB/src/ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
// read config file
vector<string> linesOfTheFile;
ofBuffer buffer = ofBufferFromFile("config.txt");
for (auto line : buffer.getLines()){
linesOfTheFile.push_back(line);
}
//for (int i = 0; i < linesOfTheFile.size(); i++) {
// ofLog(OF_LOG_VERBOSE, "cur.width = %s", linesOfTheFile[i].c_str());
//}
debug = ofToBool(linesOfTheFile[1]);
camWidth = ofToInt(linesOfTheFile[4]);
camHeight = ofToInt(linesOfTheFile[7]);
fullScreen = ofToBool(linesOfTheFile[10]);
frameRate = ofToInt(linesOfTheFile[13]);
concurrentVisit = ofToBool(linesOfTheFile[16]);
minROI = ofToFloat(linesOfTheFile[19]);
maxROI = ofToFloat(linesOfTheFile[22]);
weightROI = ofToFloat(linesOfTheFile[25]);
recordFile = linesOfTheFile[28];
if(debug)
{
ofLog() << "****** ofApp::setup()";
ofLog() << "debug: " << debug;
ofLog() << "camWidth: " << camWidth;
ofLog() << "camHeight: " << camHeight;
ofLog() << "fullScreen: " << fullScreen;
ofLog() << "frameRate: " << frameRate;
ofLog() << "concurrentVisit: " << concurrentVisit;
ofLog() << "minROI: " << minROI;
ofLog() << "maxROI: " << maxROI;
ofLog() << "weightROI: " << weightROI;
ofLog() << "recordFile: " << recordFile;
}
// read totalSeconds record file
vector<string> totalSecondsFile;
ofBuffer sbuffer = ofBufferFromFile(recordFile);
for (auto line : sbuffer.getLines()){
totalSecondsFile.push_back(line);
}
readSeconds = ofToFloat(totalSecondsFile[0]);
if(debug)
{
ofLog() << "****** read totalSeconds record file";
ofLog() << "readSeconds: " << readSeconds;
}
//ofSetFrameRate(5);
ofSetFrameRate(frameRate);
//@add for VideoGrabber
//we can now get back a list of devices.
vector<ofVideoDevice> devices = vidGrabber.listDevices();
for(int i = 0; i < devices.size(); i++){
if(devices[i].bAvailable){
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName;
}else{
ofLogNotice() << devices[i].id << ": " << devices[i].deviceName << " - unavailable ";
}
}
vidGrabber.setDeviceID(0);
//vidGrabber.setDesiredFrameRate(60);
vidGrabber.setDesiredFrameRate(frameRate);
vidGrabber.initGrabber(camWidth, camHeight);
ofSetVerticalSync(true);
//@add for CvHaarFinder
//img.load("test.jpg");
finder.setup("haarcascade_frontalface_default.xml");
//finder.findHaarObjects(img);
//@add for GPIO
//bus = new I2CBus("/dev/i2c-1");
//bus->addressSet(0x04);
//@add for GPIO (pin)
//ofxGPIO.setup("7");
//ofxGPIO.export_gpio();
//ofxGPIO.setdir_gpio("out");
ofxGPIO = new GPIO("7");
ofxGPIO->export_gpio();
ofxGPIO->setdir_gpio("out");
}
//--------------------------------------------------------------
void ofApp::update(){
//@add for VideoGrabber
ofBackground(100, 100, 100);
vidGrabber.update();
//@add for CvHaarFinder
if(vidGrabber.isFrameNew()){
ofPixels & pixels = vidGrabber.getPixels();
finder.findHaarObjects(pixels);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
//@add for VideoGrabber
ofSetHexColor(0xffffff);
vidGrabber.draw(0, 0);
//@add for CvHaarFinder
//img.draw(0, 0);
ofNoFill();
for(unsigned int i = 0; i < finder.blobs.size(); i++) {
ofRectangle cur = finder.blobs[i].boundingRect;
//if(debug){
// ofLog(OF_LOG_VERBOSE, "ROI width = %f", cur.width);
// ofLog(OF_LOG_VERBOSE, "ROI height = %f", cur.height);
//}
//if((cur.width >= 50.0 && cur.width <= 150.0) &&
// (cur.height >= 50.0 && cur.height <= 150.0)) {
if((cur.width >= (minROI * camWidth) && cur.width <= (maxROI * camWidth)) &&
(cur.height >= (minROI * camWidth) && cur.height <= (maxROI * camWidth))) {
countROI++;
ofSetColor(255, 0, 0); // red color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
if(debug) {
ofSetColor(ofColor::yellow);
ofDrawBitmapString("ROI = " + ofToString(cur.width) + " x " + ofToString(cur.height), camWidth/5, camHeight/1.02);
}
// not concurrent Visit
if(!concurrentVisit){
break;
}
}
else {
ofSetColor(0, 0, 255); // blue color
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
}
}
// writeSeconds
writeSeconds = readSeconds + (countROI / frameRate * weightROI); // weightROI: for calibrate detect missing
if(writeSeconds > latestSeconds){
// Write data
if(debug) {
// totalSeconds record file
ofLog() << "****** write totalSeconds record file: " << writeSeconds;
}
ofBuffer secondsBuff;
secondsBuff.set(ofToString(writeSeconds));
bool fileWritten = ofBufferToFile(recordFile, secondsBuff);
latestSeconds = writeSeconds;
//@add for GPIO
//bus->writeByte(0x04, writeSeconds);
//usleep(50000); // sleep for 0.05 second
//@add for test
/*
bus->writeByte(0x04,1);
usleep(500000); // sleep for 0.5 second
bus->writeByte(0x04,0);
usleep(500000); // sleep for 0.5 second
*/
//@add for GPIO
//ofxGPIO.setval_gpio("1");
//usleep(50000); // sleep for 0.05 second
//ofxGPIO.setval_gpio("0");
//usleep(50000); // sleep for 0.05 second
ofxGPIO->setval_gpio("1");
usleep(50000); // sleep for 0.05 second
ofxGPIO->setval_gpio("0");
usleep(50000); // sleep for 0.05 second
}
if(debug){
// show message
ofSetColor(ofColor::white);
ofDrawBitmapString("total seconds: " + ofToString(writeSeconds), camWidth/5, camHeight/1.2);
ofDrawBitmapString(ofToString(minROI * camWidth) + " <= ROI <= " + ofToString(maxROI * camWidth), camWidth/5, camHeight/1.1);
}
ofSetColor(100, 100, 100);
}
/*
//--------------------------------------------------------------
void ofApp::exit(){
//@add for GPIO
ofxGPIO.unexport_gpio();
}
*/
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
//@add for VideoGrabber
if(key == 'd' || key == 'D'){
debug = !debug;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
6. 編譯與執行:
$ cd /home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB
$ make
$ ./bin/DPHeadTrackingUSB
7. 開機後自動執行: (console 的訊息會出不來, 建議改成在 .bashrc 裡執行程式)
$ cd
$ touch startHeadTracking.sh
$ chmod 755 startHeadTracking.sh
$ sudo nano startHeadTracking.sh
#!/bin/bash
sleep 5
cd /home/pi/of_v0.9.3/apps/myApps/DPHeadTrackingUSB
./bin/DPHeadTrackingUSB
$ cat startHeadTracking.sh
$ sudo crontab -e
....
@reboot /home/pi/startHeadTracking.sh
$ sudo crontab -l
標籤:
OpenCV,
openFrameworks,
Raspberry Pi
2016年1月15日 星期五
openFrameworks: Reading From And Writing To Files With ofBuffer Class
since: 2016/01/15
update: 2016/01/15
reference:
A. in header file:
....
/* config file */
bool debug; // debug mode: to show more message or not
float checkTime_second;
/* record datetime to file */
ofBuffer readBuffer;
ofBuffer checkTimeBuffer;
string nowDateString;
-----------------------------------------------------------------------------------------------
B. in c++ file:
1. Read File:
....
// read config file
readBuffer = ofBufferFromFile("config.txt");
//cout << readBuffer.getText(); // let's see what it says
vector<string> lines = ofSplitString(readBuffer, "\n");
for( int i = 0; i < lines.size(); i++){
//cout << lines[i] << endl;
if(i == 1) {
debug = ofToBool(lines[i]);
if(debug) {
ofLog(OF_LOG_NOTICE, ">>>>> debug true");
}
else {
ofLog(OF_LOG_NOTICE, ">>>>> debug false");
}
}
else if(i == 4) {
checkTime_second = ofToFloat(lines[i]);
ofLogNotice(">>>>>>>>> checkTime_second") << checkTime_second;
}
2. Write File:
....
// inner some loop
if(ofGetElapsedTimef() >= checkTime_second) {
ofResetElapsedTimeCounter();
nowDateString = ofGetTimestampString("%Y%m%d%H%M%S");
checkTimeBuffer.set(nowDateString.c_str(), nowDateString.size());
if(ofBufferToFile("dateTime.txt", checkTimeBuffer)) {
ofLogNotice(">>>>>>>>> update checking dateTime") << nowDateString;
}
}
update: 2016/01/15
reference:
A. in header file:
....
/* config file */
bool debug; // debug mode: to show more message or not
float checkTime_second;
/* record datetime to file */
ofBuffer readBuffer;
ofBuffer checkTimeBuffer;
string nowDateString;
-----------------------------------------------------------------------------------------------
B. in c++ file:
1. Read File:
....
// read config file
readBuffer = ofBufferFromFile("config.txt");
//cout << readBuffer.getText(); // let's see what it says
vector<string> lines = ofSplitString(readBuffer, "\n");
for( int i = 0; i < lines.size(); i++){
//cout << lines[i] << endl;
if(i == 1) {
debug = ofToBool(lines[i]);
if(debug) {
ofLog(OF_LOG_NOTICE, ">>>>> debug true");
}
else {
ofLog(OF_LOG_NOTICE, ">>>>> debug false");
}
}
else if(i == 4) {
checkTime_second = ofToFloat(lines[i]);
ofLogNotice(">>>>>>>>> checkTime_second") << checkTime_second;
}
2. Write File:
....
// inner some loop
if(ofGetElapsedTimef() >= checkTime_second) {
ofResetElapsedTimeCounter();
nowDateString = ofGetTimestampString("%Y%m%d%H%M%S");
checkTimeBuffer.set(nowDateString.c_str(), nowDateString.size());
if(ofBufferToFile("dateTime.txt", checkTimeBuffer)) {
ofLogNotice(">>>>>>>>> update checking dateTime") << nowDateString;
}
}
標籤:
Mac OS X,
openFrameworks
2015年12月23日 星期三
openFrameworks: Kinect For Windows V2
since: 2015/12/23
update: 2015/12/23
reference:
1. Setting up Kinect 2 for OpenFrameworks | rbarraza.com
2. elliotwoods/ofxKinectForWindows2 at 0.8.4
A. 系統需求:
1. A Kinect for Windows v2 Device (K4W2)
2. 64bit computer with a dedicated USB 3.0
3. Windows 10, 8, 8.1
4. Update your latest video card driver
5. Install DirectX 11
備註:
a. How to Check Direct X Version in Windows:
(1). Click “Start” > “Run” or hold down the “Windows Key” and press “R“.
(2). Type “dxdiag“, then click “OK“.
(3). The version of DirectX you are currently running will be displayed on your screen.
b. check your system: run the Kinect Configuration Verifier tool
-----------------------------------------------------------------------------------------------
B. 安裝軟體:
1. Kinect SDK v2
2. Visual Studio Express 2012
3. openframeworks v0.8.4
4. ofxKinectForWindows2 addon
-----------------------------------------------------------------------------------------------
C. 執行範例專案:
1. 開啟範例專案
.... openFrameworks\of_v0.8.4\addons\ofxKinectForWindows2\
example\example.sln
2. 設定為啟始專案:
3. 調整 example 與 ofxKinect4Windows2Lib 專案的 "平台工具組":
分別對 example 與 ofxKinect4Windows2Lib 專案作以下的調整:
"點選該專案" > 屬性:
所有組態 > 一般 > 平台工具組 > Visual Studio 2012 (v110)
example:
ofxKinect4Windows2Lib:
4. 建置方案與除錯:
a. 建置 > 建置方案
檢視 > 錯誤清單
b. 將紅線框內的三行程式碼作註解
5. 啟動程式:
建置 > 建置方案 > (啟動)本機 Winows 偵錯工具
-----------------------------------------------------------------------------------------------
D. 建立新的 ofxKinect For Windows2 專案:
1. 使用 OpenFrameworks 的 Project Generator 來產生新的專案:
.... openFrameworks\of_v0.8.4\projectGenerator\projectGenerator.exe
2. 設定專案名稱與要使用的 Addons:
專案名稱: KinectTest
Addons: ofxKinectForWindows2
> CREATE PROJECT
2. 開啟剛剛建立的方案:
.... openFrameworks\of_v0.8.4\apps\myApps\
KinectTest\KinectTest.sln
3. 加入 addons 的 ofxKinectForWindows2 專案:
a. 方案 > 加入 > 現有專案...
b. 瀏覽選取 ....
> ....openFrameworks\of_v0.8.4\addons\ofxKinectForWindows2
\ofxKinectForWindows2Lib\ofxKinectForWindows2Lib.vcxproj
c. 完成後, 可以看到多出了一個新的專案:
ofxKinectForWindows2Lib
4. 幫專案新增 ofxKinectForWindows2.props 屬性工作表:
a. 檢視 > 屬性管理員
b. 加入現有屬性工作表
c. 瀏覽選取屬性工作表:
.... openFrameworks\of_v0.8.4\addons\
ofxKinectForWindows2\ofxKinectForWindows2.props
d. 儲存:
-----------------------------------------------------------------------------------------------
E. 調整專案配置
KinectTest 專案 > 屬性:
1. 所有組態 > 組態屬性 > VC++ 目錄 > Include 目錄 >
加入: $(KINECTSDK20_DIR)\inc
2. 所有組態 > 組態屬性 > VC++ 目錄 > 程式庫目錄 >
加入: $(KINECTSDK20_DIR)\lib\x86
3. 所有組態 > 組態屬性 > 連結器 > 一般 > 其他程式庫目錄 >
加入: $(KINECTSDK20_DIR)\Lib\x86
4. 所有組態 > 組態屬性 > 連結器 > 輸入> 其他相依性 >
加入: $(KINECTSDK20_DIR)\Lib\x86\Kinect20.lib
-----------------------------------------------------------------------------------------------
F. 撰寫程式:
1. 開啟 ofApp.h 程式, 修改如下:
#pragma once
#include "ofMain.h"
#include "ofxKinectForWindows2.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxKFW2::Device kinect;
};
2. 開啟 ofApp.cpp 程式, 修改如下:
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
kinect.open();
kinect.initBodyIndexSource();
kinect.initColorSource();
}
//--------------------------------------------------------------
void ofApp::update(){
this->kinect.update();
}
//--------------------------------------------------------------
void ofApp::draw(){
this->kinect.getBodyIndexSource()->draw(0,0,512,424)
this->kinect.getColorSource()->draw(532,0,640,360);
}
....
-----------------------------------------------------------------------------------------------
G. 建置與執行: (只取部份畫面)
update: 2015/12/23
reference:
1. Setting up Kinect 2 for OpenFrameworks | rbarraza.com
2. elliotwoods/ofxKinectForWindows2 at 0.8.4
A. 系統需求:
1. A Kinect for Windows v2 Device (K4W2)
2. 64bit computer with a dedicated USB 3.0
3. Windows 10, 8, 8.1
4. Update your latest video card driver
5. Install DirectX 11
備註:
a. How to Check Direct X Version in Windows:
(1). Click “Start” > “Run” or hold down the “Windows Key” and press “R“.
(2). Type “dxdiag“, then click “OK“.
(3). The version of DirectX you are currently running will be displayed on your screen.
b. check your system: run the Kinect Configuration Verifier tool
-----------------------------------------------------------------------------------------------
B. 安裝軟體:
1. Kinect SDK v2
2. Visual Studio Express 2012
3. openframeworks v0.8.4
4. ofxKinectForWindows2 addon
-----------------------------------------------------------------------------------------------
C. 執行範例專案:
1. 開啟範例專案
.... openFrameworks\of_v0.8.4\addons\ofxKinectForWindows2\
example\example.sln
分別對 example 與 ofxKinect4Windows2Lib 專案作以下的調整:
"點選該專案" > 屬性:
所有組態 > 一般 > 平台工具組 > Visual Studio 2012 (v110)
example:
a. 建置 > 建置方案
檢視 > 錯誤清單
5. 啟動程式:
建置 > 建置方案 > (啟動)本機 Winows 偵錯工具
D. 建立新的 ofxKinect For Windows2 專案:
1. 使用 OpenFrameworks 的 Project Generator 來產生新的專案:
.... openFrameworks\of_v0.8.4\projectGenerator\projectGenerator.exe
專案名稱: KinectTest
Addons: ofxKinectForWindows2
> CREATE PROJECT
.... openFrameworks\of_v0.8.4\apps\myApps\
KinectTest\KinectTest.sln
a. 方案 > 加入 > 現有專案...
b. 瀏覽選取 ....
> ....openFrameworks\of_v0.8.4\addons\ofxKinectForWindows2
\ofxKinectForWindows2Lib\ofxKinectForWindows2Lib.vcxproj
ofxKinectForWindows2Lib
a. 檢視 > 屬性管理員
b. 加入現有屬性工作表
c. 瀏覽選取屬性工作表:
.... openFrameworks\of_v0.8.4\addons\
ofxKinectForWindows2\ofxKinectForWindows2.props
E. 調整專案配置
KinectTest 專案 > 屬性:
1. 所有組態 > 組態屬性 > VC++ 目錄 > Include 目錄 >
加入: $(KINECTSDK20_DIR)\inc
加入: $(KINECTSDK20_DIR)\lib\x86
加入: $(KINECTSDK20_DIR)\Lib\x86
加入: $(KINECTSDK20_DIR)\Lib\x86\Kinect20.lib
F. 撰寫程式:
1. 開啟 ofApp.h 程式, 修改如下:
#pragma once
#include "ofMain.h"
#include "ofxKinectForWindows2.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxKFW2::Device kinect;
};
2. 開啟 ofApp.cpp 程式, 修改如下:
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
kinect.open();
kinect.initBodyIndexSource();
kinect.initColorSource();
}
//--------------------------------------------------------------
void ofApp::update(){
this->kinect.update();
}
//--------------------------------------------------------------
void ofApp::draw(){
this->kinect.getBodyIndexSource()->draw(0,0,512,424)
this->kinect.getColorSource()->draw(532,0,640,360);
}
....
-----------------------------------------------------------------------------------------------
G. 建置與執行: (只取部份畫面)
標籤:
Kinect,
openFrameworks,
Windows
訂閱:
文章 (Atom)






















