首页 > 解决方案 > 人脸检测在android studio中不起作用

问题描述

我是 android studio 的新手,想做人脸检测。人脸检测器代码使用opencv,用python编写。我使用 chaquopy 作为 python SDK。当我运行该应用程序时,未检测到人脸。它也没有显示任何错误。有人可以帮我吗。下面是我的 MainActivity.java 代码:

public class MainActivity extends AppCompatActivity {

Button btn;
ImageView iv;


// now take bitmap and bitmap drawable to get image from image view
BitmapDrawable drawable;
Bitmap bitmap;
String imageString="";


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

    btn = (Button)findViewById(R.id.submit);
    iv = (ImageView)findViewById(R.id.image_view);


    if(!Python.isStarted())
        Python.start(new AndroidPlatform(this));

    final Python py = Python.getInstance();


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // on click over button, get image from image view
            drawable = (BitmapDrawable)iv.getDrawable();
            bitmap = drawable.getBitmap();
            imageString = getStringImage(bitmap);
            // now in imagestring, we get encoded image string
            // now pass this input string to python script

            PyObject pyo = py.getModule("myscript");
            // calling the main function in python code and passing image string as parameter
            PyObject obj = pyo.callAttr("main", imageString);

            // obj will return value ie. our image string
            String str = obj.toString();
            // convert it to byte array
            byte data[] = android.util.Base64.decode(str, android.util.Base64.DEFAULT);
            // now convert it to bitmap
            Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

            //now set this bitmap to imageview
            iv.setImageBitmap(bmp);


        }
    });
}

// function to convert this image into byte array and finally into base 64 string
private String getStringImage(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    // store in byte array
    byte[] imageBytes = baos.toByteArray();
    // finally encode to string
    String encodedImage = android.util.Base64.encodeToString(imageBytes, android.util.Base64.DEFAULT); // Base64.DEFAULT
    return encodedImage;

}

}

下面显示的是我的python脚本“myscript.py”

import numpy as np
import cv2
import io
from PIL import Image
import base64
import face_recognition

def main(data):
    decoded_data = base64.b64decode(data)
    np_data = np.fromString(decoded_data, np.uint8)
    img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED)
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face_locations =  face_recognition.face_locations(img_gray)
    for(top,right,bottom,left) in face_locations:
        cv2.rectangle(img_rgb, (left,top),(right,bottom),(0,0,255),8)
    # convert this image to PIL
    pil_im = Image.fromarray(img_rgb)
    #convert this image to byte
    buff = io.BytesIO()
    pil_im.save(buff, format="PNG")
    #converting to base64
    img_str = base64.b64encode(buff.getvalue())
    return ""+str(img_str, 'utf-8')

我的 minSdkVersion 是 16,targetSdkVersion 是 30。我不明白这段代码有什么问题。有人能帮我吗。提前致谢。

标签: python-3.xandroid-studiochaquopy

解决方案


推荐阅读