【发布时间】:2021-10-01 11:24:30
【问题描述】:
我有一个从树莓派中弹出的帧,它通过网络广播其信号。摄像机源在第 37 行用 cv2 打开的帧中打开并运行良好。但是,我试图在 tkinter 中显示摄像机源,如第 42 行及以后所见。 简而言之:我需要在 tkinter 中显示相机供稿。 非常感谢任何帮助。
import io
import socket
import struct
import cv2
import numpy as np
from PIL import Image
import tkinter as tk
global image
# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
# all interfaces)
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 5001))
server_socket.listen(0)
# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('rb')
while True:
# Read the length of the image as a 32-bit unsigned int. If the
# length is zero, quit the loop
image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
if not image_len:
break
# Construct a stream to hold the image data and read the image
# data from the connection
global image
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
# Rewind the stream, open it as an image with opencv and do some
# processing on it
image_stream.seek(0)
image = Image.open(image_stream)
data = np.frombuffer(image_stream.getvalue(), dtype=np.uint8)
imagedisp = cv2.imdecode(data, 1)
cv2.imshow("Frame",imagedisp)
cv2.waitKey(1) #imshow will not output an image if you do not use waitKey
#cleanup windows
root = tk.Tk()
image = Image.fromarray(image)
photo = ImageTk.PhotoImage(image2) # it has to be after `tk.Tk()`
canvas = tk.Canvas(root, width=photo.width(), height=photo.height())
canvas.pack(side='left', fill='both', expand=True)
canvas.create_image((0,0), image2=photo, anchor='nw')
description = tk.Label(root, text="Place for description")
description.pack(side='right')
# - start -
update_frame() # update it first time
root.mainloop() # start program - this loop runs all time
根据我对@TheLizzard 的理解进行了一些更改。它仍然没有拉起页面。这些是变化:
from tkinter import *
from PIL import ImageTk, Image
import cv2
import io
import socket
import struct
import numpy as np
# Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
# all interfaces)
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 5001))
server_socket.listen(0)
# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('rb')
try:
while True:
# Read the length of the image as a 32-bit unsigned int. If the
# length is zero, quit the loop
image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
if not image_len:
break
# Construct a stream to hold the image data and read the image
# data from the connection
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
# Rewind the stream, open it as an image with opencv and do some
# processing on it
image_stream.seek(0)
image = Image.open(image_stream)
data = np.fromstring(image_stream.getvalue(), dtype=np.uint8)
imagedisp = cv2.imdecode(data, 1)
#cleanup windows
finally:
connection.close()
server_socket.close()
root = Tk()
# Create a frame
app = Frame(root, bg="white")
app.grid()
# Create a label in the frame
lmain = Label(app)
lmain.grid()
# Capture from camera
cap = cv2.VideoCapture(0)
# function for video streaming
def video_stream():
_, frame = cap.read()
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(1, video_stream)
video_stream()
root.mainloop()
客户端:
import io
import socket
import struct
import time
import picamera
# Connect a client socket to my_server:8000 (change my_server to the
# hostname of your server)
client_socket = socket.socket()
client_socket.connect(('192.168.1.000', 5001))
# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
# Start a preview and let the camera warm up for 2 seconds
camera.start_preview()
time.sleep(1)
# Note the start time and construct a stream to hold image data
# temporarily (we could write it directly to connection but in this
# case we want to find out the size of each capture first to keep
# our protocol simple)
start = time.time()
stream = io.BytesIO()
for foo in camera.capture_continuous(stream, 'jpeg', use_video_port=True):
# Write the length of the capture to the stream and flush to
# ensure it actually gets sent
connection.write(struct.pack('<L', stream.tell()))
connection.flush()
# Rewind the stream and send the image data over the wire
stream.seek(0)
connection.write(stream.read())
# Reset the stream for the next capture
stream.seek(0)
stream.truncate()
# Write a length of zero to the stream to signal we're done
connection.write(struct.pack('<L', 0))
finally:
connection.close()
client_socket.close()
【问题讨论】:
-
查看
.after脚本。你也不需要使用cv2。在使用tkinter时,大多数时候也不应该使用while True循环。如果你想要一个例子,请看here。 -
@TheLizzard,感谢您的快速回复。我添加了我从你所说的内容中理解的内容,这就是结果。如果这是您的意思,请告诉我,我会将其添加到主线程中
-
已添加。感谢您到目前为止的帮助以及您可以提供的任何其他内容@TheLizzard
-
你仍然运行
while True(在tk.Tk()之前),它会阻止所有代码。这是主要问题。您必须将套接字代码放入函数中并使用after而不是while True运行它。或者您必须在分隔符thread中运行套接字 -
我制作了
server,它应该可以工作,但我没有client来测试它。
标签: python opencv tkinter raspberry-pi