我正在通过一个项目学习C++,该项目涉及将视频从一个设备流式传输到另一个设备以进行显示。我有一个PiCamera和以下客户端代码,用于捕获帧并通过TCP流发送它们。
import io
import socket
import struct
import time
import picamera
def stream():
servver_ip = "0.0.0.0" #intentionally left out
client_socket = socket.socket()
client_socket.connect((server_ip, 8123))
connection = client_socket.makefile("wb")
try:
camera = picamera.PiCamera()
camera.resolution = (640, 480)
camera.start_preview()
time.sleep(2)
start = time.time()
stream = io.BytesIO()
for image in camera.capture_continuous(stream, 'jpeg'):
connection.write(struct.pack("<L", stream.tell()))
connection.flush()
stream.seek(0)
connection.write(stream.read())
stream.seek(0)
stream.truncate()
connection.write(struct.pack("<L", 0))
except Exception as e:
raise e
finally:
connection.close()
client_socket.close()
def main():
while True:
try:
stream()
except ConnectionRefusedError as e:
print("Connection refused. It the server on?")
time.sleep(2)
except Exception as e:
print(e)
break
if __name__ == "__main__":
main()
上面的代码直接来自于一个PiCamera的示例,如果我在另一端有一个Python脚本,则它可以很好地工作。然而,当我尝试使用以下C ++代码接收和显示流时,我只能得到部分帧,且流程不流畅。有时我会得到0数据、1帧或慢慢杂乱无章的消息。改变usleep增量似乎可以得到一些改善,但我担心这不是正确的答案。我还尝试在recv中使用MSG_WAITALL标志,但这似乎阻止了任何数据的传输,这可能表明我的缓冲区大小的值不正确。
// the standard stuff
#include <iostream>
#include <string>
#include <unistd.h>
// opencv
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
//socket stuffs
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
int main(int argc, const char * argv[]) {
int key;
int yes = 1;
int height = 480;
int width = 640;
char* listenPort = "8123";
int bytes = 0;
int status;
struct addrinfo hints; // define our interface
struct addrinfo *serviceinfo; // will point to results
struct sockaddr_storage their_addr;
memset(&hints, 0, sizeof(hints)); // make sure the struct is empy
hints.ai_family = AF_UNSPEC; // don't care if IP4 or IP6
hints.ai_socktype = SOCK_STREAM; // TCP
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
// look up address info for this machine. Will populate service info
if ((getaddrinfo(NULL, listenPort, &hints, &serviceinfo)) == -1){
fprintf(stderr, "getaddinfo error: %s\n", gai_strerror(status));
exit(1);
}
// make the socket
int sockfd = socket(serviceinfo->ai_family, serviceinfo->ai_socktype, serviceinfo->ai_protocol);
//bind to the correct port
if ((bind(sockfd, serviceinfo->ai_addr, serviceinfo->ai_addrlen)) == -1){
fprintf(stderr, "failed to bind: %s\n", gai_strerror(status));
exit(1);
}
// allow us to reuse the port
if ((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))) == -1){
perror("setsockopt");
exit(1);
}
// number of connections to let in the queue
int backlog = 1;
// start listening for incoming connections
if ((listen(sockfd, backlog)) == -1){
perror("listen");
exit(1);
}
// start accepting incoming connections
socklen_t addr_size = sizeof(their_addr);
int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size); // use this for all send and receive calls
if (new_fd == -1){
perror("accept");
exit(1);
}
// empty image object
cv::Mat img = cv::Mat::zeros(height, width, CV_8UC1);
if (!img.isContinuous()){
img = img.clone();
}
int image_size = img.total() * img.elemSize();
cv::Mat rawImage = cv::Mat::zeros(1, image_size, CV_8UC1);
if (!rawImage.isContinuous()){
rawImage = rawImage.clone();
}
cv::Mat flipped = cv::Mat::zeros(height, width, CV_8UC1);
std::cout << "Press q to quit" << std::endl;
cv::namedWindow("Sentry", cv::WINDOW_AUTOSIZE);
while (key != 'q'){
std::cout << "Capturing" << std::endl;
if ((bytes = recv(new_fd, rawImage.data, image_size, 0)) == -1){
perror("Failed to receive bytes!");
exit(1);
}
img = cv::imdecode(rawImage, CV_LOAD_IMAGE_COLOR);
cv::flip(img, flipped, 1);
if (flipped.data != NULL) {
// show image. using a global here
cv::imshow("Sentry", flipped);
}
memset(rawImage.data, 0x0, sizeof(rawImage));
// look for quit key
key = cv::waitKey(10);
// pause for half a second
usleep(500000);
};
cv::destroyAllWindows();
freeaddrinfo(serviceinfo); // clear the linked list
close(sockfd);
return 0;
}
我正在寻找任何提示、答案或只是指向正确方向的建议。谢谢提前。
编辑:可行的解决方案
首先,感谢Kevin帮助我。我的问题在于我没有意识到我的初始Python客户端正在发送图像大小。通过搜索和使用下面的答案,我开始抓取那前4个字节,并调整我cv :: Mat rawImage
的大小。我不再编写检查recv
以确保我得到所有数据的逻辑,而是使用MSG_WAITALL
。这个标志与获取正确的有效载荷大小结合起来,使一切都可以顺利地工作。这是一个非常令人惊讶的经历。
#define CHUNKSIZE 500
int main(int argc, const char * argv[]) {
// skipping code from before
int32_t image_size = 0;
cv::Mat rawImage;
long data_received = 0;
long success;
cv::namedWindow("Sentry", cv::WINDOW_AUTOSIZE);
while (key != 'q'){
std::cout << "Capturing" << std::endl;
// Very important!! Grab the image_size here
success = recv(new_fd, &image_size, 4 , NULL);
if (success == -1){
perror("Failed to receive file size!");
exit(1);
}
// if your image size is extremely large, it's probably
// because you're grabing the wrong number of bytes above
if (image_size > 300000){
std::cout << "Image size is too large " << image_size << std::endl;
exit(1);
}
if (image_size > 0) {
//
rawImage = cv::Mat::zeros(1, image_size, CV_8UC1);
success = recv(new_fd, rawImage.data, image_size, MSG_WAITALL);
if (success == -1){
perror("Failed to receive");
exit(1);
}
img = cv::imdecode(rawImage, CV_LOAD_IMAGE_COLOR);
if (img.data != NULL) {
// show image. using a global here
cv::imshow("Sentry", img);
} else {
std::cout << "Image is null!" << std::endl;
}
memset(&rawImage, 0x0, sizeof(rawImage));
// look for quit key
key = cv::waitKey(10);
} else {
std::cout << "No message yet" << std::endl;
}
image_size = 0;
// pause for half a second
usleep(500000);
};
cv::destroyAllWindows();
freeaddrinfo(serviceinfo); // clear the linked list
close(sockfd);
return 0;
}
&fileData[dataReceived]
应该是什么?那应该是imageData
还是另一个缓冲区? - Andrew