Andrea Rovaichell-gibson-10
Published © Beerware

UDOO Gesture Control

A project which gives to your UDOO gesture recognition and control capabilities.

Full instructions provided2,477
UDOO Gesture Control

Things used in this project

Hardware components

UDOO DUAL
UDOO DUAL
×1

Software apps and online services

OpenCV
OpenCV

Story

Read more

Code

file_9471.txt

Plain text
Arduino code:
#include "variant.h"

#include "stdio.h"

#include "adk.h"



#define  LED_PIN1  10

#define  LED_PIN2  11

#define  LED_PIN3  12

#define  LED_PIN4  13



// Accessory descriptor. It's how Arduino identifies itself to Android.

char descriptionName[] = "ArduinoADK_2"; 

char modelName[] = "UDOO_ADK";           // your Arduino Accessory name (Need to be the same defined in the Android App)

char manufacturerName[] = "Aidilab";     // manufacturer (Need to be the same defined in the Android App)



// Make up anything you want for these

char versionNumber[] = "1.0";            // version (Need to be the same defined in the Android App)

char serialNumber[] = "1";

char url[] = "http://udoomv.blogspot.it/";      // If there isn't any compatible app installed, Android suggest to visit this url



USBHost Usb;

ADK adk(&Usb, manufacturerName, modelName, descriptionName, versionNumber, url, serialNumber);



#define RCVSIZE 128

uint8_t buf[RCVSIZE];

uint32_t bytesRead = 0;



void setup()

{

  Serial.begin(115200);   

  pinMode(LED_PIN1, OUTPUT);

  pinMode(LED_PIN2, OUTPUT);

  pinMode(LED_PIN3, OUTPUT);

  pinMode(LED_PIN4, OUTPUT);

  delay(500);

  Serial.println("UDOO ADK demo start...");

}



void loop()

{

  Usb.Task();



  if (adk.isReady()) {

    adk.read(&bytesRead, RCVSIZE, buf);// read data into buf variable

    if (bytesRead > 0) {

      if (parseCommand(buf[0]) == 1) {// compare received data

        // Received "1" - turn on LED

        digitalWrite(LED_PIN1, HIGH);

      } 

      else if (parseCommand(buf[0]) == 0) {

        // Received "0" - turn off LED

        digitalWrite(LED_PIN1, LOW); 

      }  



      if (parseCommand(buf[1]) == 1) {// compare received data

        // Received "1" - turn on LED

        digitalWrite(LED_PIN2, HIGH);

      } 

      else if (parseCommand(buf[1]) == 0) {

        // Received "0" - turn off LED

        digitalWrite(LED_PIN2, LOW); 

      }  



      if (parseCommand(buf[2]) == 1) {// compare received data

        // Received "1" - turn on LED

        digitalWrite(LED_PIN3, HIGH);

      } 

      else if (parseCommand(buf[2]) == 0) {

        // Received "0" - turn off LED

        digitalWrite(LED_PIN3, LOW); 

      }  



      if (parseCommand(buf[3]) == 1) {// compare received data

        // Received "1" - turn on LED

        digitalWrite(LED_PIN4, HIGH);

      } 

      else if (parseCommand(buf[3]) == 0) {

        // Received "0" - turn off LED

        digitalWrite(LED_PIN4, LOW); 

      }  

    }

  } 

}

// the characters sent to Arduino are interpreted as ASCII, we decrease 48 to return to ASCII range.

uint8_t parseCommand(uint8_t received) {

  return received - 48;

}

file_9473.txt

Plain text
Java code:
private AdkManager mAdkManagerX;



        mAdkManagerX = new AdkManager((UsbManager) getSystemService(Context.USB_SERVICE));

 registerReceiver(mAdkManagerX.getUsbReceiver(),mAdkManagerX.getDetachedFilter());

file_9474.txt

Plain text
code:
private CameraBridgeViewBase mOpenCvCameraView;



        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view);

 mOpenCvCameraView.mCameraIndex = 0;

 mOpenCvCameraView.setCvCameraViewListener(this);



        public Mat onCameraFrame(CvCameraViewFrame inputFrame) 

        {

             ......

            Imgproc.blur(mGray, mGray, new Size(5,5));

            mNativeDetector.detect2(mGray, faces2); 

             ......

            mAdkManagerX.writeSerial("11AA"); 

             ...... 

         }

file_9475.txt

Plain text
JNI C++ code: 
#include "DetectionBasedTracker_jni.h"

#include "opencv2/core/core.hpp"

#include "opencv2/contrib/detection_based_tracker.hpp"

#include "opencv/cv.h"

#include "opencv/highgui.h"

#include "stdio.h"

#include "string"

#include "vector"

#include "android/log.h"

#define LOG_TAG "FaceDetection/DetectionBasedTracker"

#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))

using namespace std;

using namespace cv;

inline void vector_Rect_to_Mat(vector& v_rect, Mat& mat)

