Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,96 @@
|
|
| 1 |
-
import tensorflow as tf
|
| 2 |
-
from keras.models import model_from_json
|
| 3 |
-
import requests
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import numpy as np
|
| 6 |
-
import PIL.Image as Image
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
from glob import glob
|
| 9 |
-
import cv2
|
| 10 |
-
import os
|
| 11 |
-
|
| 12 |
-
dataset = 'Color'
|
| 13 |
-
|
| 14 |
-
model_architecture = 'Models/Model_Color_128_128.json'
|
| 15 |
-
|
| 16 |
-
model_weights = 'Models/Model_Color_128_128.h5'
|
| 17 |
-
|
| 18 |
-
RESCALE_IMG_WIDTH, RESCALE_IMG_HEIGHT = 128, 128
|
| 19 |
-
SIZE = 128
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def resize_image_pil(img, new_width, new_height):
|
| 23 |
-
|
| 24 |
-
# Convert to PIL image
|
| 25 |
-
img = Image.fromarray(img)
|
| 26 |
-
|
| 27 |
-
# Get original size
|
| 28 |
-
width, height = img.size
|
| 29 |
-
|
| 30 |
-
# Calculate scale
|
| 31 |
-
width_scale = new_width / width
|
| 32 |
-
height_scale = new_height / height
|
| 33 |
-
scale = min(width_scale, height_scale)
|
| 34 |
-
|
| 35 |
-
# Resize
|
| 36 |
-
resized = img.resize((int(width*scale), int(height*scale)), Image.NEAREST)
|
| 37 |
-
|
| 38 |
-
# Crop to exact size
|
| 39 |
-
resized = resized.crop((0, 0, new_width, new_height))
|
| 40 |
-
|
| 41 |
-
return resized
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# def transform_img(img):
|
| 45 |
-
# img_blur = cv2.GaussianBlur(img, (3 ,3), 0, 0)
|
| 46 |
-
# kernel = np.array([[-3,-1,-1], [-1,9,-1], [-1,-1,1]])
|
| 47 |
-
# im = cv2.filter2D(img_blur, -1, kernel)
|
| 48 |
-
# img_inv = cv2.bitwise_not(im)
|
| 49 |
-
|
| 50 |
-
return img_inv
|
| 51 |
-
|
| 52 |
-
def classify_image(inp):
|
| 53 |
-
label = ['Dog','Cat']
|
| 54 |
-
|
| 55 |
-
# inp2 = transform_img(inp)
|
| 56 |
-
img_resized = resize_image_pil(inp,RESCALE_IMG_WIDTH, RESCALE_IMG_HEIGHT)
|
| 57 |
-
img = np.array(img_resized)
|
| 58 |
-
img = img * (1/255)
|
| 59 |
-
reshaped_array = img.reshape((-1, RESCALE_IMG_WIDTH, RESCALE_IMG_HEIGHT , 3))
|
| 60 |
-
|
| 61 |
-
predictions = model.predict(x=reshaped_array).flatten()
|
| 62 |
-
|
| 63 |
-
confidences = {label[i]: float(predictions[i]) for i in range(2)}
|
| 64 |
-
|
| 65 |
-
# img_resized = transform_img(inp)
|
| 66 |
-
img_resized = Image.fromarray(
|
| 67 |
-
img_resized = img_resized.resize(size = (512,512), resample = Image.NEAREST)
|
| 68 |
-
|
| 69 |
-
return [confidences, img_resized]
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# load json and create model
|
| 73 |
-
json_file = open(model_architecture, 'r')
|
| 74 |
-
loaded_model_json = json_file.read()
|
| 75 |
-
json_file.close()
|
| 76 |
-
|
| 77 |
-
model = model_from_json(loaded_model_json)
|
| 78 |
-
# load weights into new model
|
| 79 |
-
model.load_weights(model_weights)
|
| 80 |
-
|
| 81 |
-
np.random.seed(123)
|
| 82 |
-
random_imgs = np.random.choice(
|
| 83 |
-
glob( dataset + '/**/*.jpg'),
|
| 84 |
-
size=128,
|
| 85 |
-
replace=False,
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
demo = gr.Interface(
|
| 89 |
-
fn=classify_image,
|
| 90 |
-
inputs=gr.Image(width=256, height=256),
|
| 91 |
-
outputs=[gr.Label(num_top_classes=2), gr.Image(width=512, height=512)],
|
| 92 |
-
examples=[img_path for img_path in random_imgs],
|
| 93 |
-
examples_per_page=64,
|
| 94 |
-
title= 'Cat & Dog classifier'
|
| 95 |
-
)
|
| 96 |
-
demo.launch(inbrowser=True)
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from keras.models import model_from_json
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
import PIL.Image as Image
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from glob import glob
|
| 9 |
+
import cv2
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
dataset = 'Color'
|
| 13 |
+
|
| 14 |
+
model_architecture = 'Models/Model_Color_128_128.json'
|
| 15 |
+
|
| 16 |
+
model_weights = 'Models/Model_Color_128_128.h5'
|
| 17 |
+
|
| 18 |
+
RESCALE_IMG_WIDTH, RESCALE_IMG_HEIGHT = 128, 128
|
| 19 |
+
SIZE = 128
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def resize_image_pil(img, new_width, new_height):
|
| 23 |
+
|
| 24 |
+
# Convert to PIL image
|
| 25 |
+
img = Image.fromarray(img)
|
| 26 |
+
|
| 27 |
+
# Get original size
|
| 28 |
+
width, height = img.size
|
| 29 |
+
|
| 30 |
+
# Calculate scale
|
| 31 |
+
width_scale = new_width / width
|
| 32 |
+
height_scale = new_height / height
|
| 33 |
+
scale = min(width_scale, height_scale)
|
| 34 |
+
|
| 35 |
+
# Resize
|
| 36 |
+
resized = img.resize((int(width*scale), int(height*scale)), Image.NEAREST)
|
| 37 |
+
|
| 38 |
+
# Crop to exact size
|
| 39 |
+
resized = resized.crop((0, 0, new_width, new_height))
|
| 40 |
+
|
| 41 |
+
return resized
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# def transform_img(img):
|
| 45 |
+
# img_blur = cv2.GaussianBlur(img, (3 ,3), 0, 0)
|
| 46 |
+
# kernel = np.array([[-3,-1,-1], [-1,9,-1], [-1,-1,1]])
|
| 47 |
+
# im = cv2.filter2D(img_blur, -1, kernel)
|
| 48 |
+
# img_inv = cv2.bitwise_not(im)
|
| 49 |
+
|
| 50 |
+
return img_inv
|
| 51 |
+
|
| 52 |
+
def classify_image(inp):
|
| 53 |
+
label = ['Dog','Cat']
|
| 54 |
+
|
| 55 |
+
# inp2 = transform_img(inp)
|
| 56 |
+
img_resized = resize_image_pil(inp,RESCALE_IMG_WIDTH, RESCALE_IMG_HEIGHT)
|
| 57 |
+
img = np.array(img_resized)
|
| 58 |
+
img = img * (1/255)
|
| 59 |
+
reshaped_array = img.reshape((-1, RESCALE_IMG_WIDTH, RESCALE_IMG_HEIGHT , 3))
|
| 60 |
+
|
| 61 |
+
predictions = model.predict(x=reshaped_array).flatten()
|
| 62 |
+
|
| 63 |
+
confidences = {label[i]: float(predictions[i]) for i in range(2)}
|
| 64 |
+
|
| 65 |
+
# img_resized = transform_img(inp)
|
| 66 |
+
img_resized = Image.fromarray(inp)
|
| 67 |
+
img_resized = img_resized.resize(size = (512,512), resample = Image.NEAREST)
|
| 68 |
+
|
| 69 |
+
return [confidences, img_resized]
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# load json and create model
|
| 73 |
+
json_file = open(model_architecture, 'r')
|
| 74 |
+
loaded_model_json = json_file.read()
|
| 75 |
+
json_file.close()
|
| 76 |
+
|
| 77 |
+
model = model_from_json(loaded_model_json)
|
| 78 |
+
# load weights into new model
|
| 79 |
+
model.load_weights(model_weights)
|
| 80 |
+
|
| 81 |
+
np.random.seed(123)
|
| 82 |
+
random_imgs = np.random.choice(
|
| 83 |
+
glob( dataset + '/**/*.jpg'),
|
| 84 |
+
size=128,
|
| 85 |
+
replace=False,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
demo = gr.Interface(
|
| 89 |
+
fn=classify_image,
|
| 90 |
+
inputs=gr.Image(width=256, height=256),
|
| 91 |
+
outputs=[gr.Label(num_top_classes=2), gr.Image(width=512, height=512)],
|
| 92 |
+
examples=[img_path for img_path in random_imgs],
|
| 93 |
+
examples_per_page=64,
|
| 94 |
+
title= 'Cat & Dog classifier'
|
| 95 |
+
)
|
| 96 |
+
demo.launch(inbrowser=True)
|