Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e8151e

Browse files
committed
Xbox-Kinect-深度相机
1 parent 8ca0270 commit 0e8151e

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ Intel Media SDK现在可以被我们的videoio模块用来进行硬件加速的
7676
- 支付宝
7777
- <img src="data/alipay_donate.jpg" width = "200" height = "200" alt="alipay_donate" />
7878

79+
- 比特币
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Xbox-Kinect-深度相机
2+
- 参考
3+
- [Using Kinect and other OpenNI compatible depth sensors](https://docs.opencv.org/3.0-rc1/d7/df5/tutorial_ug_highgui.html)
4+
- [Use Kinect with OpenCV (Python)](https://gist.github.com/joinAero/1f76844278f141cea8338d1118423648)
5+
- [Experimenting with Kinect using opencv, python and open kinect (libfreenect)](https://naman5.wordpress.com/2014/06/24/experimenting-with-kinect-using-opencv-python-and-open-kinect-libfreenect/)
6+
- [Getting Started With Kinect and OpenCV](https://electronicsforu.com/electronics-projects/software-projects-ideas/getting-started-kinect-opencv)

‎cv-Kinect深度相机/camera.py‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年12月8日 19:37
3+
# @Author : play4fun
4+
# @File : camera.py
5+
# @Software: PyCharm
6+
7+
"""
8+
camera.py:
9+
"""
10+
11+
import cv2
12+
import sys
13+
14+
15+
class Camera(object):
16+
17+
def __init__(self, index=0):
18+
self.cap = cv2.VideoCapture(index)
19+
self.openni = index in (cv2.CAP_OPENNI, cv2.CAP_OPENNI2)
20+
self.fps = 0
21+
22+
def __enter__(self):
23+
return self
24+
25+
def __exit__(self, exc_type, exc_value, traceback):
26+
self.release()
27+
28+
def release(self):
29+
if not self.cap: return
30+
self.cap.release()
31+
self.cap = None
32+
33+
def capture(self, callback, gray=True):
34+
if not self.cap:
35+
sys.exit('The capture is not ready')
36+
37+
while True:
38+
t = cv2.getTickCount()
39+
40+
if self.openni:
41+
if not self.cap.grab():
42+
sys.exit('Grabs the next frame failed')
43+
ret, depth = self.cap.retrieve(cv2.CAP_OPENNI_DEPTH_MAP)
44+
ret, frame = self.cap.retrieve(cv2.CAP_OPENNI_GRAY_IMAGE
45+
if gray else cv2.CAP_OPENNI_BGR_IMAGE)
46+
if callback:
47+
callback(frame, depth, self.fps)
48+
else:
49+
ret, frame = self.cap.read()
50+
if not ret:
51+
sys.exit('Reads the next frame failed')
52+
if gray:
53+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
54+
if callback:
55+
callback(frame, self.fps)
56+
57+
t = cv2.getTickCount() - t
58+
self.fps = cv2.getTickFrequency() / t
59+
60+
# esc, q
61+
ch = cv2.waitKey(10) & 0xFF
62+
if ch == 27 or ch == ord('q'):
63+
break
64+
65+
def fps(self):
66+
return self.fps
67+
68+
def get(self, prop_id):
69+
return self.cap.get(prop_id)
70+
71+
def set(self, prop_id, value):
72+
self.cap.set(prop_id, value)
73+
74+
75+
if __name__ == '__main__':
76+
callback = lambda gray, fps: cv2.imshow('gray', gray)
77+
78+
with Camera(0) as cam:
79+
print("Camera: %dx%d, %d" % (
80+
cam.get(cv2.CAP_PROP_FRAME_WIDTH),
81+
cam.get(cv2.CAP_PROP_FRAME_HEIGHT),
82+
cam.get(cv2.CAP_PROP_FPS)))
83+
cam.capture(callback)
84+
85+
cv2.destroyAllWindows()

‎cv-Kinect深度相机/draw.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年12月8日 19:37
3+
# @Author : play4fun
4+
# @File : draw.py
5+
# @Software: PyCharm
6+
7+
"""
8+
draw.py:
9+
"""
10+
11+
import cv2
12+
13+
14+
FONT_FACE = cv2.FONT_HERSHEY_SIMPLEX
15+
FONT_SCALE = 1
16+
THICKNESS = 1
17+
18+
19+
class Gravity:
20+
TOP_LEFT = 1
21+
TOP_RIGHT = 2
22+
BOTTOM_LEFT = 3
23+
BOTTOM_RIGHT = 4
24+
25+
26+
def put_text(img, text, gravity, margin=10):
27+
h, w = img.shape[:2]
28+
# getTextSize, result: ((width, height), baseline)
29+
x, y = cv2.getTextSize(text, FONT_FACE, FONT_SCALE, THICKNESS)[0];
30+
# putText(img, text, org, fontFace, fontScale, color, thickness, lineType)
31+
org = {
32+
Gravity.TOP_LEFT: (margin, margin+y),
33+
Gravity.TOP_RIGHT: (w-margin-x, margin+y),
34+
Gravity.BOTTOM_LEFT: (margin, h-margin),
35+
Gravity.BOTTOM_RIGHT: (w-margin-x, h-margin),
36+
}.get(gravity, (margin, margin+y))
37+
cv2.putText(img, text, org, FONT_FACE, FONT_SCALE,
38+
(255,255,255), THICKNESS, cv2.LINE_AA)

‎cv-Kinect深度相机/kinect.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年12月8日 19:38
3+
# @Author : play4fun
4+
# @File : kinect.py
5+
# @Software: PyCharm
6+
7+
"""
8+
kinect.py:
9+
"""
10+
11+
#!/usr/bin/env python
12+
# -*- coding:utf-8 -*-
13+
14+
import cv2
15+
import numpy as np
16+
17+
from .camera import Camera
18+
from .draw import Gravity, put_text
19+
20+
21+
def main():
22+
def callback(frame, depth, fps):
23+
# Normalize the depth for representation
24+
min, max = depth.min(), depth.max()
25+
depth = np.uint8(255 * (depth - min) / (max - min))
26+
27+
# Unable to retrieve correct frame, it's still depth here
28+
put_text(frame, "{1}x{0}".format(*frame.shape), Gravity.TOP_LEFT)
29+
put_text(depth, "{1}x{0}".format(*depth.shape), Gravity.TOP_LEFT)
30+
31+
put_text(depth, "%.1f" % fps, Gravity.TOP_RIGHT)
32+
33+
cv2.imshow('frame', frame)
34+
cv2.imshow('depth', depth)
35+
36+
with Camera(cv2.CAP_OPENNI2) as cam:
37+
print("Camera: %dx%d, %d" % (
38+
cam.get(cv2.CAP_OPENNI_IMAGE_GENERATOR + cv2.CAP_PROP_FRAME_WIDTH),
39+
cam.get(cv2.CAP_OPENNI_IMAGE_GENERATOR + cv2.CAP_PROP_FRAME_HEIGHT),
40+
cam.get(cv2.CAP_OPENNI_IMAGE_GENERATOR + cv2.CAP_PROP_FPS)))
41+
cam.capture(callback, False)
42+
43+
cv2.destroyAllWindows()
44+
45+
46+
if __name__ == '__main__':
47+
main()
48+
49+
50+
# Using Kinect and other OpenNI compatible depth sensors:
51+
# http://docs.opencv.org/master/d7/d6f/tutorial_kinect_openni.html
52+
# OpenCV Python unable to access correct OpenNI device channels:
53+
# https://github.com/opencv/opencv/issues/4735

‎cv-Kinect深度相机/kinect_test.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年12月8日 19:34
3+
# @Author : play4fun
4+
# @File : kinect_test.py
5+
# @Software: PyCharm
6+
7+
"""
8+
kinect_test.py:
9+
https://naman5.wordpress.com/2014/06/24/experimenting-with-kinect-using-opencv-python-and-open-kinect-libfreenect/
10+
"""
11+
12+
# import the necessary modules
13+
import freenect
14+
import cv2
15+
import numpy as np
16+
17+
18+
# function to get RGB image from kinect
19+
def get_video():
20+
array, _ = freenect.sync_get_video()
21+
array = cv2.cvtColor(array, cv2.COLOR_RGB2BGR)
22+
return array
23+
24+
25+
# function to get depth image from kinect
26+
def get_depth():
27+
array, _ = freenect.sync_get_depth()
28+
array = array.astype(np.uint8)
29+
return array
30+
31+
32+
if __name__ == "__main__":
33+
while 1:
34+
# get a frame from RGB camera
35+
frame = get_video()
36+
# get a frame from depth sensor
37+
depth = get_depth()
38+
# display RGB image
39+
cv2.imshow('RGB image', frame)
40+
# display depth image
41+
cv2.imshow('Depth image', depth)
42+
43+
# quit program when 'esc' key is pressed
44+
k = cv2.waitKey(5) & 0xFF
45+
if k == 27:
46+
break

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /