TKinter python with librealsense2
Hi everyone.
I Have question.
Now i using Camera 3d D435 I want stream video with GUI i using Tkinter. I do many way but i can show my stream with d435.
My stream only show first frame and then freeze. I can not find any document for my problem.
This is my code below, Pleas help me. Thank you very much.
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
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
colorizer = rs.colorizer()
#depth_image = np.asanyarray(colorizer.colorize(depth_frame).get_data())
#depth_image1 = np.asanyarray(depth_frame.get_data())
#depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
# frame=cv2.resize(color_image,dsize=None,fx=0.5,fy=0.5)
frame = cv2.cvtColor(color_image, cv2.COLOR_BGR2RGB)
# cv2.imwrite("sao.jpg",frame)
photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame))
print(photo)
# depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
canvas.create_image(0, 0, image=photo, anchor=tkinter.NW)
window.mainloop()
update()
#window.after(15, update)
update()
window.mainloop()
-
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.
-
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() -
There is only one available reference for streaming live RealSense images with Tkinter (in the link below), and that project also had problems.
Please sign in to leave a comment.
Comments
4 comments