23A066X / app.py
23A066X's picture
Update app.py
5628e28
raw
history blame
No virus
2.24 kB
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
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 gradio as gr
import os
import cv2
def greet(name):
return "Hello " + name + "!!"
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()
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)
return img_array
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)
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=.6,
agnostic_mode=False,
line_thickness=2
)
result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
return result_pil_img
detection_model = load_model()
# Specify paths to example images
sample_images = [["br_61.jpg"], ["br_61.jpg"],
]
tab1 = gr.Interface(
fn=predict,
inputs=gr.Image(label='Upload an expressway image', type="pil"),
outputs=gr.Image(type="pil"),
title='Image Processing',
examples=sample_images
)
# Create a Multi Interface with Tabs
iface = gr.TabbedInterface([tab1], title='Cauliflower and Beetroot Detection via ssd_resnet50_v1_fpn_640x640_coco17_tpu-8')
# Launch the interface
iface.launch(share=True)