forked from ArduCAM/picamera2_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHatSwitchCameraTakePicture.py
75 lines (64 loc) · 2.11 KB
/
HatSwitchCameraTakePicture.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
#!/usr/bin/python3
import cv2
import sys
import argparse
from picamera2 import Picamera2
import subprocess
import time
def run_cmd(cmd):
print(f'{cmd}')
try:
subprocess.run(cmd, universal_newlines=True, check=False, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except RuntimeError as e:
print(f'Error: {e}')
except:
print("Set up error!")
argv = sys.argv[:]
parser = argparse.ArgumentParser()
parser.add_argument(
"--choose",
# action='store_true',
nargs = "+",
help="Select camera"
)
args = parser.parse_args()
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"format": 'RGB888', "size": (640, 480)}))
picam2.start()
# while True:
im = picam2.capture_array()
index = 0
if args.choose:
for i in range(len(args.choose)):
# print(args.choose)
if args.choose[i] == 'a':
# picam2.stop()
run_cmd("i2cset -y 10 0x24 0x24 0x02")
time.sleep(0.5)
im = picam2.capture_array()
cv2.imwrite("camera_a_picture_{}.jpg".format(index), im)
print("Camera A save picture success.")
index += 1
if args.choose[i] == 'b':
run_cmd("i2cset -y 10 0x24 0x24 0x12")
time.sleep(0.5)
im = picam2.capture_array()
cv2.imwrite("camera_b_picture_{}.jpg".format(index), im)
print("Camera B save picture success.")
index += 1
if args.choose[i] == 'c':
run_cmd("i2cset -y 10 0x24 0x24 0x22")
time.sleep(0.5)
im = picam2.capture_array()
cv2.imwrite("camera_c_picture_{}.jpg".format(index), im)
print("Camera C save picture success.")
index += 1
if args.choose[i] == 'd':
run_cmd("i2cset -y 10 0x24 0x24 0x32")
time.sleep(0.5)
im = picam2.capture_array()
cv2.imwrite("camera_d_picture_{}.jpg".format(index), im)
print("Camera D save picture success.")
index += 1
print("The image is saved successfully and the program ends.")