-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
52 lines (40 loc) · 1.36 KB
/
Copy pathapp.py
File metadata and controls
52 lines (40 loc) · 1.36 KB
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
#!/usr/bin/env python
import sys
import cv2
video_uri = "http://192.168.1.7:8080/video" # IP Webcam App
#video_uri = "http://192.168.1.3:8080" # VLC
class ComputerVisionApp(object):
cap = cv2.VideoCapture(video_uri)
title = "CV App"
win = cv2.namedWindow(title, 1)
def main_loop(self):
while True:
frame = self.get_frame()
cv2.imshow(self.title, frame)
key = cv2.waitKey(1)
if key == 27: # ESC
self.on_escape()
elif key == 32: # Space
self.on_space()
elif 0 < key < 256:
method_name = "do_" + chr(key)
try:
method = getattr(self, method_name)
except AttributeError:
print >> sys.stderr, "e. Invalid key pressed:", chr(key)
continue
method()
def on_escape(self):
raise SystemExit("Clean exit")
def on_space(self):
''' Subclasses welcome to override '''
print "<space> pressed"
def get_frame(self):
success, frame = self.cap.read()
return self.to_grayscale(frame) if success else None
def to_grayscale(self, frame):
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
CVApp = ComputerVisionApp
if __name__ == "__main__":
cvapp = CVApp()
cvapp.main_loop()