import datetime import tkinter import PIL.Image, PIL.ImageTk import tisgrabber as IC from PIL import Image, ImageTk class VideoRec: _shock_delivery = False def __init__(self, window): self.window = window # Create the camera object. self.camera = IC.TIS_CAM() self.devices = self.camera.GetDevices() for i in range(len(self.devices)): print(str(i) + " : " + str(self.devices[i])) self.camera.ShowDeviceSelectionDialog() self.frame_rate = 100 # fixed framerate of 100Hz self.camera.SetPropertySwitch("Exposure","Auto",0) self.camera.SetPropertyAbsoluteValue("Exposure","Value",0.01) # fixed exposure time of 10ms self.camera.SetFrameRate(self.frame_rate) self.record = False self.__createWidgets() # icon ico = Image.open('img\\Arrenberg-Lab-Logo-karminrot-64x41.png') photo = ImageTk.PhotoImage(ico) self.window.wm_iconphoto(False, photo) def __createWidgets(self): # create canvas self.canvas = tkinter.Canvas(self.window, width=self.camera.get_video_format_width(), height=self.camera.get_video_format_height()) self.canvas.grid(column=0, row=0, columnspan=4) # create buttons self.btn_record = tkinter.Button(self.window, text="start recording", command=self.__toggleStartRecord) self.btn_record.grid(column=1, row=1, ipadx=20, ipady=5, padx=3, pady=5) self.btn_snap = tkinter.Button(self.window, text="snapshot", command=self.saveSnapshot) self.btn_snap.grid(column=2, row=1, ipadx=20, ipady=5, padx=3, pady=5) self.lbl_record = tkinter.Label(self.window, text="", fg='red') self.lbl_record.grid(column=0, row=1, ipadx=20, ipady=5, padx=3, pady=5) def __flashRecord(self): bg = self.lbl_record.cget('background') fg = self.lbl_record.cget('foreground') self.lbl_record.config(bg=fg, fg=bg) if self.record: self.window.after(250, self.__flashRecord) def __toggleStartRecord(self): if self.btn_record['text'] == 'start recording': fourcc = cv2.VideoWriter_fourcc(*'MJPG') video_name = "recordings\\recording_" + str(datetime.datetime.now()) video_name = video_name.replace(' ', '_') video_name = video_name.replace(':', '-') video_name = video_name.replace('.', '-') self.video_name = video_name + ".avi" self.writer = cv2.VideoWriter(self.video_name, fourcc, self.frame_rate, (self.camera.get_video_format_width(), self.camera.get_video_format_height())) self.record = True self.btn_record.config(text='stop recording') print('sys: start recording') self.lbl_record.config(text='RECORDING', fg='red', bg=self.window.cget("background")) self.__flashRecord() elif self.btn_record['text'] == 'stop recording': self.record = False self.writer.release() self.btn_record.config(text='start recording') print("sys: stop recording") self.lbl_record.config(text='', fg=self.window.cget("background"), bg=self.window.cget("background")) def saveSnapshot(self): snapshot_name = "snapshots\\snapshot_" + str(datetime.datetime.now()) snapshot_name = snapshot_name.replace(' ', '_') snapshot_name = snapshot_name.replace(':', '-') snapshot_name = snapshot_name.replace('.', '-') cv2.imwrite(snapshot_name + ".png", self.image) print("sys: save snapshot") def openCamera(self): self.camera.StartLive(0) self.delay = 15 self.__update() self.window.protocol("WM_DELETE_WINDOW", self.fakeClose) def __update(self): self.camera.SnapImage() self.image = self.camera.GetImage() cv2.putText(self.image, str(datetime.datetime.now()), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0,)) if self._shock_delivery: cv2.putText(self.image, 'SHOCK', (20, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255)) if self.record: self.writer.write(self.image) self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(self.image)) self.canvas.create_image(0, 0, image=self.photo, anchor=tkinter.NW) self.window.after(self.delay, self.__update) def closeWindow(self): self.camera.StopLive() self.window.destroy() def fakeClose(self): pass