Python 实现摄像头功能

正确的程序如下所示:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

from VideoCapture import Device

import time

import sys,pygame

pygame.init()

size = width,height = 620,485

speed = [2,2]

black = 0,0,0

pygame.display.set_caption(‘视频窗口‘)

screen = pygame.display.set_mode(size)

#抓去频率,抓去一次

sleep_time_long = 0.1

#初始化摄像头

cam = Device(devnum=0,showVideoWindow=0)

while 1:

#抓图

cam.saveSnapshot(‘test.jpg‘,timestamp=3,boldfont=1,quality=75)

#加载图像

image = pygame.image.load(‘test.jpg‘)

#传送画面

screen.blit(image,speed)

#显示图像

pygame.display.flip()

#休眠一下,等待一分钟

time.sleep(sleep_time_long)

以下为笔者所写程序:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

from VideoCapture import DEVIDE

import time

import sys,pygame

[A1]

size = width,height = 600,485

speed = [2,2[A2] ]

black[A3]  = (0,0,0)

pygame.display.set_Caption(‘视频窗口‘)

pygame[A4] .set_mode(size)

sleep_time_long=0.1

cam = DEVICE(devnum=0,VideoWindow=0)

while true:

cam.saveSnapshot(‘test.jpg‘,timestamp=3,boldfont=1,quality=75)

image = pygame.iamge.load(‘test.jpg‘)

screen.blit[A5] ()

pygame.display.flip()

time.sleep(sleep_time_long)

解释及延伸:

pygame.init

initializeall imported pygame modules

pygame.init():return (numpass, numfail)

Initialize all imported Pygamemodules. No exceptions will be raised if a module fails, but the total numberif successful and failed inits will be returned as a tuple. You can alwaysinitialize individual modules manually, but pygame.init is aconvenient
way to get everything started. The init() functions forindividual modules will raise exceptions when they fail.

You may want to initalise thedifferent modules seperately to speed up your program or to not use things yourgame does not.

Itis safe to call this init() more than once: repeated calls willhave no effect. This is true even if you have pygame.quit –uninitializeall
pygame modules– all the modules.

pygame.display.set_caption

set the current window caption

pygame.display.set_caption(title,icontitle=None): return None

If the display has a window title, thisfunction will change the name on the window. Some systems support an alternateshorter title to be used for minimized displays.

pygame.display.set_mode

initialize a window or screen for display

pygame.display.set_mode(resolution=(0,0),flags=0, depth=0): return Surface

This function will create a display Surface.The arguments passed in are requests for a display type. The actual createddisplay will be the best possible match supported by the system.

The resolution argument is a pair of numbersrepresenting the width and height. The flags argument is a collection ofadditional options. The depth argument represents the number of bits to use forcolor.

The Surface that gets returned can be drawnto like a regular Surface but changes will eventually be seen on the monitor.

If no resolution is passed or is set to (0,0) and pygame uses SDL version 1.2.10 or above, the created Surfacewill have the same size as the current screen resolution. If only the width orheight are set to 0, the Surface will have the same width
or height as thescreen resolution. Using a SDL version prior to 1.2.10 will raise anexception.

It is usually best to not pass the depthargument. It will default to the best and fastest color depth for the system.If your game requires a specific color format you can control the depth withthis argument. Pygame will emulate an unavailable
color depth which can beslow.

When requesting fullscreen display modes,sometimes an exact match for the requested resolution cannot be made. In thesesituations pygame will select the closest compatable match. The returnedsurface will still always match the requested resolution.

The flags argument controls which type ofdisplay you want. There are several to choose from, and you can even combinemultiple types using the bitwise or operator, (the pipe "|"character). If you pass 0 or no flags argument it will default to
a softwaredriven window. Here are the display flags you will want to choose from:

pygame.FULLSCREEN    create afullscreen display

pygame.DOUBLEBUF     recommendedfor HWSURFACE or OPENGL

pygame.HWSURFACE     hardwareaccelerated, only in FULLSCREEN

pygame.OPENGL        create anopengl renderable display

pygame.RESIZABLE     displaywindow should be sizeable

pygame.NOFRAME       displaywindow will have no border or controls

surface和屏幕

pygame 最重要的部分就是surface。我们可以把surface看作是一张白纸。你可以对surface作很多操作,比如在surface上画线、用某种颜色 填充surface上的部分区域、把图片拷贝到surface上去,把图片从surface上复制下来、设置或者读取surface上某个点的颜色。一个 surface可以是任何大小,一个游戏可以有任意多surface。其中有一个surface是特别的,就是用 pygame.display.set_mode()创建的display surface。它代表了屏幕,对它的任何操作会出现在用户的屏幕上。一个游戏只能有一个这样的surface,这是SDL的限制。

