您的位置:首页 > 编程语言 > Python开发

在WIN7-64下使用PYTHON获得摄像头图片

2012-06-07 16:51 441 查看
环境是 Python27

需要下载库:

pygame 处理帧

http://www.lfd.uci.edu/~gohlke/pythonlibs/bpchanvi/pygame-1.9.2pre.win-amd64-py2.7.exe

VideoCature取得摄像头图像

http://www.lfd.uci.edu/~gohlke/pythonlibs/bpchanvi/VideoCapture-0.9.5.win-amd64-py2.7.exe

PIL python Image Lib 图像处理,最基础的依赖包

http://www.lfd.uci.edu/~gohlke/pythonlibs/bpchanvi/PIL-1.1.7.win-amd64-py2.7.exe

先来个摄像头截取的代码(从DEMO中抠出来的)

import VideoCapture

cam = VideoCapture.Device(devnum=0)
cam.saveSnapshot('test.jpg', quality=75, timestamp=3, boldfont=1)


 

然后是比较复杂的连续从摄像头中取得视频流,把原有的DEMO代码加了注释调整了下顺序

#coding=utf-8
from VideoCapture import Device
import ImageDraw, sys, pygame, time
from pygame.locals import *
from PIL import ImageEnhance

#   设置窗口、摄像头分辨率
res = (640,480)

#   初始化窗口
pygame.init()
pygame.display.set_caption('Webcam')
pygame.font.init()

screen = pygame.display.set_mode((640,480))
font   = pygame.font.SysFont("Courier",11)

#   取得摄像头设备,设置分辨率
cam = Device()
cam.setResolution(res[0],res[1])

#   显示文字信息函数
def disp(phrase,loc):
s = font.render(phrase, True, (200,200,200))
sh = font.render(phrase, True, (50,50,50))
screen.blit(sh, (loc[0]+1,loc[1]+1))
screen.blit(s, loc)

#   亮度,对比度
brightness = 1.0
contrast = 1.0
#   截图计数器
shots = 0

while 1:
camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
keyinput = pygame.key.get_pressed()
#   快捷键处理,调节亮度对比度
if keyinput[K_1]: brightness -= .1
if keyinput[K_2]: brightness += .1
if keyinput[K_3]: contrast -= .1
if keyinput[K_4]: contrast += .1
#   呼出调试
if keyinput[K_q]: cam.displayCapturePinProperties()
if keyinput[K_w]: cam.displayCaptureFilterProperties()
#   截图保存,shots为计数器
if keyinput[K_s]:
filename = str(time.time()) + ".jpg"
cam.saveSnapshot(filename, quality=80, timestamp=0)
shots += 1
#   从摄像头中取得图像
camshot = pygame.image.frombuffer(camshot.tostring(), res, "RGB")
screen.blit(camshot, (0,0))
disp("S:" + str(shots), (10,4))
disp("B:" + str(brightness), (10,16))
disp("C:" + str(contrast), (10,28))
#   填充图像
pygame.display.flip()


自己是准备处理图像,把截屏出来的那个图片,取得边缘。

import Image
import ImageFilter
img = Image.open('test.jpg')
new_img = img.filter(ImageFilter.FIND_EDGES)
new_img.save('test2.jpg')


原本的想法是从摄像头中取得图像进行分析,分析眼动来替代鼠标的位置。不过取出的视频精度显然很低。看来这个要以来高精度的摄像头,高精度的摄像头带来的问题就是处理的速度降低。

同时,摄像头在本本上,来定位脑袋、眼球、笔记本、摄像头,转换为屏幕上的鼠标轨迹,显然有点困难。不过在家里面做个体感的游戏还凑合。

整体上看,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: