Skip to content

Commit afcc53d

Browse files
rndsrcpiedar
authored andcommitted
python: make all cv examples to work with cv2
(cherry picked from commit b6453ae) Signed-off-by: Benn Snyder <[email protected]>
1 parent dbaa1ec commit afcc53d

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

wrappers/python/demo_cv2_async.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
import freenect
3+
import cv2
4+
import frame_convert2
5+
6+
cv2.namedWindow('Depth')
7+
cv2.namedWindow('RGB')
8+
keep_running = True
9+
10+
11+
def display_depth(dev, data, timestamp):
12+
global keep_running
13+
cv2.imshow('Depth', frame_convert2.pretty_depth_cv(data))
14+
if cv2.waitKey(10) == 27:
15+
keep_running = False
16+
17+
18+
def display_rgb(dev, data, timestamp):
19+
global keep_running
20+
cv2.imshow('RGB', frame_convert2.video_cv(data))
21+
if cv2.waitKey(10) == 27:
22+
keep_running = False
23+
24+
25+
def body(*args):
26+
if not keep_running:
27+
raise freenect.Kill
28+
29+
30+
print('Press ESC in window to stop')
31+
freenect.runloop(depth=display_depth,
32+
video=display_rgb,
33+
body=body)

wrappers/python/demo_cv2_sync.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
import freenect
3+
import cv2
4+
import frame_convert2
5+
6+
cv2.namedWindow('Depth')
7+
cv2.namedWindow('Video')
8+
print('Press ESC in window to stop')
9+
10+
11+
def get_depth():
12+
return frame_convert2.pretty_depth_cv(freenect.sync_get_depth()[0])
13+
14+
15+
def get_video():
16+
return frame_convert2.video_cv(freenect.sync_get_video()[0])
17+
18+
19+
while 1:
20+
cv2.imshow('Depth', get_depth())
21+
cv2.imshow('Video', get_video())
22+
if cv2.waitKey(10) == 27:
23+
break
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
"""This goes through each kinect on your system, grabs one frame and
3+
displays it. Uncomment the commented line to shut down after each frame
4+
if your system can't handle it (will get very low FPS but it should work).
5+
This will keep trying indeces until it finds one that doesn't work, then it
6+
starts from 0.
7+
"""
8+
import freenect
9+
import cv2
10+
import frame_convert2
11+
12+
cv2.namedWindow('Depth')
13+
cv2.namedWindow('Video')
14+
ind = 0
15+
print('%s\nPress ESC to stop' % __doc__)
16+
17+
18+
def get_depth(ind):
19+
return frame_convert2.pretty_depth_cv(freenect.sync_get_depth(ind)[0])
20+
21+
22+
def get_video(ind):
23+
return frame_convert2.video_cv(freenect.sync_get_video(ind)[0])
24+
25+
26+
while 1:
27+
print(ind)
28+
try:
29+
depth = get_depth(ind)
30+
video = get_video(ind)
31+
except TypeError:
32+
ind = 0
33+
continue
34+
ind += 1
35+
cv2.imshow('Depth', depth)
36+
cv2.imshow('Video', video)
37+
if cv2.waitKey(10) == 27:
38+
break
39+
#freenect.sync_stop() # NOTE: Uncomment if your machine can't handle it
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python
2+
"""Sweeps throught the depth image showing 100 range at a time"""
3+
import freenect
4+
import cv2
5+
import numpy as np
6+
import time
7+
8+
cv2.namedWindow('Depth')
9+
10+
11+
def disp_thresh(lower, upper):
12+
depth, timestamp = freenect.sync_get_depth()
13+
depth = 255 * np.logical_and(depth > lower, depth < upper)
14+
depth = depth.astype(np.uint8)
15+
cv2.imshow('Depth', depth)
16+
cv2.waitKey(10)
17+
18+
19+
lower = 0
20+
upper = 100
21+
max_upper = 2048
22+
while upper < max_upper:
23+
print('%d < depth < %d' % (lower, upper))
24+
disp_thresh(lower, upper)
25+
time.sleep(.1)
26+
lower += 20
27+
upper += 20
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
import freenect
3+
import cv2
4+
import frame_convert2
5+
import numpy as np
6+
7+
8+
threshold = 100
9+
current_depth = 0
10+
11+
12+
def change_threshold(value):
13+
global threshold
14+
threshold = value
15+
16+
17+
def change_depth(value):
18+
global current_depth
19+
current_depth = value
20+
21+
22+
def show_depth():
23+
global threshold
24+
global current_depth
25+
26+
depth, timestamp = freenect.sync_get_depth()
27+
depth = 255 * np.logical_and(depth >= current_depth - threshold,
28+
depth <= current_depth + threshold)
29+
depth = depth.astype(np.uint8)
30+
cv2.imshow('Depth', depth)
31+
32+
33+
def show_video():
34+
cv2.imshow('Video', frame_convert2.video_cv(freenect.sync_get_video()[0]))
35+
36+
37+
cv2.namedWindow('Depth')
38+
cv2.namedWindow('Video')
39+
cv2.createTrackbar('threshold', 'Depth', threshold, 500, change_threshold)
40+
cv2.createTrackbar('depth', 'Depth', current_depth, 2048, change_depth)
41+
42+
print('Press ESC in window to stop')
43+
44+
45+
while 1:
46+
show_depth()
47+
show_video()
48+
if cv2.waitKey(10) == 27:
49+
break

wrappers/python/frame_convert2.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import numpy as np
2+
3+
4+
def pretty_depth(depth):
5+
"""Converts depth into a 'nicer' format for display
6+
7+
This is abstracted to allow for experimentation with normalization
8+
9+
Args:
10+
depth: A numpy array with 2 bytes per pixel
11+
12+
Returns:
13+
A numpy array that has been processed with unspecified datatype
14+
"""
15+
np.clip(depth, 0, 2**10 - 1, depth)
16+
depth >>= 2
17+
depth = depth.astype(np.uint8)
18+
return depth
19+
20+
21+
def pretty_depth_cv(depth):
22+
"""Converts depth into a 'nicer' format for display
23+
24+
This is abstracted to allow for experimentation with normalization
25+
26+
Args:
27+
depth: A numpy array with 2 bytes per pixel
28+
29+
Returns:
30+
A numpy array with unspecified datatype
31+
"""
32+
return pretty_depth(depth)
33+
34+
35+
def video_cv(video):
36+
"""Converts video into a BGR format for display
37+
38+
This is abstracted out to allow for experimentation
39+
40+
Args:
41+
video: A numpy array with 1 byte per pixel, 3 channels RGB
42+
43+
Returns:
44+
A numpy array with with 1 byte per pixel, 3 channels BGR
45+
"""
46+
return video[:, :, ::-1] # RGB -> BGR

0 commit comments

Comments
 (0)