public
Description: A collection of object-oriented Python wrappers for OpenCV
Clone URL: git://github.com/cv/opencv-wrappers.git
fixed indentation (soft tabs=2 ftw), objectified finger tracking demo - 
looks more verbose, but it's easier to expand with new key events and the 
image manipulation stuff is a little more contained
cv (author)
Thu May 15 16:22:31 -0700 2008
commit  627bd70a6b50bf1f4a84ffae8a54426d629f2e9b
tree    61871949ec7758125439b176826b9954f5aea626
parent  93d69c8ae49b582eb57ea982c68658f3e90126af
...
1
2
3
4
5
 
 
 
 
 
 
 
 
6
7
 
 
 
 
 
 
 
8
9
10
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
14
15
16
17
18
 
 
19
...
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
0
@@ -1,18 +1,40 @@
0
 from wrappers import *
0
 
0
-camera = Camera(CV_CAP_ANY)
0
-window = Window()
0
-initial = False
0
+class FingerTracking:
0
+
0
+ def __init__(self):
0
+ self.camera = Camera(CV_CAP_ANY)
0
+ self.window = Window()
0
+ self.initial = False
0
+ self.threshold = 50
0
+ self.running = True
0
 
0
-while True:
0
+ def run(self):
0
+ while self.running:
0
+ current = self.camera.frame().grayscale()
0
+ if not self.initial:
0
+ self.initial = current
0
+ self.window.show(current.sub(self.initial).threshold(self.threshold, mode=CV_THRESH_BINARY_INV))
0
+ self._handleKeyboardEvents()
0
 
0
- current = camera.frame().grayscale().invert()
0
 
0
- if not initial:
0
- initial = current
0
+ def _handleKeyboardEvents(self):
0
+ key = getKeyPressed()
0
+
0
+ if key == '\x1b': # escape
0
+ self.window.destroy()
0
+ self.running = False
0
+
0
+ elif key == ' ':
0
+ self.initial = False
0
+
0
+ elif key == '+':
0
+ self.threshold += 10
0
+ print 'Threshold:', self.threshold
0
+
0
+ elif key == '-':
0
+ self.threshold -= 10
0
+ print 'Threshold:', self.threshold
0
 
0
- window.show(current.sub(initial).threshold(20, mode=CV_THRESH_BINARY_INV))
0
-
0
- if escape_pressed():
0
- window.destroy()
0
- break
0
+if __name__ == '__main__':
0
+ FingerTracking().run()
0
\ No newline at end of file
...
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
59
60
61
62
63
64
65
66
 
 
 
 
67
68
69
70
71
72
 
 
 
 
73
74
75
 
 
 
 
76
 
 
 
 
 
 
 
 
 
77
...
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
59
60
61
62
 
 
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
0
@@ -2,76 +2,76 @@ from opencv import *
0
 from opencv.highgui import *
0
 
0
 class Window:
0
- def __init__(self, name="Main Window"):
0
- self.name = name
0
- cvNamedWindow(name)
0
-
0
- def show(self, image):
0
- cvShowImage(self.name, image.image)
0
-
0
- def destroy(self):
0
- cvDestroyWindow(self.name)
0
+ def __init__(self, name="Main Window"):
0
+ self.name = name
0
+ cvNamedWindow(name)
0
+
0
+ def show(self, image):
0
+ cvShowImage(self.name, image.image)
0
+
0
+ def destroy(self):
0
+ cvDestroyWindow(self.name)
0
 
0
 class Camera:
0
     
0
- def __init__(self, id):
0
- self.capture = cvCreateCameraCapture(id)
0
-
0
- def frame(self):
0
- return Image(cvQueryFrame(self.capture))
0
+ def __init__(self, id):
0
+ self.capture = cvCreateCameraCapture(id)
0
+
0
+ def frame(self):
0
+ return Image(cvQueryFrame(self.capture))
0
 
0
 class Image:
