import base64 import io import cv2 import requests import json import gradio as gr import os from PIL import Image import numpy as np from PIL import ImageOps # Accessing a specific environment variable api_key = os.environ.get('devisionx') # Checking if the environment variable exists if not api_key: print("devisionx environment variable is not set.") exit() # Define a function to call the API and get the results def base64str_to_PILImage(base64str): base64_img_bytes = base64str.encode('utf-8') base64bytes = base64.b64decode(base64_img_bytes) bytesObj = io.BytesIO(base64bytes) return ImageOps.exif_transpose(Image.open(bytesObj)) def get_results(image, prompt,segment): threshold = 0.5 # Convert the NumPy array to PIL image image = Image.fromarray(image) # Convert the image to base64 string with io.BytesIO() as output: image.save(output, format="JPEG") base64str = base64.b64encode(output.getvalue()).decode("utf-8") # Prepare the payload (Adjust this part according to the API requirements) #payload = json.dumps({"base64str": base64str, "classes": prompt}) task_="0" if segment == "Segmentation": task_="1" payload =json.dumps({ "base64str": base64str, "classes": prompt, "segment": task_ }) # Prepare the payload (Adjust this part according to the API requirements) # Send the request to the API response = requests.put(api_key, data=payload) # Parse the JSON response data = response.json() print(response.status_code) print(data) # Access the values (Adjust this part according to the API response format) output_image_base64 = data['firstName'] # Assuming the API returns the output image as base64 # Convert the output image from base64 to PIL and then to NumPy array output_image = base64str_to_PILImage(output_image_base64) output_image = np.array(output_image) return output_image # Define the input components for Gradio (adding a new input for the prompt) # image_input = gr.inputs.Image() # text_input = gr.inputs.Textbox(label="Prompt") # New input for the text prompt # # Define the output components for Gradio (including both image and text) # outputs = gr.Image(type="numpy", label="Output Image") # Define the text description within an HTML
element description_html = """ Tuba AI Auto-Annotation

Tuba AI Auto-Annotation 🚀

Saving Time, Bounding Boxes & Polygons at a Time

Introduction

Welcome to the world of DevisionX, where AI meets vision to revolutionize annotation. Our mission is to make computer vision accessible to all, and this README is your gateway to understanding how our auto-annotation model can change the way you work.

Meet Tuba.AI - Your Partner in Vision

What is Tuba?

Tuba is the secret sauce behind DevisionX, your no-code/low-code companion for all things computer vision. It's your toolkit for labeling, training data, and deploying AI-vision applications faster and easier than ever before.

The DevisionX Auto-Annotation

Our auto-annotation model is a game-changer. It takes input text and images, weaving them together to generate precise bounding boxes. This AI marvel comes with a plethora of benefits:

Let's Build Together

We are here to redefine the way you approach computer vision. Join us in this exciting journey, where AI meets creativity, and innovation knows no bounds.

Get started today and be a part of the future of vision.

""" title = "autoannotation" description = "This is a project description. It demonstrates how to use Gradio with an image and text input to interact with an API." import os examples = [ ["traffic.jpg", 'person,car,traffic sign,traffic light', "Segmentation"], # Example with "Segmentation" selected ["3000.jpeg", 'person,car,traffic sign,traffic light', "Detection"], # Example with "Detection" selected ] # Create a Blocks object and use it as a context manager with gr.Blocks() as demo: gr.Markdown( """

Tuba Autoannotation Demo

A prompt based controllable model for auto annotation (Detection and Segmentation)

Saving Time, Bounding Boxes & Polygons at a Time

Powered by Tuba
""" ) # Define the input components and add them to the layout with gr.Row(): image_input = gr.Image() output = gr.Image(type="numpy", label="Output Image") # Define the output component and add it to the layout with gr.Row(): text_input = gr.Textbox(label="Prompt") # text_input = gr.inputs.Textbox(label="Prompt") with gr.Row(): segment_checkbox = gr.Radio(["Segmentation", "Detection"], value="file",label="Select Detection or Segmentation",info="Select Segmentation to extract Polygons or Detection to extract only the bounding boxes of of the desired objects automatically") #segment_checkbox = gr.inputs.Checkbox(label="Segment", default=False) with gr.Row(): button = gr.Button("Run") # Define the event listener that connects the input and output components and triggers the function button.click(fn=get_results, inputs=[image_input, text_input, segment_checkbox], outputs=output, api_name="get_results") # Add the description below the layout gr.Examples( fn=get_results, examples=examples, inputs=[image_input, text_input,segment_checkbox], outputs=[output] ) gr.Markdown(description_html) # Launch the app demo.launch(share=False)