142 lines
5.2 KiB
Python
142 lines
5.2 KiB
Python
import ch.psi.pshell.imaging.ImageBuffer as ImageBuffer
|
|
import java.math.BigInteger as BigInteger
|
|
import org.python.core.PyLong as PyLong
|
|
import org.python.core.PyFloat as PyFloat
|
|
import json
|
|
import traceback
|
|
import datetime
|
|
|
|
|
|
if get_exec_pars().source == CommandSource.ui:
|
|
camera_name = "SARBD02-DSCR050_sp1" # "SLG-LCAM-C041_sp"
|
|
shared = True
|
|
images = 1
|
|
interval = -1
|
|
roi = "" #"[540, 200, 430,100]"
|
|
else:
|
|
camera_name = args[0]
|
|
shared = args[1].lower() == "true"
|
|
images = int(args[2])
|
|
interval = int(args[3])
|
|
roi = args[4]
|
|
|
|
|
|
set_exec_pars(name="camera_snapshot")
|
|
|
|
cam_server.start(camera_name, shared)
|
|
|
|
if roi is not None and len(roi.strip())>0:
|
|
roi = json.loads(roi)
|
|
cam_server.setRoi(roi[0], roi[2], roi[1], roi[3])
|
|
while True:
|
|
cam_server.waitNext(10000)
|
|
r = json.loads(cam_server.stream.take()["processing_parameters"])
|
|
if roi == r["image_region_of_interest"]:
|
|
break;
|
|
else:
|
|
cam_server.waitNext(10000)
|
|
|
|
|
|
def _create_tables( stream_value, paths, data_type, shape):
|
|
create_dataset(paths["image"], data_type, dimensions = [images, shape[0], shape[1]])
|
|
create_dataset(paths["pid"], 'l', dimensions = [images])
|
|
create_dataset(paths["timestamp_str"], 's', dimensions = [images])
|
|
for id in stream_value.identifiers:
|
|
val = stream_value.getValue(id)
|
|
if id == "image":
|
|
pass
|
|
elif id == "processing_parameters":
|
|
val = json.loads(val)
|
|
for key in val.keys():
|
|
set_attribute(paths["image"], key, "" if val[key] is None else val[key] )
|
|
elif isinstance(val, PyArray):
|
|
create_dataset("/"+id, 'd', dimensions = [images, len(val)])
|
|
elif isinstance(val, PyLong):
|
|
create_dataset("/"+id, 'l', dimensions = [images])
|
|
elif isinstance(val, PyFloat):
|
|
create_dataset("/"+id, 'd', dimensions = [images])
|
|
else:
|
|
print "Unmanaged stream type: ", val, type(val)
|
|
pass
|
|
|
|
|
|
def _append_frame(stream_value, index, paths, data_type, shape):
|
|
print "Saving frame :", index
|
|
#append_dataset(paths["image"], data, index, type = data_type)
|
|
append_dataset(paths["image"], stream_value.getValue("image"),[index,0,0], type = data_type, shape=[1, shape[0], shape[1]])
|
|
append_dataset(paths["pid"], stream_value.getPulseId(), index)
|
|
append_dataset(paths["timestamp_str"], datetime.datetime.fromtimestamp(stream_value.timestampNanos/1e9).strftime('%Y-%m-%d %H:%M:%S'), index)
|
|
|
|
for id in stream_value.identifiers:
|
|
try:
|
|
val = stream_value.getValue(id)
|
|
if id == "image":
|
|
pass
|
|
elif isinstance(val, PyArray):
|
|
append_dataset("/"+id, val, index)
|
|
elif isinstance(val, PyLong):
|
|
append_dataset("/"+id, int(val), index)
|
|
elif isinstance(val, PyFloat):
|
|
append_dataset("/"+id, float(val), index)
|
|
else:
|
|
pass
|
|
except:
|
|
print id, val
|
|
traceback.print_exc()
|
|
|
|
print "Saved frame: ", index
|
|
|
|
def save_camera(camera_name, shared = False, server = cam_server, images = 1, interval = -1, parallel = True, pause = False):
|
|
stream_value = server.stream.take()
|
|
if stream_value is None:
|
|
server.waitNext(10000)
|
|
stream_value = server.stream.take()
|
|
|
|
paths = {"image":"/image", "pid":"/pulse_id", "timestamp_str":"/timestamp_str"}
|
|
shape = [stream_value.getValue("height"),stream_value.getValue("width")]
|
|
|
|
data_type = type(stream_value.getValue("image")[0])
|
|
if data_type== float: data_type = 'f'
|
|
elif data_type == short: data_type = 'h'
|
|
elif data_type == int: data_type = 'i'
|
|
elif data_type == long: data_type = 'l'
|
|
elif data_type == byte: data_type = 'b'
|
|
else: data_type = 'd'
|
|
print "Data type: ", data_type
|
|
|
|
tasks = []
|
|
if pause:
|
|
server.paused = True
|
|
try:
|
|
for i in range(images):
|
|
if i==0:
|
|
_create_tables(stream_value, paths, data_type, shape)
|
|
|
|
start = time.time()
|
|
stream_value = server.stream.take()
|
|
if parallel:
|
|
tasks.extend( fork((_append_frame,(stream_value, i, paths, data_type, shape)),) )
|
|
else:
|
|
_append_frame(stream_value, i, paths, data_type, shape)
|
|
|
|
if i< (images-1):
|
|
if interval<=0:
|
|
server.stream.waitCacheChange(10000)
|
|
else:
|
|
sleep_time = float(interval)/1000.0 - (time.time()-start)
|
|
time.sleep(max(sleep_time,0))
|
|
finally:
|
|
if pause:
|
|
server.paused = False
|
|
pass
|
|
|
|
print "Waiting finish persisting..."
|
|
join(tasks)
|
|
print "Done"
|
|
|
|
save_camera(camera_name, shared, cam_server, images, interval, True, True)
|
|
|
|
|
|
data_file = get_exec_pars().path
|
|
|
|
set_return(data_file) |