环境是 Python27html
须要下载库:python
pygame 处理帧web
http://www.lfd.uci.edu/~gohlke/pythonlibs/bpchanvi/pygame-1.9.2pre.win-amd64-py2.7.exeide
VideoCature取得摄像头图像函数
http://www.lfd.uci.edu/~gohlke/pythonlibs/bpchanvi/VideoCapture-0.9.5.win-amd64-py2.7.exespa
PIL python Image Lib 图像处理,最基础的依赖包调试
http://www.lfd.uci.edu/~gohlke/pythonlibs/bpchanvi/PIL-1.1.7.win-amd64-py2.7.execode
先来个摄像头截取的代码(从DEMO中抠出来的)视频
1 import VideoCapture2 3 cam = VideoCapture.Device(devnum=0)4 cam.saveSnapshot('test.jpg', quality=75, timestamp=3, boldfont=1)
而后是比较复杂的连续从摄像头中取得视频流,把原有的DEMO代码加了注释调整了下顺序htm
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.031 contrast = 1.032 # 截图计数器 33 shots = 034 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 -= .143 if keyinput[K_2]: brightness += .144 if keyinput[K_3]: contrast -= .145 if keyinput[K_4]: contrast += .146 # 呼出调试 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 += 154 # 从摄像头中取得图像 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 Image2 import ImageFilter3 img = Image.open('test.jpg')4 new_img = img.filter(ImageFilter.FIND_EDGES)5 new_img.save('test2.jpg')
本来的想法是从摄像头中取得图像进行分析,分析眼动来替代鼠标的位置。不过取出的视频精度显然很低。看来这个要以来高精度的摄像头,高精度的摄像头带来的问题就是处理的速度下降。
同时,摄像头在本本上,来定位脑壳、眼球、笔记本、摄像头,转换为屏幕上的鼠标轨迹,显然有点困难。不过在家里面作个体感的游戏还凑合。
总体上看,