Tensorflow/keras image model is not working post installation of streamlit

Hi,
I am new to streamlit and would like to build GUI using streamlit. Post installation of streamlit libarary in my venv, app is simply stuck at image vector generation that is pretrained ResNet50 model.

Before installation of streamlit it works and post installation it just stuck at that step without any error.

requirements.txt


absl-py==2.1.0

astunparse==1.6.3

certifi==2024.2.2

charset-normalizer==3.3.2

et-xmlfile==1.1.0

filelock==3.13.3

flatbuffers==24.3.25

fsspec==2024.3.1

gast==0.5.4

google-pasta==0.2.0

grpcio==1.62.1

h5py==3.10.0

huggingface-hub==0.22.1

idna==3.6

Jinja2==3.1.3

joblib==1.3.2

keras==3.1.1

libclang==18.1.1

Markdown==3.6

markdown-it-py==3.0.0

MarkupSafe==2.1.5

mdurl==0.1.2

ml-dtypes==0.3.2

mpmath==1.3.0

namex==0.0.7

networkx==3.2.1

numpy==1.26.4

opencv-python==4.9.0.80

openpyxl==3.1.2

opt-einsum==3.3.0

optree==0.11.0

packaging==24.0

pandas==2.2.1

pillow==10.2.0

protobuf==4.25.3

Pygments==2.17.2

python-dateutil==2.9.0.post0

pytz==2024.1

PyYAML==6.0.1

regex==2023.12.25

requests==2.31.0

rich==13.7.1

safetensors==0.4.2

scikit-learn==1.4.1.post1

scipy==1.12.0

sentence-transformers==2.6.1

six==1.16.0

sympy==1.12

tensorboard==2.16.2

tensorboard-data-server==0.7.2

tensorflow==2.16.1

tensorflow-io-gcs-filesystem==0.36.0

termcolor==2.4.0

threadpoolctl==3.4.0

tokenizers==0.15.2

torch==2.2.1

tqdm==4.66.2

transformers==4.39.1

typing_extensions==4.10.0

tzdata==2024.1

urllib3==2.2.1

Werkzeug==2.2.2

wrapt==1.16.0

openai==1.14.3

Code where it stucks:

import pandas as pd

import numpy as np

import os

from sentence_transformers import SentenceTransformer,util

import cv2

import numpy as np

from tensorflow.keras.applications import ResNet50

from tensorflow.keras.applications.resnet50 import preprocess_input

from sklearn.preprocessing import normalize

from clusterByErrorDesc import create_clusters

# import db_test

# Load the pre-trained ResNet50 model

model = ResNet50(weights='imagenet', include_top=False, pooling='avg')

ef get_image_vector(input_image_path, model=model):
    """
    Extracts a feature vector from an image using a pre-trained model.

    Args:
        input_image_path (str): Path to the input image.
        model: The pre-trained CNN model for feature extraction.

    Returns:
        np.ndarray (shape=(2048,)): The extracted image feature vector, or a zero vector if errors occur.

    Raises:
        ValueError: If the input image path is invalid or empty.
    """
    print("generating image vector")
    try:
        # Read and preprocess the image within the try block for error handling
        image = cv2.imread(input_image_path)

        # if image is None:
        #     raise FileNotFoundError("Image file not found or invalid path.")

        # Combine steps for efficiency, assuming BGR to RGB conversion and resizing are always needed
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, (512, 512))

        # Add batch dimension and preprocess consistently for the model
        image = np.expand_dims(image, axis=0)
        image = preprocess_input(image.copy()) 

        # Get the image vector (assuming model.predict returns a 2D array)
        image_vector = model.predict(image) ## Code execution is stuck at this line
        return image_vector.flatten()

    except FileNotFoundError as e:
        print(f"Error: {e}")
        return np.zeros(2048)

    except ValueError as e:
        print(f"Error: {e}")
        return np.zeros(2048)

    except Exception as e:
        print(f"Unexpected error: {e}")
        return np.zeros(2048)

And Im using MacOS intel(Sonoma 14.4.1) , python 3.11

Do you really need and use all those libraries? Clean it up.

use opencv-python-headless instead

What is the error message?


Please share a link to your public github repo

@Franky1 - I changed the library to opencv-python-headless still facing the same issue.

I am not getting any error message but code execution is just stuck for hours and i had to cancel the execution.
Call to get_image_vector function never returns any results or error messages.

Sorry, i dont posted my code to github public repo yet, just trying locally

I assumed it was a deployment issue…
I don’t see any streamlit related code in your code snippet.

I just added streamlit libraray via pip in the virutal environment and post that application is not working. I would like to develop GUI, using streamlit.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.