RedBottle13 commited on
Commit
6cfe3a5
·
verified ·
1 Parent(s): 9b308bc

Upload sky_segmentation.ipynb

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