0

I am building an Android app that will allow the user to get a picture either by taking it in real time or uploading it from their saved images. Then, it will go through a machine learning script in python to determine their location. Before I completely connect to the algorithm, I am trying a test program that just returns a double.

from os.path import dirname, join
import csv
import random
filename = join(dirname(__file__), "new.csv")

def testlat():
    return 30.0

def testlong():
    return 30.0

These returned values are used in a Kotlin file that will then send those values to the Google Maps activity on the app for the location to be plotted.

class MainActivity : AppCompatActivity() {
    var lat = 0.0
    var long = 0.0
    var dynamic = false
    private val cameraRequest = 1888
    lateinit var imageView: ImageView
    lateinit var button: Button
    private val pickImage = 100
    private var imageUri: Uri? = null
    var active = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Accesses the info image button
        val clickMe = findViewById<ImageButton>(R.id.imageButton)

        // Runs this function when the info icon is pressed by the user
        // It will display the text in the variable infoText
        clickMe.setOnClickListener {
            Toast.makeText(this, infoText, Toast.LENGTH_LONG).show()
        }

        if (ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.CAMERA),
                cameraRequest
            )
        }
        imageView = findViewById(R.id.imageView)
        val photoButton: Button = findViewById(R.id.button2)
        photoButton.setOnClickListener {
            val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(cameraIntent, cameraRequest)
            dynamic = true
        }

        /*
        The below will move to external photo storage once button2 is clicked
         */
        button = findViewById(R.id.button)
        button.setOnClickListener {
            val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
            startActivityForResult(gallery, pickImage)
        }

        // PYTHON HERE
        if (! Python.isStarted()) {
            Python.start(AndroidPlatform(this))
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == pickImage) {
            imageUri = data?.data
            imageView.setImageURI(imageUri)

            // PYTHON HERE
            val py = Python.getInstance()
            val pyobj = py.getModule("main")
            this.lat = pyobj.callAttr("testlat").toDouble()
            this.long = pyobj.callAttr("testlong").toDouble()

            /* Open the map after image has been received from user
             This will be changed later to instead call the external object recognition/pathfinding
             scripts and then pull up the map after those finish running
             */

            val mapsIntent = Intent(this, MapsActivity::class.java)
            startActivity(mapsIntent)
        }
    }
}

I set up chaquopy and the gradle is building successfully, but everytime I get to the python part of emulating the app, it crashes. I'm not quite sure why that is; I thought maybe the program was too much for the phone to handle but it is a very basic python script so I doubt that's the issue.

ba1
  • 41
  • 4

1 Answers1

2

If your app crashes, you can find the stack trace in the Logcat.

In this case, it's probably caused by the line return = 30.0. The correct syntax is return 30.0.

mhsmith
  • 6,675
  • 3
  • 41
  • 58