Search code examples
pythonandroidintegrationchaquopy

TypeError calling python function using chaquopy


I have a python file which has many 'def' values in it. When I try to integrate the python file with android studio, I get a type error. The input is a image file and I want the lowerRange and upperRange to be based on that image, so I cannot define a value to them, since image size can vary everytime.

import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
from PIL import Image


def host(croppedImage,lowerRange, upperRange):
    mask_yellow = cv2.inRange(croppedImage,lowerRange,upperRange)
    dilatation_type = cv2.MORPH_RECT
    dilatation_size = 1
    element = cv2.getStructuringElement(dilatation_type, (dilatation_size + 2, dilatation_size+2), (dilatation_size, dilatation_size))
    dilated_mask_image = cv2.dilate(mask_yellow, element)
    return dilated_mask_image

def DrawContourRect(contour):
    rect = cv2.minAreaRect(contour)
    return cv2.boxPoints(rect)

-----------------------------This is just a part of code---------------------------------

And this is the xml code for python object:

PyObject pyo = py.getModule("file");
PyObject obj = pyo.callAttr("host", imageString);

And the error is this:

com.chaquo.python.PyException: TypeError: detect() missing 2 required positional arguments: 'lowerRange' and 'upperRange'
at <python>.chaquopy_java.call(chaquopy_java.pyx:285)
at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrows(chaquopy_java.pyx:257)

Is there any way to solve this problem and how can the chaquopy read each and every 'def' value (host and DrawContourRect.


Solution

  • The error message and code don't seem to match, but I assume that host and detect are either the same function, or that they have the same signature.

    If that's correct, then the problem is simply that you're passing 1 argument to a function that requires 3. If you want the lowerRange and upperRange to be based on the image, then you'll have to either:

    • Calculate them on the Java side and pass them to Python using the 2 extra arguments; OR
    • Remove the 2 extra arguments from the function, and calculate the range on the Python side.