Python获取摄像头数据

不使用OpenCV,以轻量级的jaraco.video包作示范,代码在Python 3.6下运行正常

try:
    from tkinter import *
    import PIL.Image as img
    import PIL.ImageTk as tkimg
    from jaraco.video import capture
    from time import time
except Exception as e:
    print('Fatal:'+str(e))
    from time import sleep
    sleep(3)
    exit()

dev=capture.Device(devnum=0)
last=time()

def getImg():
    return tkimg.PhotoImage(img.frombytes('RGB',dev.get_buffer()[1],dev.get_buffer()[0]).transpose(img.FLIP_TOP_BOTTOM))

root=Tk()
fps=StringVar()
i=getImg()

label=Label(root,image=i)
label.image=i
fpsl=Label(root,textvariable=fps)

label.pack()
fpsl.pack()

def refresh():
    global last
    newimg=getImg()
    fpsR=1/(time()-last)
    fps.set('FPS:'+str(int(fpsR)))
    label.configure(image=newimg)
    label.image=newimg
    last=time()
    root.after_idle(func=refresh)
    
root.after(100,func=refresh)

mainloop()

加入对话

1条评论

留下评论