0
-
0
- def __init__(self, image):
0
- self.image = image
0
- self.size = cvSize(image.width, image.height)
0
-
0
- def __del__(self):
0
- if self and self.image:
0
- cvReleaseImage(self.image)
0
-
0
- def grayscale(self, channels=1):
0
- result = cvCreateImage(self.size, self.image.depth, channels)
0
- cvCvtColor(self.image, result, CV_RGB2GRAY)
0
- return Image(result)
0
+
0
+ def __init__(self, image):
0
+ self.image = image
0
+ self.size = cvSize(image.width, image.height)
0
+
0
+ def __del__(self):
0
+ if self and self.image:
0
+ cvReleaseImage(self.image)
0
+
0
+ def grayscale(self, channels=1):
0
+ result = cvCreateImage(self.size, self.image.depth, channels)
0
+ cvCvtColor(self.image, result, CV_RGB2GRAY)
0
+ return Image(result)
0
 
0
- def threshold(self, threshold, max_value=255, mode=CV_THRESH_BINARY, channels=1):
0
- result = cvCreateImage(self.size, self.image.depth, channels)
0
- cvThreshold(self.image, result, threshold, 255, mode)
0
- return Image(result)
0
-
0
- def add(self, anotherImage, channels=1):
0
- result = cvCreateImage(self.size, self.image.depth, channels)
0
- cvAdd(self.image, anotherImage, result)
0
- return Image(result)
0
+ def threshold(self, threshold, max_value=255, mode=CV_THRESH_BINARY, channels=1):
0
+ result = cvCreateImage(self.size, self.image.depth, channels)
0
+ cvThreshold(self.image, result, threshold, 255, mode)
0
+ return Image(result)
0
+
0
+ def add(self, anotherImage, channels=1):
0
+ result = cvCreateImage(self.size, self.image.depth, channels)
0
+ cvAdd(self.image, anotherImage, result)
0
+ return Image(result)
0
 
0
- def sub(self, anotherImage, channels=1):
0
- result = cvCreateImage(self.size, self.image.depth, channels)
0
- cvSub(self.image, anotherImage.image, result)
0
- return Image(result)
0
+ def sub(self, anotherImage, channels=1):
0
+ result = cvCreateImage(self.size, self.image.depth, channels)
0
+ cvSub(self.image, anotherImage.image, result)
0
+ return Image(result)
0
 
0
- def xor(self, anotherImage, channels=1):
0
- result = cvCreateImage(self.size, self.image.depth, channels)
0
- cvXor(self.image, anotherImage, result)
0
- return Image(result)
0
-
0
- def _and(self, anotherImage, channels=1):
0
- result = cvCreateImage(self.size, self.image.depth, channels)
0
- cvAnd(self.image, anotherImage, result)
0
- return Image(result)
0
-
0
- def _not(self, channels=1):
0
- result = cvCreateImage(self.size, self.image.depth, channels)
0
- cvNot(self.image, result)
0
- return Image(result)
0
+ def xor(self, anotherImage, channels=1):
0
+ result = cvCreateImage(self.size, self.image.depth, channels)
0
+ cvXor(self.image, anotherImage, result)
0
+ return Image(result)
0
     
0
- def invert(self, channels=1):
0
- return self._not(channels)
0
-
0
- def nand(self, anotherImage, channels=1):
0
- return self._not(self.image)._and(anotherImage)
0
+ def _and(self, anotherImage, channels=1):
0
+ result = cvCreateImage(self.size, self.image.depth, channels)
0
+ cvAnd(self.image, anotherImage, result)
0
+ return Image(result)
0
 
0
-def escape_pressed():
0
- return cvWaitKey(10) == '\x1b'
0
+ def _not(self, channels=1):
0
+ result = cvCreateImage(self.size, self.image.depth, channels)
0
+ cvNot(self.image, result)
0
+ return Image(result)
0
 
0
+ def invert(self, channels=1):
0
+ return self._not(channels)
0
+
0
+ def nand(self, anotherImage, channels=1):
0
+ return self._not(self.image)._and(anotherImage)
0
+
0
+def getKeyPressed(wait=10):
0
+ return cvWaitKey(wait)
0
+
0
 cvStartWindowThread()

Comments

    No one has commented yet.