Created
April 7, 2022 15:36
-
-
Save fredvol/fa817dc8ee193f3cda9be672a848e2f8 to your computer and use it in GitHub Desktop.
saving frame from crappy based on gui state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% | |
#################################################################### | |
# wind tunel crappy file | |
# | |
# This script only read the data send via usb/serial by an esp32 | |
# | |
# | |
# 17/03/2022 | |
# https://crappy.readthedocs.io/en/latest/installation.html | |
#%% import | |
import datetime | |
from csv import reader | |
from pathlib import Path | |
import crappy | |
from crappy._global import CrappyStop | |
from time import time | |
import tkinter as tk | |
#%% CONFIG | |
# constant | |
offset_diff_pression = 174 # Pa | |
rho_air = 1.22 # kg/m3 | |
# Log | |
log_folder = "logs" | |
study_name = "cam_log_fps" | |
path_raw = Path(__file__).parent / log_folder / study_name / "data_raw.csv" | |
path_event = Path(__file__).parent / log_folder / study_name / "event.csv" | |
path = Path(__file__).parent / log_folder / study_name / "data.csv" | |
# %% class definition | |
# GUI | |
class WT_GUI(crappy.blocks.Block): | |
"""Block to send a signal based on a user input.""" | |
def __init__( | |
self, | |
label: str = ["t0", "step", "state", "cmd"], | |
spam: bool = False, | |
font_label=("Arial", 15), | |
) -> None: | |
super().__init__() | |
self.spam = spam # Send the values only once or at each loop ? | |
self.i = 0 # The value to be sent | |
self.state = False # The value to be sent | |
self.t_last_change = datetime.datetime.now().timestamp() # The value to be sent | |
self.label_font = font_label | |
self.abort = False | |
if isinstance(label, list): | |
self.labels = label | |
else: | |
self.labels = ["t0", label] | |
def prepare(self) -> None: | |
self.root = tk.Tk() | |
self.root.title("GUI block") | |
self.root.protocol("WM_DELETE_WINDOW", self.end) | |
self.label_title = tk.Label( | |
self.root, | |
text=f"Study name : {study_name} \n ---- \n", | |
font=self.label_font, | |
) | |
self.label_title.pack() | |
self.label = tk.Label(self.root, text="step: 0", font=self.label_font) | |
self.label.pack() | |
self.label_state = tk.Label( | |
self.root, text=f"State: {self.state}", font=self.label_font | |
) | |
self.label_state.pack() | |
self.label_run_time = tk.Label( | |
self.root, | |
text=f"run: {datetime.datetime.now().timestamp() - self.t_last_change} s", | |
font=self.label_font, | |
) | |
self.label_run_time.pack() | |
self.button = tk.Button( | |
self.root, text="Next step WT", command=self.callback, bg="#5CA098" | |
) | |
self.button.pack() | |
self.button_state = tk.Button( | |
self.root, text="toogle state", command=self.callback_state, bg="#5CA098" | |
) | |
self.button_state.pack() | |
self.send([0, self.i, self.state]) | |
def loop(self) -> None: | |
self.label_run_time.configure( | |
text=f"run: {round(datetime.datetime.now().timestamp() - self.t_last_change)} s" | |
) | |
if self.spam: | |
self.send([time() - self.t0, self.i, self.state]) | |
if self.abort: | |
raise CrappyStop | |
self.root.update() | |
def end(self) -> None: | |
self.abort = True | |
def callback(self) -> None: | |
self.i += 1 | |
self.send([time() - self.t0, self.i, self.state]) | |
self.label.configure(text="step: %d" % self.i) | |
def callback_state(self) -> None: | |
self.state = not self.state | |
self.t_last_change = datetime.datetime.now().timestamp() | |
self.send([time() - self.t0, self.i, self.state]) | |
if self.state: | |
color_state = "#CDFF7F" | |
else: | |
color_state = "red" | |
self.label_state.configure(text="State: %s" % self.state, bg=color_state) | |
def finish(self) -> None: | |
self.root.destroy() | |
# save frame | |
class save_cam(crappy.Block): | |
"""to record img only when state = ON from mgui""" | |
def __init__( | |
self, | |
): # Optional | |
# If you define your own constructor, do not forget to init Block ! | |
super().__init__() | |
# Example argument, here the period of the sawtooth | |
self.state = False | |
def prepare(self): # Optional | |
pass | |
def begin(self): # Optional | |
pass | |
def loop(self): | |
try: | |
for link in self.inputs: | |
d = link.recv_last() | |
if d is not None: | |
print( | |
d.keys() | |
) # just try to visual the dict passed trought the link | |
except: | |
pass | |
def finish(self): # Optional | |
print("==> finish has been called") | |
#%% MAIN | |
if __name__ == "__main__": | |
# BLOCKS | |
record_event = crappy.blocks.Recorder(filename=path_event) | |
save_cam_blck = save_cam() | |
espion = crappy.blocks.Reader() | |
mgui = WT_GUI(spam=False) | |
# camera | |
camera = crappy.blocks.Camera( | |
camera="Webcam", | |
verbose=True, | |
max_fps=20, | |
) | |
disp = crappy.blocks.Displayer(framerate=30) | |
# LINKS | |
crappy.link(mgui, record_event) | |
crappy.link(mgui, save_cam_blck) | |
crappy.link(camera, save_cam_blck) | |
crappy.link(camera, disp) | |
crappy.start() | |
# Versions: | |
# python: sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0) | |
# crappy: 1.5.7 | |
# numpy: 1.22.0 | |
# pandas: 1.4.0rc0 | |
# serial: 3.5 | |
#################################################################"" | |
# %% | |
# https://askubuntu.com/questions/348838/how-to-check-available-webcams-from-the-command-line | |
## kill cam when lock after crappy crash | |
# fuser /dev/video0 | |
# kill -9 22160 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment