phitran/image-processing
0
1import gradio as gr2import cv23import numpy as np4from PIL import Image5from ultralytics import YOLO # YOLOv8 from Ultralytics6 7# Load YOLOv8 model (pre-trained on COCO dataset)8model = YOLO("yolov8n.pt") # Using the "nano" model (fast & lightweight)9 10#apply smoothing using OpenCV's medianBlur11def smooth_image(image):12 image = np.array(image) # Convert PIL image to NumPy array13 smoothed = cv2.medianBlur(image, 15) # Apply median blur with kernel size 514 return Image.fromarray(smoothed) # Convert back to PIL image15 16#apply Erosion Morphological Transformation17def erode_image(image):18 image = np.array(image)19 kernel = np.ones((3, 3), np.uint8) # Define a 3x3 kernel20 eroded = cv2.erode(image, kernel, iterations=1) # Apply erosion21 return Image.fromarray(eroded) # Convert back to PIL image22 23#apply image segmentation using Otsu's Thresholding24def segment_image(image):25 image = np.array(image)26 gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Convert to grayscale27 _, segmented = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Apply Otsu's thresholding28 return Image.fromarray(segmented) # Convert back to PIL image29 30#apply Fourier Transform and display magnitude spectrum31def fourier_transform(image):32 image = np.array(image)33 gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Convert to grayscale34 dft = np.fft.fft2(gray) # Compute Fourier Transform35 dft_shift = np.fft.fftshift(dft) # Shift zero frequency to center36 magnitude_spectrum = 20 * np.log(np.abs(dft_shift) + 1) # Compute magnitude spectrum37 magnitude_spectrum = np.uint8(255 * (magnitude_spectrum / np.max(magnitude_spectrum))) # Normalize for display38 return Image.fromarray(magnitude_spectrum)39 40def detect_objects(image):41 image = np.array(image) # Convert PIL image to NumPy array42 43 # Perform object detection44 results = model(image)45 46 # Process detections47 for result in results:48 boxes = result.boxes.xyxy # Bounding boxes (x1, y1, x2, y2)49 confidences = result.boxes.conf # Confidence scores50 class_ids = result.boxes.cls.int().tolist() # Class labels51 52 for box, conf, class_id in zip(boxes, confidences, class_ids):53 x1, y1, x2, y2 = map(int, box.tolist())54 label = f"{model.names[class_id]} ({conf:.2f})"55 56 # Draw bounding box & label57 cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)58 cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)59 60 return Image.fromarray(image) # Convert back to PIL Image for Gradio61 62 63 64def create_interface():65 with gr.Blocks() as demo:66 with gr.Row():67 with gr.Column():68 image_input = gr.Image(label="Upload Image", type="pil")69 with gr.Column():70 output_image = gr.Image(label="Processed Image", type="pil")71 72 with gr.Row():73 smoothing_button = gr.Button("Smoothing/ Blurring")74 morphological_transform_button = gr.Button("Morphological Transformations")75 fourier_transform_button = gr.Button("Fourier Transform")76 segmentation_button = gr.Button("Segmentation")77 object_recognition_button = gr.Button("Object Recognition (YOLO)")78 79 # Link buttons to their respective functions80 smoothing_button.click(smooth_image, inputs=image_input, outputs=output_image)81 morphological_transform_button.click(erode_image, inputs=image_input, outputs=output_image)82 fourier_transform_button.click(fourier_transform, inputs=image_input, outputs=output_image)83 segmentation_button.click(segment_image, inputs=image_input, outputs=output_image)84 object_recognition_button.click(detect_objects, inputs=image_input, outputs=output_image)85 86 return demo87 88 89# Launch the Gradio app90app = create_interface()91app.launch()92 