Spaces:
Sleeping
Sleeping
Initial commit
Browse files- app.py +91 -0
- requirements.txt +5 -0
- yolov8n.pt +3 -0
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from ultralytics import YOLO # YOLOv8 from Ultralytics
|
6 |
+
|
7 |
+
# Load YOLOv8 model (pre-trained on COCO dataset)
|
8 |
+
model = YOLO("yolov8n.pt") # Using the "nano" model (fast & lightweight)
|
9 |
+
|
10 |
+
#apply smoothing using OpenCV's medianBlur
|
11 |
+
def smooth_image(image):
|
12 |
+
image = np.array(image) # Convert PIL image to NumPy array
|
13 |
+
smoothed = cv2.medianBlur(image, 15) # Apply median blur with kernel size 5
|
14 |
+
return Image.fromarray(smoothed) # Convert back to PIL image
|
15 |
+
|
16 |
+
#apply Erosion Morphological Transformation
|
17 |
+
def erode_image(image):
|
18 |
+
image = np.array(image)
|
19 |
+
kernel = np.ones((3, 3), np.uint8) # Define a 3x3 kernel
|
20 |
+
eroded = cv2.erode(image, kernel, iterations=1) # Apply erosion
|
21 |
+
return Image.fromarray(eroded) # Convert back to PIL image
|
22 |
+
|
23 |
+
#apply image segmentation using Otsu's Thresholding
|
24 |
+
def segment_image(image):
|
25 |
+
image = np.array(image)
|
26 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Convert to grayscale
|
27 |
+
_, segmented = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Apply Otsu's thresholding
|
28 |
+
return Image.fromarray(segmented) # Convert back to PIL image
|
29 |
+
|
30 |
+
#apply Fourier Transform and display magnitude spectrum
|
31 |
+
def fourier_transform(image):
|
32 |
+
image = np.array(image)
|
33 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # Convert to grayscale
|
34 |
+
dft = np.fft.fft2(gray) # Compute Fourier Transform
|
35 |
+
dft_shift = np.fft.fftshift(dft) # Shift zero frequency to center
|
36 |
+
magnitude_spectrum = 20 * np.log(np.abs(dft_shift) + 1) # Compute magnitude spectrum
|
37 |
+
magnitude_spectrum = np.uint8(255 * (magnitude_spectrum / np.max(magnitude_spectrum))) # Normalize for display
|
38 |
+
return Image.fromarray(magnitude_spectrum)
|
39 |
+
|
40 |
+
def detect_objects(image):
|
41 |
+
image = np.array(image) # Convert PIL image to NumPy array
|
42 |
+
|
43 |
+
# Perform object detection
|
44 |
+
results = model(image)
|
45 |
+
|
46 |
+
# Process detections
|
47 |
+
for result in results:
|
48 |
+
boxes = result.boxes.xyxy # Bounding boxes (x1, y1, x2, y2)
|
49 |
+
confidences = result.boxes.conf # Confidence scores
|
50 |
+
class_ids = result.boxes.cls.int().tolist() # Class labels
|
51 |
+
|
52 |
+
for box, conf, class_id in zip(boxes, confidences, class_ids):
|
53 |
+
x1, y1, x2, y2 = map(int, box.tolist())
|
54 |
+
label = f"{model.names[class_id]} ({conf:.2f})"
|
55 |
+
|
56 |
+
# Draw bounding box & label
|
57 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
58 |
+
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
59 |
+
|
60 |
+
return Image.fromarray(image) # Convert back to PIL Image for Gradio
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
def create_interface():
|
65 |
+
with gr.Blocks() as demo:
|
66 |
+
with gr.Row():
|
67 |
+
with gr.Column():
|
68 |
+
image_input = gr.Image(label="Upload Image", type="pil")
|
69 |
+
with gr.Column():
|
70 |
+
output_image = gr.Image(label="Processed Image", type="pil")
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
smoothing_button = gr.Button("Smoothing/ Blurring")
|
74 |
+
morphological_transform_button = gr.Button("Morphological Transformations")
|
75 |
+
fourier_transform_button = gr.Button("Fourier Transform")
|
76 |
+
segmentation_button = gr.Button("Segmentation")
|
77 |
+
object_recognition_button = gr.Button("Object Recognition (YOLO)")
|
78 |
+
|
79 |
+
# Link buttons to their respective functions
|
80 |
+
smoothing_button.click(smooth_image, inputs=image_input, outputs=output_image)
|
81 |
+
morphological_transform_button.click(erode_image, inputs=image_input, outputs=output_image)
|
82 |
+
fourier_transform_button.click(fourier_transform, inputs=image_input, outputs=output_image)
|
83 |
+
segmentation_button.click(segment_image, inputs=image_input, outputs=output_image)
|
84 |
+
object_recognition_button.click(detect_objects, inputs=image_input, outputs=output_image)
|
85 |
+
|
86 |
+
return demo
|
87 |
+
|
88 |
+
|
89 |
+
# Launch the Gradio app
|
90 |
+
app = create_interface()
|
91 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==5.23.3
|
2 |
+
numpy==2.2.4
|
3 |
+
opencv_python==4.11.0.86
|
4 |
+
Pillow==11.1.0
|
5 |
+
ultralytics==8.3.100
|
yolov8n.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
|
3 |
+
size 6549796
|