実現したいこと
androidstudioでMediapipeを使用して、JPEGファイルから顔を検出するアプリを作りたいです。しかしMediaPipeを使おうとするとどうしてもエラーが出てしまいます。ですのでまずは顔検出(FaceDetector)の初期化を目標にコードを作成中ですがうまくいきません。現在のコードの間違い箇所が分かれば教えていただきたいです。
発生している問題・分からないこと
JPEG画像を表示はできますが、FaceDetectorの初期化がうまくできません。
(”画像の読み込み成功”、”FaceDetector の初期化に失敗しました”とログに表示されます)
該当のソースコード
package com.example.myapplication import com.google.mediapipe.tasks.vision.facedetector.FaceDetector import com.google.mediapipe.tasks.vision.facedetector.FaceDetector.FaceDetectorOptions // ← 修正 import com.google.mediapipe.tasks.vision.facedetector.FaceDetectorResult import com.google.mediapipe.framework.image.BitmapImageBuilder import android.util.Log // ← LOG使用追加 //assetsフォルダから画像を読み込んでImageViewに設定 import android.graphics.BitmapFactory import android.widget.ImageView import android.os.Bundle import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var faceDetector: FaceDetector // FaceDetectorの変数 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_main) // 背景画像を設定 try { val imageView = findViewById<ImageView>(R.id.imageView) val assetManager = assets val inputStream = assetManager.open("background.jpg") val bitmap = BitmapFactory.decodeStream(inputStream) if (bitmap != null) { imageView.setImageBitmap(bitmap) Log.d("DEBUG", "画像の読み込み成功") } else { Log.e("ERROR", "Bitmapがnullです") } } catch (e: Exception) { Log.e("ERROR", "画像の読み込みに失敗しました: ${e.message}") } // ★★ MediaPipeの顔検出を初期化 ★★ try { val options = FaceDetectorOptions.builder() .setMinDetectionConfidence(0.5f) .build() faceDetector = FaceDetector.createFromOptions(this, options) Log.d("DEBUG", "FaceDetector の初期化成功") } catch (e: Exception) { Log.e("ERROR", "FaceDetector の初期化に失敗しました: ${e.message}") } } }
plugins { alias(libs.plugins.android.application) alias(libs.plugins.kotlin.android) } android { namespace = "com.example.myapplication" compileSdk = 35 defaultConfig { applicationId = "com.example.myapplication" minSdk = 24 targetSdk = 35 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "11" } } dependencies { //MediaPipe顔検出依存関係 implementation ("com.google.mediapipe:tasks-vision:0.10.2") implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) implementation(libs.material) implementation(libs.androidx.activity) implementation(libs.androidx.constraintlayout) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) }
<?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/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- 背景画像を設定 --> <!-- MainActivity.ktに設定を追加 --> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:contentDescription="背景画像" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <!-- テキスト --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
①androidstudioで新規作成からEmpty Views Activvityを選択②添付の通りにコードを修正③app/src/main/assets/ フォルダに 画像ファイル( background.jpg)と顔検出用の学習データを置く。④dependencies { implementation ("com.google.mediapipe:tasks-vision:0.10.2")}部分を、他のバージョンに変えてみる→効果なし。
補足
言語:kotlin
Minimum SDK:API24("Nougat";Android7.0)
