Errors with Image transfer through dlib::bridge
Problem
I am trying to transfer up to 2MP images over dlib::bridge. I wrote a simple applications for server (the one who accepts images) and client (the one who sends images). The example works fine when i transfer relatively small images (e.g. 256x256).
However as the size of image increases the recieved image becomes only partially transfered: to bottom part if image is set to zero (see examples).
My suggestion was that the bridge object in client (sender) app dies before the transfer is completed, however, inserting a sleep after pipe::enqueue(...) call didn't help.
I am not sure if this a bug in dlib::bridge or improper use of the class.
Code
client.cpp:
#include <thread>
#include <string>
#include <iostream>
#include <chrono>
#include <dlib/bridge.h>
#include <dlib/array2d.h>
#include <dlib/pixel.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
int main()
{
// setting up pipe
dlib::pipe<dlib::array2d<dlib::rgb_pixel> > out_image(1);
dlib::bridge b_img(dlib::connect_to_ip_and_port("127.0.0.1", 3323), dlib::transmit(out_image));
// generating image crops to send
std::cout << "preparing image to send" << std::endl;
dlib::array2d<dlib::rgb_pixel> image;
try
{
dlib::load_bmp(image, "/sample_pictures/cone_scene.bmp");
}
catch( dlib::image_load_error & )
{
std::cerr << "error reading image from hard drive" << std::endl;
}
dlib::array2d<dlib::rgb_pixel> crop1;
dlib::array2d<dlib::rgb_pixel> crop2;
dlib::assign_image(crop1, image);
dlib::assign_image(crop2, dlib::sub_image(image, dlib::rectangle(400, 800, 700, 1200)));
// here we send images
std::cout << "sending image of size (" << crop1.nc() << ", " << crop1.nr() << ") ..." << std::endl;
out_image.enqueue(crop1);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
// second one
std::cout << "sending image of size (" << crop2.nc() << ", " << crop2.nr() << ") ..." << std::endl;
out_image.enqueue(crop2);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
return 0;
}
server.cpp:
#include <thread>
#include <string>
#include <chrono>
#include <dlib/bridge.h>
#include <dlib/array2d.h>
#include <dlib/image_io.h>
#include <dlib/pixel.h>
typedef dlib::array2d<dlib::rgb_pixel> img_type;
class MessageReciever : public dlib::threaded_object
{
public:
explicit MessageReciever(dlib::pipe<img_type> &inPipe, int max_recieved_count_ = 10)
: in(inPipe)
, recieved_count(0)
, max_recieved_count(max_recieved_count_)
{}
void thread() override
{
img_type value;
while(recieved_count < max_recieved_count)
{
in.dequeue(value);
++recieved_count;
callback(value);
}
}
void callback(const img_type &val) const
{
std::cout << "rgb image recieved: (" << val.nc() << ", " << val.nr() << ")" << std::endl;
std::ostringstream ss;
ss << "recieved_" << std::setfill('0') << std::setw(2) << recieved_count << ".bmp";
std::string filename = ss.str();
std::cout << "saving to " << filename << std::endl;
dlib::save_bmp(val, filename);
}
private:
dlib::pipe<img_type> ∈
int recieved_count;
const int max_recieved_count;
};
int main()
{
dlib::pipe<img_type> in_img(1);
dlib::bridge b_img(dlib::listen_on_port(3323), dlib::receive(in_img));
MessageReciever img_reciever(in_img);
img_reciever.start();
img_reciever.wait();
return 0;
}
CMakeLists.txt
project (pipe_test)
# some line to include dlib into cmake project
include(../dlib/cmake)
add_executable(pipe_test_client client.cpp)
target_link_libraries(pipe_test_client dlib)
add_executable(pipe_test_server server.cpp)
target_link_libraries(pipe_test_server dlib)
test data
Original image that is being sent (i had to convert them to png manually to be able to upload to github):

results:
I start server.exe, then run clinet.exe for several times. i get following output:
rgb image recieved: (2048, 1088)
saving to recieved_01.bmp
rgb image recieved: (2048, 1088)
saving to recieved_02.bmp
rgb image recieved: (2048, 1088)
saving to recieved_03.bmp
rgb image recieved: (2048, 1088)
saving to recieved_04.bmp
rgb image recieved: (2048, 1088)
saving to recieved_05.bmp
rgb image recieved: (2048, 1088)
saving to recieved_06.bmp
rgb image recieved: (301, 288)
saving to recieved_07.bmp
rgb image recieved: (2048, 1088)
saving to recieved_08.bmp
rgb image recieved: (301, 288)
saving to recieved_09.bmp
rgb image recieved: (2048, 1088)
saving to recieved_10.bmp
This shows that sometimes the smaller image is lost. Here are the artifacts on the images that were recieved:

