Spaces:
Sleeping
Sleeping
import matplotlib.pyplot as plt | |
import numpy as np | |
from six import BytesIO | |
from PIL import Image | |
import tensorflow as tf | |
from object_detection.utils import label_map_util | |
from object_detection.utils import visualization_utils as viz_utils | |
from object_detection.utils import ops as utils_op | |
import tarfile | |
import wget | |
import gradio as gr | |
from huggingface_hub import snapshot_download | |
import os | |
import pathlib | |
REPO_ID = 'liewchooichin/hb' | |
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_image_into_numpy_array(path): | |
image = None | |
image_data = tf.io.gfile.GFile(path, 'rb').read() | |
image = Image.open(BytesIO(image_data)) | |
return pil_image_as_numpy_array(image) | |
def load_model(): | |
download_dir = snapshot_download(repo_id=REPO_ID) | |
print(f"{download_dir=}") | |
saved_model_dir = os.path.join(download_dir, "saved_model") | |
print(f"{saved_model_dir=}") | |
detection_model = tf.saved_model.load(saved_model_dir) | |
return detection_model | |
def predict(pilimg): | |
image_np = pil_image_as_numpy_array(pilimg) | |
results = detection_model(image_np) | |
# different object detection models have additional results | |
result = {key: value.numpy() for key, value in results.items()} | |
label_id_offset = 0 | |
image_np_with_detections = image_np.copy() | |
viz_utils.visualize_boxes_and_labels_on_image_array( | |
image_np_with_detections[0], | |
result['detection_boxes'][0], | |
(result['detection_classes'][0] + label_id_offset).astype(int), | |
result['detection_scores'][0], | |
category_index, | |
use_normalized_coordinates=True, | |
max_boxes_to_draw=200, | |
min_score_thresh=.60, | |
agnostic_mode=False, | |
line_thickness=2) | |
result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0]) | |
return result_pil_img | |
# My model in the HF repo | |
detection_model = load_model() | |
data_dir = "test_samples" # contain the samples | |
sample_dir = os.path.join(os.path.dirname(__file__), data_dir) | |
sample_files = list(pathlib.Path(sample_dir).glob("*.jpg")) | |
print(f"Sample files: {sample_files}") | |
examples = [ | |
sample_files[0], | |
sample_files[1], | |
sample_files[2], | |
sample_files[3], | |
] | |
title = "Detecting hamster and butterfly" | |
description = "Using TensorFlow Object Detection API." | |
gr.Interface( | |
title=title, | |
description=description, | |
fn=predict, | |
inputs=gr.Image(type="pil", sources=["upload", "clipboard"]), | |
outputs=gr.Image(type="pil", interactive=False), | |
examples=examples | |
).launch(share=True) | |