Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F43429647
logo-detection: prototype for JSON input, preprocess with Keras, and return JSON output
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
kevinbazira
Mar 26 2024, 10:13 AM
2024-03-26 10:13:23 (UTC+0)
Size
2 KB
Referenced Files
None
Subscribers
None
logo-detection: prototype for JSON input, preprocess with Keras, and return JSON output
View Options
# !pip install keras==3.0.4
# !pip install keras-cv==0.8.1
import os
import requests
import json
import tempfile
import shutil
os.environ["KERAS_BACKEND"] = "tensorflow" # Set the Keras backend environment variable to "tensorflow"
import keras
import keras_cv
import numpy as np
BATCH_SIZE = 64
IMAGE_SIZE = (224, 224)
target = "logo" # Predicted target class: "album", "book", "logo", "screenshot"
label_mode = "binary" # Model type: "binary" for binary classification, "categorical" for multiclass
model_path = "/content/logo_detection/model/logo_max_all.keras" # Path to a trained Keras model with ".keras" extension
# Load the model
model = keras.models.load_model(model_path)
# Input data in JSON format, each containing filename, URL, and target class
input_data = [
{
"filename": "Cambia_logo.png",
"url": "http://www.kevinbazira.com/images/kevin-bazira-playing-basketball.jpg",
"target": "logo"
},
{
"filename": "Blooming_bush_(14248894271).jpg",
"url": "http://www.kevinbazira.com/images/kevin-bazira-playing-basketball.jpg",
"target": "logo"
}
]
# Create a temporary directory to store images
temp_dir = tempfile.mkdtemp()
# Create subdirectories for "logo" and "out_of_domain"
logo_dir = os.path.join(temp_dir, "logo")
out_of_domain_dir = os.path.join(temp_dir, "out_of_domain")
os.makedirs(logo_dir)
os.makedirs(out_of_domain_dir)
# Download images from URLs and save them to appropriate directories
for idx, data in enumerate(input_data):
image_url = data["url"]
image_filename = os.path.join(temp_dir, data["target"], data["filename"])
with open(image_filename, "wb") as f:
response = requests.get(image_url)
f.write(response.content)
# Use keras.utils.image_dataset_from_directory to create test_set
test_set = keras.utils.image_dataset_from_directory(
temp_dir,
labels="inferred",
label_mode=label_mode,
class_names=["out_of_domain", "logo"],
batch_size=BATCH_SIZE,
image_size=IMAGE_SIZE,
shuffle=False,
)
predictions_response = []
# Iterate through the test set and make predictions
for images, labels in test_set:
predictions = model(images) # Pass images directly to the model
for i in range(len(predictions)):
file_path = test_set.file_paths[i]
file_name = os.path.basename(file_path)
rounded_predictions = np.around(predictions[i].numpy(), decimals=2).astype(float)
prediction = {
"filename": file_name,
"target": target,
"prediction": {
"logo": rounded_predictions[1],
"out_of_domain": rounded_predictions[0]
}
}
predictions_response.append(prediction)
# Output response in JSON format
print(json.dumps(predictions_response, indent=4))
# Delete the temporary directory after use
shutil.rmtree(temp_dir)
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15096821
Default Alt Text
logo-detection: prototype for JSON input, preprocess with Keras, and return JSON output (2 KB)
Attached To
Mode
P58917 logo-detection: prototype for JSON input, preprocess with Keras, and return JSON output
Attached
Detach File
Event Timeline
Log In to Comment