1

呈现的模块是 Android Studio 项目的一部分。我正在构建用于年龄和性别检测的应用程序。我在 Google Colab 和 VS Code 中测试了这个模块并加载了 keras 模型,一切都在那里完美运行。模块尚未完成,这就是为什么静态导入图片等。h5 和 pb 保存的模型存在于给定目录中并且拼写正确。请帮忙。

构建.gradle:

python {
        buildPython "C:/Windows/py.exe", "-3.8"
        pip{
            install "Keras-Applications"
            install "numpy"
            install "Pillow"
            install "opencv-python"
            install "tensorflow"
            install "dlib"
            install "face-cropper"
            install "keras==2.3.0"
        }
    }

age_and_gender_detection.py:

    
import numpy as np
import tensorflow as tf
import cv2
from face_cropper import crop
from os.path import dirname, join
from keras.applications.vgg16 import VGG16

def main(slika):

    loaded_model=None
    cropped_image=None

    try:
        #1.tryout
        dir1 = join(dirname(__file__), "age_and_gender_3_after.h5")
        loaded_model = tf.keras.models.load_model(dir1)

        #2.tryout
        #dir2 = join(dirname(__file__), "saved_model.pb")
        #loaded_model = tf.saved_model.load(dir2)

        #3.tryout
        #model = VGG16()
        print(loaded_model.summary())

        #print(list(loaded.signatures.keys()))  # ["serving_default"]
        #loaded_model.summary()
        print('Model loaded successfully')

    except:
        print('Error while loading model')


    print('tu sam2')
    print('modell: ', loaded_model)
    img_path = join(dirname(__file__), "marin.jpg")

    #paket face-cropper
    cropped_image = crop(
        image_path = img_path,
    )


    if cropped_image==None:
        raise Exception('Image format unknown')

    else:
        #PIL format u cv2
        imcv = cv2.cvtColor(np.asarray(cropped_image), cv2.COLOR_RGB2BGR)

        #podesavanje u prikladan format za fittanje na model
        img_resized = cv2.resize(imcv,(48,48))
        image = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
        image_test=image/255

        prediction = loaded_model.predict(np.array([image_test]))

        gender_f=['Male','Female']
        age=int(np.round(prediction[1][0]))
        gender=int(np.round(prediction[0][0]))

        print("Predicted Age: "+ str(age))
        print("Predicted Gender: "+ gender_f[gender])

    return "test123"

错误:


E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.facefun, PID: 6639
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:7448) 
        at android.view.View.performClickInternal(View.java:7425) 
        at android.view.View.access$3600(View.java:810) 
        at android.view.View$PerformClick.run(View.java:28305) 
        at android.os.Handler.handleCallback(Handler.java:938) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: com.chaquo.python.PyException: AttributeError: 'NoneType' object has no attribute 'predict'
        at <python>.age_and_gender_detection.main(age_and_gender_detection.py:56)
        at <python>.chaquopy_java.call(chaquopy_java.pyx:380)
        at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrowsNative(chaquopy_java.pyx:352)
        at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
        at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
        at com.chaquo.python.PyObject.callAttr(PyObject.java:221)
        at com.example.facefun.UploadPhoto.LaunchResultPage(UploadPhoto.java:143)

4

0 回答 0