Search code examples
javapythonandroidopencvchaquopy

converting a PyObject in python to multiple 2d arrays in android using Chaquopy


here is my python script:

import cv2
import numpy as np
import copy
import imutils
def test(red,green,blue):
  mask = np.zeros((480,360,3),dtype=np.uint8)
  for i in range(0,360):
      for j in range(0,480):
           a=blue[i][j]
           b=green[i][j]
           c=red[i][j]
           mask[j,i]=(110,23,245);
  mask[0:480,0:360]=(110,23,245)
  cv2.rectangle(mask, (100,100), (200,200), (255, 255, 255), -1)
  b, g, r = cv2.split(mask)
  return b,g,r

now in mainactivity.java in android it is received by creating a pyObject ,but python function returns 3 ,2 dimensional arrays as tuple ,i need to convert this pyObject to 3 ,2dimensional java array

Python py=Python.getInstance();
PyObject pyf =py.getModule("hello");
PyObject obj=pyf.callAttr("test", red,green,blue);
int[][] data = pyf.callAttr("test", red,green,blue).toJava(int[][].class);

but as you can see if my python function returned 1 ,2d list i can convert into a java 2d array ,but what to do when it returns 3 , 2d array ?


Solution

  • You can just add another dimension to the Java data type:

    int[][][] data = pyf.callAttr("test", red,green,blue).toJava(int[][][].class);
    

    Now data[0] is b, data[1] is g, and data[2] is r.