-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.py
52 lines (39 loc) · 1.35 KB
/
camera.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Interface for camera hardware.
Created on Fri May 11 22:00:00 2018
Author: Prasun Roy | CVPRU-ISICAL (http://www.isical.ac.in/~cvpr)
GitHub: https://github.com/prasunroy/air-writing
"""
# imports
import cv2
import psutil
# VideoStream class
class VideoStream(object):
# ~~~~~~~~ constructor ~~~~~~~~
def __init__(self, src=0):
if psutil.WINDOWS:
self.video = cv2.VideoCapture(src, cv2.CAP_DSHOW)
else:
self.video = cv2.VideoCapture(src)
self.setFrameSize((640, 480))
return
# ~~~~~~~~ set target frame dimension ~~~~~~~~
def setFrameSize(self, size=(-1, -1)):
if size[0] > 0:
self.video.set(cv2.CAP_PROP_FRAME_WIDTH, size[0])
if size[1] > 0:
self.video.set(cv2.CAP_PROP_FRAME_HEIGHT, size[1])
return
# ~~~~~~~~ get frame from device ~~~~~~~~
def getFrame(self, flip=None):
self.frame = self.video.read()[1]
if not self.frame is None:
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
if type(flip) is int:
self.frame = cv2.flip(self.frame, flip)
return self.frame
# ~~~~~~~~ clean up and release resources ~~~~~~~~
def clear(self):
self.video.release()
return