python:在python中使用opencv进行摄像头编程初体验

闲着没事做,前段时间买了个摄像头,在ubuntu上用。打开cheese这个软件,一片空白,怎么不能用阿!

google一番,装上gspca,还是不能用!

用lsusb命令查看下

lingshangwen@eagle:~$ lsusb
Bus 005 Device 001: ID 0000:0000
Bus 004 Device 001: ID 0000:0000
Bus 003 Device 001: ID 0000:0000
Bus 002 Device 002: ID 0c45:5208 Microdia
Bus 002 Device 001: ID 0000:0000
Bus 001 Device 006: ID 058f:3820 Alcor Micro Corp.
Bus 001 Device 005: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 001 Device 004: ID 05e3:0606 Genesys Logic, Inc. D-Link DUB-H4 USB 2.0 Hub
Bus 001 Device 001: ID 0000:0000

摄像头已经被识别出来,怎么就是不能用阿!!!!!!

 

还是自己动手,用python+opencv写段简单的代码吧,然后就有了下面的代码:

 

Python代码 复制代码
  1. import wx   
  2. from opencv.cv import *   
  3. from opencv.highgui import *   
  4.     
  5. class MyFrame(wx.Frame):   
  6.     def __init__(self):   
  7.         wx.Frame.__init__(selfNone, -1'camera')   
  8.         self.SetClientSize((640480))   
  9.     
  10.         self.cap = cvCreateCameraCapture(0)   
  11.         self.Bind(wx.EVT_IDLE, self.onIdle)   
  12.     
  13.     def onIdle(self, event):   
  14.         img = cvQueryFrame(self.cap)   
  15.         self.displayImage(img)   
  16.         event.RequestMore()   
  17.     
  18.     def displayImage(self, img, offset=(0,0)):   
  19.         bitmap = wx.BitmapFromBuffer(img.width, img.height, img.imageData)   
  20.         dc = wx.ClientDC(self)   
  21.         dc.DrawBitmap(bitmap, offset[0], offset[1], False)   
  22.     
  23. if __name__=="__main__":   
  24.     app = wx.App()   
  25.     frame = MyFrame()   
  26.     frame.Show(True)   
  27.     app.MainLoop()  
import wx
from opencv.cv import *
from opencv.highgui import *
 
class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'camera')
        self.SetClientSize((640, 480))
 
        self.cap = cvCreateCameraCapture(0)
        self.Bind(wx.EVT_IDLE, self.onIdle)
 
    def onIdle(self, event):
        img = cvQueryFrame(self.cap)
        self.displayImage(img)
        event.RequestMore()
 
    def displayImage(self, img, offset=(0,0)):
        bitmap = wx.BitmapFromBuffer(img.width, img.height, img.imageData)
        dc = wx.ClientDC(self)
        dc.DrawBitmap(bitmap, offset[0], offset[1], False)
 
if __name__=="__main__":
    app = wx.App()
    frame = MyFrame()
    frame.Show(True)
    app.MainLoop()

 

 运行之

 

原文地址:http://eagle516.iteye.com/blog/428801

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值