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

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

      1  import cv2, base64, numpy as np, StringIO, pyforms.Utils.tools as tools
      2  from pyforms.web.Controls.ControlBase import ControlBase
      3  from PIL import Image
      4
      5  from django.conf import settings
      6
      7 -class ControlPlayer(ControlBase):
      8      _currentFrame = None
      9      _min = 0
     10      _max = 0
     11      _filename = ''
     12
     13 -    def initControl(self):
     14          self._currentFrame = None
     15          return "controls.push(new ControlPlayer('"+self._name+"','%s'));" % self.help
     16
     17 -    def processFrame(self, frame):  pass
     18
     19 -    def updateFrame(self):          pass
     20
     21 -    def videoPlay_clicked(self):    pass
     22
     23 -    def save(self, data):           pass
     24
     25 -    def load(self, data):           pass
     26
     27 -    def refresh(self):              pass
     28
     29 -    def convertFrameToTime(self, frame):
     30          currentMilliseconds = (frame / self.value.videoFrameRate) * 1000
     31          totalseconds = int(currentMilliseconds/1000)
     32          minutes = int(totalseconds / 60)
     33          seconds = totalseconds - (minutes*60)
     34          milliseconds = currentMilliseconds - (totalseconds*1000)
     35          return ( minutes, seconds, milliseconds )
     36
     37 -    def videoProgress_valueChanged(self):   pass
     38
     39 -    def videoProgress_sliderReleased(self): pass
     40
     41 -    def videoFrames_valueChanged(self):     pass
     42
     43 -    def isPlaying(self):    pass
     44
     45 -    def changed(self):      pass
     46
     47
     48      @property
     49 -    def value(self):
     50          if self._value==None or self._value=='': return None
     51          result = {
     52              'min': self._min,
     53              'max': self._max,
     54              'position': self.video_index,
     55              'frame': '',
     56              'filename': self._filename }
     57          capture = self._value
     58          _, image = capture.read()
     59
     60          if isinstance(image, np.ndarray):
     61              image = self.processFrame(image)
     62
     63              if isinstance(image, list) or isinstance(image, tuple):
     64                  image = tools.groupImage(image, True)
     65
     66              if len(image.shape)>2: image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
     67              image = Image.fromarray(image)
     68              buff = StringIO.StringIO()
     69              image.save(buff, format="PNG")
     70              content = buff.getvalue()
     71              buff.close()
     72              result['frame'] = base64.b64encode(content)
     73
     74          return result
     75
     76      @value.setter
     77 -    def value(self, value):
     78          if isinstance( value, (str, unicode) ):
     79              link = self.storage.public_link(value)
     80              link = "{1}/index.php/s/{0}/download".format(link.token, settings.OWNCLOUD_LINK)
     81              ControlBase.value.fset(self, cv2.VideoCapture( link ) )
     82              self._filename = value
     83          if isinstance( value, dict ):
     84              filename = value['filename']
     85              if len(filename)>0:
     86                  link = self.storage.public_link(value['filename'])
     87                  link = "{1}/index.php/s/{0}/download".format(link.token, settings.OWNCLOUD_LINK)
     88                  ControlBase.value.fset(self, cv2.VideoCapture(link) )
     89                  if 'position' in value.keys(): self.video_index = int(value['position'])
     90                  self._filename = value['filename']
     91              else:
     92                  self._value = None
     93
     94          if self._value!=None:
     95              self._max = int( self._value.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) )
     96
     97
     98
     99      @property
    100 -    def video_index(self): return int(self._value.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) if self._value else 0
    101
    102      @video_index.setter
    103 -    def video_index(self, value):
    104          self._value.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, float(value))
    105
    106
    107
    108      @property
    109 -    def startFrame(self):
    110          if self._value: return self._value.startFrame
    111          else: return -1
    112
    113      @startFrame.setter
    114 -    def startFrame(self, value):
    115          if self._value: self._value.startFrame = value
    116
    117      @property
    118 -    def endFrame(self):
    119          if self._value: return self._value.startFrame
    120          else: return -1
    121
    122      @endFrame.setter
    123 -    def endFrame(self, value):
    124          if self._value: self._value.endFrame = value
    125
    126      @property
    127 -    def image(self):
    128          _, image = self._value.read()
    129
    130          return image
    131