我正在尝试将视频从客户端发送到服务器,并在服务器中进行了面部检测处理后,最终希望实时视频流再次在客户端显示。问题是我运行代码时客户端没有将帧不断发送到服务器。客户端只发送一次视频帧。同时,我得到空的无响应窗口。
这是代码:-
server.py
import socket
import sys
import cv2
import pickle
import numpy as np
import struct
import logging as log
import datetime as dt
from time import sleep
cascPath = "/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='webcam.log',level=log.INFO)
anterior = 0
HOST=''
PORT=2222
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print ('Socket created')
s.bind((HOST,PORT))
print ('Socket bind complete')
s.listen(10)
print ('Socket now listening')
conn,addr=s.accept()
data = b''
payload_size = struct.calcsize("L")
while True:
while len(data) < payload_size:
data += conn.recv(4096)
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
###
frame=pickle.loads(frame_data)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
a=cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
print(a)
data1 = pickle.dumps(a)
conn.sendall(struct.pack("L", len(data1))+data1)
client.py
import cv2
import numpy as np
import socket
import sys
import pickle
import struct
cap=cv2.VideoCapture(0)
clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('localhost',2222))
while True:
ret,frame=cap.read()
data = pickle.dumps(frame)
clientsocket.sendall(struct.pack("L", len(data))+data)
data = b''
payload_size = struct.calcsize("L")
while True:
while len(data) < payload_size:
data += clientsocket.recv(4096)
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
while len(data) < msg_size:
data += clientsocket.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
###
frame=pickle.loads(frame_data)
print(frame)
cv2.imshow('frame',frame)
# add this
if cv2.waitKey(1) & 0xFF == ord('q'):
break