Skip to content

Instantly share code, notes, and snippets.

@gyillikci
Last active October 21, 2024 14:39
Show Gist options
  • Save gyillikci/8e55118350e24de0b0752d130ea82158 to your computer and use it in GitHub Desktop.
Save gyillikci/8e55118350e24de0b0752d130ea82158 to your computer and use it in GitHub Desktop.
import socket
import cv2
import base64
import numpy as np
def udp_listener(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('', port)
sock.bind(server_address)
print("LISTNENING PORT")
data = ''
"""data,_ = sock.recvfrom(65536)
sock.close()
nparr = np.frombuffer(data, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow("Received image", image)
cv2.waitKey(0)
cv2.destoryAllWindows()"""
while True:
packet, address = sock.recvfrom(4096)
data += packet.decode('utf-8')
if len(packet) < 4096:
image_data = base64.b64decode(data)
nparr = np.frombuffer(image_data, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow("Received image", image)
cv2.waitKey(0)
cv2.destoryAllWindows()
data = ''
sock.close()
if __name__ == "__main__":
port=10000
udp_listener(port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment