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

环境是 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中抠出来的)

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

 

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

 1 #coding=utf-8
 2 from VideoCapture import Device
 3 import ImageDraw, sys, pygame, time
 4 from pygame.locals import *
 5 from PIL import ImageEnhance
 6 
 7 #   设置窗口、摄像头分辨率
 8 res = (640,480)
 9 
10 #   初始化窗口
11 pygame.init()
12 pygame.display.set_caption('Webcam')
13 pygame.font.init()
14 
15 screen = pygame.display.set_mode((640,480))
16 font   = pygame.font.SysFont("Courier",11)
17 
18 #   取得摄像头设备,设置分辨率
19 cam = Device()
20 cam.setResolution(res[0],res[1])
21 
22 #   显示文字信息函数
23 def disp(phrase,loc):
24     s = font.render(phrase, True, (200,200,200))
25     sh = font.render(phrase, True, (50,50,50))
26     screen.blit(sh, (loc[0]+1,loc[1]+1))
27     screen.blit(s, loc)
28 
29 #   亮度,对比度
30 brightness = 1.0
31 contrast = 1.0
32 #   截图计数器
33 shots = 0
34 
35 while 1:
36     camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
37     camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
38     for event in pygame.event.get():
39         if event.type == pygame.QUIT: sys.exit()
40     keyinput = pygame.key.get_pressed()
41     #   快捷键处理,调节亮度对比度
42     if keyinput[K_1]: brightness -= .1
43     if keyinput[K_2]: brightness += .1
44     if keyinput[K_3]: contrast -= .1
45     if keyinput[K_4]: contrast += .1
46     #   呼出调试
47     if keyinput[K_q]: cam.displayCapturePinProperties()
48     if keyinput[K_w]: cam.displayCaptureFilterProperties()
49     #   截图保存,shots为计数器
50     if keyinput[K_s]:
51         filename = str(time.time()) + ".jpg"
52         cam.saveSnapshot(filename, quality=80, timestamp=0)
53         shots += 1
54     #   从摄像头中取得图像
55     camshot = pygame.image.frombuffer(camshot.tostring(), res, "RGB")
56     screen.blit(camshot, (0,0))
57     disp("S:" + str(shots), (10,4))
58     disp("B:" + str(brightness), (10,16))
59     disp("C:" + str(contrast), (10,28))
60     #   填充图像
61     pygame.display.flip()

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

1 import Image
2 import ImageFilter
3 img = Image.open('test.jpg')
4 new_img = img.filter(ImageFilter.FIND_EDGES)
5 new_img.save('test2.jpg')

 

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

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

 

整体上看,

posted on 2012-06-07 16:51  webooxx  阅读(6374)  评论(0编辑  收藏  举报

导航