Skip to content

Commit

Permalink
Update streamClient.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ngthadangg committed Dec 29, 2023
1 parent b8bd81c commit a45a301
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions client/streamClient.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
import socket
import cv2
import pyautogui
import zlib
import struct
import numpy as np
import socket
import pickle

# Tạo socket UDP
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Địa chỉ và cổng của server
server_address = ("192.168.1.13", 9999)

UDP_IP = "192.168.1.5"
UDP_PORT = 5005
# Kết nối tới server
client_socket.sendto(b"connect_request", server_address)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Khởi tạo OpenCV để chụp video
cap = cv2.VideoCapture(0)

# Thiết lập kích thước gói tin UDP tối đa (số byte)
UDP_MAX_SIZE = 65500

buffer = []

while True:
# Capture the screen
img = pyautogui.screenshot()
ret, frame = cap.read()

# Chuyển đổi frame thành dạng dữ liệu có thể truyền qua mạng
data = pickle.dumps(frame)

# Gửi dữ liệu theo từng phần nhỏ
while data:
if len(data) > UDP_MAX_SIZE:
fragment, data = data[:UDP_MAX_SIZE], data[UDP_MAX_SIZE:]
else:
fragment, data = data, b""

# Convert the screenshot to a numpy array
frame = np.array(img)
client_socket.sendto(fragment, server_address)

cv2.imshow("Client", frame)

# Encode the frame
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
result, imgencode = cv2.imencode('.jpg', frame, encode_param)
data = np.array(imgencode)

# Compress the data using zlib
compressed_data = zlib.compress(data, zlib.Z_BEST_COMPRESSION)
if cv2.waitKey(1) & 0xFF == ord("q"):
break

# Send the compressed data to the server
sock.sendto(compressed_data, (UDP_IP, UDP_PORT))
cap.release()
cv2.destroyAllWindows()
client_socket.close()

0 comments on commit a45a301

Please sign in to comment.