V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
qinghon
V2EX  ›  Python

Python 镜像文件流

  •  
  •   qinghon ·
    qinghon · 2019-05-07 13:00:36 +08:00 · 1813 次点击
    这是一个创建于 1788 天前的主题,其中的信息可能已经有所发展或是发生改变。

    背景

    最近做毕设需要做用树莓派视频流式处理,copy 了一个 picamera 的[源码]( https://github.com/waveform80/pistreaming)(大致思路是树莓派录制原是视频流 yuv,用 ffmpeg 转成 mpeg1 喂给 jsmpeg)并改造到了 flask 里用,
    直接用的效果挺好,前提是不加其他骚操作,但现在需要定时从摄像头提取画面,但这样会造成视频流丢帧
    所以我就想到了将视频流复制出来,需要提取的时候就取一帧就好,
    

    问题

    现在的问题是被卡在了复制流上
    尝试写出了这样的同步代码,但同样会造成丢帧
    
        将输入流写入多个文件流
    class Mirror(object):
        def __init__(self, items):
            self.items = items
    
        def write(self, b):
            for x in self.items:
                x.write(b)
    
        def flush(self):
            for x in self.items:
                x.flush()
    
    不知道还有没有什么好办法,python 基础不牢,请大佬们轻拍
    ps:开线程的话会造成频繁删建线程,似乎会造成性能问题,SF 上看到的
    
    4 条回复    2019-05-12 17:18:59 +08:00
    Cooky
        1
    Cooky  
       2019-05-07 13:17:14 +08:00 via Android
    那就线程池呗,提前开出来
    量不大就塞内存里慢慢写
    helior
        2
    helior  
       2019-05-07 14:02:39 +08:00 via iPhone   ❤️ 1
    picamera 有个从录制视频中提取一帧的方法 ,camera.capture(,这里啥参数我忘了)你查一下
    qinghon
        3
    qinghon  
    OP
       2019-05-07 18:35:36 +08:00
    @helior 感谢,虽然截取的质量低了一点,但能用就行,就是不知道为什么容易导致关闭不了摄像头,导致第二次启动的时候进程锁死,要重启才行,头疼
    参数是`use_video_port=True`,给后来人
    qinghon
        4
    qinghon  
    OP
       2019-05-12 17:18:59 +08:00
    时隔几天回来补充一下
    如果捕获图片时用 use_video_port=True 参数并没有什么好处,而且因为会请求视频端口的原因,很容易造成错误退出时不能正常关闭视频端口,下一次启动的时候就锁死进程,重启才能释放.
    直接捕获反而不会有这问题,
    The general idea here is that the capture (still) port operates on its own, while the video port is always connected to a splitter component, so requests for a video port also have to specify which splitter port they want to use.
    源代码里的原话
    这坑里有屎
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2449 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 15:52 · PVG 23:52 · LAX 08:52 · JFK 11:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.