怎 样创建surface?刚才提到,用pygame.display.set_mode()可以创建特殊的display surface。此外,还可以用image.load()创建一个包含图片的surface,还可以用font.render()创建一个包含文字的 surface。你甚至可以用Surface()创建一个不包含任何东西的surface。

surface的大部分方法都不重要,只要学习其中的blit(), fill(), set_at()和get_at()就够用了。

display surface的初始化操作是这样的:

1 screen = pygame.display.set_mode((1024, 768))

或者

screen = pygame.display.set_mode((1024, 768), pygame.FULLSCREEN)

你可以用set_mode把原来窗口的游戏变成全屏。其它的俄显式模式(可以用|连接)有

DOUBLEBUF: 对于平滑的动画所必须 OPENGL: 让你可以用PyOpenGL,但是不能用pygame的绘图函数 还有一个可选的depth参数,用来控制颜色显示的深度。一般情况下不用指定这个参数,只要用默认值就可以了。

如果使用DOUBLEBUF,你需要用flip函数来把绘制的内容显示到屏幕上。

>>> pygame.display.flip()

画图

接下来,我们在屏幕上画一幅图像。我们通过最重要的画图原语BLIT(BLock Image Transfer)来实现,它可以把图像从一个地方(比如源图像)拷贝到另一个地方(比如屏幕上的某个位置)。

>>> car = pygame.image.load(‘car.png‘)>>> screen.blit(car, (50, 100))>>> pygame.display.flip()

这时,图片car.png中的内容会显示在屏幕上,图片的左上角在屏幕上的坐标是(50, 100)。屏幕坐标的X轴从作往右的,Y轴是从上往下的。然后我们可以用如下语句来旋转图片:

>>> rotated = pygame.transform.rotate(car, 90)>>> screen.blit(car, (50, 100))>>> pygame.display.flip()

要让屏幕上的任何东西动起来,需要一个这样的过程:画一个场景,然后把它擦掉,然后再画一幅稍微不同的场景,然后再擦掉……如此反复。比如:

1 for i in range(100):   2     screen.fill((0, 0, 0))   3     screen.blit(rotated, (i*10, 0))   4     pygame.display.flip()

注意:这里我们把整个屏幕清楚然后重新绘制不是很好的方法。最好是只把屏幕上变化的部分擦掉重画,其它部分不变。Sprite模块可以帮助我们做这个事情。

 

pygame.image.load

load newimage from a file

pygame.image.load(filename):return Surface

pygame.image.load(fileobj,namehint=""): return Surface

Load an image from a file source. Youcan pass either a filename or a Python file-like object.

Pygame will automatically determinethe image type (e.g., GIF or bitmap)and create a new Surface object from the data. In some cases it will need toknow the file extension (e.g., GIF images shouldend in ".gif"). If you pass a raw file-like
object, you may also wantto pass the original filename as the namehint argument.

The returnedSurface will contain the same color format, colorkey and alpha transparency asthe file it came from. You will often want to callSurface.convert –changethe
pixel format of an image– with no arguments, to create a copythat will draw more quickly on the screen.

For alpha transparency, like in .pngimages use the convert_alpha() method afterloading so that the image has per pixel transparency.

Pygame may not always be built tosupport all image formats. At minimum it will support uncompressed BMP. If pygame.image.get_extended –testif
extended image formats can be loaded– returns‘True‘, you should be able to load most images (including png, jpg and gif).

You should use os.path.join() forcompatibility.

  eg. asurf = pygame.image.load(os.path.join(‘data‘, ‘bla.png‘))

pygame.display.flip

update the full display Surface to thescreen

pygame.display.flip():return None

This will update the contents of the entire display. Ifyour display mode is using the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this will wait for a verticalretrace and swap the surfaces. If you are using a different type of displaymode,
it will simply update the entire contents of the surface.

When using an pygame.OPENGL display mode this will perform a gl buffer swap.


[A1]需要初始化pygame.init()

[A2]参数意思没搞明白

[A3]Black没搞明白做啥用

[A4]缺少screen

[A5]缺少参数

Python 实现摄像头功能

时间: 2024-08-27 21:44:10

Python 实现摄像头功能的相关文章

为python 添加新功能-dump

