修改代码使其能动态调整分辨率 def run(self): self.sock.bind(self.ADDR) self.sock.listen() print('视频服务器已经启动...') print(f'\n工作地址是:{self.ADDR}') conn,addr = self.sock.accept() print(f'\n接受了远程视频客户端{addr}的连接...') data = "".encode("utf-8") payload_size = struct.calcsize("L") cv2.namedWindow('Remote',cv2.WINDOW_AUTOSIZE) while True: cap = cv2.VideoCapture(0) # 动态调整画面分辨率 width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) if width > 800: scale_percent = 50 # 缩小画面尺寸 new_width = int(width * scale_percent / 100) new_height = int(height * scale_percent / 100) frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_AREA) while len(data)<payload_size: data+=conn.recv(81920) packed_size = data[:payload_size] data = data[payload_size:] msg_size = struct.unpack("L",packed_size)[0] while len(data) < msg_size: data += conn.recv(81920) zframe_data = data[:msg_size] data = data[msg_size:] frame_data = zlib.decompress(zframe_data) #解压数据 frame = pickle.loads(frame_data) #还原 cv2.imshow('Remote',frame) if cv2.waitKey(1) & 0xFF ==27: break
时间: 2024-01-01 22:04:28 浏览: 116
基于udp协议的通信模型udpServer.c和udpClient.c
您可以将动态调整画面分辨率的代码移动到接收视频帧之前,并将其放在每次循环之前,以便可以实时调整视频分辨率。以下是修改后的示例代码:
```
def run(self):
self.sock.bind(self.ADDR)
self.sock.listen()
print('视频服务器已经启动...')
print(f'\n工作地址是:{self.ADDR}')
conn, addr = self.sock.accept()
print(f'\n接受了远程视频客户端{addr}的连接...')
data = "".encode("utf-8")
payload_size = struct.calcsize("L")
cv2.namedWindow('Remote',cv2.WINDOW_AUTOSIZE)
while True:
# 动态调整画面分辨率
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if width > 800:
scale_percent = 50 # 缩小画面尺寸
new_width = int(width * scale_percent / 100)
new_height = int(height * scale_percent / 100)
else:
new_width = width
new_height = height
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_AREA)
while len(data) < payload_size:
data += conn.recv(81920)
packed_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_size)[0]
while len(data) < msg_size:
data += conn.recv(81920)
zframe_data = data[:msg_size]
data = data[msg_size:]
frame_data = zlib.decompress(zframe_data) #解压数据
frame = pickle.loads(frame_data) #还原
cv2.imshow('Remote',frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
conn.close()
```
在每次循环开始之前,您可以使用OpenCV的VideoCapture对象获取当前视频帧的分辨率,并根据需要进行调整。然后,您可以使用cv2.resize函数将帧调整为新分辨率。最后,您可以通过调整后的帧继续进行后续处理和发送。
阅读全文