Android + yolov8 + Image detect

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/back_btn"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ImageActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/btnBack"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="back" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginBottom="30dp"
            android:src="@drawable/image_preview_foreground" />

        <TextView
            android:id="@+id/result_txv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:text="result"
            android:textSize="34sp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.tencent.yolov8ncnn;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;

public class ImageActivity extends AppCompatActivity {

    private YoloAPI yolo_api = new YoloAPI();
    private Yolov8Ncnn yolov8ncnn =new Yolov8Ncnn();

    private int current_model = 0;
    private int current_cpugpu = 0;

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

        ImageView image_from_gallery = findViewById(R.id.imageView);

        boolean initializationSuccessful = yolo_api.Init(getAssets(), 0);


        reload();

        if (initializationSuccessful) {
            if (getIntent().hasExtra("IMAGE_URI")) {
                String imageUriString = getIntent().getStringExtra("IMAGE_URI");
                assert imageUriString != null;
                Log.d("Image_path_VideoActivity", imageUriString);

                Uri imageUri = Uri.parse(imageUriString);
                image_from_gallery.setImageURI(imageUri);

                Bitmap bitmap = null;
                try {
                    bitmap = uriToBitmap(imageUri);
                    // Now you have the Bitmap object, you can use it as needed
                } catch (IOException e) {
                    Log.d("uriToBitmap",e.getMessage());
                    // Handle any errors that may occur during the conversion
                }

                YoloAPI.Obj[] detectedObjects = YoloAPI.Detect(bitmap,true);
//                Log.d("detectedObjects", String.valueOf(detectedObjects.length));

                //detect somethings
                if (detectedObjects.length != 0) {
                    int count = 0 ;
                    for (YoloAPI.Obj detectedObject : detectedObjects){
                        count+=1;

                        Log.d("detectedObjects", count+",label:"+YoloAPI.CLASSNAME[detectedObject.label]+",prob:"+detectedObject.prob);
                    }
                    // Handle the case where no objects were detected

                }
            }



        }


        Button btnBack = findViewById(R.id.btnBack);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                onBackPressed();
                Intent intent =new Intent(ImageActivity.this,NavActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    public Bitmap uriToBitmap(Uri uri) throws IOException {
        InputStream inputStream = getContentResolver().openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        inputStream.close();
        return bitmap;
    }



    private void reload()
    {
        boolean ret_init = yolov8ncnn.loadModel(getAssets(), current_model, current_cpugpu);
        if (!ret_init)
        {
            Log.e("ImageActivity", "yolov8ncnn loadModel failed");
        }
    }



}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值