09-20-2025 01:25 AM
Hello,
I have a Python TCP server that streams webcam video frames as JPEG over the network. Now I want to build a LabVIEW client that can:
Connect to the Python server
Read the first 4 bytes (frame length)
Receive the JPEG frame data
Display the image on the LabVIEW Front Panel
Note: I do not have NI Vision Toolkit, so I need a solution using .NET (MemoryStream + Image.FromStream) or any other method to display the image.
Can someone share a simple example VI or block diagram to achieve this?
Thanks 🙏
Solved! Go to Solution.
09-20-2025 12:46 AM
Body:
Hello everyone,
I have a Python TCP/IP server that streams webcam video frames as JPEG data. The code is below:
import socket
import cv2
import numpy as np
import struct
def main():
HOST = "127.0.0.1"
PORT = 5555
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(1)
print(f"✅ Server ready on port {PORT}...")
conn, addr = server_socket.accept()
print(f"📡 Connection from: {addr}")
data = b""
payload_size = struct.calcsize("L") # 4 bytes for frame length
while True:
# get frame size
while len(data) < payload_size:
packet = conn.recv(4096)
if not packet:
break
data += packet
if len(data) < payload_size:
break
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
# get frame data
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
# decode and display
frame = cv2.imdecode(np.frombuffer(frame_data, dtype=np.uint8), cv2.IMREAD_COLOR)
cv2.imshow("TCP Server - Received Video", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
conn.close()
server_socket.close()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
What I need help with:
👉 How can I build the LabVIEW client that connects to this Python server, reads the data, and displays the video in real time?
Specifically:
How to use TCP Open Connection.vi and TCP Read.vi to receive the 4-byte length prefix and then the JPEG frame data.
How to decode the received JPEG byte array into an image in LabVIEW. (I don’t have the NI Vision Toolkit, so I may need to use .NET MemoryStream + System.Drawing.Image.FromStream.)
How to continuously display the frames on the LabVIEW Front Panel.
If anyone has a small VI example or block diagram screenshot, it would be a huge help 🙏
Thanks!
09-20-2025 03:49 AM
As a very first step I would like to recommend to take a look on any VI which is able to convert JPEG string to 2D array, like this example — jpeg string to picture, the start with code like this:
09-21-2025 03:35 AM - edited 09-21-2025 03:38 AM
It would seem to me that you somehow are mixing up client and server here. Your webcam_tcp_client is a TCP client but you talk in your post about a server, which it kind of is in terms of webcam functionality, but still it makes things a bit confusing.
Other than that there are a few other inconsistencies. Your webcam server which assumes the TCP client role tried to connect to port 9999, but your webcam client which assumes the TCP server listens on port 5555.
One other point: you use whatever endianess is the native one on either side. While it is fairly hard to find hardware that uses BigEndian format nowadays it does exist and is coincidentially the default for network data transfers. You definitely should always decide on an explicit endianess when streaming binary data over a network connection as the remote side definitely could reside on a hardware with a different native Endianess.
LabVIEW's Typecast function happens to assume BigEndian too, but that has mostly historical reasons since LabVIEW originates from the Mac where it originally ran on Motorola 680xx hardware which happened to be a BigEndian architecture.
Changing your struct.pack() and struct.unpack() format codes from "L" to ">L" will make sure the number is transfered as BigEndian and you can forget the ReverseString node in the example posted by Andrey.
09-21-2025 06:11 AM
Thank you for your help, my dear friend. But I tried the program, and unfortunately, I couldn’t capture an image from Python. Is there another way to capture webcam video using Python?
"And my LabVIEW version is 2023 64-bit. Won’t the code work in my LabVIEW?"