-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrPiHQCamServer2.py
750 lines (549 loc) · 26.9 KB
/
rPiHQCamServer2.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
##################################################################################
# PyCam2-Server for capturing images as RAW images.
# #
# How to use (variable explanation): #
# xCmd: Path to the 7-zip executable. #
# parentDir: Folder which is scanned recursevly for measurement-files. #
# picDir: Foldername of the subdir where the images for the data- #
# extraction are stored (mean-images of #
# _ConvertBayerToGrayScale.py) #
# saveDir: The second argument of the replace-function determines the #
# folder in which the extraction results are stored. #
# This can be used to separate source and destination folder. #
# opt: Is an instance of the PiMagePro options. This class has a #
# built in store-function to have the used extraction-option #
# as file on disk (see options below). #
# LogFilePath: If a filename is given, a logger-instance is created which #
# print the console messages to console as wall as to a file. #
# LogLen: Defines the length of a log-line (so that all logs have the #
# same length). #
# #
# 2023 © haum (OTH-Regensburg) #
##################################################################################
# Imports
import os
from os.path import join, basename, dirname, isdir, isfile
from time import time, sleep
import socket
import pickle
import numpy as np
# Custom libs
from _libHQCam2.archive import ArchiveFolder #, CompressFolder
from _libHQCam2.ramdisk import RAMDisk, CreateFolder4User
from _libHQCam2.misc import duration, how_long, DecodeBoolStr
from _libHQCam2.Logger import StdOutLogger, LogLineLeft, LogLineLeftRight
from picamera2.controls import Controls
from _libHQCam2.PiCam2 import PiCam2
### Defaults
# Paths
ramdisk = None # Object instance for the used RAM-Disk
mntPnt_RAMDisk = r"/media/ramdisk" # Mounting Point of the ramdisk; Comment out to avoid a RAMDISK
SDCardPath = r"/home/pi/Pictures/Captures" # Path if no RAM-Disk is used
imFolderPath = join(mntPnt_RAMDisk, "Captures") # Path to image folder
# Static responses
ackStr = "ack" # Standard-Response on success
nakStr = "nak" # Standard-Response on failure
# Server settings
port = 5060 # Socket-Port
servSock = None # Object instance the socket
servConn = None # Object instance the client-connection
# Camera settings
# See also into SetupCamera2
cam = None # Object instance of PiCam2
# Server settings
srvr_ClipWinBayer = [4056,3040] # Default Clip-Window:
# - [width, height] : Imagewidth and -height around the center
# - [X1, Y1, width, height] : Imagewidth and -height starting on the left upper corner (X1, Y1)
# To avoid any problems, use only even numbers (0, 2, 4, ...).
# Odd numbers lead to half-indicies (*.5) which are not exist!
srvr_DemosaicClippedBayerImgs = False # True: Server saves demosaicked images; False: Server saves RAW Bayer images
srvr_ShrinkHalfDemosaicedIterations = 0 # 2^x pixels in X and Y are combined to one value (artificial pixel-binning)
# Logger (can be used optional)
# logFilePath = "/home/pi/RPiHQCam2.log"
logger = None # None = No log-file; Str-Path = Log-File is created
# Local Server-Functions
def SetupServer():
"""Sets up the server-socket and returns the instance.
Returns:
socket: Server-socket
"""
LogLineLeft("Creating socket")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # SO_REUSEADDR=1: Allow reuse when socket-port already exists
print("ok")
LogLineLeft("Binding socket")
try:
s.bind(('', port))
except socket.error as msg:
print(msg)
print("ok")
return s
def AwaitIncomingConnection(socket:socket):
"""Uses the given socket to check if there are incomming connections.
Args:
socket (socket): Server-socket which is checked for connection-requests.
Returns:
connection: Returns the accepted socket-connection.
"""
print("Awaiting connection request from client...")
socket.listen(5) # Allow up to 5 connections
sConn, connIP = socket.accept()
LogLineLeftRight("Client connected:", f"{connIP[0]}:{connIP[1]}")
return sConn
def SetupCamera2(fr=10.0):
"""Sets up the PiCam2 object instance.
!!! Note: It directly writes the instance to the global cam-variable !!!
Args:
fr (float, optional): FrameRate in Frames Per Second (FPS). Defaults to 10.0.
Returns:
PiCam2: Camera object instance.
"""
global cam
# Init PiCam2
cam = PiCam2(fr=fr)
LogLineLeftRight("Reading IDN:", cam.IDN())
return
def IDN():
"""Grabs the ID-String of the camera-object.
Returns:
str: IDN-String of the PiCam2 instance.
"""
return cam.IDN()
def ECHO(payloadArr):
"""Builds an space separated string from all arguments of the given iterable object.
Args:
payloadArr (iterable, str): Iterable object with strings to echo.
Returns:
str: Space separated string created from the iterable object elements.
"""
return " ".join(payloadArr)
def Server_ClipWinBayerImage(ClipWinBayerByServer):
"""Adjusts the clip-windows for pre-clipping.
Args:
ClipWinBayerByServer ([(x,y,)w,h]]): Imagewidth and -heigt (w,h) around the center or, if given, starting at (x,y) = left upper corner.
Returns:
str: Standard "ack" or "nak"
"""
global srvr_ClipWinBayer
clpWin = [int(val) for val in ClipWinBayerByServer.split(":")]
nVals = len(clpWin)
if nVals == 2 or nVals == 4:
for iVal in range(nVals):
oldVal = clpWin[iVal]
clpWin[iVal] = clpWin[iVal] - (clpWin[iVal] % 2)
if oldVal != clpWin[iVal]: # Value was corrected
print(f"Adjusted (odd) clipWin[{iVal}] value: {oldVal} -> {clpWin[iVal]}")
srvr_ClipWinBayer = clpWin
return ackStr
return nakStr
def Server_DemosaicClippedBayerImgs(DebayerByServer:bool):
"""Adjusts the option if the PyCam2 Server should demosaick the images before save.
Args:
DebayerByServer (bool): Demosaick before save True or False
Returns:
str: Standard "ack" or "nak"
"""
global srvr_DemosaicClippedBayerImgs
srvr_DemosaicClippedBayerImgs = DecodeBoolStr(DebayerByServer)
if not srvr_DemosaicClippedBayerImgs: # Shrinking needs debayered data
Server_SWPixelBinning("0")
return ackStr
def Server_SWPixelBinning(pxBinIters:int):
"""Adjusts if the server should combine pixels in x and y direction by software (pixel-binning)
Args:
pxBinIters (int): 2^pxBinIters pixels are combined in x and y to one pixel.
Returns:
str: Standard "ack" or "nak"
"""
global srvr_ShrinkHalfDemosaicedIterations
srvr_ShrinkHalfDemosaicedIterations = int(pxBinIters)
if srvr_ShrinkHalfDemosaicedIterations < 0:
srvr_ShrinkHalfDemosaicedIterations = 0
# raise Exception("ShrinkHalf - Can't iterate negative.") # Old
if srvr_ShrinkHalfDemosaicedIterations > 0: # For shrinking, data must be debayered!
Server_DemosaicClippedBayerImgs("1")
return ackStr
# def Server_Compress(compressPath:str, tarGzFName:str, Multicore=True, SuppressParents=True):
# sCmprss = time()
# retVal = CompressFolder(compressPath, tarGzFName, Multicore, SuppressParents)
# how_long(sCmprss, "Compressing")
# return ackStr if retVal == 0 else nakStr
def Server_Archive(archiveFolderPath:str, archiveFName:str, compress:bool=True, multicore:bool=True, suppressParents:bool=True):
"""Converts the given folder into archive. Optionally it can compress the archive and use multicore to speed up the compression.
Args:
archiveFolderPath (str): Folderpath which should be archived.
archiveFName (str): Target archive filename.
compress (bool, optional): Should the *.tar get compressed to *.tar.gz. Defaults to True.
multicore (bool, optional): Use multiple cores to compress. Defaults to True.
suppressParents (bool, optional): Only archive the foldercontents not including the parent folder (archiveFolderPath). Defaults to True.
Returns:
str: Standard "ack" or "nak"
"""
sCmprss = time()
retVal = ArchiveFolder(archiveFolderPath, archiveFName, compress, multicore, suppressParents)
how_long(sCmprss, "Archiving" + ("+Compression" if compress else ""))
return ackStr if retVal == 0 else nakStr
def ConfAwait(tVal, SetFunc, GetFunc, LoBnd=0.95, HiBnd=1.05, MaxTries=10):
"""Generic waiting function if a value which was configured to the camera is in the target tolerance.
For that it uses the given set and get functions including tolerance-values and a maximum try-counter.
Args:
tVal (_type_): Target value or iterable object with values which should be set.
SetFunc (_type_): Function to set the target value.
GetFunc (_type_): Function to read out the current value.
LoBnd (float, optional): Lower tolerance in %. Defaults to 0.95.
HiBnd (float, optional): Upper tolernace in %. Defaults to 1.05.
MaxTries (int, optional): Amount of tries before returning failure. Defaults to 10.
Returns:
_type_, bool: Returns the current value set and if a timeout-occured (more tries than MaxTries)
"""
SetFunc(tVal)
try:
valIsList = True
_ = (e for e in tVal) # Dummy if exception occures -> Then its not iterable
except TypeError:
valIsList = False
tVal = [tVal]
# Do the checks
nVals = len(tVal)
vInRange = [False] * nVals
# start = time()
while MaxTries > 0:
cVal = GetFunc()
for _iVal in range(len(tVal)):
_tVal = tVal[_iVal]
lo = _tVal * LoBnd
hi = _tVal * HiBnd
if valIsList:
_cVal = cVal[_iVal]
else:
_cVal = cVal
if _cVal >= lo and _cVal <= hi:
vInRange[_iVal] = True
# sleep(0.05) # GetFunc always gets via cam.capture_metadata() which waits for a frame
if _iVal == (nVals-1) and all(vInRange):
break
MaxTries -= 1
# how_long(start, "inner loop")
timedout = True if MaxTries <= 0 else False
return cVal, timedout
def ConfShutterspeed(tVal:int, LoBnd:float=0.95, HiBnd:float=1.05):
"""Adjusts the shutterspeed.
Args:
tVal (int): Shutterspeed value in µs
LoBnd (float, optional): Lower tolerance in %. Defaults to 0.95.
HiBnd (float, optional): Upper tolerance in %. Defaults to 1.05.
Returns:
str: Standard "ack" or "nak"
"""
start = time()
tVal = int(tVal)
###### Old testcode, as there was problems during the development -> Get removed in future.
### startSet = time()
### cam.SetSS(tVal)
### cam.GetCamera().set_controls({"ExposureTime": tVal})
### how_long(startSet, "SetSS")
###
### startCheck = time()
### i = 0
### metadata = cam.GetCamera().capture_metadata() # dauert immer einen frame -> 1/fps
### cur_ss = metadata["ExposureTime"]
### while tVal<(cur_ss*0.95) or tVal>(cur_ss*1.05):
### # time.sleep(0.02)
### #start_loop=time.time()
### metadata = cam.GetCamera().capture_metadata() # dauert immer einen frame -> 1/fps
### cur_ss = metadata["ExposureTime"]
### #start_loop=how_long(start_loop, 'Schleife')
### print('ist: '+str(cur_ss)+', It: '+str(i))
### i = i + 1
### how_long(startCheck, "SS await")
### how_long(start, str.format("SS-Change S:{}, I:{} - Timeout:{}", tVal, cur_ss, False))
if tVal > 0:
cVal, TO = ConfAwait(tVal, cam.SetSS, cam.GetSS, LoBnd=LoBnd, HiBnd=HiBnd, MaxTries=15)
else:
cam.SetSS(tVal)
cVal = cam.GetSS()
TO = False
### how_long(startCheck, "SS await")
how_long(start, str.format("SS-Change S:{}, I:{} - Timeout:{}", tVal, cVal, TO))
return ackStr, cVal, TO
def ConfAnalogGain(tVal:str="1.0"):
"""Adjusts the analog gain value.
Args:
tVal (str, optional): Analog gain. Defaults to "1.0".
Returns:
str: Standard "ack" or "nak"
"""
start = time()
tVal = float(tVal)
cVal, TO = ConfAwait(tVal, cam.SetAG, cam.GetAG)
how_long(start, str.format("AG-Change S:{}, I:{} - Timeout:{}", tVal, cVal, TO))
return ackStr
def ConfWhiteBalance(tVal="1.0:1.0"):
"""Configures auto-white-balance (AWB).
Args:
tVal (str, optional): (R:B) values for AWB. Defaults to "1.0:1.0".
Returns:
str: Standard "ack" or "nak"
"""
start = time()
tVal = [float(v) for v in tVal.split(":")]
cVal, TO = ConfAwait(tVal, cam.SetAWB, cam.GetAWB)
how_long(start, str.format("AWB-Change S:{}, I:{} - Timeout:{}", tVal, cVal, TO))
return ackStr
def ConfScalerCrop(offsetXY="0:0", sizeWH="4056:3040"):
"""Should be a method to direct clip images from the sensor, but didn't worked correctly?
!!! Note: This method is implemented, but basically never used and therefore the full image resolution is always used !!!
Args:
offsetXY (str, optional): (R:B) values of the AWB for Red and Blue. Defaults to "0:0".
sizeWH (str, optional): Image width and height. Defaults to "4056:3040".
Returns:
str: Standard "ack" or "nak"
"""
start = time()
offsetXY = list(offsetXY.split(":"))
sizeWH = list(sizeWH.split(":"))
tVal = [int(v) for v in offsetXY + sizeWH]
cVal, TO = ConfAwait(tVal, cam.SetScalerCrop, cam.GetScalerCrop)
how_long(start, str.format("ScalerCrop-Change S:{}, I:{} - Timeout:{}", tVal, cVal, TO))
return ackStr
def ConfFramerate(FR="10.0"):
"""Adjusts the framerate.
Args:
FR (str, optional): Framerate in Frames Per Second (FPS). Defaults to "10.0".
Returns:
str: Standard "ack" or "nak"
"""
start = time()
tVal = float(FR)
cVal, TO = ConfAwait(tVal, cam.SetFR, cam.GetFR)
how_long(start, str.format("FrameRate-Change S:{}, I:{} - Timeout:{}", tVal, cVal, TO))
return ackStr
def CaptureShutterspeedSequence(Prefix:str, StorePath:str, SS:str="1000:3150:10000:31500", nPics:str="3", tMax:str="3.0", SaveSSLog:str="True"):
"""Captures a sequence of raw images and stores them on (ram)disk.
Args:
Prefix (str): Image-prefix.
StorePath (_type_): Path where the images are stored.
SS (str, optional): A set of shutterspeeds separated by ":". Defaults to "1000:3150:10000:31500".
nPics (int, optional): Number of images per SS. Defaults to 3.
tMax (float, optional): Maximum time before a warning is generated (not implemented yet!). Defaults to 3.0.
SaveSSLog (bool, optional): Append a shutterspeed-log into the image folder. Defaults to True.
Raises:
Exception: When no store-path is given, an exception is thrown.
Returns:
str: Standard "ack" or "nak"
"""
global cam, srvr_ClipWinBayer # Used for presetting SS
SS = [int(_ss) for _ss in SS.split(":")] # ShutterSpeeds -> int
nPics = int(nPics) # nPics -> int
tMax = float(tMax) # TimeMax (interval) -> float
SaveSSLog = DecodeBoolStr(SaveSSLog) # SaveSSLog (True/False, 1/0, yes/no) -> bool
if StorePath == None or StorePath == "":
raise Exception("CaptureSequence() - No storage-path given.")
if SaveSSLog:
ssLogStr = ""
####### Calculate crop-coordinates #######
# w32 = 4064 # Captured array-Width aligned to 32
# h16 = 3040 # Captured array-Height aigned to 16
if len(srvr_ClipWinBayer) == 2: # Only Size is given!
w4k = 4056 # Px-Width of raw bayer-data
h4k = 3040 # Px-Height of raw bayer-data
wWin = srvr_ClipWinBayer[0] # Is window-Px-Width when only size is given
hWin = srvr_ClipWinBayer[1] # Is window-Px-Height when only size is given
x1 = (w4k - wWin) // 2
y1 = (h4k - hWin) // 2
else: # Offset + Size given
wWin = srvr_ClipWinBayer[2] # Is window-Px-Width when offset + size is given
hWin = srvr_ClipWinBayer[3] # Is window-Px-Height when offset + size is given
x1 = srvr_ClipWinBayer[0] # Is offsetX when offset + size is given
y1 = srvr_ClipWinBayer[1] # Is offsetY when offset + size is given
x2 = x1 + wWin
y2 = y1 + hWin
if srvr_ClipWinBayer:
cx1 = int(x1 * 1.5) # * 1.5 (12bit / 8bit)
cx2 = int(x2 * 1.5) # * 1.5 (12bit / 8bit)
cy1 = int(y1) # / 1 = Height not affected by bit-size
cy2 = int(y2) # / 1 = Height not affected by bit-size
####### Take the pictures #######
sSeq = time()
cntSS = len(SS)
for _iSS in range(cntSS):
_tSS = SS[_iSS] # Grab ss directly from list
_ack, _cSS, _TO = ConfShutterspeed(_tSS)
### Build filenames ###
fNames = []
for _iPic in range(nPics): # Prepare filenames
fName = str.format("{}_ss={}_{}.{}", Prefix, "<SS>", str(_iPic).zfill(4), "raw" )
fNames.append(join(StorePath, fName))
### Raw-capture the images into a list ###
raws = []
if SaveSSLog:
rawMetas = []
dCaps = []
sCapAll = time()
for _iPic in range(nPics):
sCap = time()
raw, meta = cam.GetCamera().capture_arrays(["raw"])
raws.append(raw[0])
if SaveSSLog:
rawMetas.append(meta)
dCaps.append(duration(sCap))
if _tSS > 0:
fNames[_iPic] = fNames[_iPic].replace("<SS>", str(_tSS))
else:
fNames[_iPic] = fNames[_iPic].replace("<SS>", str(meta["ExposureTime"]))
print(f"Capturing {fNames[_iPic]} @Gain:{str(meta['AnalogueGain'])} took {dCaps[-1]:.3f}")
dCapAll = duration(sCapAll)
print(f"Raw Capturing of SS-Sequence took {dCapAll:.3f}")
### Preset to next SS, so that the camera settles during the following code runs ###
if _iSS < (cntSS-1): # Preset only, if not already the last SS -> set back to SS[0] at the end of this method
LogLineLeftRight(f"Presetting SS={SS[_iSS + 1]}:", "ok")
cam.SetSS(SS[_iSS + 1])
### Do post-processing to the images ###
sPostProcessingAll = time()
dPostProcessings = []
for _iPic in range(nPics):
sPostProcessing = time()
raw = raws[_iPic] # Grab current image as numpy
raw = raw[cy1:cy2, cx1:cx2] # Preclip bayer data to reduce the amount of data to handle
raw = raw.astype(np.uint16) # Target-Type needs to be uint16. uint8 can clip the 4 upper MSB when shifting up
if srvr_DemosaicClippedBayerImgs: # Debayer residual data
dMosaic = np.zeros((hWin, wWin), dtype=np.uint16)
for byte in range(2):
dMosaic[:, byte::2] = ( (raw[:, byte::3] << 4) | ((raw[:, 2::3] >> (byte * 4)) & 0b1111) )
raw = dMosaic
if srvr_ShrinkHalfDemosaicedIterations > 0:
# raw = (raw[::2, ::2] + raw[1::2, ::2] + raw[::2, 1::2] + raw[1::2, 1::2]) # Old code without saturation detection
# Reduce resolution of image and mask to 507x380 by addition ( //4 für Mittelung)
satBright=0xFFF # 0xFFF = (2**12)-1 = Maximum Sensor-Value = Saturation
satMask = (raw >= satBright) # mark saturated pixels
for _iShrinkIter in range(srvr_ShrinkHalfDemosaicedIterations): # Combines 2x2 Pxls per iteration!
satMask = (satMask[::2, ::2] | satMask[1::2, ::2] | satMask[::2, 1::2] | satMask[1::2, 1::2])
raw = (raw[::2, ::2] + raw[1::2, ::2] + raw[::2, 1::2] + raw[1::2, 1::2]) #// 4 #jeweils halbiert
raw[satMask]=0xFFFF # 0xFFFF = (2**16)-1 = Maximum uint16-value to clearly mark saturated pixels
dPostProcessings.append(duration(sPostProcessing))
fName = fNames[_iPic]
print(f"PostProcessing {fName} took {dPostProcessings[-1]:.3f}")
dPostProcessingAll = duration(sPostProcessingAll)
print(f"All Post-processing took {dPostProcessingAll:.3f}")
### Saving the postprocessed images ###
for _iPic in range(nPics):
fName = fNames[_iPic]
sSav = time()
f = open(fName, "wb")
pickle.dump(raw, f)
f.close()
dSav = duration(sSav)
print(f"Saving {fName} took {dSav:.3f}")
if SaveSSLog:
_cSS = rawMetas[_iPic]["ExposureTime"]
_fd = rawMetas[_iPic]["FrameDuration"]
_dCap = dCaps[_iPic]
ssLogStr += str.format("fName:{};tCap:{:.3f};tSav{:.3f};sSS:{};iSS:{};FD:{}\n", fName, _dCap, dSav, _tSS, _cSS, _fd)
how_long(sSeq, "Entire CaptureShutterspeedSequence")
LogLineLeftRight(f"Presetting SS={SS[0]}:", "ok")
cam.SetSS(SS[0]) # Preset the fastest SS for next call
# Save the SS-Log
if SaveSSLog:
f = open(join(StorePath, f"{Prefix}_SSCapture.log"), "w")
f.writelines(ssLogStr)
f.close()
return ackStr
### Start script ###
# Init logger if wanted
if "logFilePath" in locals():
logger = StdOutLogger(logFilePath)
# (Re-)Create RAMDisk
if "mntPnt_RAMDisk" in locals() and mntPnt_RAMDisk != None:
sRAMDisk = time()
if(not isdir(mntPnt_RAMDisk)):
os.system(f"mkdir -p {mntPnt_RAMDisk}")
ramdisk = RAMDisk(mntPnt_RAMDisk)
CreateFolder4User(imFolderPath)
LogLineLeftRight("Setup RAMDisk took", f"{duration(sRAMDisk):.3f}s")
# Create Camera
SetupCamera2(10.0)
# ConfShutterspeed(0)
# for _i in range(100):
# CaptureShutterspeedSequence(Prefix=f"TestImg_#{_i:04d}", StorePath=imFolderPath, SS="0", nPics="3")
# (Re-)Create Server
sServer = time()
servSock = SetupServer()
LogLineLeftRight("Server set up in: ", f"{duration(sServer):.3f}s")
servConn = AwaitIncomingConnection(servSock)
# Run main server-loop
LogLineLeftRight("Starting main server-loop", "ok")
keepConnection = True
closeApp = False
excptnCnt = 0
while keepConnection: # as long the connection is active, iterate infinite
try:
# Receive the data
waited4Msg = time()
LogLineLeft("Receive data")
rcvd = servConn.recv(1024) # receive the data
rcvd = rcvd.decode('utf-8')
print("ok")
LogLineLeftRight("Received: ", rcvd)
LogLineLeftRight("Waited for Receive:", f"{(time() - waited4Msg):.3f} s")
dataMessage = rcvd.split(' ') # Split the incomming message
cmd = dataMessage[0] # Get command and
payload = dataMessage[1:] # the arguments separately
############## If-Elif-Else command structure ##############
####### Capture (most used) #######
# print(cmd)
if cmd == 'CAP:SEQFET':
reply = CaptureShutterspeedSequence(Prefix=payload[0], StorePath=imFolderPath, SS=payload[1], nPics=payload[2], tMax=payload[3], SaveSSLog=payload[4])
elif cmd == "SRV:ARCHV":
reply = Server_Archive(archiveFolderPath=payload[0], archiveFName=payload[1], compress=DecodeBoolStr(payload[2]), multicore=DecodeBoolStr(payload[3]), suppressParents=DecodeBoolStr(payload[4]))
####### Camera Conf #######
elif cmd == "CAM:CONF:SS": # ShutterSpeed (SS)
reply = ConfShutterspeed(payload[0])[0] # Only get Ack-String (index: 0)
elif cmd == "CAM:CONF:FR": # FrameRate (FR)
reply = ConfFramerate(payload[0])
elif cmd == "CAM:CONF:AG": # AnalogGain
reply = ConfAnalogGain(payload[0])
elif cmd == "CAM:CONF:AWB": # AutoWhiteBalance
reply = ConfWhiteBalance(payload[0])
elif cmd == "CAM:CONF:SCLCRP": # SCaLerCRoP (Camera-Internal precrop of the image!)
reply = ConfScalerCrop(payload[0], payload[1])
####### Server Image Commands #######
elif cmd == "SRV:IMG:BCLP": # Clip of bayer-data by server
reply = Server_ClipWinBayerImage(payload[0])
elif cmd == "SRV:IMG:DBAY": # Do a debayer of the image
reply = Server_DemosaicClippedBayerImgs(payload[0])
elif cmd == "SRV:IMG:SRNK": # Shrink size by half after debayer
reply = Server_SWPixelBinning(payload[0])
####### Server Common Commands #######
elif cmd == "IDN?":
reply = IDN()
elif cmd == "SRV:ECHO":
reply = ECHO(payload)
elif cmd == "SRV:PATH:RDDIR?":
reply = mntPnt_RAMDisk
elif cmd == "SRV:PATH:SDDIR?":
reply = SDCardPath
elif cmd == "SRV:PATH:IMDIR?":
reply = imFolderPath
elif cmd == "SRV:CLOSE":
keepConnection = False
closeApp = True
reply = ackStr
else:
reply = 'Unknown Command'
####### Command Tree finished #######
# Response
servConn.sendall(str.encode(reply))
LogLineLeftRight("Sent response", reply)
except Exception as e:
LogLineLeftRight("Exception occured:", e)
excptnCnt += 1
if excptnCnt > 3: # 3 attemps ok!
keepConnection = False
continue
# conn.close()
LogLineLeftRight("Closed connection", "ok")
if logger != None:
LogLineLeft("Flushing logger")
logger.close() # Flush last log-lines and dispose
print("ok")
print("Closing app. Bye...")