一直觉得thinkphp提供的dump函数挺好用的,但是python里面没有,就一直想着写个简单的. dir是我比较常用的一个内置函数了,但是显示效果实在有点受不了,每次我都要从大量的字符串里找到我需要的,眼都花了. 所以我就想,一行显示一个就好了. 所以我就写了一个模块,命名为dp 1 #!/usr/bin/env python 2 #coding:utf-8 3 4 """ 5 dump variable 6 """ 7 def dump(v

Python骚操作:利用Python获取摄像头并实时控制人脸!

实现流程 从摄像头获取视频流,并转换为一帧一帧的图像,然后将图像信息传递给opencv这个工具库处理,返回灰度图像(就像你使用本地静态图片一样)程序启动后,根据监听器信息,使用一个while循环,不断的加载视频图像,然后返回给opencv工具呈现图像信息.创建一个键盘事件监听,按下"d"键,则开始执行面部匹配,并进行面具加载(这个过程是动态的,你可以随时移动).面部匹配使用Dlib中的人脸检测算法来查看是否有人脸存在.如果有,它将为每个人脸创建一个结束位置,眼镜和烟卷会移动到那里结束.

python专题-爬虫功能

在我们日常上网浏览网页的时候,经常会看到一些好看的图片,我们就希望把这些图片保存下载,或者用户用来做桌面壁纸,或者用来做设计的素材. 我们最常规的做法就是通过鼠标右键,选择另存为.但有些图片鼠标右键的时候并没有另存为选项,还有办法就通过就是通过截图工具截取下来,但这样就降低图片的清晰度.好吧-!其实你很厉害的,右键查看页面源代码. 我们可以通过python 来实现这样一个简单的爬虫功能,把我们想要的代码爬取到本地.下面就看看如何使用python来实现这样一个功能. 一,获取整个页面数据 首先我们

python实现发送邮件功能

前一段时间实现了一个python脚本发送邮件的功能,该脚本是借用smtp服务器发送邮件,邮件以附件的形式发出,如果要添加正文,修改添加即可. #!/usr/bin/env python #coding: utf-8 import smtplib, re, sys, os import xlwt from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.im

利用python收发邮件功能实现远程电脑的控制

功能并非原创, 只是重复实现了 http://codecloud.net/python-control-128.html 中描述的功能. 实现功能: 通过给固定邮件地址发送命令(包含在主题中)的方式控制远程电脑实现相应功能(譬如关机等, 可以根据实际需要实现更复杂的功能) 实现原理: 远程电脑不断(每隔10s)利用 python 脚本检查邮件服务器是否有新邮件, 如有则对邮件进行分析, 如果包含可执行命令, 则执行相应命令 你需要的: 装有 PYTHON 的 PC; 最好两个邮箱(一个用于接收命

python爬虫高级功能

上一篇文章中我们介绍了爬虫的实现,及爬虫爬取数据的功能,这里会遇到几个问题,比如网站中robots.txt文件,里面有禁止爬取的URL,还有爬虫是否支持代理功能,及有些网站对爬虫的风控措施,设计的爬虫下载限速功能. 1.解析robots.txt 首先,我们需要解析robots.txt文件,以避免下载禁止爬取的URL.适用Python自带的robotparser模块,就可以轻松的完成这项工作,如下面的代码. robotparser模块首先加载robots.txt文件,然后通过can_fetch()

python int 内部功能剖析

在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下int类型对象具有哪些功能,我们平常是怎么使用的. int 类源码分析 class int(object): """ int(x=0) -> int or long int(x, base=10) -> int or long Convert a number or string to an integer, or return 0 if no arguments

在web中使用windows控件,实现摄像头功能

最近做的一个Web版的视频会议项目,需要在网页中播放来自远程摄像头采集的实时视频,我们已经有了播放远程实时视频的使用C#编写的windows控件,如何将其嵌入到网页中去了?这需要使用一种古老的技术,ActiveX. 1.将.Net控件转化为ActiveX控件 首先要做的就是将我们的windows视频播放控件转化为ActiveX控件.先看看我们视频播放控件的定义,其基于OMCS实现,相当简单: [csharp] view plain copy public partial class Camera

利用PYTHON设计计算器功能

通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3)/(16-3*2)) 我的处理计算基本思路是: 解题思路是,需要优先处理内层括号运算--外层括号运算--先乘除后加减的原则:1.正则处理用户输入的字符串,然后对其进行判断,判断计算公式是否有括号,有就先将计算公式进行正则处理,先获取最里层的每一个数据,然后一一计算 所要用到的正则是: inner