This is a face recognition project based Android
Project: https://code.google.com/p/opencv4udoo/
IMage 1
sudo apt-get install python-dev python-numpy sudo apt-get update sudo apt-cache search opencv sudo apt-get install libcv-dev libcv2.3 libcvaux-dev libcvaux2.3 libhighgui-dev libhighgui2.3 libopencv-calib3d-dev libopencv-calib3d2.3 libopencv-contrib-dev libopencv-contrib2.3 libopencv-core-dev libopencv-core2.3 libopencv-dev libopencv-features2d-dev libopencv-features2d2.3 libopencv-flann-dev libopencv-flann2.3 libopencv-highgui-dev libopencv-highgui2.3 libopencv-imgproc-dev libopencv-imgproc2.3 libopencv-legacy-dev libopencv-legacy2.3 libopencv-ml-dev libopencv-ml2.3 libopencv-objdetect-dev libopencv-objdetect2.3 libopencv-video-dev libopencv-video2.3 python-opencv
import sys
import cv2
import cv2.cv as cv
from cv2.cv import *
from cv2 import *
if __name__=='__main__':
if len(sys.argv)>1:
image=cv2.imread(sys.argv[1])
cv2.imshow("UDOO-OPENCV",image )
cv2.waitKey(0)
car.jpeg
#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;
}
We need install Opencv for Android
http://opencv.org/platforms/android.html
And Use Eclipse
private AdkManager mAdkManagerX;
mAdkManagerX = new AdkManager((UsbManager) getSystemService(Context.USB_SERVICE));
registerReceiver(mAdkManagerX.getUsbReceiver(),mAdkManagerX.getDetachedFilter());
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");
......
}
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"); }
The Same :
static char text[20];
static CvFont font1;
memset(text,0,sizeof(text));
cvInitFont(&font1, CV_FONT_HERSHEY_SIMPLEX, 2.2, 2.2, 0, 6, 8);
static GstFlowReturn
gst_cv_smooth_transform (GstOpencvBaseTransform * base, GstBuffer * buf,
IplImage * img, GstBuffer * outbuf, IplImage * outimg)
{
GstCvSmooth *filter = GST_CV_SMOOTH (base);
cvSmooth (img, outimg, filter->type, filter->param1, filter->param2,
filter->param3, filter->param4);
sprintf(text,"UDOO-OpenCV Test: %d",ivv);
cvPutText(outimg, text, cvPoint(100, 100), &font1, CV_RGB(255,0,0));
return GST_FLOW_OK;
}
run_it.sh:
export PKG_CONFIG_PATH=/usr/lib/pkgconfig ./autogen.sh ./congigure sudo make clean make sudo make install sudo cp ./src/.libs/libgstopencv.so /usr/lib/gstreamer-0.10/libgstopencv.so sudo cp ./src/.libs/libgstopencv.la /usr/lib/gstreamer-0.10/libgstopencv.la gst-inspect-0.10 opencv gst-launch-0.10 mfw_v4lsrc ! 'video/x-raw-yuv,width=640,height=480,framerate=90/1' ! ffmpegcolorspace ! cvsmooth ! ffmpegcolorspace ! ximagesink
import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
import time
class GTK_Main:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("UDOO-Camera")
window.set_default_size(560, 480)
window.connect("destroy", gtk.main_quit, "WM destroy")
vbox = gtk.VBox()
window.add(vbox)
self.movie_window = gtk.DrawingArea()
vbox.add(self.movie_window)
hbox = gtk.HBox()
vbox.pack_start(hbox, False)
hbox.set_border_width(10)
hbox.pack_start(gtk.Label())
self.button = gtk.Button("snapshot")
self.button.connect("clicked", self.camera_snapshot)
hbox.pack_start(self.button, False)
self.button2 = gtk.Button("Quit")
self.button2.connect("clicked", self.exit)
hbox.pack_start(self.button2, False)
hbox.add(gtk.Label())
window.show_all()
self.player = gst.parse_launch ("mfw_v4lsrc ! mfw_v4lsink")
bus = self.player.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("sync-message::element", self.on_sync_message)
self.player.set_state(gst.STATE_PLAYING)
def camera_snapshot(self, w):
image= self.movie_window.window.get_image(0, 0, 560, 480)
print "Snapshot: "+str(time.time())
def exit(self, widget, data=None):
gtk.main_quit()
def on_sync_message(self, bus, message):
if message.structure is None:
return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
# Assign the viewport
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(self.movie_window.window.xid)
GTK_Main()
gtk.gdk.threads_init()
gtk.main()