Spaces:
Build error
Build error
File size: 3,927 Bytes
826b125 4e528fa 37ab326 4e528fa daf9957 4e528fa 37ab326 4e528fa e3f36c1 daf9957 4e528fa e3f36c1 4e528fa e3f36c1 4e528fa e3f36c1 4e528fa daf9957 4e528fa 37ab326 daf9957 a2fee98 4e528fa |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
import os
import gradio as gr
from huggingface_hub import snapshot_download
import tensorflow as tf
from PIL import Image
import numpy as np
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
# Path to the label map
PATH_TO_LABELS = 'data/label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
def pil_image_as_numpy_array(pilimg):
img_array = tf.keras.utils.img_to_array(pilimg)
img_array = np.expand_dims(img_array, axis=0)
return img_array
def load_model(repo_id):
download_dir = snapshot_download(repo_id)
saved_model_dir = os.path.join(download_dir, "saved_model")
detection_model = tf.saved_model.load(saved_model_dir)
return detection_model
# List of sample images in 'test_samples' folder
sample_images = [file for file in os.listdir('test_samples') if file.endswith(('.png', '.jpg', '.jpeg'))]
sample_images.sort() # Optional: sort the file names
def predict(uploaded_img, use_sample, sample_choice):
if use_sample:
# Use the selected sample image
pilimg = Image.open(os.path.join('test_samples', sample_choice))
else:
# Use the uploaded image
pilimg = uploaded_img
image_np = pil_image_as_numpy_array(pilimg)
return predict_combined_models(image_np, detection_model_fin, detection_model_ini)
def predict_combined_models(image_np, model1, model2):
# Process with first model
results1 = model1(image_np)
result1 = {key:value.numpy() for key,value in results1.items()}
# Process with second model
results2 = model2(image_np)
result2 = {key:value.numpy() for key,value in results2.items()}
# Modify category index for each model
category_index_model1 = {k: {**v, 'name': v['name'] + ' - Model 1'} for k, v in category_index.items()}
category_index_model2 = {k: {**v, 'name': v['name'] + ' - Model 2'} for k, v in category_index.items()}
# Visualization for model 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections[0],
result1['detection_boxes'][0],
(result1['detection_classes'][0]).astype(int),
result1['detection_scores'][0],
category_index_model1,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.60,
agnostic_mode=False,
line_thickness=2)
# Visualization for model 2
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections[0],
result2['detection_boxes'][0],
(result2['detection_classes'][0]).astype(int),
result2['detection_scores'][0],
category_index_model2,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.60,
agnostic_mode=False,
line_thickness=2)
# Combine and return final image
result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
return result_pil_img
# Load your models
REPO_ID1 = "dtyago/23a052w-iti107-assn2_tfodmodel"
REPO_ID2 = "dtyago/23a052w-iti107-assn2_tfodmodel_run1"
detection_model_fin = load_model(REPO_ID1)
detection_model_ini = load_model(REPO_ID2)
# Gradio interface
gr.Interface(
fn=predict,
inputs=[
gr.Image(type="pil"), # Image upload
gr.Checkbox(label="Use a sample image instead"), # Checkbox to choose a sample
gr.Dropdown(choices=sample_images, label="Select a Sample Image") # Dropdown to select a sample
],
outputs=gr.Image(type="pil"),
title="Gramophone & Veena Detection with TensorFlow Models",
description="This app uses TensorFlow models to detect objects in images. Upload an image to see the detected gramophone and veena. Bounding boxes labeled Run1 = - Model 2, Run2= - Model 1"
).launch(share=True)
|