【python模塊 VideoCapture 使用】


【python模塊 VideoCapture 使用】

網址:http://videocapture.sourceforge.net/  需要配合PIL使用

也就是python的攝像頭模塊,官方介紹如下:

VideoCapture is a Pythonextension for Win32 which makes it possible to access video-capture devices (e.g. a USB webcam). It consists of a low-level native module (vidcap.pyd) and a high-level module written in Python (VideoCapture.py) which should be used solely.

Also included are some tools which can be used to periodically upload a WebCam-image to a WebServer, monitor and save a picture at a given URL to the local disk, and so on. Please refer to the README.txt file in the 'Tools' folder for further details.

從網站上拉下來的是一個集合包,包含了python各版本的支持,安裝的時候直接把相應版本的lib,libs目錄覆蓋到python目錄下即可。

最簡單的使用方法如下:

from VideoCapture import Device
cam = Device()
cam.SaveSnapshot("xxx.jpg") #使用到圖片庫

唯一的一個類是Device,包含如下方法:

 

 1 Methods defined here:
2
3 __init__(self, devnum=0, showVideoWindow=0)
4 devnum: VideoCapture enumerates the available video capture devices
5 on your system. If you have more than one device, specify
6 the desired one here. The device number starts from 0.
7
8 showVideoWindow: 0 ... do not display a video window (the default)
9 1 ... display a video window
10
11 Mainly used for debugging, since the video window
12 can not be closed or moved around.
13 displayCaptureFilterProperties(self)
14 Displays a dialog containing the property page of the capture filter.
15
16 For VfW drivers you may find the option to select the resolution most
17 likele here.
18 displayCapturePinProperties(self)
19 Displays a dialog containing the property page of the capture pin.
20
21 For WDM drivers you may find the option to select the resolution most
22 likele here.
23 displayPropertyPage(self)
24 deprecated
25
26 Use the methods displayCaptureFilterProperties() and
27 displayCapturePinProperties() instead.
28 getBuffer(self)
29 Returns a string containing the raw pixel data.
30
31 You probably don't want to use this function, but rather getImage() or
32 saveSnapshot().
33 getImage(self, timestamp=0, boldfont=0, textpos='bl')
34 Returns a PIL Image instance.
35
36 timestamp: 0 ... no timestamp (the default)
37 1 ... simple timestamp
38 2 ... timestamp with shadow
39 3 ... timestamp with outline
40
41 boldfont: 0 ... normal font (the default)
42 1 ... bold font
43
44 textpos: The position of the timestamp can be specified by a string
45 containing a combination of two characters. One character
46 must be either t or b, the other one either l, c or r.
47
48 t ... top
49 b ... bottom
50
51 l ... left
52 c ... center
53 r ... right
54
55 The default value is 'bl'
56 saveSnapshot(self, filename, timestamp=0, boldfont=0, textpos='bl', **keywords)
57 Saves a snapshot to the harddisk.
58
59 The filetype depends on the filename extension. Everything that PIL
60 can handle can be specified (foo.jpg, foo.gif, foo.bmp, ...).
61
62 filename: String containing the name of the resulting file.
63
64 timestamp: see getImage()
65
66 boldfont: see getImage()
67
68 textpos: see getImage()
69
70 Additional keyword arguments can be give which are just passed to the
71 save() method of the Image class. For example you can specify the
72 compression level of a JPEG image by quality=75 (which is the default
73 value anyway).
74 setResolution(self, width, height)
75 Sets the capture resolution. (without dialog)
76
77 (contributed by Don Kimber <kimber@fxpal.com>)
78
79 --------------------------------------------------------------------------------
80 Data and non-method functions defined here:
81
82 __doc__ = 'Create instances of this class which will then r...lete\n the instance first (e.g. del cam).\n\n '
83 __module__ = 'VideoCapture'

這個庫很簡單,主要是GetImage和GetSnapshot兩個方法的使用

網上的一段有意思的代碼:

View Code
 1 from VideoCapture import Device
2 import ImageDraw, sys, pygame, time
3 from pygame.locals import *
4 from PIL import ImageEnhance
5
6 res = (640,480)
7 pygame.init()
8 cam = Device()
9 cam.setResolution(res[0],res[1])
10 screen = pygame.display.set_mode((640,480))
11 pygame.display.set_caption(Webcam)
12 pygame.font.init()
13 font = pygame.font.SysFont("Courier",11)
14
15 def disp(phrase,loc):
16 s = font.render(phrase, True, (200,200,200))
17 sh = font.render(phrase, True, (50,50,50))
18 screen.blit(sh, (loc[0]+1,loc[1]+1))
19 screen.blit(s, loc)
20
21 brightness = 1.0
22 contrast = 1.0
23 shots = 0
24
25 while 1:
26 camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
27 camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
28 for event in pygame.event.get():
29 if event.type == pygame.QUIT: sys.exit()
30 keyinput = pygame.key.get_pressed()
31 if keyinput[K_1]: brightness -= .1
32 if keyinput[K_2]: brightness += .1
33 if keyinput[K_3]: contrast -= .1
34 if keyinput[K_4]: contrast += .1
35 if keyinput[K_q]: cam.displayCapturePinProperties()
36 if keyinput[K_w]: cam.displayCaptureFilterProperties()
37 if keyinput[K_s]:
38 filename = str(time.time()) + ".jpg"
39 cam.saveSnapshot(filename, quality=80, timestamp=0)
40 shots += 1
41 camshot = pygame.image.frombuffer(camshot.tostring(), res, "RGB")
42 screen.blit(camshot, (0,0))
43 disp("S:" + str(shots), (10,4))
44 disp("B:" + str(brightness), (10,16))
45 disp("C:" + str(contrast), (10,28))
46 pygame.display.flip()


 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM