Spaces:
Runtime error
Runtime error
Zai
commited on
Commit
·
1b63b70
1
Parent(s):
6fd45eb
test upload
Browse files- __pycache__/algorithms.cpython-310.pyc +0 -0
- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +42 -0
- requriements.txt +4 -0
- utils.py +134 -0
__pycache__/algorithms.cpython-310.pyc
ADDED
Binary file (3.02 kB). View file
|
|
__pycache__/utils.cpython-310.pyc
ADDED
Binary file (3.84 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from utils import generate, opencv_to_pil
|
4 |
+
|
5 |
+
|
6 |
+
algorithm_choices = [
|
7 |
+
"Sobel Edge Detection",
|
8 |
+
"Canny Edge Detection",
|
9 |
+
# "Hough Lines",
|
10 |
+
"Laplacian Edge Detection",
|
11 |
+
# "Contours Detection",
|
12 |
+
"Prewitt Edge Detection",
|
13 |
+
"Gradient Magnitude",
|
14 |
+
# "Corner Detection",
|
15 |
+
]
|
16 |
+
|
17 |
+
|
18 |
+
def main():
|
19 |
+
st.title("Line Detection Algorithms")
|
20 |
+
|
21 |
+
st.write("Upload an image and select a line detection algorithm.")
|
22 |
+
|
23 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
24 |
+
|
25 |
+
algorithm_choice = st.selectbox(
|
26 |
+
"Select Line Detection Algorithm", algorithm_choices
|
27 |
+
)
|
28 |
+
|
29 |
+
if uploaded_file is not None:
|
30 |
+
image = Image.open(uploaded_file)
|
31 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
32 |
+
|
33 |
+
if st.button("Generate Output"):
|
34 |
+
output = generate(image, algorithm_choice)
|
35 |
+
|
36 |
+
result = opencv_to_pil(output)
|
37 |
+
|
38 |
+
st.image(result, caption="Generated Output", use_column_width=True)
|
39 |
+
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
main()
|
requriements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
numpy
|
3 |
+
opencv-python
|
4 |
+
PIL
|
utils.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
|
6 |
+
def pil_to_opencv(image):
|
7 |
+
numpy_image = np.array(image)
|
8 |
+
|
9 |
+
opencv_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
|
10 |
+
|
11 |
+
return opencv_image
|
12 |
+
|
13 |
+
|
14 |
+
def opencv_to_pil(image):
|
15 |
+
# Convert OpenCV BGR image to NumPy array
|
16 |
+
numpy_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
17 |
+
|
18 |
+
# Convert NumPy array to PIL Image
|
19 |
+
pil_image = Image.fromarray(numpy_image)
|
20 |
+
|
21 |
+
return pil_image
|
22 |
+
|
23 |
+
|
24 |
+
def generate(image, algorithm_name):
|
25 |
+
algorithm_functions = {
|
26 |
+
"Sobel Edge Detection": sobel_edge_detection,
|
27 |
+
"Canny Edge Detection": canny_edge_detection,
|
28 |
+
"Hough Lines": hough_lines,
|
29 |
+
"Laplacian Edge Detection": laplacian_edge_detection,
|
30 |
+
"Contours Detection": contours_detection,
|
31 |
+
"Prewitt Edge Detection": prewitt_edge_detection,
|
32 |
+
"Gradient Magnitude": gradient_magnitude,
|
33 |
+
"Corner Detection": corner_detection,
|
34 |
+
}
|
35 |
+
|
36 |
+
if algorithm_name in algorithm_functions:
|
37 |
+
algorithm_function = algorithm_functions[algorithm_name]
|
38 |
+
processed_image = algorithm_function(image)
|
39 |
+
else:
|
40 |
+
processed_image = ()
|
41 |
+
|
42 |
+
return processed_image
|
43 |
+
|
44 |
+
|
45 |
+
def sobel_edge_detection(image):
|
46 |
+
gray = pil_to_opencv(image)
|
47 |
+
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
|
48 |
+
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
|
49 |
+
magnitude = np.sqrt(sobelx**2 + sobely**2)
|
50 |
+
magnitude = np.uint8(magnitude)
|
51 |
+
return magnitude
|
52 |
+
|
53 |
+
|
54 |
+
def canny_edge_detection(image):
|
55 |
+
gray = pil_to_opencv(image)
|
56 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
57 |
+
return edges
|
58 |
+
|
59 |
+
|
60 |
+
def hough_lines(image):
|
61 |
+
gray = pil_to_opencv(image)
|
62 |
+
edges = cv2.Canny(gray, 50, 150)
|
63 |
+
lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=100)
|
64 |
+
result = image.copy()
|
65 |
+
for line in lines:
|
66 |
+
rho, theta = line[0]
|
67 |
+
a = np.cos(theta)
|
68 |
+
b = np.sin(theta)
|
69 |
+
x0 = a * rho
|
70 |
+
y0 = b * rho
|
71 |
+
x1 = int(x0 + 1000 * (-b))
|
72 |
+
y1 = int(y0 + 1000 * (a))
|
73 |
+
x2 = int(x0 - 1000 * (-b))
|
74 |
+
y2 = int(y0 - 1000 * (a))
|
75 |
+
cv2.line(result, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
76 |
+
print("passed")
|
77 |
+
|
78 |
+
return result
|
79 |
+
|
80 |
+
|
81 |
+
def laplacian_edge_detection(image):
|
82 |
+
gray = pil_to_opencv(image)
|
83 |
+
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
|
84 |
+
laplacian = np.uint8(np.absolute(laplacian))
|
85 |
+
return laplacian
|
86 |
+
|
87 |
+
|
88 |
+
def contours_detection(image):
|
89 |
+
gray = pil_to_opencv(image)
|
90 |
+
contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
91 |
+
result = np.zeros_like(image)
|
92 |
+
|
93 |
+
cv2.drawContours(result, contours, -1, (0, 255, 0), 2)
|
94 |
+
print("passed")
|
95 |
+
|
96 |
+
return result
|
97 |
+
|
98 |
+
|
99 |
+
def prewitt_edge_detection(image):
|
100 |
+
gray = pil_to_opencv(image)
|
101 |
+
prewittx = cv2.filter2D(
|
102 |
+
gray, cv2.CV_64F, np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
|
103 |
+
)
|
104 |
+
prewitty = cv2.filter2D(
|
105 |
+
gray, cv2.CV_64F, np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])
|
106 |
+
)
|
107 |
+
magnitude = np.sqrt(prewittx**2 + prewitty**2)
|
108 |
+
magnitude = np.uint8(magnitude)
|
109 |
+
return magnitude
|
110 |
+
|
111 |
+
|
112 |
+
def gradient_magnitude(image):
|
113 |
+
gray = pil_to_opencv(image)
|
114 |
+
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
|
115 |
+
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
|
116 |
+
magnitude = np.sqrt(sobelx**2 + sobely**2)
|
117 |
+
magnitude = np.uint8(magnitude)
|
118 |
+
print("passed")
|
119 |
+
|
120 |
+
return magnitude
|
121 |
+
|
122 |
+
|
123 |
+
def corner_detection(image):
|
124 |
+
gray = pil_to_opencv(image)
|
125 |
+
corners = cv2.goodFeaturesToTrack(
|
126 |
+
gray, maxCorners=100, qualityLevel=0.01, minDistance=10
|
127 |
+
)
|
128 |
+
result = np.zeros_like(image)
|
129 |
+
corners = np.int0(corners)
|
130 |
+
for i in corners:
|
131 |
+
x, y = i.ravel()
|
132 |
+
cv2.circle(result, (x, y), 3, 255, -1)
|
133 |
+
print("passed")
|
134 |
+
return result
|