forked from ArduCAM/ArduCAM_USB_Camera_Shield_Python_Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduCam_Demo.py
89 lines (62 loc) · 2.27 KB
/
ArduCam_Demo.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
import argparse
import time
import signal
import cv2
from Arducam import *
from ImageConvert import *
exit_ = False
def sigint_handler(signum, frame):
global exit_
exit_ = True
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGTERM, sigint_handler)
def display_fps(index):
display_fps.frame_count += 1
current = time.time()
if current - display_fps.start >= 1:
print("fps: {}".format(display_fps.frame_count))
display_fps.frame_count = 0
display_fps.start = current
display_fps.start = time.time()
display_fps.frame_count = 0
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--config-file', type=str, required=True, help='Specifies the configuration file.')
parser.add_argument('-v', '--verbose', action='store_true', required=False, help='Output device information.')
parser.add_argument('--preview-width', type=int, required=False, default=-1, help='Set the display width')
parser.add_argument('-n', '--nopreview', action='store_true', required=False, help='Disable preview windows.')
args = parser.parse_args()
config_file = args.config_file
verbose = args.verbose
preview_width = args.preview_width
no_preview = args.nopreview
camera = ArducamCamera()
if not camera.openCamera(config_file):
raise RuntimeError("Failed to open camera.")
if verbose:
camera.dumpDeviceInfo()
camera.start()
# camera.setCtrl("setFramerate", 2)
# camera.setCtrl("setExposureTime", 20000)
# camera.setCtrl("setAnalogueGain", 800)
scale_width = preview_width
while not exit_:
ret, data, cfg = camera.read()
display_fps(0)
if no_preview:
continue
if ret:
image = convert_image(data, cfg, camera.color_mode)
if scale_width != -1:
scale = scale_width / image.shape[1]
image = cv2.resize(image, None, fx=scale, fy=scale)
cv2.imshow("Arducam", image)
else:
print("timeout")
key = cv2.waitKey(1)
if key == ord('q'):
exit_ = True
elif key == ord('s'):
np.array(data, dtype=np.uint8).tofile("image.raw")
camera.stop()
camera.closeCamera()