devisionx/auto-annotation-segmentation
4
1import base642import io3import cv24import requests5import json6import gradio as gr7import os8from PIL import Image9import numpy as np10from PIL import ImageOps11 12# Accessing a specific environment variable13api_key = os.environ.get('devisionx')14 15# Checking if the environment variable exists16if not api_key:17 print("devisionx environment variable is not set.")18 exit()19 20# Define a function to call the API and get the results21 22def base64str_to_PILImage(base64str):23 base64_img_bytes = base64str.encode('utf-8')24 base64bytes = base64.b64decode(base64_img_bytes)25 bytesObj = io.BytesIO(base64bytes)26 return ImageOps.exif_transpose(Image.open(bytesObj))27 28def get_results(image, prompt,segment):29 threshold = 0.530 31 # Convert the NumPy array to PIL image32 image = Image.fromarray(image)33 34 # Convert the image to base64 string35 with io.BytesIO() as output:36 image.save(output, format="JPEG")37 base64str = base64.b64encode(output.getvalue()).decode("utf-8")38 39 # Prepare the payload (Adjust this part according to the API requirements)40 #payload = json.dumps({"base64str": base64str, "classes": prompt})41 task_="0"42 if segment == "Segmentation":43 task_="1"44 payload =json.dumps({45 "base64str": base64str,46 "classes": prompt,47 "segment": task_ })48 # Prepare the payload (Adjust this part according to the API requirements)49 # Send the request to the API50 response = requests.put(api_key, data=payload)51 52 # Parse the JSON response53 data = response.json()54 print(response.status_code)55 print(data)56 57 # Access the values (Adjust this part according to the API response format)58 output_image_base64 = data['firstName'] # Assuming the API returns the output image as base6459 60 61 # Convert the output image from base64 to PIL and then to NumPy array62 output_image = base64str_to_PILImage(output_image_base64)63 output_image = np.array(output_image)64 65 return output_image66 67 68# Define the input components for Gradio (adding a new input for the prompt)69# image_input = gr.inputs.Image()70# text_input = gr.inputs.Textbox(label="Prompt") # New input for the text prompt71 72 73# # Define the output components for Gradio (including both image and text)74# outputs = gr.Image(type="numpy", label="Output Image")75 76# Define the text description within an HTML <div> element77description_html = """78<!DOCTYPE html>79<html>80<head>81 <title>Tuba AI Auto-Annotation </title>82</head>83<body>84 <h1>Tuba AI Auto-Annotation ๐</h1>85 <h2>Saving Time, Bounding Boxes & Polygons at a Time </h2>86 <h2>Introduction</h2>87 <p>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.</p>88 <h2>Meet Tuba.AI - Your Partner in Vision</h2>89 <h3>What is Tuba?</h3>90 <p>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.</p>91 <ul>92 <li>No-Code/Low-Code: Say goodbye to complex coding. Tuba's user-friendly interface makes it accessible to everyone.</li>93 <li>Labeling Made Easy: Annotate your data effortlessly with Tuba's intuitive tools.</li>94 <li>Faster Deployment: Deploy your AI models with ease, whether you're building a standalone app or integrating within an existing one.</li>95 <li>State-of-the-Art Technology: Tuba is powered by the latest AI tech and follows production-ready standards.</li>96 </ul>97 <h2>The DevisionX Auto-Annotation</h2>98 <p>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:</p>99 <ul>100 <li>Time Saver: Say goodbye to hours of manual annotation. Let our model do the heavy lifting.</li>101 <li>Annotation Formats: It speaks the language of YOLO and COCO, making it versatile for various projects.</li>102 <li>Human Assistance: While it's incredibly efficient, it also respects human creativity and can be your reliable assistant.</li>103 </ul>104 <h2>Let's Build Together</h2>105 <p>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.</p>106 <p>Get started today and be a part of the future of vision.</p>107</body>108</html>109 110 111 112"""113title = "autoannotation"114 115description = "This is a project description. It demonstrates how to use Gradio with an image and text input to interact with an API."116 117import os118examples = [119 ["traffic.jpg", 'person,car,traffic sign,traffic light', "Segmentation"], # Example with "Segmentation" selected120 ["3000.jpeg", 'person,car,traffic sign,traffic light', "Detection"], # Example with "Detection" selected121]122 123 124 125# Create a Blocks object and use it as a context manager126with gr.Blocks() as demo:127 gr.Markdown(128 """129 <div style="text-align: center;">130 <h1>Tuba Autoannotation Demo</h1>131 <h3>A prompt based controllable model for auto annotation (Detection and Segmentation) </h3>132 <h3>Saving Time, Bounding Boxes & Polygons at a Time </h3>133 Powered by <a href="https://Tuba.ai">Tuba</a>134 </div>135 """136 )137 # Define the input components and add them to the layout138 139 with gr.Row():140 image_input = gr.Image()141 output = gr.Image(type="numpy", label="Output Image")142 143 # Define the output component and add it to the layout144 with gr.Row():145 text_input = gr.Textbox(label="Prompt")146 147 # text_input = gr.inputs.Textbox(label="Prompt")148 with gr.Row():149 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")150 #segment_checkbox = gr.inputs.Checkbox(label="Segment", default=False)151 with gr.Row():152 button = gr.Button("Run")153 154 # Define the event listener that connects the input and output components and triggers the function155 button.click(fn=get_results, inputs=[image_input, text_input, segment_checkbox], outputs=output, api_name="get_results")156 # Add the description below the layout157 gr.Examples(158 fn=get_results,159 examples=examples,160 inputs=[image_input, text_input,segment_checkbox],161 outputs=[output]162 )163 gr.Markdown(description_html)164# Launch the app165demo.launch(share=False)