V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
momosolaris
V2EX  ›  Android

利用 chaquopy 运行 Python 代码在‘Android studio 中,

  •  
  •   momosolaris · 2023-03-03 19:28:49 +08:00 · 7338 次点击
    这是一个创建于 617 天前的主题,其中的信息可能已经有所发展或是发生改变。
    各位大佬们,我需要你们的帮助!
    题目:

    我正在创建一个涉及图像转换的应用程序,利用 chaquopy ( https://github.com/chaquo )来在 android studio 中运行 python 代码,来处理图像转换。

    在其中,我必须将图像从 java 发送到 python 文件,并使用图像数据进行图像处理,例如在这里我需要实现 gabor 变换。

    但不幸的是,我得到的结果是一张完全黑色的照片。

    我们必须将图像转换为位图,然后将位图发送到
    数组到 Python.--我的理解正确吗?

    my gabor.py under src/main/python folder

    import cv2
    import numpy as np
    import math

    import base64

    np.set_printoptions(threshold= np.inf)
    def trans_gabor(data, wavelength, orientation):
    image = np.frombuffer(base64.b64decode(data), dtype=np.uint8)
    image = cv2.imdecode(image, cv2.COLOR_BGR2GRAY)
    orientation = - orientation / 180 * math.pi
    sigma = 0.5 * wavelength * 1
    gamma = 0.5
    shape = 1 + 2 * math.ceil(16 * sigma)
    shape = (shape, shape)
    scale_fctr = 1/wavelength # percent of original size
    gabor_filter_imag = cv2.getGaborKernel(shape, sigma, orientation, wavelength, gamma, psi=math.pi/2)
    gabor_im = cv2.filter2D(image, -1, gabor_filter_imag)
    width = math.ceil(gabor_im.shape[1] * scale_fctr)
    height = math.ceil(gabor_im.shape[0] * scale_fctr)
    grayImage = cv2.resize(gabor_im, (width, height), interpolation=cv2.INTER_CUBIC)
    MedianTreshold = 255
    (thresh, blackAndWhiteImage) = cv2.threshold(grayImage, MedianTreshold, 255, cv2.THRESH_BINARY)
    blackAndWhiteImage = blackAndWhiteImage/255
    _, buffer = cv2.imencode('.png', blackAndWhiteImage)
    return base64.b64encode(buffer).decode()



    and my MainACtivtiy.class
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;

    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.os.ParcelFileDescriptor;
    import android.provider.MediaStore;
    import android.util.Base64;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.chaquo.python.PyObject;
    import com.chaquo.python.Python;
    import com.chaquo.python.android.AndroidPlatform;

    import org.beyka.tiffbitmapfactory.TiffBitmapFactory;

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;

    public class MainActivity2 extends AppCompatActivity {

    Bitmap bmp1;
    EditText gabor_a1;
    EditText gabor_a2 ;

    String imageString;
    //String x ;
    //String y ;
    String ImageString;

    Uri uri;



    ImageView imageView3;
    Button button3;
    Button button4;

    ImageView imageView4;

    private static final int REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    imageView3 =findViewById(R.id.imageView3);
    imageView4 = findViewById(R.id.imageView4);
    button3 = findViewById(R.id.button3);
    button4 = findViewById(R.id.button4);
    gabor_a1= findViewById(R.id.gabor_a1);// wavelet
    gabor_a2 = findViewById(R.id.gabor_a2);// orientation




    if(!Python.isStarted())
    Python.start(new AndroidPlatform(this));


    button3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {



    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_PICK);
    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
    startActivityForResult(intent, REQUEST_CODE);
    }
    });

    button4.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    int a_value = Integer.parseInt(gabor_a1.getText().toString());
    int b_value = Integer.parseInt(gabor_a2.getText().toString());


    Bitmap bitmap = null;

    try {
    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
    PyObject transGaborFunc = Python.getInstance().getModule("gabor").get("trans_gabor");


    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    String imageString = Base64.encodeToString(byteArray, Base64.DEFAULT);

    PyObject result = transGaborFunc.call(imageString, a_value, b_value);
    String resultString = result.toString();
    byte[] decodedString = Base64.decode(resultString, Base64.DEFAULT);
    Bitmap resultBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

    imageView4.setImageBitmap(resultBitmap); // ImageView1

    } catch (IOException e) {
    e.printStackTrace();
    }


    }
    });

    }

    }

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {

    uri = data.getData();



    imageView3.setImageURI(uri);



    }
    }

    }
    这是我在 stackoverflow 写的,里面有图,就是得到的结果是个黑色的图像! 谁能帮我看一下,我到底哪里错了!我有点难过,这么点 code 就是出不来结果图像

    https://stackoverflow.com/questions/75620836/image-processing-using-chaquopy-in-android-studio-error-output-with-gabor-transf
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2751 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 08:52 · PVG 16:52 · LAX 00:52 · JFK 03:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.