{

    mat = Mat(v_rect, true);

}

JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject1

(JNIEnv * jenv, jclass, jstring jFileName, jint faceSize)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject enter");

    const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL);

    string stdFileName(jnamestr);

    jlong result = 0;

    try

    {

        DetectionBasedTracker::Parameters DetectorParams;

        if (faceSize > 0)

            DetectorParams.minObjectSize = faceSize;

        result = (jlong)new DetectionBasedTracker(stdFileName, DetectorParams);

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeCreateObject caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeCreateObject caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

        return 0;

    }



    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject exit");

    return result;

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject1

(JNIEnv * jenv, jclass, jlong thiz)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject enter");

    try

    {

        if(thiz != 0)

        {

            ((DetectionBasedTracker*)thiz)->stop();

            delete (DetectionBasedTracker*)thiz;

        }

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeestroyObject caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeDestroyObject caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject exit");

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart1

(JNIEnv * jenv, jclass, jlong thiz)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart enter");

    try

    {

        ((DetectionBasedTracker*)thiz)->run();

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeStart caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeStart caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart exit");

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop1

(JNIEnv * jenv, jclass, jlong thiz)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop enter");

    try

    {

        ((DetectionBasedTracker*)thiz)->stop();

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeStop caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeStop caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop exit");

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize1

(JNIEnv * jenv, jclass, jlong thiz, jint faceSize)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize enter");

    try

    {

        if (faceSize > 0)

        {

            DetectionBasedTracker::Parameters DetectorParams = \

            ((DetectionBasedTracker*)thiz)->getParameters();

            DetectorParams.minObjectSize = faceSize;

            ((DetectionBasedTracker*)thiz)->setParameters(DetectorParams);

        }

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeStop caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeSetFaceSize caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize exit");

}





JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect1

(JNIEnv * jenv, jclass, jlong thiz, jlong imageGray, jlong faces)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect enter");

    try

    {

        vector RectFaces;

        ((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));

        ((DetectionBasedTracker*)thiz)->getObjects(RectFaces);

        vector_Rect_to_Mat(RectFaces, *((Mat*)faces));

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeCreateObject caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeDetect caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect exit");

}



JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject2

(JNIEnv * jenv, jclass, jstring jFileName, jint faceSize)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject enter");

    const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL);

    string stdFileName(jnamestr);

    jlong result = 0;



    try

    {

        DetectionBasedTracker::Parameters DetectorParams;

        if (faceSize > 0)

            DetectorParams.minObjectSize = faceSize;

        result = (jlong)new DetectionBasedTracker(stdFileName, DetectorParams);

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeCreateObject caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeCreateObject caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

        return 0;

    }



    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject exit");

    return result;

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject2

(JNIEnv * jenv, jclass, jlong thiz)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject enter");

    try

    {

        if(thiz != 0)

        {

            ((DetectionBasedTracker*)thiz)->stop();

            delete (DetectionBasedTracker*)thiz;

        }

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeestroyObject caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeDestroyObject caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDestroyObject exit");

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart2

(JNIEnv * jenv, jclass, jlong thiz)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart enter");

    try

    {

        ((DetectionBasedTracker*)thiz)->run();

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeStart caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeStart caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStart exit");

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop2

(JNIEnv * jenv, jclass, jlong thiz)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop enter");

    try

    {

        ((DetectionBasedTracker*)thiz)->stop();

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeStop caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeStop caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeStop exit");

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize2

(JNIEnv * jenv, jclass, jlong thiz, jint faceSize)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize enter");

    try

    {

        if (faceSize > 0)

        {

            DetectionBasedTracker::Parameters DetectorParams = \

            ((DetectionBasedTracker*)thiz)->getParameters();

            DetectorParams.minObjectSize = faceSize;

            ((DetectionBasedTracker*)thiz)->setParameters(DetectorParams);

        }

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeStop caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeSetFaceSize caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeSetFaceSize exit");

}



JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect2

(JNIEnv * jenv, jclass, jlong thiz, jlong imageGray, jlong faces)

{

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect enter");

    try

    {

        vector RectFaces;

        ((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));

        ((DetectionBasedTracker*)thiz)->getObjects(RectFaces);

        vector_Rect_to_Mat(RectFaces, *((Mat*)faces));

    }

    catch(cv::Exception& e)

    {

        LOGD("nativeCreateObject caught cv::Exception: %s", e.what());

        jclass je = jenv->FindClass("org/opencv/core/CvException");

        if(!je)

            je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, e.what());

    }

    catch (...)

    {

        LOGD("nativeDetect caught unknown exception");

        jclass je = jenv->FindClass("java/lang/Exception");

        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");

    }

    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect exit");

}

Credits

Dong Zhang

Posted by Andrea Rovai

Comments