File size: 5,805 Bytes
782c482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{
 "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",
      "Running on public URL: https://a2d6699e89843c017c.gradio.live\n",
      "\n",
      "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"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://a2d6699e89843c017c.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "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(share=True)\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
}