Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141import gradio as grimport osfrom io import BytesIOfrom PIL import Image, ImageDraw, ImageFontfrom PIL import ImageColorimport jsonimport google.generativeai as genaifrom google.generativeai import typesfrom dotenv import load_dotenvfrom IPython.display import display# 1. SETUP API KEY# ----------------load_dotenv()api_key = os.getenv("Gemini_API_Key")# Configure the Google AI librarygenai.configure(api_key=api_key)# 2. DEFINE MODEL AND INSTRUCTIONSbounding_box_system_instructions = """Return bounding boxes as a JSON array with labels. Never return masks or code fencing. Limit to 25 objects.If an object is present multiple times, name them according to their unique characteristic (colors, size, position, unique characteristics, etc..)."""model = genai.GenerativeModel( model_name='gemini-2.5-flash', system_instruction=bounding_box_system_instructions)generation_config = genai.types.GenerationConfig(temperature=0.5,)def generate_bounding_boxes(prompt, image):image = image.resize((1024, int(1024 * image.height / image.width)))response = model.generate_content([prompt, image], generation_config=generation_config)bounding_boxes = parse_json(response.text)img=plot_bounding_boxes(image, bounding_boxes)return imgdef parse_json(json_output):lines = json_output.splitlines()for i, line in enumerate(lines):if line == "```json":json_output = "\n".join(lines[i+1:]) # Remove everything before "```json"json_output = json_output.split("```")[0] # Remove everything after the closing "```"breakreturn json_outputdef plot_bounding_boxes(im, bounding_boxes):"""Plots bounding boxes on an image with labels."""additional_colors = [colorname for (colorname, colorcode) in ImageColor.colormap.items()]im = im.copy()width, height = im.sizedraw = ImageDraw.Draw(im)colors = ['red', 'green', 'blue', 'yellow', 'orange', 'pink', 'purple', 'cyan','lime', 'magenta', 'violet', 'gold', 'silver'] + additional_colorstry:# Use a default font if NotoSansCJK is not availabletry:font = ImageFont.load_default()except OSError:print("NotoSansCJK-Regular.ttc not found. Using default font.")font = ImageFont.load_default()bounding_boxes_json = json.loads(bounding_boxes)for i, bounding_box in enumerate(bounding_boxes_json):color = colors[i % len(colors)]abs_y1 = int(bounding_box["box_2d"][0] / 1000 * height)abs_x1 = int(bounding_box["box_2d"][1] / 1000 * width)abs_y2 = int(bounding_box["box_2d"][2] / 1000 * height)abs_x2 = int(bounding_box["box_2d"][3] / 1000 * width)if abs_x1 > abs_x2:abs_x1, abs_x2 = abs_x2, abs_x1if abs_y1 > abs_y2:abs_y1, abs_y2 = abs_y2, abs_y1# Draw bounding box and labeldraw.rectangle(((abs_x1, abs_y1), (abs_x2, abs_y2)), outline=color, width=4)if "label" in bounding_box:draw.text((abs_x1 + 8, abs_y1 + 6), bounding_box["label"], fill=color, font=font)except Exception as e:print(f"Error drawing bounding boxes: {e}")return imdef gradio_interface():"""Gradio app interface for bounding box generation with example pairs."""# Example image + prompt pairsexamples = [["cookies.jpg", "Detect the cookies and label their types."],["messed_room.jpg", "Find the unorganized item and suggest action in label in the image to fix them."],["yoga.jpg", "Show the different yoga poses and name them."],["zoom_face.png", "Label the tired faces in the image."]]with gr.Blocks(gr.themes.Glass(secondary_hue= "rose")) as demo:gr.Markdown("# Gemini Bounding Box Generator")with gr.Row():with gr.Column():gr.Markdown("### Input Section")input_image = gr.Image(type="pil", label="Input Image")input_prompt = gr.Textbox(lines=2, label="Input Prompt", placeholder="Describe what to detect.")submit_btn = gr.Button("Generate")with gr.Column():gr.Markdown("### Output Section")output_image = gr.Image(type="pil", label="Output Image")#output_json = gr.Textbox(label="Bounding Boxes JSON")gr.Markdown("### Examples")gr.Examples(examples=examples,inputs=[input_image, input_prompt],label="Example Images with Prompts")# Event to generate bounding boxessubmit_btn.click(generate_bounding_boxes,inputs=[input_prompt, input_image],outputs=[output_image])return demoif __name__ == "__main__":app = gradio_interface()app.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141import gradio as grimport osfrom io import BytesIOfrom PIL import Image, ImageDraw, ImageFontfrom PIL import ImageColorimport jsonimport google.generativeai as genaifrom google.generativeai import typesfrom dotenv import load_dotenv# 1. SETUP API KEY# ----------------load_dotenv()api_key = os.getenv("Gemini_API_Key")# Configure the Google AI librarygenai.configure(api_key=api_key)# 2. DEFINE MODEL AND INSTRUCTIONSbounding_box_system_instructions = """Return bounding boxes as a JSON array with labels. Never return masks or code fencing. Limit to 25 objects.If an object is present multiple times, name them according to their unique characteristic (colors, size, position, unique characteristics, etc..)."""model = genai.GenerativeModel( model_name='gemini-2.5-flash', system_instruction=bounding_box_system_instructions)generation_config = genai.types.GenerationConfig(temperature=0.5,)def generate_bounding_boxes(prompt, image):image = image.resize((1024, int(1024 * image.height / image.width)))response = model.generate_content([prompt, image], generation_config=generation_config)bounding_boxes = parse_json(response.text)img=plot_bounding_boxes(image, bounding_boxes)return imgdef parse_json(json_output):lines = json_output.splitlines()for i, line in enumerate(lines):if line == "```json":json_output = "\n".join(lines[i+1:]) # Remove everything before "```json"json_output = json_output.split("```")[0] # Remove everything after the closing "```"breakreturn json_outputdef plot_bounding_boxes(im, bounding_boxes):"""Plots bounding boxes on an image with labels."""additional_colors = [colorname for (colorname, colorcode) in ImageColor.colormap.items()]im = im.copy()width, height = im.sizedraw = ImageDraw.Draw(im)colors = ['red', 'green', 'blue', 'yellow', 'orange', 'pink', 'purple', 'cyan','lime', 'magenta', 'violet', 'gold', 'silver'] + additional_colorstry:# Use a default font if NotoSansCJK is not availabletry:font = ImageFont.load_default()except OSError:print("NotoSansCJK-Regular.ttc not found. Using default font.")font = ImageFont.load_default()bounding_boxes_json = json.loads(bounding_boxes)for i, bounding_box in enumerate(bounding_boxes_json):color = colors[i % len(colors)]abs_y1 = int(bounding_box["box_2d"][0] / 1000 * height)abs_x1 = int(bounding_box["box_2d"][1] / 1000 * width)abs_y2 = int(bounding_box["box_2d"][2] / 1000 * height)abs_x2 = int(bounding_box["box_2d"][3] / 1000 * width)if abs_x1 > abs_x2:abs_x1, abs_x2 = abs_x2, abs_x1if abs_y1 > abs_y2:abs_y1, abs_y2 = abs_y2, abs_y1# Draw bounding box and labeldraw.rectangle(((abs_x1, abs_y1), (abs_x2, abs_y2)), outline=color, width=4)if "label" in bounding_box:draw.text((abs_x1 + 8, abs_y1 + 6), bounding_box["label"], fill=color, font=font)except Exception as e:print(f"Error drawing bounding boxes: {e}")return imdef gradio_interface():"""Gradio app interface for bounding box generation with example pairs."""# Example image + prompt pairsexamples = [["cookies.jpg", "Detect the cookies and label their types."],["messed_room.jpg", "Find the unorganized item and suggest action in label in the image to fix them."],["yoga.jpg", "Show the different yoga poses and name them."],["zoom_face.png", "Label the tired faces in the image."]]with gr.Blocks(gr.themes.Glass(secondary_hue= "rose")) as demo:gr.Markdown("# Gemini Bounding Box Generator")with gr.Row():with gr.Column():gr.Markdown("### Input Section")input_image = gr.Image(type="pil", label="Input Image")input_prompt = gr.Textbox(lines=2, label="Input Prompt", placeholder="Describe what to detect.")submit_btn = gr.Button("Generate")with gr.Column():gr.Markdown("### Output Section")output_image = gr.Image(type="pil", label="Output Image")#output_json = gr.Textbox(label="Bounding Boxes JSON")gr.Markdown("### Examples")gr.Examples(examples=examples,inputs=[input_image, input_prompt],label="Example Images with Prompts")# Event to generate bounding boxessubmit_btn.click(generate_bounding_boxes,inputs=[input_prompt, input_image],outputs=[output_image])return demoif __name__ == "__main__":app = gradio_interface()app.launch()
app.py+140-140