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
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "22553677-3a88-4f35-a99c-a6ec7375ef7c",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stderr",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"/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",
|
| 14 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"name": "stdout",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"Running on local URL: http://127.0.0.1:7860\n",
|
| 22 |
+
"Running on public URL: https://a2d6699e89843c017c.gradio.live\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"data": {
|
| 29 |
+
"text/html": [
|
| 30 |
+
"<div><iframe src=\"https://a2d6699e89843c017c.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 31 |
+
],
|
| 32 |
+
"text/plain": [
|
| 33 |
+
"<IPython.core.display.HTML object>"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"output_type": "display_data"
|
| 38 |
+
}
|
| 39 |
+
],
|
| 40 |
+
"source": [
|
| 41 |
+
"import cv2\n",
|
| 42 |
+
"import numpy as np\n",
|
| 43 |
+
"from matplotlib import pyplot as plt\n",
|
| 44 |
+
"import gradio as gr\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"# Helper function to detect sky condition and get the HSV range\n",
|
| 47 |
+
"def detect_sky_color(hsv_image):\n",
|
| 48 |
+
" # Crop the image to the upper half, because we assume the sky is always on the upper half of the image\n",
|
| 49 |
+
" height = hsv_image.shape[0]\n",
|
| 50 |
+
" upper_half_image = hsv_image[:height//2, :]\n",
|
| 51 |
+
"\n",
|
| 52 |
+
" # Define color ranges in HSV\n",
|
| 53 |
+
" blue_lower = np.array([46, 17, 148], np.uint8)\n",
|
| 54 |
+
" blue_upper = np.array([154, 185, 249], np.uint8)\n",
|
| 55 |
+
" orange_lower = np.array([10, 100, 100], np.uint8)\n",
|
| 56 |
+
" orange_upper = np.array([25, 183, 254], np.uint8)\n",
|
| 57 |
+
" pale_lower = np.array([0, 0, 129], np.uint8)\n",
|
| 58 |
+
" pale_upper = np.array([171, 64, 225], np.uint8)\n",
|
| 59 |
+
"\n",
|
| 60 |
+
" # Create masks for colors\n",
|
| 61 |
+
" blue_mask = cv2.inRange(upper_half_image, blue_lower, blue_upper)\n",
|
| 62 |
+
" orange_mask = cv2.inRange(upper_half_image, orange_lower, orange_upper)\n",
|
| 63 |
+
" pale_mask = cv2.inRange(upper_half_image, pale_lower, pale_upper)\n",
|
| 64 |
+
"\n",
|
| 65 |
+
" # Calculate the percentage of cropped image covered by each color\n",
|
| 66 |
+
" blue_percentage = np.sum(blue_mask > 0) / (upper_half_image.shape[0] * upper_half_image.shape[1]) * 100\n",
|
| 67 |
+
" orange_percentage = np.sum(orange_mask > 0) / (upper_half_image.shape[0] * upper_half_image.shape[1]) * 100\n",
|
| 68 |
+
" pale_percentage = np.sum(pale_mask > 0) / (upper_half_image.shape[0] * upper_half_image.shape[1]) * 100\n",
|
| 69 |
+
"\n",
|
| 70 |
+
" # Determine the predominant color in the upper half\n",
|
| 71 |
+
" max_color = max(blue_percentage, orange_percentage, pale_percentage)\n",
|
| 72 |
+
" if max_color == blue_percentage:\n",
|
| 73 |
+
" return blue_lower, blue_upper\n",
|
| 74 |
+
" elif max_color == orange_percentage:\n",
|
| 75 |
+
" return orange_lower, orange_upper\n",
|
| 76 |
+
" else:\n",
|
| 77 |
+
" return pale_lower, pale_upper\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"\n",
|
| 80 |
+
"# Main function to process image and display sky masks\n",
|
| 81 |
+
"def sky_segmentation(uploaded_image):\n",
|
| 82 |
+
" # Read the image\n",
|
| 83 |
+
" image = cv2.imread(uploaded_image)\n",
|
| 84 |
+
"\n",
|
| 85 |
+
" # Convert to HSV image\n",
|
| 86 |
+
" hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n",
|
| 87 |
+
"\n",
|
| 88 |
+
" # Determine HSV range based on helper function\n",
|
| 89 |
+
" (hsv_lower, hsv_upper) = detect_sky_color(hsv)\n",
|
| 90 |
+
"\n",
|
| 91 |
+
" # Use hsv_lower and hsv_upper to create a mask, which isolates the sky region\n",
|
| 92 |
+
" mask_initial = cv2.inRange(hsv, hsv_lower, hsv_upper)\n",
|
| 93 |
+
"\n",
|
| 94 |
+
" # Apply morphological operations to fine-tune the mask\n",
|
| 95 |
+
" kernel = np.ones((3,3), np.uint8)\n",
|
| 96 |
+
" mask_fine_tuned = cv2.erode(mask_initial, kernel, iterations=1)\n",
|
| 97 |
+
" mask_fine_tuned = cv2.dilate(mask_fine_tuned, kernel, iterations=1)\n",
|
| 98 |
+
"\n",
|
| 99 |
+
" # Perform connected component analysis\n",
|
| 100 |
+
" num_labels, labels_im = cv2.connectedComponents(mask_fine_tuned)\n",
|
| 101 |
+
"\n",
|
| 102 |
+
" # Create an array to hold the size of each component\n",
|
| 103 |
+
" sizes = np.bincount(labels_im.flatten())\n",
|
| 104 |
+
"\n",
|
| 105 |
+
" # Set the size of the background (label 0) to zero\n",
|
| 106 |
+
" sizes[0] = 0\n",
|
| 107 |
+
"\n",
|
| 108 |
+
" # Find the largest component\n",
|
| 109 |
+
" max_label = np.argmax(sizes)\n",
|
| 110 |
+
"\n",
|
| 111 |
+
" # Create a mask with only the largest component\n",
|
| 112 |
+
" sky_mask = np.zeros_like(mask_fine_tuned)\n",
|
| 113 |
+
" sky_mask[labels_im == max_label] = 255 \n",
|
| 114 |
+
" \n",
|
| 115 |
+
" return sky_mask\n",
|
| 116 |
+
"\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"# Create a Gradio demo\n",
|
| 119 |
+
"demo = gr.Interface(sky_segmentation, gr.Image(type='filepath'), \"image\")\n",
|
| 120 |
+
"if __name__ == \"__main__\":\n",
|
| 121 |
+
" demo.launch(share=True)\n"
|
| 122 |
+
]
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"cell_type": "code",
|
| 126 |
+
"execution_count": null,
|
| 127 |
+
"id": "1e4ad199-ca35-48c0-9889-66fa874c4d9d",
|
| 128 |
+
"metadata": {},
|
| 129 |
+
"outputs": [],
|
| 130 |
+
"source": []
|
| 131 |
+
}
|
| 132 |
+
],
|
| 133 |
+
"metadata": {
|
| 134 |
+
"kernelspec": {
|
| 135 |
+
"display_name": "Python 3 (ipykernel)",
|
| 136 |
+
"language": "python",
|
| 137 |
+
"name": "python3"
|
| 138 |
+
},
|
| 139 |
+
"language_info": {
|
| 140 |
+
"codemirror_mode": {
|
| 141 |
+
"name": "ipython",
|
| 142 |
+
"version": 3
|
| 143 |
+
},
|
| 144 |
+
"file_extension": ".py",
|
| 145 |
+
"mimetype": "text/x-python",
|
| 146 |
+
"name": "python",
|
| 147 |
+
"nbconvert_exporter": "python",
|
| 148 |
+
"pygments_lexer": "ipython3",
|
| 149 |
+
"version": "3.10.9"
|
| 150 |
+
}
|
| 151 |
+
},
|
| 152 |
+
"nbformat": 4,
|
| 153 |
+
"nbformat_minor": 5
|
| 154 |
+
}
|