39 #include "PracticalSockets.h"
43 #if TRANSFORM_TYPE == 1
52 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_NUMPI)
53 #include "opencv2/opencv.hpp"
54 #include "../../../../../../ROLE/vision/hls/warp_transform/include/xf_ocv_ref.hpp"
60 void delay(
unsigned int mseconds)
62 clock_t goal = mseconds +
clock();
63 while (goal >
clock());
69 cout <<
"...build with: " << endl;
70 cout <<
" ██████╗███████╗██████╗ ███████╗ ██████╗ ██████╗ " << endl;
71 cout <<
"██╔════╝██╔════╝██╔══██╗ ╚══███╔╝██╔═══██╗██╔═══██╗ " << endl;
72 cout <<
"██║ █████╗ ██████╔╝ ███╔╝ ██║ ██║██║ ██║ " << endl;
73 cout <<
"██║ ██╔══╝ ██╔═══╝ ███╔╝ ██║ ██║██║ ██║ " << endl;
74 cout <<
"╚██████╗██║ ██║███████╗███████╗╚██████╔╝╚██████╔╝ " << endl;
75 cout <<
" ╚═════╝╚═╝ ╚═╝╚══════╝╚══════╝ ╚═════╝ ╚═════╝ " << endl;
76 cout <<
"A cloudFPGA project from IBM ZRL v1.0 " << endl;
77 cout <<
"Quantitative Finance Monte-Carlo European Pricing Engine " << endl;
95 int min_size = min(
h, w);
96 int x = w/2-min_size/2;
97 int y =
h/2-min_size/2;
99 cv::Mat crop_img =
input(Rect(
x,
y, min_size, min_size));
100 resize(crop_img,
output, Size(dstSize.width, dstSize.height), 0, 0, interpolation);
105 unsigned int bytes_per_line = 8;
106 char tx_cmd [bytes_per_line];
107 char img_cmd [bytes_per_line];
108 char value_cmd[bytes_per_line];
111 for (
unsigned int k = 0; k < bytes_per_line; k++) {
112 value_cmd[k] = (char)0;
115 img_cmd[k] = (char)0;
119 img_cmd[k] = (char)2;
122 out =
out.append(tx_cmd,bytes_per_line);
126 for (
int i = 0; i < 8; i++)
128 memcpy(value_cmd+off, (
float*)transform_matrix+i, 4);
130 off = off % bytes_per_line;
134 out =
out.append(value_cmd,bytes_per_line);
138 unsigned int zero_constant = 0;
139 memcpy(value_cmd, (
char*)transform_matrix+8, 4);
140 memcpy(value_cmd, (
char*)&zero_constant, 4);
141 out =
out.append(value_cmd,bytes_per_line);
143 memcpy(img_cmd+6, (
char*)&
rows, 2);
144 memcpy(img_cmd+4, (
char*)&
cols, 2);
146 out =
out.append(img_cmd,bytes_per_line);
153 #if PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME
154 void warp_transform(
char *s_servAddress,
char *s_servPort,
char *input_str,
char *output_img_str,
char *output_points_str)
155 #elif PY_WRAP == PY_WRAP_WARPTRANSFORM_NUMPI
156 void warp_transform(
int total_size,
unsigned char *input_img,
int total_size2,
unsigned char *output_img,
char *s_servAddress,
char *s_servPort)
164 int main(int argc, char * argv[]) {
165 if ((argc < 3) || (argc > 4)) {
166 cerr <<
"Usage: " << argv[0] <<
" <Server> <Server Port> <optional input image>\n";
176 assert ((argc == 3) || (argc == 4));
177 string s_servAddress = argv[1];
178 char *s_servPort = argv[2];
181 string servAddress = s_servAddress;
182 unsigned short servPort;
184 if (net_type ==
udp) {
185 servPort = Socket::resolveService(s_servPort,
"udp");
187 else if (net_type ==
tcp) {
188 servPort = atoi(s_servPort);
191 cout <<
"ERROR: Invalid type of socket type provided: " << net_type <<
" Choosed one of (tcp=0 or udp=1)" << endl;
195 unsigned int recvMsgSize;
197 #ifdef INPUT_FROM_CAMERA
200 #if PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME
201 input_num = atoi(input_str);
202 input_string =
"./cam"+to_string(input_num);
208 else if (argc == 4) {
209 input_num = atoi(argv[3]);
211 input_string =
"./cam"+to_string(input_num);
215 #if PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME
216 input_string.assign(input_str);
221 input_string.assign(
"../../../../../../ROLE/vision/hls/warp_transform/test/8x8.png");
223 else if (argc == 4) {
224 input_string.assign(argv[3]);
230 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
243 float transformation_matrix_float [9] = {1.5,0,0,0,1.8,0,0,0,0};
247 string out_video_file;
252 out_video_file.assign(input_string);
253 out_video_file +=
"_fpga_video_out.avi";
255 #if CV_MAJOR_VERSION < 4
271 #ifndef TB_SIM_CFP_VITIS
272 UDPSocket sock(servPort);
278 TCPSocket sock(servAddress, servPort);
282 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
289 vector < uchar > encoded;
291 #ifdef INPUT_FROM_CAMERA
293 VideoCapture
cap(input_num);
294 if (!
cap.isOpened()) {
295 cerr <<
"OpenCV Failed to open camera " + input_num << endl;
301 VideoCapture
cap(input_string);
302 if (!
cap.isOpened()) {
303 cerr <<
"OpenCV Failed to open file " + input_string << endl;
314 clock_t start_cycle_main =
clock();
316 if (
frame.empty())
break;
317 if(
frame.size().width==0)
continue;
318 cout <<
" ___________________________________________________________________ " << endl;
319 cout <<
"/ \\" << endl;
320 cout <<
"INFO: Frame # " << ++
num_frame << endl;
321 #if CV_MAJOR_VERSION < 4
328 cout <<
"WARNING: Input frame was resized from " <<
frame.cols <<
"x"
329 <<
frame.rows <<
" to " <<
send.cols <<
"x" <<
send.rows << endl;
331 imwrite(
"testimg.png",
frame);
338 namedWindow(
"host_send", CV_WINDOW_NORMAL);
339 imshow(
"host_send",
send);
345 assert(
send.isContinuous());
347 unsigned int send_total =
send.total();
348 unsigned int send_channels =
send.channels();
349 unsigned int warptx_cmd_size = warptx_cmd.length();
352 unsigned int send_total = (unsigned int)total_size + warptx_cmd_size;
353 unsigned int send_channels = 1;
354 unsigned char * sendarr = input_img;
357 unsigned int total_pack = 1 + (send_total * send_channels - 1 + warptx_cmd_size) /
PACK_SIZE;
358 unsigned int total_bytes = total_pack *
PACK_SIZE;
359 unsigned int bytes_in_last_pack = send_total * send_channels + warptx_cmd_size - (total_pack - 1) *
PACK_SIZE;
362 unsigned int total_pack_rx = 1 + (send_total * send_channels - 1) /
PACK_SIZE;
363 unsigned int total_bytes_rx = total_pack_rx *
PACK_SIZE;
364 unsigned int bytes_in_last_pack_rx = send_total * send_channels - (total_pack_rx- 1) *
PACK_SIZE;
367 unsigned char * longbuf = (
unsigned char *) malloc (
PACK_SIZE * total_pack_rx *
sizeof (
unsigned char));
369 cout <<
"INFO: FPGA destination : " << servAddress <<
":" << servPort << endl;
370 cout <<
"INFO: Network socket : " << ((
NET_TYPE ==
tcp) ?
"TCP" :
"UDP") << endl;
371 cout <<
"INFO: Total packets to send= " << total_pack << endl;
372 cout <<
"INFO: Total packets to receive = " << total_pack_rx << endl;
373 cout <<
"INFO: Total bytes to send = " << send_total * send_channels + warptx_cmd_size << endl;
374 cout <<
"INFO: Total bytes to receive = " << send_total * send_channels << endl;
375 cout <<
"INFO: Total bytes in " << total_pack <<
" packets = " << total_bytes << endl;
376 cout <<
"INFO: Total bytes in " << total_pack_rx <<
" packets = " << total_bytes_rx << endl;
377 cout <<
"INFO: Bytes in last packet = " << bytes_in_last_pack << endl;
378 cout <<
"INFO: Bytes in last packet to receive = " << bytes_in_last_pack_rx << endl;
379 cout <<
"INFO: Packet size (custom MTU) = " <<
PACK_SIZE << endl;
381 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
386 clock_t start_cycle_warp_transform_sw =
clock();
389 clock_t end_cycle_warp_transform_sw =
clock();
390 double duration_warp_transform_sw = (end_cycle_warp_transform_sw - start_cycle_warp_transform_sw) /
391 (
double) CLOCKS_PER_SEC;
392 cout <<
"INFO: SW exec. time:" << duration_warp_transform_sw <<
" seconds" << endl;
393 cout <<
"INFO: Effective FPS SW:" << (1 / duration_warp_transform_sw) <<
" \tkbps:" <<
394 (
PACK_SIZE * total_pack / duration_warp_transform_sw / 1024 * 8) << endl;
405 unsigned char * sendarr_img =
send.isContinuous()?
send.data:
send.clone().data;
408 unsigned char * sendarr = (
unsigned char *) malloc (send_total * send_channels + warptx_cmd_size);
409 memcpy(sendarr,warptx_cmd.c_str(), warptx_cmd_size);
410 memcpy(sendarr+warptx_cmd_size,sendarr_img, send_total * send_channels);
414 clock_t start_cycle_warp_transform_hw =
clock();
419 clock_t last_cycle_tx =
clock();
421 for (
unsigned int i = 0; i < total_pack; i++) {
422 if ( i == total_pack - 1 ) {
423 sending_now = bytes_in_last_pack;
426 sock.sendTo( & sendarr[i *
PACK_SIZE], sending_now, servAddress, servPort);
428 sock.send( & sendarr[i *
PACK_SIZE], sending_now);
433 clock_t next_cycle_tx =
clock();
434 double duration_tx = (next_cycle_tx - last_cycle_tx) / (
double) CLOCKS_PER_SEC;
435 cout <<
"INFO: Effective FPS TX:" << (1 / duration_tx) <<
" \tkbps:" << (
PACK_SIZE *
436 total_pack / duration_tx / 1024 * 8) << endl;
437 last_cycle_tx = next_cycle_tx;
443 clock_t last_cycle_rx =
clock();
445 cout <<
"INFO: Expecting length of packs:" << total_pack_rx <<
" from " << servAddress <<
":" << servPort << endl;
447 unsigned int loopi=0;
448 for (
unsigned int i = 0; i < send_total; ) {
454 recvMsgSize = sock.recvFrom(buffer,
BUF_LEN, servAddress, servPort);
456 recvMsgSize = sock.recv(buffer,
BUF_LEN);
458 if (recvMsgSize != receiving_now) {
459 cerr <<
"WARNING: at i=" << i <<
" received unexpected size pack:" << recvMsgSize <<
". Expected: " <<
460 receiving_now << endl;
463 memcpy( & longbuf[i], buffer, recvMsgSize);
468 cout <<
"INFO: Received packet from " << servAddress <<
":" << servPort << endl;
470 clock_t next_cycle_rx =
clock();
471 double duration_rx = (next_cycle_rx - last_cycle_rx) / (
double) CLOCKS_PER_SEC;
472 cout <<
"INFO: Effective FPS RX:" << (1 / duration_rx) <<
" \tkbps:" << (
PACK_SIZE *
473 total_pack_rx / duration_rx / 1024 * 8) << endl;
474 last_cycle_rx = next_cycle_rx;
476 clock_t end_cycle_warp_transform_hw = next_cycle_rx;
478 double duration_warp_transform_hw = (end_cycle_warp_transform_hw - start_cycle_warp_transform_hw) /
479 (
double) CLOCKS_PER_SEC;
480 cout <<
"INFO: HW exec. time:" << duration_warp_transform_hw <<
" seconds" << endl;
481 cout <<
"INFO: Effective FPS HW:" << (1 / duration_warp_transform_hw) <<
" \tkbps:" <<
482 (
PACK_SIZE * total_pack / duration_warp_transform_hw / 1024 * 8) << endl;
485 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
488 if (
frame.size().width == 0) {
489 cerr <<
"receive failure!" << endl;
493 namedWindow(
"host_recv", CV_WINDOW_NORMAL);
494 imshow(
"host_recv",
frame);
504 string windowName =
"cFp_Vitis End2End";
507 #ifdef WRITE_OUTPUT_FILE
509 out_img_file.assign(input_string);
510 out_img_file +=
"_fpga_image_out_frame_" + to_string(
num_frame) +
".png";
511 #if defined(PY_WRAP) && (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
513 if (!strcpy(output_img_str, &out_img_file[0])) {
514 cerr <<
"ERROR: Cannot write to output image string." << endl;
517 cout <<
"INFO: The output image file is stored at : " << out_img_file << endl;
519 imwrite(out_img_file,
frame);
526 cout <<
"INFO: The output video file is stored at : " << out_video_file << endl;
528 if (
frame.channels() != 1) {
532 cvtColor(
frame, tovideo, COLOR_GRAY2BGR);
534 video.write(tovideo);
538 double duration_main = (
clock() - start_cycle_main) / (
double) CLOCKS_PER_SEC;
539 cout <<
"INFO: Effective FPS E2E:" << (1 / duration_main) << endl;
540 cout <<
"\\___________________________________________________________________/" << endl
560 }
catch (SocketException & e) {
561 cerr << e.what() << endl;
cat GET request dos socat stdio tcp
void ocv_ref(cv::Mat img_gray, cv::Mat &ocv_out_img, float Th)
def send(cFcluster cluster, node_id, data_obj, proto='udp', port=CF_DEFAULT_PORT)
Common utility functions headers, i.e. timing, printing wrappers.