为什么程序只能运行一次。(PYTHON)

2024-06-12 19:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject #,Gtk
from gi.repository import Gst as gst
import datetime

def take_photo():        
    GObject.threads_init()
    gst.init(None)
    pipeline = gst.Pipeline()
    video_source = gst.ElementFactory.make('v4l2src', 'video_source')
    vconvert = gst.ElementFactory.make('videoconvert', 'vconvert')
    clock = gst.ElementFactory.make('clockoverlay', 'clock')
    timer= gst.ElementFactory.make('timeoverlay','timer')
    vrate = gst.ElementFactory.make('videorate', 'vrate')
    sconvert = gst.ElementFactory.make('videoconvert', 'sconvert')
    png = gst.ElementFactory.make('pngenc', 'png')
    multi_sink = gst.ElementFactory.make('multifilesink', 'multi_sink')
    caps = gst.caps_from_string("video/x-raw,format=RGB,width=800,height=600,framerate=5/1")
    timer.set_property('valignment','bottom')
    timer.set_property('halignment','right')
    clock.set_property('time-format','%Y/%m/%d %H:%M:%S')
    clock.set_property('valignment','bottom')
    caps1 = gst.caps_from_string("video/x-raw,framerate=1/1")
    png.set_property('snapshot',True)
    multi_sink.set_property('location','/home/pi/frame%05d.png')
    filter = gst.ElementFactory.make("capsfilter", "filter")
    filter.set_property("caps", caps)
    filter1 = gst.ElementFactory.make("capsfilter", "filter1")
    filter1.set_property("caps", caps1)
    pipeline.add(video_source)
    pipeline.add(vconvert)
    pipeline.add(timer)
    pipeline.add(clock)
    pipeline.add(filter)
    pipeline.add(vrate)
    pipeline.add(filter1)
    pipeline.add(sconvert)
    pipeline.add(png)
    pipeline.add(multi_sink)
    video_source.link(filter)
    filter.link(vconvert)
    vconvert.link(timer)
    timer.link(clock)
    clock.link(vrate)
    vrate.link(filter1)
    filter1.link(sconvert)
    sconvert.link(png)
    png.link(multi_sink)
    bus = pipeline.get_bus()

    pipeline.set_state(gst.State.PLAYING)
    print "Capture started"

    bus = pipeline.get_bus()#class

    msg = bus.timed_pop_filtered(gst.CLOCK_TIME_NONE,gst.MessageType.ERROR | gst.MessageType.EOS)
    print msg
    pipeline.set_state(gst.State.NULL)

一旦程序第一次运行,它会捕获图像,而当我第二次运行时,什么都不会发生。我需要重新启动整个python程序才能让它再次运行。有人能帮我解决吗?在


Tags: addmakepipelinepngvideolinkpropertycaps
1条回答
网友
1楼 · 发布于 2024-06-12 19:24:42

Treagsmer如何正确地实现类初始化。。在“拍照”中,你只需吹奏烟斗,然后停止。。那么in应该是可重用的(从gstreamer的角度来看。。管道可以变为空并反复播放):

class TakePhoto:

    def __init__(self):
        GObject.threads_init()
        gst.init(None)
        self.pipeline = gst.Pipeline()
        .. do the element creation and their linking etc ..

    def take_photo(self): #this is reusable
        bus = self.pipeline.get_bus()
        self.pipeline.set_state(gst.State.PLAYING)
        print "Capture started"

        bus = self.pipeline.get_bus()#class

        msg = bus.timed_pop_filtered(gst.CLOCK_TIME_NONE,gst.MessageType.ERROR | gst.MessageType.EOS)
        print msg
        self.pipeline.set_state(gst.State.NULL)

然后,python shell可以初始化一个实例并多次调用take\u photo:

^{pr2}$

这个代码是我脑子里想出来的,所以我不承担任何责任,如果它沸腾了你的硬盘或任何东西。。我也不习惯用python编写代码。。所以它可能充满了错误:D

但是

相关问题 更多 >