1
+ import sys
2
+ import time
3
+ import PySpin
4
+ import os
5
+
6
+ import numpy as np
7
+ import cv2
8
+
9
+
10
+ import argparse
11
+
12
+ # construct the argument parse and parse the arguments
13
+ ap = argparse .ArgumentParser ()
14
+ ap .add_argument ("-c" , "--camera" , type = str , default = "0" , help = "camera by id" )
15
+
16
+ args = vars (ap .parse_args ())
17
+
18
+ camera_serial = args ["camera" ]
19
+
20
+
21
+
22
+
23
+ def set_trigger_mode_software (cam ):
24
+ cam .TriggerMode .SetValue (PySpin .TriggerMode_Off )
25
+ cam .TriggerSource .SetValue (PySpin .TriggerSource_Software )
26
+ cam .TriggerMode .SetValue (PySpin .TriggerMode_On )
27
+ print ("set trigger mode software" )
28
+
29
+
30
+ def reset_trigger_mode_software (cam ):
31
+ cam .TriggerMode .SetValue (PySpin .TriggerMode_Off )
32
+ print ("reset trigger mode" )
33
+
34
+
35
+ #
36
+ # setup
37
+ #
38
+
39
+ system = PySpin .System .GetInstance ()
40
+ cam_list = system .GetCameras ()
41
+
42
+ if cam_list .GetSize () == 0 :
43
+ print ("no cameras found, aborting" )
44
+ system .ReleaseInstance ()
45
+ del system
46
+ sys .exit ()
47
+
48
+ cameras = []
49
+ for i in range (cam_list .GetSize ()):
50
+ cam = cam_list .GetByIndex (i )
51
+ print ("camera {} serial: {}" .format (i , cam .GetUniqueID ()))
52
+
53
+
54
+ #camera_serial = "18284509"
55
+
56
+ cam = cam_list .GetBySerial (camera_serial )
57
+
58
+ try :
59
+ cam .Init ()
60
+ cam .AcquisitionMode .SetValue (PySpin .AcquisitionMode_Continuous )
61
+ set_trigger_mode_software (cam )
62
+ cam .BeginAcquisition ()
63
+
64
+ except :
65
+ print ("error initializing camera {}" .format (camera_serial ))
66
+ sys .exit ()
67
+
68
+
69
+ os .mkdir (camera_serial )
70
+
71
+
72
+
73
+ #
74
+ # loop
75
+ #
76
+
77
+ count = 0
78
+
79
+ while 1 :
80
+ key = cv2 .waitKey (1 )
81
+
82
+
83
+
84
+ if key == 27 : # ESC
85
+ cv2 .destroyAllWindows ()
86
+ break
87
+
88
+ cam .TriggerSoftware ()
89
+ i = cam .GetNextImage ()
90
+ #print(i.GetWidth(), i.GetHeight(), i.GetBitsPerPixel())
91
+ cvi = None
92
+
93
+ if i .IsIncomplete ():
94
+ pass
95
+ else :
96
+ # see documentation: enum ColorProcessingAlgorithm
97
+ image_converted = i .Convert (PySpin .PixelFormat_BGR8 , PySpin .DIRECTIONAL_FILTER )
98
+ image_data = image_converted .GetData ()
99
+ cvi = np .frombuffer (image_data , dtype = np .uint8 )
100
+ cvi = cvi .reshape ((i .GetHeight (),i .GetWidth (),3 ))
101
+ cv2 .imshow ("cam1" ,cvi )
102
+ i .Release ()
103
+ del i
104
+
105
+ if key == 32 :
106
+ cv2 .imwrite ("{}/capture_{}.jpg" .format (camera_serial ,count ), cvi )
107
+ count += 1
108
+ print ("saved image {}" .format (count ))
109
+ #
110
+ # cleanup
111
+ #
112
+
113
+ cam .EndAcquisition ()
114
+ reset_trigger_mode_software (cam )
115
+ cam .DeInit ()
116
+ del cam
117
+ del cam_list
118
+
119
+ system .ReleaseInstance ()
120
+ del system
0 commit comments