Spaces:
Sleeping
Sleeping
Zai
commited on
Commit
·
acad9f5
1
Parent(s):
24e7122
line detection
Browse files- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +48 -0
- flagged/input_image/497a84a343b3f1e548bca2c7a19db9a72960b0fd/tmpluaqy_ap.png +0 -0
- flagged/log.csv +2 -0
- flagged/output/6e4d7bc7046277291635b69cd1c9655fe13a7564/tmptlbwx7ok.png +0 -0
- requirements.txt +3 -0
- test.py +55 -0
- test2.png +0 -0
- utils.py +72 -0
__pycache__/utils.cpython-310.pyc
ADDED
|
Binary file (2.78 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils import (
|
| 3 |
+
sobel_edge_detection,
|
| 4 |
+
canny_edge_detection,
|
| 5 |
+
hough_lines,
|
| 6 |
+
laplacian_edge_detection,
|
| 7 |
+
contours_detection,
|
| 8 |
+
prewitt_edge_detection,
|
| 9 |
+
gradient_magnitude,
|
| 10 |
+
corner_detection,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def predict_image( input_image,algorithm):
|
| 14 |
+
algorithm_functions = {
|
| 15 |
+
"Sobel Edge Detection": sobel_edge_detection,
|
| 16 |
+
"Canny Edge Detection": canny_edge_detection,
|
| 17 |
+
"Hough Lines": hough_lines,
|
| 18 |
+
"Laplacian Edge Detection": laplacian_edge_detection,
|
| 19 |
+
"Contours Detection": contours_detection,
|
| 20 |
+
"Prewitt Edge Detection": prewitt_edge_detection,
|
| 21 |
+
"Gradient Magnitude": gradient_magnitude,
|
| 22 |
+
"Corner Detection": corner_detection,
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Apply the selected image processing algorithm
|
| 26 |
+
if algorithm in algorithm_functions:
|
| 27 |
+
processed_image = algorithm_functions[algorithm](input_image)
|
| 28 |
+
else:
|
| 29 |
+
processed_image = input_image # Default to original image if algorithm not found
|
| 30 |
+
|
| 31 |
+
return processed_image
|
| 32 |
+
|
| 33 |
+
GrImage = gr.Image()
|
| 34 |
+
GrDropdown = gr.Dropdown(
|
| 35 |
+
[
|
| 36 |
+
"Sobel Edge Detection",
|
| 37 |
+
"Canny Edge Detection",
|
| 38 |
+
"Hough Lines",
|
| 39 |
+
"Laplacian Edge Detection",
|
| 40 |
+
"Contours Detection",
|
| 41 |
+
"Prewitt Edge Detection",
|
| 42 |
+
"Gradient Magnitude",
|
| 43 |
+
"Corner Detection",
|
| 44 |
+
]
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
iface = gr.Interface(fn=predict_image, inputs=[GrImage, GrDropdown], outputs="image")
|
| 48 |
+
iface.launch()
|
flagged/input_image/497a84a343b3f1e548bca2c7a19db9a72960b0fd/tmpluaqy_ap.png
ADDED
|
flagged/log.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
input_image,output,flag,username,timestamp
|
| 2 |
+
C:\Users\Myo Win Zaw\Desktop\2k3\ai\day4\line-detection\flagged\input_image\497a84a343b3f1e548bca2c7a19db9a72960b0fd\tmpluaqy_ap.png,C:\Users\Myo Win Zaw\Desktop\2k3\ai\day4\line-detection\flagged\output\6e4d7bc7046277291635b69cd1c9655fe13a7564\tmptlbwx7ok.png,,,2023-12-23 16:39:29.327812
|
flagged/output/6e4d7bc7046277291635b69cd1c9655fe13a7564/tmptlbwx7ok.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
numpy
|
| 3 |
+
opencv-python
|
test.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils import (
|
| 3 |
+
sobel_edge_detection,
|
| 4 |
+
canny_edge_detection,
|
| 5 |
+
hough_lines,
|
| 6 |
+
laplacian_edge_detection,
|
| 7 |
+
contours_detection,
|
| 8 |
+
prewitt_edge_detection,
|
| 9 |
+
gradient_magnitude,
|
| 10 |
+
corner_detection,
|
| 11 |
+
)
|
| 12 |
+
import cv2
|
| 13 |
+
import numpy as np
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def predict_image(algorithm, image):
|
| 17 |
+
# Apply edge detection (e.g., Canny)
|
| 18 |
+
edges = cv2.Canny(image, 50, 150, apertureSize=3)
|
| 19 |
+
|
| 20 |
+
# Apply Hough Line Transform
|
| 21 |
+
lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=100)
|
| 22 |
+
|
| 23 |
+
# Draw lines on the original image
|
| 24 |
+
for line in lines:
|
| 25 |
+
rho, theta = line[0]
|
| 26 |
+
a = np.cos(theta)
|
| 27 |
+
b = np.sin(theta)
|
| 28 |
+
x0 = a * rho
|
| 29 |
+
y0 = b * rho
|
| 30 |
+
x1 = int(x0 + 1000 * (-b))
|
| 31 |
+
y1 = int(y0 + 1000 * (a))
|
| 32 |
+
x2 = int(x0 - 1000 * (-b))
|
| 33 |
+
y2 = int(y0 - 1000 * (a))
|
| 34 |
+
cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 35 |
+
|
| 36 |
+
return edges
|
| 37 |
+
|
| 38 |
+
GrImage = gr.Image()
|
| 39 |
+
GrDropdown = gr.Dropdown(
|
| 40 |
+
[
|
| 41 |
+
"Sobel Edge Detection",
|
| 42 |
+
"Canny Edge Detection",
|
| 43 |
+
"Hough Lines",
|
| 44 |
+
"Laplacian Edge Detection",
|
| 45 |
+
"Contours Detection",
|
| 46 |
+
"Prewitt Edge Detection",
|
| 47 |
+
"Gradient Magnitude",
|
| 48 |
+
"Corner Detection",
|
| 49 |
+
]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
GrOutput = gr.Image()
|
| 53 |
+
|
| 54 |
+
iface = gr.Interface(fn=predict_image, inputs=[ GrDropdown,GrImage], outputs=GrOutput)
|
| 55 |
+
iface.launch()
|
test2.png
ADDED
|
utils.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
def sobel_edge_detection(image):
|
| 5 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 6 |
+
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
|
| 7 |
+
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
|
| 8 |
+
magnitude = np.sqrt(sobelx**2 + sobely**2)
|
| 9 |
+
magnitude = np.uint8(magnitude)
|
| 10 |
+
return magnitude
|
| 11 |
+
|
| 12 |
+
def canny_edge_detection(image):
|
| 13 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 14 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
| 15 |
+
return edges
|
| 16 |
+
|
| 17 |
+
def hough_lines(image):
|
| 18 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 19 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 20 |
+
lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=100)
|
| 21 |
+
result = image.copy()
|
| 22 |
+
for line in lines:
|
| 23 |
+
rho, theta = line[0]
|
| 24 |
+
a = np.cos(theta)
|
| 25 |
+
b = np.sin(theta)
|
| 26 |
+
x0 = a * rho
|
| 27 |
+
y0 = b * rho
|
| 28 |
+
x1 = int(x0 + 1000 * (-b))
|
| 29 |
+
y1 = int(y0 + 1000 * (a))
|
| 30 |
+
x2 = int(x0 - 1000 * (-b))
|
| 31 |
+
y2 = int(y0 - 1000 * (a))
|
| 32 |
+
cv2.line(result, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 33 |
+
return result
|
| 34 |
+
|
| 35 |
+
def laplacian_edge_detection(image):
|
| 36 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 37 |
+
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
|
| 38 |
+
laplacian = np.uint8(np.absolute(laplacian))
|
| 39 |
+
return laplacian
|
| 40 |
+
|
| 41 |
+
def contours_detection(image):
|
| 42 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 43 |
+
contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 44 |
+
result = np.zeros_like(image)
|
| 45 |
+
cv2.drawContours(result, contours, -1, (0, 255, 0), 2)
|
| 46 |
+
return result
|
| 47 |
+
|
| 48 |
+
def prewitt_edge_detection(image):
|
| 49 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 50 |
+
prewittx = cv2.filter2D(gray, cv2.CV_64F, np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]))
|
| 51 |
+
prewitty = cv2.filter2D(gray, cv2.CV_64F, np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]))
|
| 52 |
+
magnitude = np.sqrt(prewittx**2 + prewitty**2)
|
| 53 |
+
magnitude = np.uint8(magnitude)
|
| 54 |
+
return magnitude
|
| 55 |
+
|
| 56 |
+
def gradient_magnitude(image):
|
| 57 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 58 |
+
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
|
| 59 |
+
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
|
| 60 |
+
magnitude = np.sqrt(sobelx**2 + sobely**2)
|
| 61 |
+
magnitude = np.uint8(magnitude)
|
| 62 |
+
return magnitude
|
| 63 |
+
|
| 64 |
+
def corner_detection(image):
|
| 65 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 66 |
+
corners = cv2.goodFeaturesToTrack(gray, maxCorners=100, qualityLevel=0.01, minDistance=10)
|
| 67 |
+
result = np.zeros_like(image)
|
| 68 |
+
corners = np.int0(corners)
|
| 69 |
+
for i in corners:
|
| 70 |
+
x, y = i.ravel()
|
| 71 |
+
cv2.circle(result, (x, y), 3, 255, -1)
|
| 72 |
+
return result
|