课程设计python调用摄像头_python开启摄像头以及深度学习实现目标检测方法

最近想做实时目标检测,需要用到python开启摄像头,我手上只有两个uvc免驱的摄像头,性能一般。利用python开启摄像头费了一番功夫,主要原因是我的摄像头都不能用cv2的VideCapture打开,这让我联想到原来opencv也打不开Android手机上的摄像头(后来采用QML的Camera模块实现的)。看来opencv对于摄像头的兼容性仍然不是很完善。

我尝了几种办法:v4l2,v4l2_capture以及simpleCV,都打不开。最后采用pygame实现了摄像头的采集功能,这里直接给大家分享具体实现代码(python3.6,cv2,opencv3.3,ubuntu16.04)。中间注释的部分是我上述方法打开摄像头的尝试,说不定有适合自己的。

import pygame.camera

import time

import pygame

import cv2

import numpy as np

def surface_to_string(surface):

"""convert pygame surface into string"""

return pygame.image.tostring(surface, 'RGB')

def pygame_to_cvimage(surface):

"""conver pygame surface into cvimage"""

#cv_image = np.zeros(surface.get_size, np.uint8, 3)

image_string = surface_to_string(surface)

image_np = np.fromstring(image_string, np.uint8).reshape(480, 640, 3)

frame = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)

return image_np, frame

pygame.camera.init()

pygame.camera.list_cameras()

cam = pygame.camera.Camera("/dev/video0", [640, 480])

cam.start()

time.sleep(0.1)

screen = pygame.display.set_mode([640, 480])

while True:

image = cam.get_

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值