-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
170 lines (134 loc) · 4.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import cv2
import numpy
import time
import copy
import svg_to_gcode.compiler as Compiler
import svg_to_gcode.compiler.interfaces as Interfaces
import svg_to_gcode.svg_parser as Parser
EdgeMin = 150
EdgeMax = 200
# code for function by ospider
# https://stackoverflow.com/questions/43108751/convert-contour-paths-to-svg-paths
def saveContoursAsSVG(contours, width, height, filename):
with open(filename, "w+") as f:
f.write(f'<svg width="{width}" height="{height}" xmlns="http://www.w3.org/2000/svg">')
for c in contours:
f.write('<path d="M')
for i in range(len(c)):
x, y = c[i][0]
f.write(f"{x} {y} ")
f.write('" style="stroke:pink"/>')
f.write("</svg>")
def contoursToSVG(contours, width, height):
result = ""
result += '<svg width="' + str(width) + '" height="' + str(height) \
+ '" xmlns="http://www.w3.org/2000/svg">'
for c in contours:
result += '<path d="M'
for i in range(len(c)):
x, y = c[i][0]
result += str(x) + " " + str(y) + " "
result += '" style="stroke:pink"/>'
result += "</svg>"
return result
def contoursToGCode(contours, width, height, filename):
compiler = Compiler.Compiler(Interfaces.Gcode, movement_speed=2000, cutting_speed=2000, pass_depth=1)
curves = Parser.parse_string(contoursToSVG(contours, width, height))
compiler.append_curves(curves)
compiler.compile_to_file(filename)
print("success!")
def scaleContours(contours, scale):
result = []
for contour in contours:
resultingContour = []
for point in contour:
x = int(float(point[0][0])*scale)
y = int(float(point[0][1])*scale)
resultingPoint = [[x, y]]
resultingContour.append(resultingPoint)
result.append(resultingContour)
return result
def getMin(a, b):
if a <= -1:
return b
elif b <= -1:
return a
elif a < b:
return a
else:
return b
def getMax(a, b):
if a > b:
return a
else:
return b
def calculateDrift(contours, frame):
xDrift = -1
yDrift = -1
for contour in contours:
for point in contour:
xDrift = getMin(point[0][0], xDrift)
yDrift = getMax(point[0][1], yDrift)
yDrift -= frame.shape[1]
return xDrift, yDrift
def correctContours(contours, frame):
result = []
x, y = calculateDrift(contours, frame)
for contour in contours:
resultingContour = []
#print(contour)
for point in contour:
#print(point)
resultingX = point[0][0]-x
resultingY = point[0][1]-y
resultingPoint = [[resultingX, resultingY]]
resultingContour.append(resultingPoint)
result.append(resultingContour)
#print(result)
return result
# displays a single frame ready to print
def Capture(frame):
windowName = "Result"
while True:
textFrame = copy.deepcopy(frame) # create copy of frame to add text to and display
cv2.putText(textFrame, "W to take new photo, E to print", (0, 450), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale=1, color=(255, 255, 255), thickness=2)
cv2.imshow(windowName, textFrame)
input = cv2.waitKey(1)
if input == ord('w'):
cv2.destroyWindow(windowName)
break
elif input == ord('e'):
# Do I really have to save then rewrite the file for this...
contours, hierachy = cv2.findContours(frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = scaleContours(contours, 0.25)
contours = correctContours(contours, frame)
contoursToGCode(contours, frame.shape[0], frame.shape[1], "test.gcode")
def main():
cap = cv2.VideoCapture(0)
liveCanny = False
if not cap.isOpened:
raise RuntimeError("Failed to instantiate VideoCapture object :(")
while True:
# take frame from camera
ret, frame = cap.read()
if not ret:
print("##WARNING## - VideoCapture object failed to capture frame")
cannyFrame = cv2.Canny(frame, EdgeMin, EdgeMax)
# input
input = cv2.waitKey(1)
if input == ord(' '):
# we need to apply canny first
Capture(cannyFrame)
elif input == ord('r'):
liveCanny = not liveCanny
elif input == ord('q'):
break
# output to screen, optionally applies edge detection
if liveCanny:
frame = cannyFrame
cv2.putText(frame, "Space to take photo | R to show edges | Q to quit", (0, 450), fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale=1, color=(255, 255, 255), thickness=2)
cv2.imshow("Camera", frame)
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()