Package pyforms :: Package gui :: Package Controls :: Package ControlPlayer :: Module ControlPlayer |
[hide private]
[frames] | no frames]

# Source Code for Module pyforms.gui.Controls.ControlPlayer.ControlPlayer

      1  #!/usr/bin/python
      2  # -*- coding: utf-8 -*-
      3
      4  """ pyforms.gui.Controls.ControlPlayer.ControlPlayer
      5
      6  """
      7
      8  import math
      9  import os
     10  from PyQt4 import uic
     11  from PyQt4 import QtCore, QtGui
     12  from pyforms.gui.Controls.ControlBase import ControlBase
     13  import pyforms.Utils.tools as tools
     14
     15  try:
     16      import cv2
     17  except:
     18      print("Warning: was not possible to import cv2 in ControlPlayer")
     19
     20  from PyQt4.QtGui import QApplication
     21  from pyforms.gui.Controls.ControlPlayer.VideoGLWidget import VideoGLWidget
     22
     23
     24  __author__ = "Ricardo Ribeiro"
     25  __credits__ = ["Ricardo Ribeiro"]
     26  __license__ = "MIT"
     27  __version__ = "0.0"
     28  __maintainer__ = "Ricardo Ribeiro"
     29  __email__ = "ricardojvr@gmail.com"
     30  __status__ = "Development"
     31
     32
     33 -class ControlPlayer(ControlBase, QtGui.QFrame):
     34
     35      _videoWidget = None
     36      _currentFrame = None
     37
     38 -    def __init__(self, *args):
     39          QtGui.QFrame.__init__(self)
     40          ControlBase.__init__(self, *args)
     41
     42
     43 -    def initForm(self):
     44          # Get the current path of the file
     45          rootPath = os.path.dirname(__file__)
     46
     47          # Load the UI for the self instance
     48          uic.loadUi(os.path.join(rootPath, "video.ui"), self)
     49
     50          # Define the icon for the Play button
     51          icon = QtGui.QIcon()
     52          pixmapOff = QtGui.qApp.style().standardPixmap(
     53              QtGui.QStyle.SP_MediaPlay)
     54          pixmapOn = QtGui.qApp.style().standardPixmap(
     55              QtGui.QStyle.SP_MediaPause)
     56          icon.addPixmap(
     57              pixmapOff, mode=QtGui.QIcon.Normal, state=QtGui.QIcon.Off)
     58          icon.addPixmap(pixmapOn, mode=QtGui.QIcon.Normal, state=QtGui.QIcon.On)
     59          self.videoPlay.setIcon(icon)
     60
     61          # Define the icon for the Show/Hide markers button
     62          icon = QtGui.QIcon()
     63          pixmapOff = QtGui.qApp.style().standardPixmap(
     64              QtGui.QStyle.SP_DialogYesButton)
     65          pixmapOn = QtGui.qApp.style().standardPixmap(
     66              QtGui.QStyle.SP_DialogNoButton)
     67          icon.addPixmap(
     68              pixmapOff, mode=QtGui.QIcon.Normal, state=QtGui.QIcon.Off)
     69          icon.addPixmap(pixmapOn, mode=QtGui.QIcon.Normal, state=QtGui.QIcon.On)
     70          self.videoHideMarkers.setIcon(icon)
     71
     72          self._videoWidget = VideoGLWidget()
     73          self._videoWidget._control = self
     74          self.videoLayout.addWidget(self._videoWidget)
     75          self.videoHideMarkers.clicked.connect(self.videoHideMarkers_clicked)
     76          self.videoPlay.clicked.connect(self.videoPlay_clicked)
     77          self.videoFPS.valueChanged.connect(self.videoFPS_valueChanged)
     78          self.videoFrames.valueChanged.connect(self.videoFrames_valueChanged)
     79          self.videoProgress.valueChanged.connect(
     80              self.videoProgress_valueChanged)
     81          self.videoProgress.sliderReleased.connect(
     82              self.videoProgress_sliderReleased)
     83          self._timer = QtCore.QTimer(self)
     84          self._timer.timeout.connect(self.updateFrame)
     85
     86          self.form.horizontalSlider.valueChanged.connect(self.__rotateZ)
     87          self.form.verticalSlider.valueChanged.connect(self.__rotateX)
     88
     89          self._currentFrame = None
     90          # Controls if anything is drawn on the video
     91          self._draw_on_video = True
     92          self._videoFPS = None  # Sets the FPS rate at which the video is played
     93
     94          self.view3D = False
     95
     96 -    def __rotateX(self):
     97          self._videoWidget.rotateX = self.form.verticalSlider.value()
     98          self.refresh()
     99
    100 -    def __rotateZ(self):
    101          self._videoWidget.rotateZ = self.form.horizontalSlider.value()
    102          self.refresh()
    103
    104      @property
    105 -    def onDoubleClick(self): return self._videoWidget.onDoubleClick
    106
    107      @onDoubleClick.setter
    108 -    def onDoubleClick(self, value): self._videoWidget.onDoubleClick = value
    109
    110      @property
    111 -    def onClick(self): return self._videoWidget.onClick
    112
    113      @onClick.setter
    114 -    def onClick(self, value):  self._videoWidget.onClick = value
    115
    116      @property
    117 -    def onDrag(self): return self._videoWidget.onDrag
    118
    119      @onDrag.setter
    120 -    def onDrag(self, value): self._videoWidget.onDrag = value
    121
    122      @property
    123 -    def onEndDrag(self): return self._videoWidget.onEndDrag
    124
    125      @onEndDrag.setter
    126 -    def onEndDrag(self, value): self._videoWidget.onEndDrag = value
    127
    128      @property
    129 -    def view3D(self): return self._videoWidget.onEndDrag
    130
    131      @view3D.setter
    132 -    def view3D(self, value):
    133          self.form.horizontalSlider.setVisible(value)
    134          self.form.verticalSlider.setVisible(value)
    135
    136      @property
    137 -    def onKeyRelease(self): return self._videoWidget.onKeyRelease
    138
    139      @onKeyRelease.setter
    140 -    def onKeyRelease(self, value): self._videoWidget.onKeyRelease = value
    141
    142      @property
    143 -    def isPainted(self): return self._draw_on_video
    144
    145 -    def processFrame(self, frame):
    146          return frame
    147
    148 -    def updateFrame(self):
    149          (success, frame) = self.value.read()
    150
    151          if frame != None:
    152              self._currentFrame = frame
    153
    154          frame = self.processFrame(self._currentFrame.copy())
    155          if isinstance(frame, list) or isinstance(frame, tuple):
    156              self._videoWidget.paint(frame)
    157          else:
    158              self._videoWidget.paint([frame])
    159
    160          if not self.videoProgress.isSliderDown():
    161              currentFrame = self.video_index
    162
    163              self.videoProgress.setValue(currentFrame)
    164              if self._updateVideoFrame:
    165                  self.videoFrames.setValue(currentFrame)
    166
    167 -    def videoHideMarkers_clicked(self):
    168          """ Slot to hide or show stuff drawn on the video."""
    169          if self.videoHideMarkers.isChecked():
    170              self._draw_on_video = True
    171          else:
    172              self._draw_on_video = False
    173          self.refresh()
    174          # print "--->", self._draw_on_video
    175
    176 -    def videoPlay_clicked(self):
    177          """Slot for Play/Pause functionality."""
    178          if self.videoPlay.isChecked():
    179              timeout_interval = (1000 / self._videoFPS)
    180              self._timer.start(timeout_interval)
    181          else:
    182              self._timer.stop()
    183
    184 -    def pausePlay(self):
    185          if not self.videoPlay.isChecked():
    186              self.videoPlay.setChecked(True)
    187              timeout_interval = (1000 / self._videoFPS)
    188              self._timer.start(timeout_interval)
    189          else:
    190              self.videoPlay.setChecked(False)
    191              self._timer.stop()
    192
    193 -    def videoFPS_valueChanged(self):
    194          """Get FPS rate from loaded video."""
    195          self._videoFPS = self.videoFPS.value()
    196          timeout_interval = (1000 / self._videoFPS)
    197          self._timer.setInterval(timeout_interval)
    198
    199 -    def save(self, data):
    200          if self.value:
    201              data['value'] = self.value.captureFrom
    202
    203 -    def load(self, data):
    204          # if 'value' in data: self.value = data['value']
    205          pass
    206
    207 -    def refresh(self):
    208          if self._currentFrame != None:
    209              frame = self.processFrame(self._currentFrame.copy())
    210              if isinstance(frame, list) or isinstance(frame, tuple):
    211                  self._videoWidget.paint(frame)
    212              else:
    213                  self._videoWidget.paint([frame])
    214
    215 -    def convertFrameToTime(self, totalMilliseconds):
    216          # totalMilliseconds = totalMilliseconds*(1000.0/self._value.get(cv2.cv.CV_CAP_PROP_FPS))
    217
    218          totalseconds = int(totalMilliseconds / 1000)
    219          minutes = int(totalseconds / 60)
    220          seconds = totalseconds - (minutes * 60)
    221          milliseconds = totalMilliseconds - (totalseconds * 1000)
    222          return (minutes, seconds, milliseconds)
    223
    224 -    def videoProgress_valueChanged(self):
    225          milli = self._value.get(cv2.cv.CV_CAP_PROP_POS_MSEC)
    226          milli -= 1000.0 / self._value.get(cv2.cv.CV_CAP_PROP_FPS)
    227          (minutes, seconds, milliseconds) = self.convertFrameToTime(milli)
    228          self.videoTime.setText(
    229              "%02d:%02d:%03d" % (minutes, seconds, milliseconds))
    230
    231      _updateVideoFrame = True
    232
    233 -    def videoProgress_sliderReleased(self):
    234          jump2Frame = self.videoProgress.value()
    235          self._value.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, jump2Frame)
    236          self._updateVideoFrame = False
    237          self.videoFrames.setValue(jump2Frame)
    238          self._value.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, jump2Frame)
    239          self._updateVideoFrame = True
    240
    241 -    def videoFrames_valueChanged(self, i):
    242          if not self.isPlaying():
    243              jump2Frame = self.videoProgress.value()
    244              diff = jump2Frame - i
    245
    246              self._value.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, jump2Frame - diff)
    247              self._updateVideoFrame = False
    248              self.updateFrame()
    249              self._updateVideoFrame = True
    250
    251 -    def isPlaying(self):
    252          return self._timer.isActive()
    253
    254 -    def updateControl(self):
    255
    256          if self._value:
    257              self.videoControl.setEnabled(True)
    258              self.videoProgress.setMinimum(self._value.startFrame)
    259              self.videoProgress.setValue(self._value.startFrame)
    260              self.videoProgress.setMaximum(self._value.endFrame)
    261              self.videoFrames.setMinimum(self._value.startFrame)
    262              self.videoFrames.setValue(self._value.startFrame)
    263              self.videoFrames.setMaximum(self._value.endFrame)
    264
    265      @property
    266 -    def value(self): return ControlBase.value.fget(self)
    267
    268      @value.setter
    269 -    def value(self, value):
    270          self._videoWidget.reset()
    271
    272          if value == 0:
    273              self._value = cv2.OTVideoInput(0)
    274          elif isinstance(value, str) and value:
    275              self._value = cv2.VideoCapture(value)
    276              self.fps = self._value.get(cv2.cv.CV_CAP_PROP_FPS)
    277              print("Open video with", self._value.get(
    278                  cv2.cv.CV_CAP_PROP_FPS), 'fps')
    279          else:
    280              self._value = None
    281
    282          if self._value and value != 0:
    283              self.videoProgress.setMinimum(0)
    284              self.videoProgress.setValue(0)
    285              self.videoProgress.setMaximum(
    286                  self._value.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    287              self.videoFrames.setMinimum(0)
    288              self.videoFrames.setValue(0)
    289              self.videoFrames.setMaximum(
    290                  self._value.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    291
    292          if self._value:
    293              self.videoControl.setEnabled(True)
    294
    295          self.refresh()
    296
    297      @property
    298 -    def startFrame(self):
    299          if self._value:
    300              return self._value.startFrame
    301          else:
    302              return -1
    303
    304      @startFrame.setter
    305 -    def startFrame(self, value):
    306          if self._value:
    307              self._value.startFrame = value
    308              self.videoProgress.setMinimum(value)
    309
    310      @property
    311 -    def endFrame(self):
    312          if self._value:
    313              return self._value.startFrame
    314          else:
    315              return -1
    316
    317      @endFrame.setter
    318 -    def endFrame(self, value):
    319
    320          if self._value:
    321              self._value.endFrame = value
    322              self.videoProgress.setValue(self._value.startFrame)
    323              self.videoProgress.setMaximum(value)
    324
    325      @property
    326 -    def video_index(self): return int(
    327          self._value.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) - 1
    328
    329      @video_index.setter
    330 -    def video_index(self, value): self._value.set(
    331          cv2.cv.CV_CAP_PROP_POS_FRAMES, value)
    332
    333      @property
    334 -    def max(self): return int(self._value.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    335
    336      @property
    337 -    def image(self): return self._currentFrame
    338
    339      @image.setter
    340 -    def image(self, value):
    341          if isinstance(value, list) or isinstance(value, tuple):
    342              self._videoWidget.paint(value)
    343          else:
    344              self._videoWidget.paint([value])
    345          QApplication.processEvents()
    346
    347      @property
    348 -    def fps(self): return self._videoFPS
    349
    350      @fps.setter
    351 -    def fps(self, value):
    352          self._videoFPS = value
    353          if math.isnan(self._videoFPS):
    354              self._videoFPS = 15.0
    355
    356      @property
    357 -    def show_markers(self): return self._draw_on_video
    358
    359      @property
    360 -    def helpText(self): return self._videoWidget._helpText
    361
    362      @fps.setter
    363 -    def helpText(self, value): self._videoWidget._helpText = value
    364
    365      @property
    366 -    def form(self): return self
    367
    368      @property
    369 -    def point(self): return self._videoWidget.point
    370
    371      @point.setter
    372 -    def point(self, value): self._videoWidget.point = value
    373
    374 -    def hide(self):
    375          QtGui.QFrame.hide(self)
    376
    377 -    def show(self):
    378          QtGui.QFrame.show(self)
    379