TypeError使用Chaquopy调用python函数 [英] TypeError calling python function using chaquopy

查看:108
本文介绍了TypeError使用Chaquopy调用python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个其中包含许多'def'值的python文件.当我尝试将python文件与android studio集成时,出现类型错误.输入是一个图像文件,我希望lowerRange和upperRange基于该图像,因此我无法为其定义值,因为图像大小可能会随时变化.

 将numpy导入为np导入cv2导入操作系统导入matplotlib.pyplot作为plt从PIL导入图像def主机(croppedImage,lowerRange,upperRange):mask_yellow = cv2.inRange(croppedImage,lowerRange,upperRange)dilatation_type = cv2.MORPH_RECTdilatation_size = 1元素= cv2.getStructuringElement(膨胀类型,(膨胀大小+ 2,膨胀大小+2),(膨胀大小,膨胀大小))dilated_mask_image = cv2.dilate(mask_yellow,元素)返回dilated_mask_imagedef DrawContourRect(轮廓):rect = cv2.minAreaRect(轮廓)返回cv2.boxPoints(rect)-----------------------------这只是代码的一部分--------------------------------- 

这是python对象的xml代码:

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

错误是这样的:

  com.chaquo.python.PyException:TypeError:detect()缺少2个必需的位置参数:"lowerRange"和"upperRange"在< python> .chaquopy_java.call(chaquopy_java.pyx:285)在< python> .chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrows(chaquopy_java.pyx:257) 

有什么方法可以解决此问题,并且chaquopy如何读取每个'def'值(主机和DrawContourRect.

解决方案

错误消息和代码似乎不匹配,但我认为 host detect 具有相同的功能或具有相同的签名.

如果这是正确的,那么问题就出在将1参数传递给需要3的函数.如果希望lowerRange和upperRange基于图像,则您必须:

  • 在Java端计算它们,并使用2个额外的参数将它们传递给Python;或
  • 从函数中删除2个额外的参数,并在Python端计算范围.

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.

解决方案

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.

这篇关于TypeError使用Chaquopy调用python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