Spaces:
Sleeping
Sleeping
Download app.py from crockrocks/object_detection: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/crockrocks/object_detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/crockrocks/object_detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/crockrocks/object_detection/resolve/main/app.py
1.05 kB
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| from PIL import Image | |
| def detect_objects(input_image): | |
| # Convert Gradio input image (PIL Image) to a NumPy array | |
| input_image = np.array(input_image) | |
| # Perform object detection on the input image | |
| gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) | |
| blur = cv2.GaussianBlur(gray, (11, 11), 0) | |
| canny = cv2.Canny(blur, 30, 150, 3) | |
| dilated = cv2.dilate(canny, (1, 1), iterations=0) | |
| (cnt, hierarchy) = cv2.findContours( | |
| dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) | |
| rgb = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB) | |
| cv2.drawContours(rgb, cnt, -1, (0, 255, 0), 2) | |
| # Convert the image back to PIL format | |
| output_image = Image.fromarray(rgb) | |
| return output_image, f"Objects in the image: {len(cnt)}" | |
| # Define a Gradio interface | |
| input_image = gr.Image() | |
| output_image = gr.Image() | |
| output_text = gr.Text() | |
| gr.Interface( | |
| fn=detect_objects, | |
| inputs=input_image, | |
| outputs=[output_image, output_text], | |
| live=True, | |
| ).launch() |