Update app.py
Browse files
app.py
CHANGED
@@ -13,46 +13,54 @@ import tensorflow_hub as hub
|
|
13 |
IMAGE_DIM = 299 # required/default image dimensionality
|
14 |
model = tf.keras.models.load_model("nsfw.299x299.h5", custom_objects={'KerasLayer': hub.KerasLayer},compile=False)
|
15 |
|
16 |
-
def load_images(image_paths, image_size, verbose=True):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
return np.asarray(loaded_images)
|
52 |
|
53 |
def classify_nd(model, nd_images, predict_args={}):
|
54 |
-
file_path = nd_images.filename
|
55 |
-
img = load_images(
|
56 |
model_preds = model.predict(img, **predict_args)
|
57 |
categories = ['drawings', 'hentai', 'neutral', 'porn', 'sexy']
|
58 |
|
|
|
13 |
IMAGE_DIM = 299 # required/default image dimensionality
|
14 |
model = tf.keras.models.load_model("nsfw.299x299.h5", custom_objects={'KerasLayer': hub.KerasLayer},compile=False)
|
15 |
|
16 |
+
# def load_images(image_paths, image_size, verbose=True):
|
17 |
+
# '''
|
18 |
+
# Function for loading images into numpy arrays for passing to model.predict
|
19 |
+
# inputs:
|
20 |
+
# image_paths: list of image paths to load
|
21 |
+
# image_size: size into which images should be resized
|
22 |
+
# verbose: show all of the image path and sizes loaded
|
23 |
|
24 |
+
# outputs:
|
25 |
+
# loaded_images: loaded images on which keras model can run predictions
|
26 |
+
# loaded_image_indexes: paths of images which the function is able to process
|
27 |
|
28 |
+
# '''
|
29 |
+
# loaded_images = []
|
30 |
+
# loaded_image_paths = []
|
31 |
|
32 |
+
# if isdir(image_paths):
|
33 |
+
# parent = abspath(image_paths)
|
34 |
+
# image_paths = [join(parent, f) for f in listdir(image_paths) if isfile(join(parent, f))]
|
35 |
+
# elif isfile(image_paths):
|
36 |
+
# image_paths = [image_paths]
|
37 |
|
38 |
+
# for img_path in image_paths:
|
39 |
+
# try:
|
40 |
+
# if verbose:
|
41 |
+
# print(img_path, "size:", image_size)
|
42 |
+
# image = keras.preprocessing.image.load_img(img_path, target_size=image_size)
|
43 |
+
# image = keras.preprocessing.image.img_to_array(image)
|
44 |
+
# image /= 255
|
45 |
+
# image
|
46 |
+
# loaded_images.append(image)
|
47 |
+
# loaded_image_paths.append(img_path)
|
48 |
+
# except Exception as ex:
|
49 |
+
# print("Image Load Failure: ", img_path, ex)
|
50 |
|
51 |
+
# return np.asarray(loaded_images)
|
52 |
+
def load_images(image):
|
53 |
+
loaded_images = []
|
54 |
+
image = keras.preprocessing.image.array_to_img(image)
|
55 |
+
image = image.resize((299, 299))
|
56 |
+
image = keras.preprocessing.image.img_to_array(image)
|
57 |
+
image /= 255
|
58 |
+
loaded_images.append(image)
|
59 |
return np.asarray(loaded_images)
|
60 |
|
61 |
def classify_nd(model, nd_images, predict_args={}):
|
62 |
+
# file_path = nd_images.filename
|
63 |
+
img = load_images(nd_images)
|
64 |
model_preds = model.predict(img, **predict_args)
|
65 |
categories = ['drawings', 'hentai', 'neutral', 'porn', 'sexy']
|
66 |
|