Spaces:
Sleeping
Sleeping
File size: 2,912 Bytes
3667d90 5677c31 3667d90 41031b1 3667d90 41031b1 3667d90 659d472 6c07c84 5677c31 659d472 3667d90 fe023be 6317bd0 fe023be 1668d94 3667d90 e741174 5677c31 fe023be 41031b1 fe023be 3667d90 77e0e96 7e042c5 1899e57 fe023be 3667d90 |
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 |
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
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():
model_dir = 'saved_model'
detection_model = tf.saved_model.load(str(model_dir))
return detection_model
def predict(image_np):
image_np = pil_image_as_numpy_array(image_np)
image_np = np.expand_dims(image_np, axis=0)
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
def predict2(pilimg):
image = load_image_into_numpy_array(pilimg)
return predict(image)
detection_model = load_model()
# Specify paths to example images
example_image_paths = ["test_1.jpg"]
# Create a list of example inputs and outputs using a for loop
example_inputs = [Image.open(path) for path in example_image_paths]
example_outputs = [predict2(input_image) for input_image in example_image_paths]
# Create the Gradio interface with examples using a for loop
examples = [[example_inputs[i], example_outputs[i]] for i in range(len(example_inputs))]
iface = gr.Interface(fn=predict,
inputs=gr.Image(label='Upload an expressway image', type="pil"),
outputs=gr.Image(type="pil"),
title='Blue and Yellow Taxi detection in live expressway traffic conditions (data.gov.sg)',
example=examples
).launch(share=True)
|