Python Forum
Setup host for consistent data transfer to client via TCP - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Setup host for consistent data transfer to client via TCP (/thread-37581.html)



Setup host for consistent data transfer to client via TCP - Gustav97 - Jun-27-2022

Hi everyone,

I use a Raspberry PI4 for running a python script, which is using the RPI camera for detecting coordinates of body joints.
Basically the skript gives back the real time coordinates of detected joints.
If I run the Server skript and printing the joint coordinates to the RPI console via the tool putty, everything is working fine.

Now I need to transfer the joint coordinates data to a client for further processing. First, I like to print the coordinates to the clients´console for checking, if everything is working well. These client is connected to my local network (=basically my laptop). The connection is settet up correctly but I didn´t find a way to consistently print these data to the client´s console.

Clients´ Code:
import socket

HOST = '192.168.0.11'  # server IP address
PORT =   17854  # server port

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.connect((HOST, PORT))
    print("connected to:", HOST)
 #while True: # if activated, this code snippet just prints empty data to the console instead of body joint coordinates    
    data = sock.recv(1024)
    #print landmarks to console
    print(data.decode()) 
Server / RPI Code:
import mediapipe as mp
import cv2
import socket

HOST = '192.168.0.11'  # localhost
PORT = 17854    # IMPORTANT !! Do not use reserved ports

mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic
mp_pose = mp.solutions.pose

cap = cv2.VideoCapture(0)

#start socket
print("waiting for client...")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
  sock.bind((HOST, PORT))
  sock.listen()
  conn, addr = sock.accept()
  with conn:
   print('Client connected by', addr)
      
 # Start image processing to generate landmarks from video
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
  
 while cap.isOpened():
  ret, frame = cap.read()

    # Recolor Feed
  image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    
    # Make Detections
  results = holistic.process(image)
        
      # Recolor image back to BGR for rendering
  image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    
    # draw connections between landmarks
  mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS)
    
    # make landmarks visible on screen. deactivate, if no screen available
    # cv2.imshow('Raw Webcam Feed', image) 
    # Extract landmarks (coordinates of human joints)
  try:  
    landmarks = results.pose_landmarks.landmark
    # print(landmarks) #print body joint coodinates to RPi conseole - working

    with conn:   
        data = landmarks.encode('utf-8') 
        print(data)  #if encoding deactivated, landmarks are shown correctly. If encoding activated, no data is visible. 
        #send data to socket
        conn.sendall(data)
                                 
  except:
    pass   
      
  if cv2.waitKey(10) & 0xFF == ord('q'):
    break
     
cap.release()
cv2.destroyAllWindows()


    
How would you go on with this issue?
I really looking forward to your input!

best regards,
Gustav