Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google.colab import drive
|
| 2 |
+
from google.colab.patches import cv2_imshow
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
def detect_sky(image_path):
|
| 8 |
+
|
| 9 |
+
img = cv2.imread(image_path)
|
| 10 |
+
|
| 11 |
+
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
| 12 |
+
|
| 13 |
+
h, s, v = cv2.split(hsv_img)
|
| 14 |
+
v = cv2.equalizeHist(v)
|
| 15 |
+
mer_img = cv2.merge((h, s, v))
|
| 16 |
+
|
| 17 |
+
iLow = np.array([100, 43, 46])
|
| 18 |
+
iHigh = np.array([124, 255, 255])
|
| 19 |
+
|
| 20 |
+
rng_img = cv2.inRange(mer_img, iLow, iHigh)
|
| 21 |
+
|
| 22 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
|
| 23 |
+
erode_img = cv2.erode(rng_img, kernel)
|
| 24 |
+
dlt_img = cv2.dilate(erode_img, kernel)
|
| 25 |
+
|
| 26 |
+
ret, mask = cv2.threshold(dlt_img, 150, 255, cv2.THRESH_BINARY)
|
| 27 |
+
notmask = cv2.bitwise_not(mask)
|
| 28 |
+
frontpic = cv2.bitwise_and(img, img, mask=notmask)
|
| 29 |
+
|
| 30 |
+
# Convert the image to RGB format (Gradio expects RGB)
|
| 31 |
+
result_rgb = cv2.cvtColor(frontpic, cv2.COLOR_BGR2RGB)
|
| 32 |
+
|
| 33 |
+
# Encode the image as bytes
|
| 34 |
+
#_, result_bytes = cv2.imencode('.png', result_rgb)
|
| 35 |
+
# Convert bytes to a NumPy array
|
| 36 |
+
return result_rgb
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(fn=detect_sky,inputs=gr.Image(type="filepath"),outputs="image")
|
| 40 |
+
|
| 41 |
+
# Launch the Gradio interface in Colab
|
| 42 |
+
demo.launch()
|