3

我正在尝试将 Microsoft 的 Face API 与 Android Studio 一起使用来制作应用程序。现在,我只是在玩 API,但我遇到了一个问题。在应该可用的面部属性类型中(参见本页),我可以选择的唯一属性类型是 Age、FacialHair、Gender、HeadPose 和 Smile。我真的很想使用 Emotion 属性类型,但无法识别。

我得到的错误:Cannot resolve symbol 'Emotion'

这是代码的相关部分:

Face[] result = faceServiceClient.detect(inputStreams[0], true, true, new FaceServiceClient.FaceAttributeType[]{FaceServiceClient.FaceAttributeType.Emotion});

这是我的 MainActivity 中的完整代码:

package me.ianterry.face;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.microsoft.projectoxford.face.*;
import com.microsoft.projectoxford.face.contract.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    private FaceServiceClient faceServiceClient =
            new FaceServiceRestClient("https://westcentralus.api.cognitive.microsoft.com/face/v1.0", "MY KEY");

    private ImageView mImageView;
    private Button mProcessButton;
    private ProgressDialog progress;
    public final String TAG = "attributeMethod";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
        mImageView = findViewById(R.id.image);
        mImageView.setImageBitmap(myBitmap);

        mProcessButton = findViewById(R.id.btn_process);
        mProcessButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                detectAndFrame(myBitmap);
            }
        });
        progress = new ProgressDialog(this);
    }


    private void detectAndFrame(final Bitmap myBitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());


        AsyncTask<InputStream, String, Face[]> detectTask = new AsyncTask<InputStream, String, Face[]>() {
            //private ProgressDialog progress = new ProgressDialog(MainActivity.this);

            @Override
            protected void onPostExecute(Face[] faces) {
                progress.dismiss();
                if (faces == null) {
                    return;
                }
                mImageView.setImageBitmap(drawFaceRectangleOnBitmap(myBitmap, faces));
                attributeMethod(faces);
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                progress.show();
            }

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);
                progress.setMessage(values[0]);
            }

            @Override
            protected Face[] doInBackground(InputStream... inputStreams) {
                //return new Face[0];
                try {

                    publishProgress("Detecting...");
                    Face[] result = faceServiceClient.detect(inputStreams[0], true, true, new FaceServiceClient.FaceAttributeType[]{FaceServiceClient.FaceAttributeType.Emotion});
                    if (result == null) {
                        publishProgress("Detection finished. Nothing detected.");
                        return null;
                    }
                    publishProgress(String.format("Detection Finished. %d face(s) detected", result.length));
                    return result;
                } catch (Exception e) {
                    publishProgress("Detection failed.");
                    return null;
                }

            }

        };
        detectTask.execute(inputStream);
    }

    private static Bitmap drawFaceRectangleOnBitmap(Bitmap myBitmap, Face[] faces) {
        Bitmap bitmap = myBitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
        int strokeWidth = 8;
        paint.setStrokeWidth(strokeWidth);
        if (faces != null) {
            for (Face face : faces) {
                FaceRectangle faceRectangle = face.faceRectangle;
                canvas.drawRect(faceRectangle.left,
                        faceRectangle.top,
                        faceRectangle.left + faceRectangle.width,
                        faceRectangle.top + faceRectangle.height,
                        paint);

            }
        }
        return bitmap;
    }

    private void attributeMethod(Face[] faces) {
        for (Face face : faces) {
            FaceAttribute attribute = face.faceAttributes;
            Log.d(TAG, "age: " + attribute.age);
            Log.d(TAG, "gender: " + attribute.gender);
        }
    }
}

此代码或多或少直接取自本教程。

4

1 回答 1

1

SDK 版本 1.2.5 中添加了对 Emotion 的支持。来源

在 1.4.3 版本发布之前,您应该使用 1.4.1 版本。

于 2018-04-17T05:48:12.180 回答