|
import os |
|
import json |
|
import pandas as pd |
|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
from theme_tops import DarkTheme |
|
from clip_base import OpenAiClipModel |
|
import tensorflow as tf |
|
tagged_images = {} |
|
|
|
MODEL_PATH = os.path.join(os.getcwd(), 'clip_tflite_model.tflite') |
|
JSON_PATH = os.path.join(os.getcwd(), 'categories.json') |
|
|
|
def test_model(image): |
|
"""Test the TFLite model with an uploaded image""" |
|
try: |
|
|
|
if not os.path.exists(MODEL_PATH): |
|
return "Error: Model file not found. Please generate the model first." |
|
if not os.path.exists(JSON_PATH): |
|
return "Error: Categories file not found. Please generate the model first." |
|
|
|
|
|
processed_image = load_and_preprocess_image(image) |
|
|
|
|
|
interpreter = tf.lite.Interpreter(model_path=MODEL_PATH) |
|
interpreter.allocate_tensors() |
|
|
|
|
|
input_details = interpreter.get_input_details() |
|
output_details = interpreter.get_output_details() |
|
|
|
interpreter.set_tensor(input_details[0]['index'], processed_image) |
|
|
|
interpreter.invoke() |
|
|
|
embeddings = interpreter.get_tensor(output_details[0]['index']) |
|
|
|
with open(JSON_PATH, 'r') as f: |
|
categories = json.load(f) |
|
|
|
scores_with_ids = [] |
|
for i, score in enumerate(embeddings.flatten()): |
|
scores_with_ids.append((float(score), i)) |
|
|
|
scores_with_ids.sort(reverse=True) |
|
|
|
top_results = scores_with_ids[:5] |
|
|
|
results = [] |
|
for score, category_id in top_results: |
|
percentage = score * 100 |
|
category = next((cat['title'] for cat in categories if cat['id'] == category_id), |
|
f"Category {category_id}") |
|
results.append(f"{category}: {percentage:.2f}%") |
|
|
|
return "\n".join(results) |
|
|
|
except Exception as e: |
|
return f"Error processing image: {str(e)}" |
|
|
|
def load_and_preprocess_image(image): |
|
"""Preprocess image for model input""" |
|
if isinstance(image, str): |
|
image = Image.open(image) |
|
elif isinstance(image, np.ndarray): |
|
image = Image.fromarray(image) |
|
|
|
image = image.resize((224, 224)) |
|
image = image.convert('RGB') |
|
image = np.array(image).astype(np.float32) / 255.0 |
|
image = np.expand_dims(image, axis=0) |
|
return image |
|
|
|
def process_images(payload): |
|
tflite_model = OpenAiClipModel(payload=payload).build_model() |
|
|
|
return tflite_model |
|
|
|
|
|
def add_tag_category(tag_category): |
|
|
|
tag_category = tag_category.strip() |
|
if not tag_category: |
|
return "Please enter a valid tag category", None |
|
|
|
|
|
if tag_category not in tagged_images: |
|
tagged_images[tag_category] = [] |
|
|
|
return f"Tag Category '{tag_category}' Added", gr.File(visible=True), "" |
|
|
|
|
|
|
|
def get_tag_category_choices(): |
|
return gr.Dropdown(choices=list(tagged_images.keys())) |
|
|
|
|
|
def show_category_images(tag_category): |
|
if not tag_category: |
|
return None, None |
|
|
|
if tag_category in tagged_images: |
|
return ( |
|
gr.Gallery(value=tagged_images[tag_category]), |
|
tagged_images[tag_category] |
|
) |
|
return None, None |
|
|
|
|
|
|
|
def upload_images_for_tag(tag_category, image_files): |
|
|
|
if tag_category not in tagged_images: |
|
return "Tag category not found. Add the tag category first.", None, None |
|
|
|
|
|
tagged_images[tag_category] = [file.name for file in image_files] |
|
|
|
return ( |
|
f"Added {len(image_files)} images to '{tag_category}'", |
|
gr.Gallery(value=[file.name for file in image_files]), |
|
tagged_images |
|
) |
|
|
|
|
|
|
|
|
|
def export_tagged_images(): |
|
return tagged_images |
|
|
|
def clear_uploaded_images(): |
|
return None, None |
|
|
|
|
|
with gr.Blocks(theme=DarkTheme()) as demo: |
|
gr.Markdown("# Clip -> Tflite - TOPS Infosolutions Pvt Ltd") |
|
gr.Markdown("Add Classification Tags") |
|
|
|
|
|
with gr.Row(): |
|
tag_category_input = gr.Textbox( |
|
label="Enter Tag Category", |
|
placeholder="e.g., Smartphone, Laptop, Tablet" |
|
) |
|
|
|
tag_category_status = gr.Textbox(label="Action Status", interactive=False) |
|
|
|
gr.Markdown("Images") |
|
|
|
|
|
with gr.Row(): |
|
tag_category_selector = gr.Dropdown(label="Select Tag Category", choices=[]) |
|
image_upload = gr.File( |
|
file_types=["image"], |
|
file_count="multiple", |
|
label="Upload Images", |
|
visible=False |
|
) |
|
upload_images_btn = gr.Button("Upload Images for Category") |
|
clear_upload_btn = gr.Button("Clear Upload") |
|
|
|
|
|
image_gallery = gr.Gallery( |
|
label="Uploaded Images", |
|
columns=[6], |
|
rows=[1], |
|
height="20", |
|
object_fit="contain", |
|
preview=False, |
|
show_label=False, |
|
elem_classes="small-gallery" |
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
|
export_output = gr.JSON(label="Exported Tagged Images") |
|
|
|
with gr.Row(): |
|
submit_btn = gr.Button("Process Images") |
|
|
|
|
|
with gr.Row(): |
|
download_button_tflite = gr.File( |
|
label="Download Tflite Model", |
|
file_count="single", |
|
interactive=False, |
|
type="filepath" |
|
) |
|
|
|
with gr.Tab("Test Model"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
test_image = gr.Image( |
|
label="Upload Image to Test", |
|
type="numpy" |
|
) |
|
test_button = gr.Button("Test Image") |
|
|
|
with gr.Column(): |
|
output_text = gr.Textbox( |
|
label="Prediction Results", |
|
lines=6, |
|
interactive=False |
|
) |
|
|
|
test_button.click( |
|
fn=test_model, |
|
inputs=[test_image], |
|
outputs=[output_text] |
|
) |
|
|
|
|
|
submit_btn.click( |
|
fn=process_images, |
|
inputs=[export_output], |
|
outputs=[download_button_tflite] |
|
) |
|
|
|
|
|
demo.load(js=""" |
|
function() { |
|
const style = document.createElement('style'); |
|
style.textContent = ` |
|
.small-gallery img { |
|
max-height: 150px !important; |
|
width: auto !important; |
|
object-fit: contain !important; |
|
} |
|
.small-gallery .grid-container { |
|
gap: 10px !important; |
|
} |
|
`; |
|
document.head.appendChild(style); |
|
} |
|
""") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tag_category_input.submit( |
|
add_tag_category, |
|
tag_category_input, |
|
[tag_category_status, image_upload, tag_category_input] |
|
).then( |
|
get_tag_category_choices, |
|
None, |
|
tag_category_selector |
|
) |
|
|
|
tag_category_selector.change( |
|
show_category_images, |
|
tag_category_selector, |
|
[image_gallery, image_upload] |
|
) |
|
|
|
upload_images_btn.click( |
|
upload_images_for_tag, |
|
[tag_category_selector, image_upload], |
|
[tag_category_status, image_gallery, export_output] |
|
) |
|
|
|
clear_upload_btn.click( |
|
clear_uploaded_images, |
|
[], |
|
[image_upload, image_gallery] |
|
) |
|
|
|
|
|
|
|
|
|
demo.launch() |