安装
更新pip 工具为最新版,因为在国内这里使用了清华源
python -m pip install --upgrade pip wheel setuptools -i https://pypi.tuna.tsinghua.edu.cn/simple
安装基本依赖
i
python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew -i https://pypi.tuna.tsinghua.edu.cn/simple
安装kivy
python -m pip install kivy -i https://pypi.tuna.tsinghua.edu.cn/simple -i https://pypi.tuna.tsinghua.edu.cn/simple
做一个小程序测试是否ok
import os
os.environ['KIVY_IMAGE'] = 'pil,sdl2'
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
如果弹出对话框和按钮代表安装ok了
实验一
我做一个调试双目摄像头的小程序,内容很简单
1、在界面中两个图框能够同时实时显示L和R两个摄像头的画面
2、一个按键能够捕获两个摄像头当前帧图片并分别起名为test_lX,test_rX(X为顺序数字)保存在image文件夹中
3、能将每次捕获的图片显示在当前程序中
程序已打包成.exe 在我的下载中
界面
kivy中可以把界面和主程序写在一个py文件中也可以单独写一个ky文件,我们只有一个界面也就只用了一个kv文件
#:import Factory kivy.factory.Factory
MainScreen:
<MainScreen>:
BoxLayout:
canvas.before:
Color:
rgba: 255,240,245, 0
GridLayout:
rows:2
BoxLayout:
KivyCamera:
id:cv2cam_l
BoxLayout:
KivyCamera:
id:cv2cam_r
BoxLayout:
rows:3
orientation: 'vertical'
BoxLayout:
Image:
id:image_l
text: 'left'
Image:
id:image_r
text: 'right'
BoxLayout:
Button:
id:button2
BoxLayout:
Button:
id:button1
text: 'CAPTURE'
<MyPopup@Popup>:
title: 'Test'
size_hint: None, None
size: 400, 400
界面是这样滴
摄像头驱动
class KivyCamera(Image):
def __init__(self, **kwargs):
super(KivyCamera, self).__init__(**kwargs)
self.capture = None
self.clock_event = None
def start(self, capture, fps=30):
self.capture = capture
self.clock_event = Clock.schedule_interval(self.update, 1.0 / fps)
def stop(self):
Clock.unschedule(self.clock_event)
if self.capture != None:
self.capture.release()
def update(self, dt):
ret, frame = self.capture.read()
if ret:
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 彩色转灰度
# convert it to texture
buf1 = cv2.flip(frame, 0)
buf = buf1.tostring()
texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.texture = texture1
主程序
class MainScreen(BoxLayout):
num = 0
def dostart(self,*largs):
self.capturel = cv2.VideoCapture(1)
self.capturer = cv2.VideoCapture(2)
self.ids.cv2cam_l.start(self.capturel)
self.ids.cv2cam_r.start(self.capturer)
self.ids.button1.bind(on_release=self.release)
self.ids.button2.bind(on_release=self.press)
def press(self,instance):
self.ids.image_l.source = ''
self.ids.image_r.source = ''
def release(self,instance):
img_l = self.ids.cv2cam_l.frame
img_r = self.ids.cv2cam_r.frame
cname_l = 'img/test_l'+str(self.num)+'.png'
cname_r = 'img/test_r' + str(self.num) + '.png'
cv2.imwrite(cname_l,img_l)
cv2.imwrite(cname_r,img_r)
self.ids.image_l.source = cname_l
self.ids.image_r.source = cname_r
self.num += 1
完成后是这样滴
实验二
使用opencv进行双目摄像头的标定
实验三
生成距离点云图
实验四
对特定物体进行检测,检测其长、宽、高并计算体积