tkinter显示C语言动态库获取的视像头视频信号

时间:2019-10-11
本文章向大家介绍tkinter显示C语言动态库获取的视像头视频信号,主要包括tkinter显示C语言动态库获取的视像头视频信号使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.显示视频先从显示单帧图片开始

#!/usr/bin/python
from Tkinter import *
from PIL import Image, ImageTk


def main():
    t = Tk()
    t.title('showVideo')
    frmT = Frame(width = 769, height = 577)
    frmB = Frame(width = 769, height = 30)
    f = open('frame.rgb','rb')
    str1 = f.read()
    im = Image.fromstring('RGB',(768,576),str1)
    photo = ImageTk.PhotoImage(im)
#    imgInfo = PhotoImage(file = photo)
    ToplImage = Label(frmT,image = photo)
    ToplImage.image = photo
    ToplImage.pack()
    frmT.grid(row=0,column=0)
    frmB.grid(row=1,column=0)
    ToplImage.grid()
    t.mainloop()

if __name__=='__main__':
    main()

2.然后用普通插入图片然后刷新的方式实现,视屏是可以显示了,但是运行5分钟后内存就因为while True爆掉了,而且还没查出来怎么爆掉的,手动清理内存都没作用

#!/usr/bin/python
import sys
import gc
from Tkinter import *
from PIL import Image, ImageTk
from ctypes import *
from threading import Thread
import time



def show():
    result = cdll.LoadLibrary("./libvideodev.so")
    buff = create_string_buffer(768*576*3)
#    t1 = Thread(target=video)
#    t1.start()
#    video()
#    time.sleep(1)
    len = 768*576*3
    retCode = result.VideoDeviceInit()
    print("VideoDeviceInit:",retCode)
    retCode = result.SetVideoSource("0")
    print("SetVideoSource:",retCode)
#    retCode = result.CaptureParamSet(516,378,7)
    retCode = result.CaptureParamSet(768,576,7)
    print("CaptureParamSet:",retCode)
    print("CaptureParamSet:",retCode)
    retCode = result.StartCapture()
    print("StartCapture:",retCode)    
#    retCode = result.GetFrame(buff,len)
#    print("GetFrame:",retCode)
#    print(buff.value)
#    print(sizeof(buff))
#    time.sleep(1)
#    print(repr(buff.raw))
    print("buffvalue")
    t = Tk()
    t.title('showVideo')
    while True:
        frmT = Frame(width = 769, height = 577)
        frmB = Frame(width = 769, height = 30)
#    f = open('frame.rgb','rb')
#    str1 = f.read()
        retCode = result.GetFrame(buff,len)
        print(sizeof(buff))
#        str1 = buff.raw
    if (sizeof(buff)==1327104):
#        im = Image.fromstring('RGB',(516,378),str1)
            im = Image.fromstring('RGB',(768,576),buff.raw)
        photo = ImageTk.PhotoImage(im)
#    imgInfo = PhotoImage(file = photo)
            ToplImage = Label(frmT,image = photo)
            ToplImage.image = photo
#            ToplImage.pack()
            frmT.grid(row=0,column=0)
            frmB.grid(row=1,column=0)
            ToplImage.grid()
    print(sys.getrefcount(ToplImage))
    print(sys.getrefcount(photo))
    print(sys.getrefcount(im))
    print(sys.getrefcount(frmT))
    print(sys.getrefcount(frmB))
    print(sys.getrefcount(retCode))
    del ToplImage
    del(photo)
    del(im)
    del frmT
    del frmB
    del retCode
    print('gc')
    print(gc.collect())
    print(gc.get_count())    
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
        t.update()
    t.mainloop()

if __name__=='__main__':
    show()

3.参考了 https://blog.csdn.net/qq_41979513/article/details/89306878 的代码 终于解决了爆内存的问题可以长期显示视屏了

#!/usr/bin/python
import sys
import gc
from Tkinter import *
from PIL import Image
from PIL import ImageTk
from ctypes import *
from threading import Thread
import time


class Extractor_GUI():
    def __init__(self):
        self.length = 768*576*3
        self.result = cdll.LoadLibrary("./libvideodev.so")
        self.buff = create_string_buffer(768*576*3)
        self.__init_video()
        self.__init_gui()
    

    def __init_video(self):
        retCode = self.result.VideoDeviceInit()
        print("VideoDeviceInit:",retCode)
        retCode = self.result.SetVideoSource("0")
            print("SetVideoSource:",retCode)
#        retCode = result.CaptureParamSet(516,378,7)
         retCode = self.result.CaptureParamSet(768,576,7)
            print("CaptureParamSet:",retCode)
            retCode = self.result.StartCapture()
            print("StartCapture:",retCode)


    def __init_gui(self):
        self.window = Tk()
        self.window.title('showVideo')
        self.canv = Canvas(self.window, width = 769, height = 577)
        self.frmB = Frame(width = 769, height = 30)
        self.canv.grid(row=0,column=0)
                self.frmB.grid(row=1,column=0)
        self.__action_read_frame()

    def __action_read_frame(self):
        while True:
            retCode = self.result.GetFrame(self.buff,self.length)
                print(sizeof(self.buff))
                if (sizeof(self.buff)==1327104):
#                   im = Image.fromstring('RGB',(516,378),str1)
                        im = Image.fromstring('RGB',(768,576),self.buff.raw)
                        photo = ImageTk.PhotoImage(im)
                        self.canv.create_image(0,0,anchor=NW,image=photo)
            self.window.update_idletasks()
#               self.window.update()

    def launch(self):
        self.window.mainloop()

if __name__ == '__main__':
    ext = Extractor_GUI()
    ext.launch()

原文地址:https://www.cnblogs.com/qijunzifeng/p/11655148.html