Advertisement
LovelessIsma

cagada

Dec 6th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import socket
  4.  
  5. class VideoStreamingTest(object):
  6.     def __init__(self, host, port):
  7.  
  8.         self.server_socket = socket.socket()
  9.         self.server_socket.bind((host, port))
  10.         self.server_socket.listen(0)
  11.         self.connection, self.client_address = self.server_socket.accept()
  12.         self.connection = self.connection.makefile('rb')
  13.         self.host_name = socket.gethostname()
  14.         self.host_ip = socket.gethostbyname(self.host_name)
  15.         self.streaming()
  16.  
  17.     def streaming(self):
  18.  
  19.         try:
  20.             print("Host: ", self.host_name + ' ' + self.host_ip)
  21.             print("Connection from: ", self.client_address)
  22.             print("Streaming...")
  23.             print("Press 'q' to exit")
  24.  
  25.             # need bytes here
  26.             stream_bytes = b' '
  27.             while True:
  28.                 stream_bytes += self.connection.read(1024)
  29.                 first = stream_bytes.find(b'\xff\xd8')
  30.                 last = stream_bytes.find(b'\xff\xd9')
  31.                 if first != -1 and last != -1:
  32.                     jpg = stream_bytes[first:last + 2]
  33.                     stream_bytes = stream_bytes[last + 2:]
  34.                     image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
  35.                     cv2.imshow('image', image)
  36.  
  37.                     if cv2.waitKey(1) & 0xFF == ord('q'):
  38.                         break
  39.         finally:
  40.             self.connection.close()
  41.             self.server_socket.close()
  42.  
  43.  
  44. if __name__ == '__main__':
  45.     # host, port
  46.     h, p = "10.151.4.49", 8000
  47.     VideoStreamingTest(h, p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement