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 = '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() saved_model_dir = os.path.join(download_dir, "saved_model.pb") detection_model = tf.saved_model.load(saved_model_dir) return detection_model def predict(pilimg): image_np = pil_image_as_numpy_array(pilimg) return predict2(image_np) detection_model = load_model() # pil_image = Image.open(image_path) # image_arr = pil_image_as_numpy_array(pil_image) # predicted_img = predict(image_arr) # predicted_img.save('predicted.jpg') gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs=gr.Image(type="pil") ).launch(share=True) gr.Interface(fn=predict, inputs=[gr.Image(type="pil",label="Input Image"),gr.Textbox(placeholder="0.50",label="Set the confidence threshold (0.00-1.00)")], outputs=gr.Image(type="pil",label="Output Image"), title="Cauliflower and Beetroot Detection", description="Model: ssd_resnet50_v1_fpn_640x640_coco17_tpu-8", theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"), examples=[["test_samples/image489.png",0.55], ["test_samples/image825.png",0.55], ["test_samples/image833.png",0.55], ["test_samples/image846.png",0.55]] ).launch(share=True)