Python Forum
Setup host for consistent data transfer to client via TCP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setup host for consistent data transfer to client via TCP
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information Estimating transfer function from frd data ymohammadi 0 1,454 Feb-10-2022, 10:00 AM
Last Post: ymohammadi
  where to host my python script tomtom 1 1,272 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  Consistent error Led_Zeppelin 1 1,645 Dec-20-2021, 01:39 AM
Last Post: snippsat
  Failing to connect to a host with WMI tester_V 6 4,401 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Password protected xls data transfer to master OTH 1 3,223 Feb-15-2021, 08:28 PM
Last Post: OTH
  LDAP code to query for host not returning data burvil 2 3,782 Oct-17-2018, 10:03 PM
Last Post: burvil
  printing text tables with consistent width Skaperen 7 10,711 Jul-01-2018, 02:34 AM
Last Post: Skaperen
  Get host from ip antmar904 3 2,892 Jan-31-2018, 08:27 PM
Last Post: antmar904
  How to use a UDP server and client to setup a chat abrew132 2 3,768 Mar-20-2017, 06:22 PM
Last Post: abrew132

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020