-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
58 lines (45 loc) · 1.68 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
53
54
55
56
57
58
import cv2
import json
class Camera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
with open('hsl.json') as file:
self.hsl = json.load(file)
def __del__(self):
self.video.release()
def get_frame(self):
success, frame = self.video.read()
mask = cv2.inRange(cv2.cvtColor(frame, cv2.COLOR_BGR2HLS),
(float(self.hsl['h']['min']), float(self.hsl['s']['min']), float(self.hsl['l']['min'])),
(float(self.hsl['h']['max']), float(self.hsl['s']['max']), float(self.hsl['l']['max'])))
result = cv2.bitwise_and(frame, frame, mask=mask)
# encode into JPG and return
result = cv2.resize(result, (0,0), fx=0.5, fy=0.5) # resize for better performance
ret, jpeg = cv2.imencode('.jpg', result)
return jpeg.tobytes()
def saveFrame(self, filename):
success, image = self.video.read()
if (success == True):
return cv2.imwrite(filename, image)
else:
return False
def getHSL(self):
with open('../hsl.json') as file:
self.hsl = json.load(file)
return self.hsl
def changeHSL(self, hsl):
if (hsl['component'] == 'h'):
hsl.pop('component', 0)
self.hsl['h'] = hsl
elif (hsl['component'] == 's'):
hsl.pop('component', 0)
self.hsl['s'] = hsl
elif (hsl['component'] == 'l'):
hsl.pop('component', 0)
self.hsl['l'] = hsl
def saveHSL(self):
with open('../hsl.json', 'w') as file:
json.dump(self.hsl, file)
return True
def setExposure(self, value):
self.video.set(16, value)