Skip to content

Commit 8366fbe

Browse files
authored
Add files via upload
1 parent 7d76916 commit 8366fbe

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Image Classification/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# **Multi Stack Games**
2+
This script performs image classification using the MobileNetV2 pre-trained model provided by TensorFlow. It allows you to classify images into different categories based on the ImageNet dataset.
3+
4+
## Prerequisites
5+
Before running the script, ensure you have the following dependencies installed:
6+
7+
- TensorFlow (version 2.x)
8+
- NumPy
9+
- Pillow
10+
11+
## Usage
12+
1. Prepare your image: Ensure you have an image file that you want to classify. Supported image formats include JPEG, PNG, and BMP.
13+
14+
2. Update the image path: In the script, locate the image_path variable and replace "image.jpg" with the path to your image file.
15+
16+
3. Run the script: Execute the script using the following command: `python image_classification.py`
17+
18+
4. View the predictions: The script will display the top 3 predictions for the image, along with their respective labels and confidence scores.
19+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import tensorflow as tf
2+
import tensorflow.keras.applications.mobilenet_v2 as mobilenet
3+
from tensorflow.keras.preprocessing import image
4+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
5+
import numpy as np
6+
7+
def classify_image(image_path):
8+
# Load the pre-trained MobileNetV2 model
9+
model = mobilenet.MobileNetV2(weights='imagenet')
10+
11+
# Load and preprocess the image
12+
img = image.load_img(image_path, target_size=(224, 224))
13+
img_array = image.img_to_array(img)
14+
img_array = np.expand_dims(img_array, axis=0)
15+
processed_img = preprocess_input(img_array)
16+
17+
# Perform image classification
18+
predictions = model.predict(processed_img)
19+
decoded_predictions = decode_predictions(predictions, top=3)[0]
20+
21+
return decoded_predictions
22+
23+
# Example usage
24+
image_path = "image.jpg"
25+
predictions = classify_image(image_path)
26+
27+
# Print the top 3 predictions
28+
for prediction in predictions:
29+
print(f"{prediction[1]}: {prediction[2]*100:.2f}%")
30+

0 commit comments

Comments
 (0)