-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvutils.py
34 lines (27 loc) · 1012 Bytes
/
cvutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import cv2 as cv
class CroppedVideoCapture:
def __init__(self, cropx, cropy, *args):
self.vcap = cv.VideoCapture(*args)
if(cropx is None):
self.cropx = self.cropy = None
else:
assert(cropx[1]-cropx[0] < self.get(cv.CAP_PROP_FRAME_WIDTH) and
cropy[1]-cropy[0] < self.get(cv.CAP_PROP_FRAME_HEIGHT)), "Crop is bigger than the video resolution!"
assert(cropx[1] > cropx[0] and cropy[1] > cropy[0])
self.cropx = slice(*cropx)
self.cropy = slice(*cropy)
def read(self):
retval, img = self.vcap.read()
if(self.cropx is None):
return retval, img
if(img is None):
return retval, None
return retval, img[self.cropy, self.cropx]
def grab(self):
self.vcap.grab()
def release(self):
self.vcap.release()
def get(self, *args):
return self.vcap.get(*args)
def set(self, *args):
return self.vcap.set(*args)