Nov-02-2019, 10:52 PM
I am trying to take pics with the webcam and send them to the client via socket.
I am not sure why, the server, which I always start first, stops almost immediately and the following error shows:
SERVER
I am not sure why, the server, which I always start first, stops almost immediately and the following error shows:
Error:Traceback (most recent call last):
File "C:/Users/PycharmProjects/client-server/server.py", line 14, in <module>
s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused ithere are both codesSERVER
import cv2
import time
import socket
from PIL import Image
import pickle
import struct
host = socket.gethostname()
port = 8485
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.listen(5)
conn, address = s.accept()
print("server started...")
capture = cv2.VideoCapture(0)
capture.set(3, 640)
capture.set(4, 480)
img_counter = 0
#frame_set = []
start_time = time.time()
while True:
ret, frame = capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if time.time() - start_time >= 1: # how often (secs) are pics taken
filename = "opencv_frame_{}.png".format(img_counter)
#cv2.imwrite(filename, frame)
data = pickle.dumps(frame, 0)
size = len(data)
print("{} written!".format(img_counter))
start_time = time.time()
s.sendall(struct.pack(">L", size) + data)
img_counter += 1
capture.release()CLIENTimport socket
import cv2
import pickle
import struct
host = socket.gethostname()
port = 8485
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
conn,addr=s.accept()
data = b""
payload_size = struct.calcsize(">L")
print("payload_size: {}".format(payload_size))
while True:
while len(data) < payload_size:
print("Recv: {}".format(len(data)))
data += conn.recv(4096)
print("Done Recv: {}".format(len(data)))
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack(">L", packed_msg_size)[0]
print("msg_size: {}".format(msg_size))
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame=pickle.loads(frame_data, fix_imports=True, encoding="bytes")
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
cv2.imshow('ImageWindow',frame)
cv2.waitKey(1)
