import gradio as gr import logging from roboflow import Roboflow from PIL import Image, ImageDraw, ImageFont, ImageFilter import os # Configure logging logging.basicConfig( level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler("debug.log"), logging.StreamHandler() ] ) # Roboflow and model configuration ROBOFLOW_API_KEY = "YOUR_ROBOFLOW_API_KEY" # Replace with your API key PROJECT_NAME = "model_verification_project" VERSION_NUMBER = 1 FONT_PATH = "./STEVEHANDWRITING-REGULAR.TTF" # Function to process image and overlay text def process_image(image, text): try: # Initialize Roboflow rf = Roboflow(api_key=ROBOFLOW_API_KEY) project = rf.workspace().project(PROJECT_NAME) model = project.version(VERSION_NUMBER).model # Save input image temporarily input_image_path = "/tmp/input_image.jpg" image.save(input_image_path) # Perform inference logging.debug("Performing inference on the image...") prediction = model.predict(input_image_path, confidence=40, overlap=30).json() logging.debug(f"Inference result: {prediction}") # Open the image for processing pil_image = image.convert("RGBA") # Iterate over detected objects for obj in prediction['predictions']: x1 = int(obj['x'] - obj['width'] / 2) y1 = int(obj['y'] - obj['height'] / 2) x2 = int(obj['x'] + obj['width'] / 2) y2 = int(obj['y'] + obj['height'] / 2) # Calculate dynamic font size box_width = x2 - x1 box_height = y2 - y1 font_size = int(min(box_width // len(text), box_height // 2) * 0.8) # Load font try: font = ImageFont.truetype(FONT_PATH, size=font_size) except Exception as e: logging.warning(f"Error loading font. Using default. {e}") font = ImageFont.load_default() # Calculate text position text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0)) text_draw = ImageDraw.Draw(text_layer) text_width, text_height = text_draw.textsize(text, font=font) text_x = x1 + (box_width - text_width) // 2 text_y = y1 + (box_height - text_height) // 2 # Draw text on a transparent layer text_draw.text((text_x, text_y), text, fill=(0, 0, 0, 180), font=font) # Apply slight blur and composite blurred_text_layer = text_layer.filter(ImageFilter.GaussianBlur(radius=1.0)) pil_image = Image.alpha_composite(pil_image, blurred_text_layer) # Save and return output image path output_image_path = "/tmp/output_image.png" pil_image.convert("RGB").save(output_image_path) return output_image_path except Exception as e: logging.error(f"Error during image processing: {e}") return None # Gradio interface function def gradio_inference(image, text): result_path = process_image(image, text) if result_path: return result_path, result_path, "Processing complete! Download the image below." return None, None, "An error occurred while processing the image. Please check the logs." # Gradio interface interface = gr.Interface( fn=gradio_inference, inputs=[ gr.Image(type="pil", label="Upload an Image"), gr.Textbox(label="Enter Text to Overlay") ], outputs=[ gr.Image(label="Processed Image Preview"), # Preview processed image gr.File(label="Download Processed Image"), # Download the image gr.Textbox(label="Status") # Status message ], title="Roboflow Detection with Text Overlay", description="Upload an image, enter text to overlay, and let the Roboflow model process the image. Preview or download the output image below.", allow_flagging="never" ) # Launch the Gradio app if __name__ == "__main__": interface.launch(share=True)