System and versions
I tried following setups:
-
PC with Intel Core i7-4790, 16GB RAM, Windows 7 Service Pack 1. Visual Studio 2012 x64 + dlib 18.14 / Visual Studio 2015 x64 + dlib 19.4 for server and client apps running through localhost
-
Laptop with Intel Core i7, 8GB RAM, Fedora Linux Release 20 x64 gcc 4.8.3 + dlib 18.14 for client app sending data to windows pc via ethernet connection.
In case with real ethernet connection situation reproduced on even smaller images.
what did i come up with so far
- Adding this_thread::sleep doesn't help.
- I tried running visual studio debug for both server and client, but couldn't find anything - i went up to calls to
WaitForSingleObject. I aslo noticed that when I am at breakpoints in both server and client, all messages miss.
You can't just terminate the program. If the bridge is still sending data it's going to kill the transmission part way though. You have to wait for it to be done, which requires some kind of acknowledgement from the other side that it's got the data.
@davisking ok, i'll try improving the example and see if it works. I thought sleeping for 5 msec would be enough for connection via localhost. Well at least now you have sources for "sending images through bridge" example .=)
update:
Thank you @davisking, the problem is solved. Tested with both localhost and ethernet. the following code works without any corruptions:
server.cpp
#include <thread>
#include <string>
#include <chrono>
#include <dlib/bridge.h>
#include <dlib/array2d.h>
#include <dlib/image_io.h>
#include <dlib/pixel.h>
typedef dlib::array2d<dlib::rgb_pixel> img_type;
class MessageReciever : public dlib::threaded_object
{
public:
explicit MessageReciever(dlib::pipe<img_type> &inPipe,
dlib::pipe<bool> &outPipe,
int max_received_count_ = 10)
: in(inPipe)
, out(outPipe)
, received_count(0)
, max_received_count(max_received_count_)
{}
void thread() override
{
img_type value;
while(received_count < max_received_count)
{
in.dequeue(value);
++received_count;
bool success = true;
out.enqueue(success);
callback(value);
}
}
void callback(const img_type &val) const
{
std::cout << "rgb image received: (" << val.nc() << ", " << val.nr() << ")" << std::endl;
std::ostringstream ss;
ss << "received_" << std::setfill('0') << std::setw(2) << received_count << ".bmp";
std::string filename = ss.str();
std::cout << "saving to " << filename << std::endl;
dlib::save_bmp(val, filename);
}
private:
dlib::pipe<img_type> ∈
dlib::pipe<bool> &out;
int received_count;
const int max_received_count;
};
int main()
{
dlib::pipe<img_type> in_img(1);
dlib::pipe<bool> out_received(1);
dlib::bridge b_img(dlib::listen_on_port(3323),
dlib::receive(in_img),
dlib::transmit(out_received));
MessageReciever img_receiver(in_img, out_received);
img_receiver.start();
img_receiver.wait();
return 0;
}
client.cpp
#include <thread>
#include <string>
#include <iostream>
#include <chrono>
#include <dlib/bridge.h>
#include <dlib/array2d.h>
#include <dlib/pixel.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
int main()
{
// setting up pipe
dlib::pipe<dlib::array2d<dlib::rgb_pixel> > out_image(1);
dlib::pipe<bool> in_received(1);
dlib::bridge b_img(dlib::connect_to_ip_and_port("127.0.0.1", 3323),
dlib::transmit(out_image),
dlib::receive(in_received));
// generating image crops to send
std::cout << "preparing image to send" << std::endl;
dlib::array2d<dlib::rgb_pixel> image;
try
{
dlib::load_bmp(image, "/sample_pictures/cone_scene.bmp");
}
catch( dlib::image_load_error & )
{
std::cerr << "error reading image from hard drive" << std::endl;
}
dlib::array2d<dlib::rgb_pixel> crop1;
dlib::array2d<dlib::rgb_pixel> crop2;
dlib::assign_image(crop1, image);
dlib::assign_image(crop2, dlib::sub_image(image, dlib::rectangle(400, 800, 700, 1200)));
// here we send images
std::cout << "sending image of size (" << crop1.nc() << ", " << crop1.nr() << ") ..." << std::endl;
out_image.enqueue(crop1);
bool received = false;
in_received.dequeue(received);
if (!received)
std::cout << "receive fail registered" << std::endl;
else
std::cout << "recueve success registered" << std::endl;
// second one
std::cout << "sending image of size (" << crop2.nc() << ", " << crop2.nr() << ") ..." << std::endl;
out_image.enqueue(crop2);
in_received.dequeue(received);
if (!received)
std::cout << "receive fail registered" << std::endl;
else
std::cout << "recueve success registered" << std::endl;
return 0;
}
udpate2:
It was unclear for me from the docs and examples that bridge does not have some internal managing of trasmission end. Maybe it should be noted explicitly somewhere in the docs.
There is no way to manage transmission end. If you kill the program you kill the program. It is not reasonable to assume that the bridge destructor would have some kind of data acknowledgement since that might hang forever waiting for acknowledgement from the other side that it got the data. It should be obvious that the bridge doesn't do this since the bridge destructor doesn't hang.
It should also be noted that even if the bridge gives all it's data to the OS and all the data looks "sent" that doesn't mean the other host eventually gets the data. If your application terminates the OS is perfectly allowed to drop those outgoing data buffers without sending them, which is often what it will do if you terminate your process before those OS networking buffers are done sending. There are also a lot of other ways the data might not eventually arrive at the other side. So you have to wait for some kind of acknowledgement back from the other end if you want to be sure it got the data.