Spaces:
Runtime error
Runtime error
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| def detect_sky(image_path): | |
| img = cv2.imread(image_path) | |
| hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
| h, s, v = cv2.split(hsv_img) | |
| v = cv2.equalizeHist(v) | |
| mer_img = cv2.merge((h, s, v)) | |
| iLow = np.array([100, 43, 46]) | |
| iHigh = np.array([124, 255, 255]) | |
| rng_img = cv2.inRange(mer_img, iLow, iHigh) | |
| kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10)) | |
| erode_img = cv2.erode(rng_img, kernel) | |
| dlt_img = cv2.dilate(erode_img, kernel) | |
| ret, mask = cv2.threshold(dlt_img, 150, 255, cv2.THRESH_BINARY) | |
| notmask = cv2.bitwise_not(mask) | |
| frontpic = cv2.bitwise_and(img, img, mask=notmask) | |
| # Convert the image to RGB format (Gradio expects RGB) | |
| result_rgb = cv2.cvtColor(frontpic, cv2.COLOR_BGR2RGB) | |
| # Encode the image as bytes | |
| #_, result_bytes = cv2.imencode('.png', result_rgb) | |
| # Convert bytes to a NumPy array | |
| return result_rgb | |
| demo = gr.Interface(fn=detect_sky,inputs=gr.Image(type="filepath"),outputs="image") | |
| # Launch the Gradio interface in Colab | |
| demo.launch() |