-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcquireAndDisplay.py
367 lines (286 loc) · 13.2 KB
/
AcquireAndDisplay.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# coding=utf-8
# =============================================================================
# This AcquireAndDisplay.py shows how to get the image data, and then display images in a GUI.
# Currently, this program is limited to single camera use.
# NOTE: OpenCV2 must be installed on Python interpreter prior to running this example.
import PySpin
import sys
import cv2
import socket
import ImgProc
import time
global continue_recording
continue_recording = True
def acquire_and_display_images(cam, nodemap, nodemap_tldevice):
"""
This function continuously acquires images from a device and display them in a GUI.
:param cam: Camera to acquire images from.
:param nodemap: Device nodemap.
:param nodemap_tldevice: Transport layer device nodemap.
:type cam: CameraPtr
:type nodemap: INodeMap
:type nodemap_tldevice: INodeMap
:return: True if successful, False otherwise.
:rtype: bool
"""
global continue_recording
sNodemap = cam.GetTLStreamNodeMap()
# Change bufferhandling mode to NewestOnly
node_bufferhandling_mode = PySpin.CEnumerationPtr(sNodemap.GetNode('StreamBufferHandlingMode'))
if not PySpin.IsAvailable(node_bufferhandling_mode) or not PySpin.IsWritable(node_bufferhandling_mode):
print('Unable to set stream buffer handling mode.. Aborting...')
return False
# Retrieve entry node from enumeration node
node_newestonly = node_bufferhandling_mode.GetEntryByName('NewestOnly')
if not PySpin.IsAvailable(node_newestonly) or not PySpin.IsReadable(node_newestonly):
print('Unable to set stream buffer handling mode.. Aborting...')
return False
# Retrieve integer value from entry node
node_newestonly_mode = node_newestonly.GetValue()
# Set integer value from entry node as new value of enumeration node
node_bufferhandling_mode.SetIntValue(node_newestonly_mode)
print('*** IMAGE ACQUISITION ***\n')
try:
node_acquisition_mode = PySpin.CEnumerationPtr(nodemap.GetNode('AcquisitionMode'))
if not PySpin.IsAvailable(node_acquisition_mode) or not PySpin.IsWritable(node_acquisition_mode):
print('Unable to set acquisition mode to continuous (enum retrieval). Aborting...')
return False
# Retrieve entry node from enumeration node
node_acquisition_mode_continuous = node_acquisition_mode.GetEntryByName('Continuous')
if not PySpin.IsAvailable(node_acquisition_mode_continuous) or not PySpin.IsReadable(
node_acquisition_mode_continuous):
print('Unable to set acquisition mode to continuous (entry retrieval). Aborting...')
return False
# Retrieve integer value from entry node
acquisition_mode_continuous = node_acquisition_mode_continuous.GetValue()
# Set integer value from entry node as new value of enumeration node
node_acquisition_mode.SetIntValue(acquisition_mode_continuous)
print('Acquisition mode set to continuous...')
# Begin acquiring images
#
# *** NOTES ***
# What happens when the camera begins acquiring images depends on the
# acquisition mode. Single frame captures only a single image, multi
# frame catures a set number of images, and continuous captures a
# continuous stream of images.
#
# *** LATER ***
# Image acquisition must be ended when no more images are needed.
cam.BeginAcquisition()
print('Acquiring images...')
# Retrieve device serial number for filename
#
# *** NOTES ***
# The device serial number is retrieved in order to keep cameras from
# overwriting one another. Grabbing image IDs could also accomplish
# this.
device_serial_number = ''
node_device_serial_number = PySpin.CStringPtr(nodemap_tldevice.GetNode('DeviceSerialNumber'))
if PySpin.IsAvailable(node_device_serial_number) and PySpin.IsReadable(node_device_serial_number):
device_serial_number = node_device_serial_number.GetValue()
print('Device serial number retrieved as %s...' % device_serial_number)
# Close program
print('Press enter to close the program..')
start_time = time.time()
# Retrieve and display images
while(continue_recording):
try:
# Retrieve next received image
#
# *** NOTES ***
# Capturing an image houses images on the camera buffer. Trying
# to capture an image that does not exist will hang the camera.
#
# *** LATER ***
# Once an image from the buffer is saved and/or no longer
# needed, the image must be released in order to keep the
# buffer from filling up.
image_result = cam.GetNextImage(1000)
# Ensure image completion
if image_result.IsIncomplete():
print('Image incomplete with image status %d ...' % image_result.GetImageStatus())
else:
# Getting the image data as a numpy array
image_data = image_result.GetNDArray()
global total_coords,ro
# Cropping the image to the ROI
crp_image = image_data[int(ro[1]):int(ro[1]+ro[3]), int(ro[0]):int(ro[0]+ro[2])]
# Getting the Coordinates of the center of Pupil
(px,py, roi, rad) = ImgProc.detect_pupil(crp_image)
csv_str = str(px)+","+str(py)+",0,0"
total_coords=total_coords+1 # Counting the number of coordinates
# Send the Coordinates over TCP/IP to Monkey Logic
global_connection.send(csv_str.encode())
# Displaying the pupil coordinates on the image
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(roi, str((px,py)), (0, 10), font, 0.4, (100, 255, 0), 2, cv2.LINE_AA)
# Draws an image on the current figure
cv2.imshow("Realtime",roi)
# Acquiring the key pressed by the user
key = cv2.waitKey(1) & 0xFF
# If user presses s, save the ROI coordinates
if key == ord('s'):
cv2.destroyAllWindows()
ro = cv2.selectROI (image_data) # Selecting the ROI
cv2.destroyAllWindows()
# Writing the ROI coordinates to a text file
with open('roi_coords.txt', 'wt') as f:
f.write('(top left x, top left y, width, height) = '+repr(ro)+'\n')
print("ROI file updated!")
f.close()
# If user presses r, reset the ROI coordinates
elif key == ord('r'):
cv2.destroyAllWindows()
ro = (0,0,540,720) # Resetting the ROI
cv2.destroyAllWindows()
# Writing the ROI coordinates to a text file
with open('roi_coords.txt', 'wt') as f:
f.write('(top left x, top left y, width, height) = '+repr(ro)+'\n')
print("ROI file updated!")
f.close()
# If user presses q, close the program
elif key == ord('q'):
print('Program is closing...')
cv2.destroyAllWindows()
continue_recording=False
# Release image
#
# *** NOTES ***
# Images retrieved directly from the camera (i.e. non-converted
# images) need to be released in order to keep from filling the
# buffer.
image_result.Release()
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
end_time = time.time()
global total_time
total_time = (end_time-start_time)*(10**3)
# End acquisition
#
# *** NOTES ***
# Ending acquisition appropriately helps ensure that devices clean up
# properly and do not need to be power-cycled to maintain integrity.
cam.EndAcquisition()
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
return False
return True
def run_single_camera(cam):
"""
This function acts as the body of the example; please see NodeMapInfo example
for more in-depth comments on setting up cameras.
:param cam: Camera to run on.
:type cam: CameraPtr
:return: True if successful, False otherwise.
:rtype: bool
"""
try:
result = True
nodemap_tldevice = cam.GetTLDeviceNodeMap()
# Initialize camera
cam.Init()
# Retrieve GenICam nodemap
nodemap = cam.GetNodeMap()
# Acquire images
result &= acquire_and_display_images(cam, nodemap, nodemap_tldevice)
# Deinitialize camera
cam.DeInit()
except PySpin.SpinnakerException as ex:
print('Error: %s' % ex)
result = False
return result
def main():
"""
Example entry point; notice the volume of data that the logging event handler
prints out on debug despite the fact that very little really happens in this
example. Because of this, it may be better to have the logger set to lower
level in order to provide a more concise, focused log.
:return: True if successful, False otherwise.
:rtype: bool
"""
result = True
# Retrieve singleton reference to system object
system = PySpin.System.GetInstance()
# Get current library version
version = system.GetLibraryVersion()
print('Library version: %d.%d.%d.%d' % (version.major, version.minor, version.type, version.build))
# Retrieve list of cameras from the system
cam_list = system.GetCameras()
# Retrieve number of cameras
num_cameras = cam_list.GetSize()
print('Number of cameras detected: %d' % num_cameras)
# Finish if there are no cameras
if num_cameras == 0:
# Clear camera list before releasing system
cam_list.Clear()
# Release system instance
system.ReleaseInstance()
print('Not enough cameras!')
input('Done! Press Enter to exit...')
return False
# Fetch ROI Coordinates from roi_coords.txt
print("Fetching ROI Coordinates...")
global ro
try:
# If roi_coords.txt is present, then fetch ROI coordinates from it
with open("roi_coords.txt", 'rt') as f:
line = f.readline()
ro = eval(line[42:].strip())
f.close()
print("ROI Coordinates fetched successfully from roi_coords.txt")
except:
# If roi_coords.txt is not present, then set ROI coordinates to (0,0,540,720)
print("Cannot Fetch ROI Coordinates from roi_coords.txt!")
ro = (0,0,540,720)
print("Setting ROI rectangle coordinates to ",ro)
# Set up a TCP/IP server
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to server address and port 10001
server_address = ('', 10001)
tcp_socket.bind(server_address)
# Listen on port 10001
tcp_socket.listen(1)
print("Waiting for connection: Kindly connect using Monkey Logic")
connection, client = tcp_socket.accept()
print("Connected to client IP: {}".format(client))
# Receive and print data 32 bytes at a time
data = connection.recv(32).decode('utf-8').strip()
print("Received data: {}".format(data))
global total_coords
total_coords=0
# If ok is received, then start the program
if data=="ok":
global global_connection
global_connection = connection
cam = cam_list[0]
result &= run_single_camera(cam)
# Release reference to camera
# NOTE: Unlike the C++ examples, we cannot rely on pointer objects being automatically
# cleaned up when going out of scope.
# The usage of del is preferred to assigning the variable to None.
del cam
# If not ok is received, then exit the program
else:
print("Error: Monkey Logic not connected properly")
print("Kindly restart Monkey Logic and Connect Again")
# Close socket
connection.close()
# Clear camera list before releasing system
cam_list.Clear()
# Release system instance
system.ReleaseInstance()
input('Done! Press Enter to exit...')
# Print Net Time, Total Coordinates Sent, Framerate and Time to send one Coordinate
global total_time
print("\nNet time: {} ms".format(total_time))
print("Total Coordinates Sent: ", total_coords)
print("Framerate: {} Hz".format(1000*total_coords/total_time))
if total_coords!=0:
print("Time to send one Coordinate: {} ms".format(total_time/total_coords))
return result
if __name__ == '__main__':
if main():
sys.exit(0)
else:
sys.exit(1)