Android 工作室使用图库中的所有图像进行人脸识别
Facerecognition in Android studio using all the images from gallery
我正在尝试使用图库中的图像制作一个 Android 应用程序(在 Android Studio 中)以进行人脸识别。这是我的源代码(使用我从开放课程网页中找到的内容),它正在收集我的图像,但我只希望这些图像显示至少包含一张脸。我知道我需要以某种方式使用 FaceDetection
,但我不知道如何使用。
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns,
null,
null,
orderBy
);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
null
);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
Bitmap bitmap_column;
bitmap_column = new Bitmap(thumbnails);
BitmapFactory.Options bitmap = new BitmapFactory.Options();
bitmap.inPreferredConfig = Bitmap.Config.RGB_565;
imageWidth = thumbnails.getWidth();
imageHeight = thumbnails.getHeight();
myFace = new FaceDetector.Face[numberOfFace];
myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
numberOfFaceDetected = myFaceDetect.findFaces(thumbnails, myFace);
imagecursor.close();
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i =0; i<len; i++)
{
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0){
Toast.makeText(
getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_LONG
).show();
} else {
Toast.makeText(
getApplicationContext(),
"You've selected Total " + cnt + " image(s).",
Toast.LENGTH_LONG
).show();
Log.d("SelectedImages", selectImages);
}
}
});
}
ImageAdapter
class:
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem,
null
);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivity(intent);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
ViewHolder
class:
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
private boolean hasFace(Bitmap inBmp){
Bitmap bmp = inBmp.copy(Bitmap.Config.RGB_565, true);
FaceDetector fd = new FaceDetector(bmp.getWidth(), bmp.getHeight(), 1);
Face faces[] = new Face[1];
int numFaces = fd.findFaces(bmp, faces);
bmp.recycle();
return numFaces > 0;
}
这将告诉您位图是否有脸。移动光标,仅将 return 为真的图像添加到传递给适配器的数组中。由于 运行 需要的时间,我强烈建议在 AsyncTask 上执行此操作。我还建议对其进行设置,以便它定期填充适配器(因为这需要一段时间)或者至少显示某种处理 UI 以便用户知道这需要一段时间。
我正在尝试使用图库中的图像制作一个 Android 应用程序(在 Android Studio 中)以进行人脸识别。这是我的源代码(使用我从开放课程网页中找到的内容),它正在收集我的图像,但我只希望这些图像显示至少包含一张脸。我知道我需要以某种方式使用 FaceDetection
,但我不知道如何使用。
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns,
null,
null,
orderBy
);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
null
);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
Bitmap bitmap_column;
bitmap_column = new Bitmap(thumbnails);
BitmapFactory.Options bitmap = new BitmapFactory.Options();
bitmap.inPreferredConfig = Bitmap.Config.RGB_565;
imageWidth = thumbnails.getWidth();
imageHeight = thumbnails.getHeight();
myFace = new FaceDetector.Face[numberOfFace];
myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
numberOfFaceDetected = myFaceDetect.findFaces(thumbnails, myFace);
imagecursor.close();
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i =0; i<len; i++)
{
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0){
Toast.makeText(
getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_LONG
).show();
} else {
Toast.makeText(
getApplicationContext(),
"You've selected Total " + cnt + " image(s).",
Toast.LENGTH_LONG
).show();
Log.d("SelectedImages", selectImages);
}
}
});
}
ImageAdapter
class:
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem,
null
);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivity(intent);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
ViewHolder
class:
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
private boolean hasFace(Bitmap inBmp){
Bitmap bmp = inBmp.copy(Bitmap.Config.RGB_565, true);
FaceDetector fd = new FaceDetector(bmp.getWidth(), bmp.getHeight(), 1);
Face faces[] = new Face[1];
int numFaces = fd.findFaces(bmp, faces);
bmp.recycle();
return numFaces > 0;
}
这将告诉您位图是否有脸。移动光标,仅将 return 为真的图像添加到传递给适配器的数组中。由于 运行 需要的时间,我强烈建议在 AsyncTask 上执行此操作。我还建议对其进行设置,以便它定期填充适配器(因为这需要一段时间)或者至少显示某种处理 UI 以便用户知道这需要一段时间。