View my account

TKinter python with librealsense2

Comments

4 comments

  • MartyG

    Hi Hvtrung  As a starting point in investigating your case, could you try moving your colorizer = rs.colorizer() instruction to the line directly beneath your pipeline start instruction so that it is not inside the while True loop, please.

    0
    Comment actions Permalink
  • Hvtrung

    I put line colorizer = rs.colorizer()  outsite def update(): and outside While loop, but nothing happen. Pleas help me. Canvas also show first frame and freeze.

     

    0
    Comment actions Permalink
  • Hvtrung

    My code

    from tkinter import *
    import cv2
    import PIL.Image,PIL.ImageTk
    import tkinter
    from time import sleep
    import pyrealsense2 as rs
    import numpy as np
    import json
    window=Tk()
    window.title("Realsense")
    jsonObj = json.load(open("sa2nho.json"))
    json_string= str(jsonObj).replace("'", '\"')

    pipeline = rs.pipeline()
    config = rs.config()

    freq=int(jsonObj['stream-fps'])
    print("W: ", int(jsonObj['stream-width']))
    print("H: ", int(jsonObj['stream-height']))
    print("FPS: ", int(jsonObj['stream-fps']))
    config.enable_stream(rs.stream.depth, int(jsonObj['stream-width']), int(jsonObj['stream-height']), rs.format.z16, int(jsonObj['stream-fps']))
    config.enable_stream(rs.stream.color, int(jsonObj['stream-width']), int(jsonObj['stream-height']), rs.format.bgr8, int(jsonObj['stream-fps']))
    cfg = pipeline.start(config)
    dev = cfg.get_device()
    advnc_mode = rs.rs400_advanced_mode(dev)
    advnc_mode.load_json(json_string)
    align_to=rs.stream.color
    align=rs.align(align_to)


    from threading import Thread
    canvas_w=640
    canvas_h=480
    canvas = Canvas(window, width=canvas_w, height=canvas_h, bg="red")
    canvas.pack()
    photo=None
    colorizer = rs.colorizer()
    def update():
    global canvas,photo
    while True:
    frames = pipeline.wait_for_frames()
    aligned_frames = align.process(frames)
    depth_frame = aligned_frames.get_depth_frame()
    color_frame = aligned_frames.get_color_frame()
    if not depth_frame or not color_frame:
    continue
    color_image = np.asanyarray(color_frame.get_data())
    frame = cv2.cvtColor(color_image, cv2.COLOR_BGR2RGB)
    photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))
    print(photo)
    canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)
    window.mainloop()
    update()
    #window.after(15, update)

    update()
    window.mainloop()




    0
    Comment actions Permalink
  • MartyX Grover

    There is only one available reference for streaming live RealSense images with Tkinter (in the link below), and that project also had problems.  

    https://github.com/IntelRealSense/librealsense/issues/4657

    0
    Comment actions Permalink

Please sign in to leave a comment.