使用python 3套接字模块发送框架

时间:2018-07-04 15:59:40

标签: python image sockets video opencv3.0

我正在尝试使用python 3中的套接字模块发送视频帧。问题是我开始发送100帧,但100帧存储在接收的第一个文件中,其余接收的帧为0字节文件?

我试图关闭我打开的每个文件,但这没有用。

Server.py

import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Started listening on 192.168.1.7')
server_socket.bind(("", 5000))
server_socket.listen(5)
import os
import time 
path = '/home/lonewolf/darknet-master/python/driving_dataset/'
client_socket, address = server_socket.accept()
print("Conencted to - ",address,"\n")

no_of_frames = 100
i = 0
while(i < no_of_frames):
    # fname = client_socket.recv(1024).decode()
    # print("The following data was received - ",fname)
    # print("Opening file - ",fname)
    img = open((path + '{}.jpg'.format(i)),'rb')
    print('Image', '{}.jpg'.format(i), ' loaded successfully')

    while True:
        strng = img.readline(512)
        #print(strng)
        if not strng:
            break
        client_socket.send(strng)
    print('Image', '{}.jpg'.format(i), ' sent successfully')
    img.close()
    i+=1
client_socket.close()
server_socket.close()
exit()

Client.py`

import socket,os
import cv2
import time

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("192.168.1.7", 5000))
size = 1024

#cv2.namedWindow('image', cv2.WINDOW_NORMAL)
no_of_frames = 100
i = 0

while(i < no_of_frames):

    fp = open('/home/pi/Documents/frame{}.jpg'.format(i),'wb')
    while True:
        strng = client_socket.recv(512)
        #print(strng)
        if not strng:
            print("a file is closed")
            fp.close()
            break
        fp.write(strng)
    fp.close()
    print("a frame Received successfully")
    image_path = '/home/pi/Documents/frame.jpg'
    image = cv2.imread(image_path, 1)

    #cv2.imshow('image', image)
    i+=1

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
client_socket.close()
cv2.destroyAllWindows()

`

0 个答案:

没有答案