gpt4 book ai didi

python - Cv2 :problem recieving full data from socket

转载 作者:行者123 更新时间:2023-12-02 16:34:26 30 4
gpt4 key购买 nike

我试图通过套接字发送帧,并且在编码后通常是50 000-80 000字节,所以我按循环接收数据,但是由于客户端始终发送帧,因此以下代码中的循环始终不会中断,因此当我运行时什么也没有发生,并且接收循环继续进行

客户

import socket
import cv2
import time
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
s.connect(("127.0.0.1",60124))
camera = cv2.VideoCapture(0)
while True :
r , f = camera.read()
f = cv2.imencode(".jpg",f)[1].tostring()
s.sendall(f)

服务器
import socket
import numpy as np
import cv2
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
s.bind(("127.0.0.1",60124))
s.listen(5)
c , a = s.accept()

while True :
data = ""
while True:
f = c.recv(1024)
if not f :
break
data += f
x = np.fromstring(data , np.uint8)
var = cv2.imdecode(x , cv2.IMREAD_COLOR)
cv2.imshow("Camera" , var)
cv2.waitKey(1)

帮助文件

最佳答案

我建议您先发送长度,然后发送数据。
当服务器接收到“有效载荷”长度时,它知道期望多少个数据字节。

客户端:

  • len(f)作为8字节发送
  • 发送数据f

  • 服务器:
  • 接收8字节以获得len_f
  • 接收数据字节的len_f
    无需以1024个字节的块的形式接收数据。

  • 在我的示例中, len_f被编码为 base64格式。
    发送 len_f的字节数始终为8个字节(由于base64自动填充)。

    您也可以将 data编码为 base64
    如果要通过HTTP发送数据,那很重要。
    我将图像数据的 base64编码/解码置于注释中。
    仅将长度编码为 base64(您也可以将长度以文本格式发送)。

    客户:
    import socket
    import numpy as np
    import cv2
    import base64

    s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
    s.connect(("127.0.0.1", 60124))

    width, height, n_frames = 640, 480, 100 # 100 frames, resolution 640x480

    for i in range(n_frames):
    # Generate synthetic image:
    img = np.full((height, width, 3), 60, np.uint8)
    cv2.putText(img, str(i+1), (width//2-100*len(str(i+1)), height//2+100), cv2.FONT_HERSHEY_DUPLEX, 10, (30, 255, 30), 20) # Green number

    # JPEG Encode img into f
    _, f = cv2.imencode('.JPEG', img)

    # Encode jpeg_img to base64 format
    #f = base64.b64encode(f)
    # Get length of f and encode to base64
    #f_len = base64.b64encode((len(f)).to_bytes(4, byteorder='little'))

    f_len = base64.b64encode((len(f)).to_bytes(4, byteorder='little'))

    # Send the length first - so the server knows how many bytes to expect.
    s.sendall(f_len) # Send 8 bytes (assumption: b64encode of 4 bytes will be 8 bytes due to the automatic padding feature).

    s.sendall(f)

    s.close()

    服务器:
    import socket
    import numpy as np
    import cv2
    import base64

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("127.0.0.1", 60124))
    s.listen(5)
    c, a = s.accept()

    while True:
    # Read 8 bytes that tells the length of encoded image to be expected.
    data = c.recv(8)

    if len(data) != 8:
    break

    # Decode f_len from base64
    f_len = base64.decodebytes(data)

    # Convet from array of 4 bytes to integer value.
    f_len = int.from_bytes(f_len, byteorder='little')

    #f = c.recv(1024)
    # Receive the encoded image.
    data = c.recv(f_len)

    if len(data) != f_len:
    break

    #x = base64.decodebytes(data) # Decode base64
    #x = np.fromstring(x , np.uint8)
    x = np.fromstring(data, np.uint8)
    var = cv2.imdecode(x, cv2.IMREAD_COLOR)

    if var is None:
    print('Invalid image')
    else:
    cv2.imshow("Camera" , var)
    cv2.waitKey(100)

    s.close()
    cv2.destroyAllWindows()

    关于python - Cv2 :problem recieving full data from socket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61147868/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com