Download src/predictor.py from iBrokeTheCode/Image_Classifier_with_CNN: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/iBrokeTheCode/Image_Classifier_with_CNN/resolve/3ce4bfcd092cc1d5f518baf6ca367ffa336d7d75/src/predictor.py
- Command line
-
hf download hf://spaces/iBrokeTheCode/Image_Classifier_with_CNN@3ce4bfcd092cc1d5f518baf6ca367ffa336d7d75/src/predictor.py
-
curl -L -o predictor.py https://huggingface.co/spaces/iBrokeTheCode/Image_Classifier_with_CNN/resolve/3ce4bfcd092cc1d5f518baf6ca367ffa336d7d75/src/predictor.py
1.48 kB
| import numpy as np | |
| from tensorflow.keras.applications.resnet50 import ( | |
| ResNet50, | |
| decode_predictions, | |
| preprocess_input, | |
| ) | |
| from tensorflow.keras.preprocessing import image | |
| # Load the model outside the function to ensure it's loaded only once | |
| model = ResNet50(include_top=True, weights="imagenet") | |
| def predict_image(img): | |
| """ | |
| Preprocesses an image and runs a pre-trained ResNet50 model to get a prediction. | |
| Parameters | |
| ---------- | |
| img : PIL.Image | |
| The image object to classify. | |
| Returns | |
| ------- | |
| class_name, pred_probability : tuple(str, float) | |
| The model's predicted class as a string and the corresponding confidence | |
| score as a number. | |
| """ | |
| # Resize the image to match model input dimensions (224, 224) | |
| img = img.resize((224, 224)) | |
| # Convert Pillow image to np.array | |
| x = image.img_to_array(img) | |
| # Add an extra dimension for the batch size | |
| x_batch = np.expand_dims(x, axis=0) | |
| # Apply ResNet50-specific preprocessing | |
| x_batch = preprocess_input(x_batch) | |
| # Make predictions | |
| predictions = model.predict(x_batch, verbose=0) | |
| # Get predictions using model methods and decode predictions | |
| top_pred = decode_predictions(predictions, top=1)[0][0] # imagenet_id, label, score | |
| _, class_name, pred_probability = top_pred | |
| # Convert probability to float and round it | |
| pred_probability = round(float(pred_probability), 4) | |
| return class_name, pred_probability | |