-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrab.py
65 lines (54 loc) · 1.92 KB
/
grab.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
import sys
import time
import os
from datetime import datetime
from pathlib import Path
import imageio
from pypylon import pylon
from pypylon import genicam
base_dir = Path(
"/home/nunoluis/Desktop/20220620_Luminy_Flydome/"
)
#introduce here Date Of Birth for start of experiment
dob = datetime.strptime("2022-06-20", "%Y-%m-%d")
def get_cameras():
transport_layer = pylon.TlFactory.GetInstance()
device_infos = transport_layer.EnumerateDevices()
cameras = {
ci.GetSerialNumber(): pylon.InstantCamera(
transport_layer.CreateDevice(ci))
for ci in device_infos
}
return cameras
def grab_image(serial, camera):
now = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
fname = f"img_{serial}_{now}.tiff"
dif = datetime.now() - dob
days_elapsed = dif.days
print(f"saving {fname} for day {days_elapsed} for camera {serial}")
camera.Open()
try:
camera.StartGrabbing()
result = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
if result.GrabSucceeded():
if serial == "22475435":
base_dir_fem_day = f"female/day_{days_elapsed}/"
if not os.path.exists(base_dir/base_dir_fem_day):
os.makedirs(base_dir/base_dir_fem_day)
imageio.imsave(base_dir/base_dir_fem_day/fname, result.Array)
else:
base_dir_mal_day = f"male/day_{days_elapsed}/"
if not os.path.exists(base_dir/base_dir_mal_day):
os.makedirs(base_dir/base_dir_mal_day)
imageio.imsave(base_dir/base_dir_mal_day/fname, result.Array)
finally:
camera.Close()
def grab_all_cameras():
cameras = get_cameras()
for serial, camera in cameras.items():
grab_image(serial, camera)
def run_every(minutes):
while True:
grab_all_cameras()
time.sleep(int(minutes*60))
run_every(float(sys.argv[1]))