videoRec.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import datetime
  2. import tkinter
  3. import PIL.Image, PIL.ImageTk
  4. import tisgrabber as IC
  5. from PIL import Image, ImageTk
  6. class VideoRec:
  7. _shock_delivery = False
  8. def __init__(self, window):
  9. self.window = window
  10. # Create the camera object.
  11. self.camera = IC.TIS_CAM()
  12. self.devices = self.camera.GetDevices()
  13. for i in range(len(self.devices)):
  14. print(str(i) + " : " + str(self.devices[i]))
  15. self.camera.ShowDeviceSelectionDialog()
  16. self.frame_rate = 100 # fixed framerate of 100Hz
  17. self.camera.SetPropertySwitch("Exposure","Auto",0)
  18. self.camera.SetPropertyAbsoluteValue("Exposure","Value",0.01) # fixed exposure time of 10ms
  19. self.camera.SetFrameRate(self.frame_rate)
  20. self.record = False
  21. self.__createWidgets()
  22. # icon
  23. ico = Image.open('img\\Arrenberg-Lab-Logo-karminrot-64x41.png')
  24. photo = ImageTk.PhotoImage(ico)
  25. self.window.wm_iconphoto(False, photo)
  26. def __createWidgets(self):
  27. # create canvas
  28. self.canvas = tkinter.Canvas(self.window, width=self.camera.get_video_format_width(),
  29. height=self.camera.get_video_format_height())
  30. self.canvas.grid(column=0, row=0, columnspan=4)
  31. # create buttons
  32. self.btn_record = tkinter.Button(self.window, text="start recording", command=self.__toggleStartRecord)
  33. self.btn_record.grid(column=1, row=1, ipadx=20, ipady=5, padx=3, pady=5)
  34. self.btn_snap = tkinter.Button(self.window, text="snapshot", command=self.saveSnapshot)
  35. self.btn_snap.grid(column=2, row=1, ipadx=20, ipady=5, padx=3, pady=5)
  36. self.lbl_record = tkinter.Label(self.window, text="", fg='red')
  37. self.lbl_record.grid(column=0, row=1, ipadx=20, ipady=5, padx=3, pady=5)
  38. def __flashRecord(self):
  39. bg = self.lbl_record.cget('background')
  40. fg = self.lbl_record.cget('foreground')
  41. self.lbl_record.config(bg=fg, fg=bg)
  42. if self.record:
  43. self.window.after(250, self.__flashRecord)
  44. def __toggleStartRecord(self):
  45. if self.btn_record['text'] == 'start recording':
  46. fourcc = cv2.VideoWriter_fourcc(*'MJPG')
  47. video_name = "recordings\\recording_" + str(datetime.datetime.now())
  48. video_name = video_name.replace(' ', '_')
  49. video_name = video_name.replace(':', '-')
  50. video_name = video_name.replace('.', '-')
  51. self.video_name = video_name + ".avi"
  52. self.writer = cv2.VideoWriter(self.video_name, fourcc, self.frame_rate, (self.camera.get_video_format_width(),
  53. self.camera.get_video_format_height()))
  54. self.record = True
  55. self.btn_record.config(text='stop recording')
  56. print('sys: start recording')
  57. self.lbl_record.config(text='RECORDING', fg='red', bg=self.window.cget("background"))
  58. self.__flashRecord()
  59. elif self.btn_record['text'] == 'stop recording':
  60. self.record = False
  61. self.writer.release()
  62. self.btn_record.config(text='start recording')
  63. print("sys: stop recording")
  64. self.lbl_record.config(text='', fg=self.window.cget("background"), bg=self.window.cget("background"))
  65. def saveSnapshot(self):
  66. snapshot_name = "snapshots\\snapshot_" + str(datetime.datetime.now())
  67. snapshot_name = snapshot_name.replace(' ', '_')
  68. snapshot_name = snapshot_name.replace(':', '-')
  69. snapshot_name = snapshot_name.replace('.', '-')
  70. cv2.imwrite(snapshot_name + ".png", self.image)
  71. print("sys: save snapshot")
  72. def openCamera(self):
  73. self.camera.StartLive(0)
  74. self.delay = 15
  75. self.__update()
  76. self.window.protocol("WM_DELETE_WINDOW", self.fakeClose)
  77. def __update(self):
  78. self.camera.SnapImage()
  79. self.image = self.camera.GetImage()
  80. cv2.putText(self.image, str(datetime.datetime.now()), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0,))
  81. if self._shock_delivery:
  82. cv2.putText(self.image, 'SHOCK', (20, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
  83. if self.record:
  84. self.writer.write(self.image)
  85. self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(self.image))
  86. self.canvas.create_image(0, 0, image=self.photo, anchor=tkinter.NW)
  87. self.window.after(self.delay, self.__update)
  88. def closeWindow(self):
  89. self.camera.StopLive()
  90. self.window.destroy()
  91. def fakeClose(self):
  92. pass