无法检测用于人脸检测Mobile Vision API的不同图像 [英] Unable to detect different images for face detection Mobile Vision API
问题描述
我在我的应用程序中使用Mobile Vision API进行人脸检测,到目前为止,我已成功完成此项工作。
它适用于我第一次运行应用程序时设置的特定图像,但之后..我试图替换不同的图像进行人脸检测,它会给出错误java.lang.OutOfMemoryError
以下是我的代码
位图myBitmap;
FaceDetector探测器;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//载入图像////
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
位图b = = BitmapFactory.decodeResource(
getApplicationContext()。getResources(),
R.drawable.image,options);
myBitmap = b.copy(Bitmap.Config.RGB_565,true);
b.recycle();
////////////////
detector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType (FaceDetector.ALL_LANDMARKS)
.build();
if(!detector.isOperational())
{Toast.makeText(MainActivity.this,NO face deteted。Toast.LENGTH_SHORT)
.show();}
else {
Frame frame = new Frame.Builder()。setBitmap(myBitmap).build();
SparseArray< com.google.android.gms.vision.face.Face> faces = detector.detect(frame);
FaceView faceView =(FaceView)findViewById(R.id.faceView);
faceView.setContent(myBitmap,faces);
$ / code>
这里是我的logcat
java.lang.OutOfMemoryError
位于com.google.android.gms.vision.Frame.zzEx(未知源代码)
位于com.google.android。 gms.vision.Frame.getGrayscaleImageData(未知来源)
在com.google.android.gms.vision.face.FaceDetector.detect(未知来源)
在com.chat.elearnplayer.mobilevisionapi.MainActivity。 onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5296)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
在android.app.ActivityThread.access $ 800(ActivityThread。 java:155)
at android.app.ActivityThread $ H.handleMe ssage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
在android.app.ActivityThread.main(ActivityThread.java:5426)$ b $在java.lang.reflect.Method.invokeNative(本地方法)$ b $在java.lang.reflect.Method.invoke(方法。 java:515)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1084)
at dalvik.system.NativeStart.main(Native Method)
...需要您的帮助...
试试这个
位图myBitmap;
FaceDetector探测器;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//载入一张图片////
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
位图b = = BitmapFactory.decodeResource(
getApplicationContext()。getResources(),
R.drawable.image,options);
myBitmap = b.copy(Bitmap.Config.RGB_565,true);
b.recycle();
//////////////////
detector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.build();
检测器<面部> safeDetector = new SafeFaceDetector(检测器);
Frame frame = new Frame.Builder()。setBitmap(myBitmap).build();
SparseArray< com.google.android.gms.vision.face.Face> faces = safeDetector.detect(frame);
if(!safeDetector.isOperational()){
Toast.makeText(MainActivity.this,NO face deteted。Toast.LENGTH_SHORT)
.show();
} else {
FaceView faceView =(FaceView)findViewById(R.id.faceView);
faceView.setContent(myBitmap,faces);
safeDetector.release();
$ b 重用facedetector release( );必须在使用后调用
。
和 SafeDetector
用法:face detector有一个小图像的bug和safeDetector是该补丁的补丁
在活动导入时添加此行
import com.google.android.gms.samples.vision.face.patch.SafeFaceDetector;
I am using Mobile Vision API in my app for face detection so far I have been successful in doing so.
It works fine for a particular image which I have set when I was running the app for the first time but after that.. I have tried to replace different images for face detection it gives the error java.lang.OutOfMemoryError
Following is my code
Bitmap myBitmap;
FaceDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load An Image////
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
Bitmap b = = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.drawable.image,options);
myBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();
////////////////
detector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.build();
if (!detector.isOperational())
{ Toast.makeText(MainActivity.this, "NO face deteted."Toast.LENGTH_SHORT)
.show();}
else {
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<com.google.android.gms.vision.face.Face> faces = detector.detect(frame);
FaceView faceView = (FaceView) findViewById(R.id.faceView);
faceView.setContent(myBitmap, faces);
}
here is my logcat
java.lang.OutOfMemoryError
at com.google.android.gms.vision.Frame.zzEx(Unknown Source)
at com.google.android.gms.vision.Frame.getGrayscaleImageData(Unknown Source)
at com.google.android.gms.vision.face.FaceDetector.detect(Unknown Source)
at com.chat.elearnplayer.mobilevisionapi.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5296)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5426)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
... Need your help ...
解决方案 Try this
Bitmap myBitmap;
FaceDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load An Image////
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
Bitmap b = = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.drawable.image,options);
myBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();
//////////////////
detector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.build();
Detector<Face> safeDetector = new SafeFaceDetector(detector);
Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
SparseArray<com.google.android.gms.vision.face.Face> faces = safeDetector.detect(frame);
if (!safeDetector.isOperational()) {
Toast.makeText(MainActivity.this, "NO face deteted."Toast.LENGTH_SHORT)
.show();
} else {
FaceView faceView = (FaceView) findViewById(R.id.faceView);
faceView.setContent(myBitmap, faces);
safeDetector.release();
}
}
for reuse facedetector release();
must be called after use .
and for SafeDetector
usage : face detector have a bug for small images and safeDetector is patch for that bug
add this line at activity imports
import com.google.android.gms.samples.vision.face.patch.SafeFaceDetector;
这篇关于无法检测用于人脸检测Mobile Vision API的不同图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!