private static final int REQUET_LOADIMAGE = 111;
private Button btnDetect;
private ImageView image;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photogallery);
image = (ImageView) findViewById(R.id.image);
btnDetect=(Button)findViewById(R.id.btnDetect);
btnDetect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
detectFacesInImage();
}
});
Intent intent = new Intent();
intent.setType("image/*"); // filter only image type files
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUET_LOADIMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUET_LOADIMAGE && resultCode == RESULT_OK) {
if (bitmap != null) {
bitmap.recycle();
}
try {
InputStream inputStream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
image.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void detectFacesInImage() {
//Create a Paint object for drawing with
Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);
//Create a Canvas object for drawing on
Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bitmap, 0, 0, null);
//Detect the Faces
FaceDetector faceDetector = new FaceDetector.Builder(getApplicationContext())
.setTrackingEnabled(false)
.build();
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<Face> faces = faceDetector.detect(frame);
if (faces.size() == 0) {
Toast.makeText(this, "None face detected!", Toast.LENGTH_SHORT).show();
} else {
//Draw Rectangles on the Faces
for (int i = 0; i < faces.size(); i++) {
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);
faceDetector.release();
}
image.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
}
}
}
我使用google play-services-vision成功实现了人脸检测:9.4.0 +&#39;并且在画布的帮助下通过简单的程序获取检测到的面部。 我想知道onClicklistner上给定照片的性别。
有没有办法做到这一点?
答案 0 :(得分:0)
尝试使用此API:性别分类 https://algorithmia.com/algorithms/deeplearning/GenderClassification
import com.algorithmia.*;
import com.algorithmia.algo.*;
String input = "{\n"
+ " \"image\": \"data://deeplearning/example_data/m_ali.jpg\"\n"
+ "}";
AlgorithmiaClient client = Algorithmia.client("YOUR_API_KEY");
Algorithm algo = client.algo("algo://deeplearning/GenderClassification/1.0.1");
AlgoResponse result = algo.pipeJson(input);
System.out.println(result.asJsonString());
Json结果:
{
"results": [
[0.9948568344116211, "Male"],
[0.0051431627944111815, "Female"]
]
}