I'm new to android studio and want to do face detection. The face detector code uses opencv and is written in python. I'm using chaquopy as the python SDK. When i run the app, the face is not detected. It is not showing any error also. Can someone help me out. Below is my MainActivity.java code:
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;
}
}
and the below shown is my python script "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')
My minSdkVersion is 16 and targetSdkVersion is 30. I'm not understanding what is the problem with this code. Can someone help me. Thanks in advance.