File size: 856 Bytes
4914bfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np

# Load the model
model = tf.keras.models.load_model('nsfw_classifier.h5')

# Load an image file to test, resizing it to 150x150 pixels (as required by this model)
img = image.load_img('', target_size=(512, 512))

# Convert the image to a numpy array
img_array = image.img_to_array(img)

# Add a fourth dimension to the image (since Keras expects a list of images, not a single image)
img_array = np.expand_dims(img_array, axis=0)/

# Normalize the image
img_array /= 255.

# Use the model to predict the image's class
pred = model.predict(img_array)

# The model returns a probability between 0 and 1
# You can convert this to the class label like this:
label = 'NSFW' if pred[0][0] > 0.5  else 'SFW'
print(pred[0][0])
print("The image is classified as:", label)