Python多线程:opencv - 串口通信

时间:2017-02-04 21:55:59

标签: python multithreading opencv pyserial

我想用Tkinter构建一个GUI,它将同时显示来自串口的摄像头输出和模拟数据。这是我的代码:

width, height = 400, 300
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

root = tk.Tk()
root.title("GUI")
root.geometry("800x600+100+100")
root.bind('<Escape>', lambda e: root.quit())
lmain = tk.Label()
lmain.pack(padx=100, pady=100)
frameLabel = tk.Frame()
text = tk.Text(frameLabel, wrap='word', font='TimesNewRoman 15', relief='flat')
frameLabel.pack()
text.pack()

def show_frame():
    _, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(10, show_frame)


def serialData():
    data = serial.Serial('COM3', baudrate=9600, timeout=1)
    while (True):
        if (data.inWaiting() > 0):
            ardu_data = data.readline()
            text.delete(1.0, 'end')
            text.insert('end', ardu_data)


t1 = threading.Thread(target=show_frame())
t2 = threading.Thread(target=serialData())

t1.start()
t2.start()

t1.join()
t2.join()


root.mainloop()

代码无法同时执行两项操作。请有人帮我处理代码。谢谢。

0 个答案:

没有答案