[Python] How can I solve "No file or directory found at ./my_model/model.h5" thrown by...

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    I created a code that tells if a picture of a wave is a raging wave when I enter it into the code. The code is shown below.

    pip install tensorflow opencv-python

    import os
    import cv2
    import numpy as np
    import tensorflow as tf

    # 모델 경로 설정 및 확인
    model_dir = './my_model'
    model_path = os.path.join(model_dir, 'model.h5') # TensorFlow 모델 파일 확장자는 일반적으로 .h5입니다.
    if not os.path.exists(model_path):
    print(f"The specified model path does not exist: {model_path}")
    exit() # 프로그램을 종료


    # 모델 로드 함수
    def load_model(model_path):
    model = tf.keras.models.load_model(model_path, compile=False)
    return model

    # 웹캠에서 이미지 캡처 및 예측을 수행하는 함수
    def capture_image(model):
    cap = cv2.VideoCapture(0) # 0은 기본 카메라
    if not cap.isOpened():
    raise Exception("Cannot open webcam")

    while True:
    ret, frame = cap.read()
    if not ret:
    print("Failed to grab frame")
    break

    # 이미지 프레임을 모델 입력에 맞게 전처리
    processed_frame = cv2.resize(frame, (224, 224)) # 모델에 따라 조정 필요
    processed_frame = processed_frame / 255.0
    processed_frame = np.expand_dims(processed_frame, axis=0)

    # 예측 수행
    predictions = model.predict(processed_frame)
    predicted_class = np.argmax(predictions)
    confidence = np.max(predictions)

    # 결과 출력
    cv2.putText(frame, f'Class: {predicted_class}, Confidence: {confidence:.2f}',
    (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2, cv2.LINE_AA)

    # 이미지 출력
    cv2.imshow('Webcam', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
    break

    cap.release()
    cv2.destroyAllWindows()

    # 모델 로드
    model = load_model(model_path)

    # 웹캠 이미지 캡처 및 예측
    capture_image(model)


    But I got a problem.

    # 모델 로드 함수
    def load_model(model_path):
    model = tf.keras.models.load_model(model_path, compile=False)
    return model


    The above code showed "No file or directory found at ./my_model/model.h5" problem. How can I fix it?

    I try changing picture name, add new code like below:

    model.save("model.h5")
    model = load_model('model.h5')


    and I recently add some code to import os, cv2, numpy as np, tensorflow as tf.

    Continue reading...

Compartilhe esta Página