Reputation: 1
I am facing an issue when I am trying to connect my Python file with the Java main code of my Android app in Android Studio ladybug 2024 using chaquopy plugin.
The motive of my app is to click a photo or select one from gallery and then processing the image using OpenCV in my Python file and then providing the area of the object (apple) in the image, I have created a Python function which will take in the image path, access it and provide return the result to the Java mainactivity code. It is saying that there is some data type error hence it is directly executing the catch block and printing the error instead of the desired result.
Here is my Java code. I am new to Android app development.
@SuppressLint("SetTextI18n")
private void processImage(Uri imageUri) {
try {
// Ensure Chaquopy is initialized
Python py = Python.getInstance();
// Get the Python module and call the method
PyObject pyModule = py.getModule("Calorie"); // The name of your Python file without .py
//String imageUriString = imageUri.toString();
String imagePath = getRealPathFromURI(imageUri);
PyObject pyResult = pyModule.callAttr("calculate_area_and_calories", imagePath); // Pass the image path
// Assuming your Python function returns a dictionary with keys "area" and "calories"
String area = String.valueOf(pyResult.asMap().get("area"));
String calories = String.valueOf(pyResult.asMap().get("calories"));
// Update the resultText with dynamic values
resultText.setText(String.format("Area: %s cm²\nCalories: %s kcal", area, calories));
//resultText.setText("Area: 100 cm²\nCalories: 52 kcal");
} catch (Exception e) {
// Handle any exceptions
//e.printStackTrace();
resultText.setText("Error processing image: " + e.getMessage() + "," + e.getCause());
}
}
Upvotes: 0
Views: 25