{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "22553677-3a88-4f35-a99c-a6ec7375ef7c", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/yunkeli/anaconda3/lib/python3.10/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import cv2\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "import gradio as gr\n", "\n", "# Helper function to detect sky condition and get the HSV range\n", "def detect_sky_color(hsv_image):\n", " # Crop the image to the upper half, because we assume the sky is always on the upper half of the image\n", " height = hsv_image.shape[0]\n", " upper_half_image = hsv_image[:height//2, :]\n", "\n", " # Define color ranges in HSV\n", " blue_lower = np.array([46, 17, 148], np.uint8)\n", " blue_upper = np.array([154, 185, 249], np.uint8)\n", " orange_lower = np.array([10, 100, 100], np.uint8)\n", " orange_upper = np.array([25, 183, 254], np.uint8)\n", " pale_lower = np.array([0, 0, 129], np.uint8)\n", " pale_upper = np.array([171, 64, 225], np.uint8)\n", "\n", " # Create masks for colors\n", " blue_mask = cv2.inRange(upper_half_image, blue_lower, blue_upper)\n", " orange_mask = cv2.inRange(upper_half_image, orange_lower, orange_upper)\n", " pale_mask = cv2.inRange(upper_half_image, pale_lower, pale_upper)\n", "\n", " # Calculate the percentage of cropped image covered by each color\n", " blue_percentage = np.sum(blue_mask > 0) / (upper_half_image.shape[0] * upper_half_image.shape[1]) * 100\n", " orange_percentage = np.sum(orange_mask > 0) / (upper_half_image.shape[0] * upper_half_image.shape[1]) * 100\n", " pale_percentage = np.sum(pale_mask > 0) / (upper_half_image.shape[0] * upper_half_image.shape[1]) * 100\n", "\n", " # Determine the predominant color in the upper half\n", " max_color = max(blue_percentage, orange_percentage, pale_percentage)\n", " if max_color == blue_percentage:\n", " return blue_lower, blue_upper\n", " elif max_color == orange_percentage:\n", " return orange_lower, orange_upper\n", " else:\n", " return pale_lower, pale_upper\n", "\n", "\n", "# Main function to process image and display sky masks\n", "def sky_segmentation(uploaded_image):\n", " # Read the image\n", " image = cv2.imread(uploaded_image)\n", "\n", " # Convert to HSV image\n", " hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n", "\n", " # Determine HSV range based on helper function\n", " (hsv_lower, hsv_upper) = detect_sky_color(hsv)\n", "\n", " # Use hsv_lower and hsv_upper to create a mask, which isolates the sky region\n", " mask_initial = cv2.inRange(hsv, hsv_lower, hsv_upper)\n", "\n", " # Apply morphological operations to fine-tune the mask\n", " kernel = np.ones((3,3), np.uint8)\n", " mask_fine_tuned = cv2.erode(mask_initial, kernel, iterations=1)\n", " mask_fine_tuned = cv2.dilate(mask_fine_tuned, kernel, iterations=1)\n", "\n", " # Perform connected component analysis\n", " num_labels, labels_im = cv2.connectedComponents(mask_fine_tuned)\n", "\n", " # Create an array to hold the size of each component\n", " sizes = np.bincount(labels_im.flatten())\n", "\n", " # Set the size of the background (label 0) to zero\n", " sizes[0] = 0\n", "\n", " # Find the largest component\n", " max_label = np.argmax(sizes)\n", "\n", " # Create a mask with only the largest component\n", " sky_mask = np.zeros_like(mask_fine_tuned)\n", " sky_mask[labels_im == max_label] = 255 \n", " \n", " return sky_mask\n", "\n", "\n", "# Create a Gradio demo\n", "demo = gr.Interface(sky_segmentation, gr.Image(type='filepath'), \"image\")\n", "if __name__ == \"__main__\":\n", " demo.launch()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1e4ad199-ca35-48c0-9889-66fa874c4d9d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }