video_player.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function, division
  4. import nixio as nix
  5. import math
  6. import cv2
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. import matplotlib.animation as animation
  10. import matplotlib
  11. matplotlib.use('TkAgg')
  12. class Playback(object):
  13. def __init__(self, fig, video_array, tracking_tag=None, show_orientation=False):
  14. self.figure = fig
  15. self.axis = fig.add_subplot(111)
  16. self.im = None
  17. self.data = video_array
  18. self.height, self.width, self.channels, self.nframes = self.data.shape
  19. dim = video_array.dimensions[-1]
  20. ticks = dim.ticks
  21. self.interval = np.mean(np.diff(ticks))
  22. self.tag = tracking_tag
  23. if self.tag is not None:
  24. self.positions = self.tag.positions
  25. self.orientations = self.tag.features[0].data
  26. self.tracked_indices = self.__track_indices(ticks, self.positions[:,3])
  27. self.x = self.positions[:,0]
  28. self.y = self.positions[:,1]
  29. self.track_counter = 0
  30. self.draw_orientation = show_orientation
  31. def __track_indices(self, ticks, times):
  32. indices = np.zeros_like(times)
  33. for i,t in enumerate(times):
  34. indices[i] = np.argmin(np.abs(np.asarray(ticks) - t*1000))
  35. return indices
  36. def __draw_circ(self, frame, x_pos, y_pos):
  37. radius = 8
  38. y, x = np.ogrid[-radius: radius, -radius: radius]
  39. index = x**2 + y**2 <= radius**2
  40. frame[y_pos-radius:y_pos+radius, x_pos-radius:x_pos+radius, 0][index] = 255
  41. frame[y_pos-radius:y_pos+radius, x_pos-radius:x_pos+radius, 1][index] = 0
  42. frame[y_pos-radius:y_pos+radius, x_pos-radius:x_pos+radius, 2][index] = 0
  43. return frame
  44. def __draw_line(self, frame, x, y, phi):
  45. length = 20
  46. dx = math.sin(phi/360.*2*np.pi) * length
  47. dy = math.cos(phi/360.*2*np.pi) * length
  48. cv2.line(frame, (int(x-dx),int(y+dy)),
  49. (int(x+dx), int(y-dy)), (250,255,0), 2)
  50. return frame
  51. def grab_frame(self, i):
  52. frame = self.data[:,:,:,i]
  53. if self.tag is not None:
  54. if i in self.tracked_indices:
  55. frame = self.__draw_circ(frame, self.x[self.track_counter],
  56. self.y[self.track_counter])
  57. if self.draw_orientation:
  58. frame = self.__draw_line(frame,self.x[self.track_counter],
  59. self.y[self.track_counter],
  60. self.orientations[self.track_counter])
  61. self.track_counter += 1
  62. if self.im == None:
  63. im = self.axis.imshow(frame)
  64. else:
  65. im.set_data(frame)
  66. return im,
  67. def start(self):
  68. ani = animation.FuncAnimation(self.figure, self.grab_frame,
  69. range(1,self.nframes,1), interval=self.interval,
  70. repeat=False, blit=True)
  71. plt.show()
  72. if __name__ == '__main__':
  73. import nix
  74. import numpy as np
  75. import matplotlib
  76. import matplotlib.pyplot as plt
  77. nix_file = nix.File.open('../data/tracking_data.h5', nix.FileMode.ReadOnly)
  78. b = nix_file.blocks[0]
  79. video = [a for a in b.data_arrays if a.name == "video"][0]
  80. tag = [t for t in b.multi_tags if t.name == "tracking"][0]
  81. fig = plt.figure(facecolor='white')
  82. pb = Playback(fig, video, tracking_tag=tag, show_orientation=True)
  83. pb.start()
  84. nix_file.close()