android - I want to use camera on background service using mobile vision -
i make android app project. application's purpose detection blinking of eye.
i use mobile vision api. using sample code face detection of mobile vision blink detection code.
my first question mobile vision api can working on background service?
my second question combine camera api mobile-vision api? camera api can work on background service can't use mobile-vision face detection class..
please, me..
my app's service code
public class serviceclass extends service{ private static final string tag = "facetracker"; public camerasource mcamerasource = null; public camerasourcepreview mpreview; private graphicoverlay mgraphicoverlay; private static final int rc_handle_gms = 9001; // permission request codes need < 256 private static final int rc_handle_camera_perm = 2; static int x=0; //camera variables //a surface holder private surfaceholder sholder; //a variable control camera private camera mcamera; //the camera parameters private camera.parameters parameters; /** called when activity first created. */ @override public void oncreate() { super.oncreate(); log.d("service", "oncreate" ); // check camera permission before accessing camera. if // permission not granted yet, request permission. int rc = activitycompat.checkselfpermission(this, manifest.permission.camera); if (rc == packagemanager.permission_granted) { createcamerasource(); } else { //requestcamerapermission(); toast.maketext(getapplicationcontext(), "need permission", toast.length_long).show(); } } /** * creates , starts camera. note uses higher resolution in comparison * other detection examples enable barcode detector detect small barcodes * @ long distances. */ public void createcamerasource() { log.d("service", "createcamerasource" ); context context = getapplicationcontext(); facedetector detector = new facedetector.builder(context) .setprominentfaceonly(true) //detection front camera .setclassificationtype(facedetector.all_classifications) .build(); detector.setprocessor( new largestfacefocusingprocessor(detector, new blinktracker())); //new multiprocessor.builder<>(new graphicfacetrackerfactory()) //.build()); if (!detector.isoperational()) { // note: first time app using face api installed on device, gms // download native library device in order detection. // completes before app run first time. if download has not yet // completed, above call not detect faces. // // isoperational() can used check if required native library // available. detector automatically become operational once library // download completes on device. log.w(tag, "face detector dependencies not yet available."); } mcamerasource = new camerasource.builder(context, detector) .setrequestedpreviewsize(640, 480) .setfacing(camerasource.camera_facing_front)//using front camera .setrequestedfps(30.0f) .build(); log.i("createcamerasource",mcamerasource.tostring()); } /** * starts camera. */ @override public int onstartcommand(intent intent, int flags, int startid) { toast.maketext(this, "service starting", toast.length_short).show(); startcamerasource(); // if killed, after returning here, restart return start_sticky; } /* @override public void onstart(intent intent, int startid) { // todo auto-generated method stub super.onstart(intent, startid); log.d("service", "onstart" ); startcamerasource(); //startcamerasource(); // mcamera = camera.open(1); }*/ //============================================================================================== // camera source preview //============================================================================================== /** * starts or restarts camera source, if exists. if camera source doesn't exist yet * (e.g., because onresume called before camera source created), called * again when camera source created. */ public void startcamerasource() { log.d("service", "startcamerasource" ); // check device has play services available. int code = googleapiavailability.getinstance().isgoogleplayservicesavailable( getapplicationcontext()); /*if (code != connectionresult.success) { dialog dlg = googleapiavailability.getinstance().geterrordialog(this, code, rc_handle_gms); dlg.show(); }*/ if (mcamerasource != null) { try { log.d("startcamerasource", "log!" ); log.i("startcamerasource1",mcamerasource.tostring()); mpreview.start(mcamerasource); } catch (ioexception e) { log.e(tag, "unable start camera source.", e); mcamerasource.release(); mcamerasource = null; } } } @override public ibinder onbind(intent intent) { // todo auto-generated method stub return null; } //============================================================================================== // eye blink tracker //============================================================================================== public class blinktracker extends tracker<face> { private final double open_threshold = 0.85; private final double close_threshold = 0.50; private int state = 0; public void onupdate(detector.detections<face> detections, face face) { log.i("blinktracker", "eye tracker start"); float left = face.getislefteyeopenprobability(); float right = face.getisrighteyeopenprobability(); if ((left == face.uncomputed_probability) || (right == face.uncomputed_probability)) { // @ least 1 of eyes not detected. return; } switch (state) { case 0: if ((left > open_threshold) && (right > open_threshold)) { // both eyes open log.i("blinktracker", "eye open"); state = 1; } break; case 1: if ((left < close_threshold) && (right < close_threshold)) { // both eyes become closed log.i("blinktracker", "blink occurred!"); state = 0; } break; /*case 2: if ((left > open_threshold) && (right > open_threshold)) { // both eyes open again log.i("blinktracker", "blink occurred!"); state = 0; } break;*/ } } } }
logcat
07-13 09:55:04.716 17707-17707/com.hyechon.etrackermv e/androidruntime: fatal exception: main process: com.hyechon.etrackermv, pid: 17707 java.lang.runtimeexception: unable start service com.hyechon.etrackermv.serviceclass@7566889 intent { cmp=com.hyechon.etrackermv/.serviceclass }: java.lang.nullpointerexception: attempt invoke virtual method 'void com.hyechon.etrackermv.camera.camerasourcepreview.start(com.google.android.gms.vision.camerasource)' on null object reference @ android.app.activitythread.handleserviceargs(activitythread.java:4079) @ android.app.activitythread.access$2400(activitythread.java:221) @ android.app.activitythread$h.handlemessage(activitythread.java:1897) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7225) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void com.hyechon.etrackermv.camera.camerasourcepreview.start(com.google.android.gms.vision.camerasource)' on null object reference @ com.hyechon.etrackermv.serviceclass.startcamerasource(serviceclass.java:255) @ com.hyechon.etrackermv.serviceclass.onstartcommand(serviceclass.java:136) @ android.app.activitythread.handleserviceargs(activitythread.java:4062) @ android.app.activitythread.access$2400(activitythread.java:221) @ android.app.activitythread$h.handlemessage(activitythread.java:1897) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7225) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)
07-13 09:55:05.666 17707-17777/com.hyechon.etrackermv i/vision: connection error: null 07-13 10:00:04.761 17707-17707/com.hyechon.etrackermv i/process: sending signal. pid: 17707 sig: 9
Comments
Post a Comment