-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaptureImages.py
More file actions
executable file
·148 lines (132 loc) · 5.32 KB
/
captureImages.py
File metadata and controls
executable file
·148 lines (132 loc) · 5.32 KB
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
#!/usr/bin/env python3
# load DuetWebAPI
import cv2
import random
import time
import os
import tarfile
import tempfile
import argparse
try:
import DuetWebAPI as DWA
except ImportError:
print("Python Library Module 'DuetWebAPI.py' is required. ")
print("Obtain from https://github.com/DanalEstes/DuetWebAPI ")
print("Place in same directory as script, or in Python libpath.")
exit(-666)
# Check if running in a graphics console
if (os.environ.get('SSH_CLIENT')):
print("This script MUST run on the graphics console, not an SSH session.")
exit(-888)
def controlPoint(printerIn,controlPointIn):
printerIn.gCode("G90 G1 X"+str(controlPointIn[0])+" Y"+str(controlPointIn[1]))
return
# parse command line arguments
parser = argparse.ArgumentParser(description='Program to capture images of endstop moving 0.025 or 0.05mm per frame. Output is saved to working directory in captures.tar.gz', allow_abbrev=False)
parser.add_argument('-duet',
type=str,
nargs=1,
default=['localhost'],
help='Name or IP address of Duet printer. You can use -duet=localhost if you are on the embedded Pi on a Duet3.')
parser.add_argument('-camera',
type=int,
nargs=1,
default=[0],
help='Index of /dev/videoN device to be used. Default 0. ')
requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-cp',
type=float,
nargs=2,
required = True,
help="x y that will put 'controlled point' on carriage over camera.")
parser.add_argument('-repeat',
type=int,
nargs=1,
default=[10],
help="Set number of captures per offset (default is 10)")
args=vars(parser.parse_args())
duet = args['duet'][0]
camera = args['camera'][0]
controlPointLocation = args['cp']
numImages = args['repeat'][0]
# setup printer communication
printer = DWA.DuetWebAPI('http://'+duet)
if (not printer.printerType()):
print('Device at '+duet+' either did not respond or is not a Duet V2 or V3 printer.')
print('')
exit(-222)
print('Unloading tools.')
printer.gCode("T-1")
while printer.getStatus() not in 'idle': time.sleep(0.5)
print('Moving to control point and running with 1st offset of 0.025mm')
controlPoint(printer, controlPointLocation)
while printer.getStatus() not in 'idle': time.sleep(0.5)
# setup capture device
try:
print('Settting up webcam for capture..')
cap = cv2.VideoCapture(camera)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FPS,25)
cap.set(cv2.CAP_PROP_BUFFERSIZE,1)
except Exception as v1:
print( 'Failed to open webcam stream' )
print( str(v1) )
exit(-1)
# create subdirectory to store capture files
try:
print( 'Settting up temporary directory for captures..' )
tempFolder = tempfile.TemporaryDirectory()
path = tempFolder.name
except OSError:
print('Failed to create capture directory \"'+path+'\".')
cap.release()
exit(-2)
direction = [1,1]
offsetAmount = 0.025
for i in range(1,numImages+1):
offsetX = offsetAmount * random.choice(direction)
offsetY = offsetAmount * random.choice(direction)
print("\rMove #"+str(i)+": G91 G1 X" + str(offsetX) + " Y" + str(offsetY) + " F12000 G90 ", end='', flush=True)
printer.gCode("G91 G1 X" + str(offsetX) + " Y" + str(offsetY) + " F12000 G90 ")
printer.gCode("M400")
for count in range (1,5):
# capture some frames to discard to allow movement of printer
(grabbed, frame) = cap.read()
(grabbed, frame) = cap.read()
filename = path+"/capture_"+str(offsetAmount)+"_{:03d}.jpg".format(i)
cv2.imwrite(filename, frame)
print('')
print('Returning to control point and running with 2nd offset of 0.05mm')
controlPoint(printer,controlPointLocation)
offsetAmount = 0.05
for i in range(1,numImages+1):
offsetX = offsetAmount * random.choice(direction)
offsetY = offsetAmount * random.choice(direction)
print("\rMove #"+str(i)+": G91 G1 X" + str(offsetX) + " Y" + str(offsetY) + " F12000 G90 ", end='', flush=True)
printer.gCode("G91 G1 X" + str(offsetX) + " Y" + str(offsetY) + " F12000 G90 ")
printer.gCode("M400")
for count in range (1,5):
# capture some frames to discard to allow movement of printer
(grabbed, frame) = cap.read()
(grabbed, frame) = cap.read()
filename = path+"/capture_"+str(offsetAmount)+"_{:03d}.jpg".format(i)
cv2.imwrite(filename, frame)
print('')
print('Returning to control point.')
controlPoint(printer,controlPointLocation)
try:
print( 'Compressing images into archive.')
with tarfile.open('./capture_offsets.tar.gz','w') as archive:
for i in os.listdir(path):
archive.add(path+'/'+i, arcname='captures/'+i)
except Exception as c1:
print( 'Cannot create tarfile.' )
print( str(c1) )
cap.release()
exit(-3)
print( '\nCapture done. Compressed archive of captures has been created\n and temp folder and files have been deleted.')
print('')
print( 'Thanks for helping out!' )
cap.release()
exit()