Home Backend Development Python Tutorial videocapture库制作python视频高速传输程序

videocapture库制作python视频高速传输程序

Jun 16, 2016 am 08:45 AM

1,首先是视频数据[摄像头图像]的采集,通常可以使用vfw在vc或者vb下实现,这个库我用的不好,所以一直不怎么会用.现在我们用到的是python的videocapture库,这个库用起来很简单,如下:

复制代码 代码如下:

from VideoCapture import Device
cam = Device()
cam.setResolution(320,240)   #设置显示分辨率
cam.saveSnapshot('demo.jpg') #抓取并保存图片

这样,你就得到了一张图片.
当然,要实现比较高速的采集,每次都要保存图片是不大可能的,这样弄下来每次采集一张图片已经接近1秒钟,这个速度我们是无法忍受的.
更好的解决方式是直接这样: im = cam.getImage(),返回的是一个Image对象,是一个内存块,对它操作就快速多了.
2,如何传输图片?我不知道pplive这种视频是怎么传输的,我的想法很简单,每次传送一幅图片.
在本程序中,每次传送的RGB图像的大小为 160*120.这样,需要的数据量为:d = 160*120*3 = 56.25 kB
我选择80000B.
下面是发送端代码:

复制代码 代码如下:

import socket
import Image
from VideoCapture import Device
cam = Device()
cam.setResolution(320,240)
clisocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while 1:
    im = cam.getImage()
    im = im.resize((160,120))
    da = im.tostring()
    clisocket.sendto(da, ("127.0.0.1", 1234))
s.close()

3,如何实时显示图片?
我使用了pygame作为实时图像显示界面,因为pygame是经过优化的高速图形库,不知道有没有使用directshow,我想应该用了吧..
关于pygame请参考 www.pygame.org
下面是接收端代码:

复制代码 代码如下:

import socket
import Image
import os,sys,pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((160,120))
pygame.display.set_caption("web cam")

pygame.display.flip()
svrsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
svrsocket.bind(("127.0.0.1", 1234))
clock = pygame.time.Clock()    #计算帧速
while 1:
    data, address = svrsocket.recvfrom(80000)
    camshot = pygame.image.frombuffer(data, (160,120), "RGB")
    for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
    screen.blit(camshot, (0,0))
    pygame.display.update()
    print clock.get_fps()     #在终端打印帧速
    clock.tick()

程序终于完成了,测试一下效果怎么样把,为了方便,我把客户端和服务端都设成了本机,端口为1234.
运行程序,哇,简直不可思议,最高竟然达到230fps!(右边的终端)

videocapture库制作python视频高速传输程序

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles