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
8 _currentFrame = None
9 _min = 0
10 _max = 0
11 _filename = ''
12
14 self._currentFrame = None
15 return "controls.push(new ControlPlayer('"+self._name+"','%s'));" % self.help
16
18
20
22
23 - def save(self, data): pass
24
25 - def load(self, data): pass
26
28
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
38
40
42
44
46
47
48 @property
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
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
101
102 @video_index.setter
104 self._value.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, float(value))
105
106
107
108 @property
110 if self._value: return self._value.startFrame
111 else: return -1
112
113 @startFrame.setter
116
117 @property
119 if self._value: return self._value.startFrame
120 else: return -1
121
122 @endFrame.setter
124 if self._value: self._value.endFrame = value
125
126 @property
128 _, image = self._value.read()
129
130 return image
131