cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
WarpTransform

This is the WarpTransform accelerated function from Vitis Vision Open Source Library. More...

Collaboration diagram for WarpTransform:

Modules

 WarpTransform Testbench
 This is a subgroup of WarpTransform accelerated function with only testbench-related functions/classes.
 
 WarpTransform HLS
 This is a subgroup of WarpTransform accelerated function with only synthesizable (HLS) functions/classes.
 
 WarpTransform Host
 This is a subgroup of WarpTransform accelerated function with only host code software.
 

Files

file  config.h
 The configuration of a WarpTransform Example application (UDP or TCP)
 
file  warp_transform_host.cpp
 WarpTransform userspace application for cF (x86, ppc64).
 
file  xf_warp_transform_config.h
 The WarpTransform configuration header.
 

Macros

#define CEIL(a, b)   (((a) + (b-1)) / (b))
 
#define FRAME_HEIGHT   32
 
#define FRAME_WIDTH   32
 
#define FRAME_INTERVAL   (1000/30)
 
#define PACK_SIZE   1024
 
#define BUF_LEN   65540
 
#define WRITE_OUTPUT_FILE
 
#define TB_SIM_CFP_VITIS
 
#define INPUT_TYPE_HOST   CV_8UC1
 
#define NET_TYPE   udp
 
#define DEBUG_LEVEL   dbgLevelNone
 
#define FRAME_TOTAL   FRAME_HEIGHT * FRAME_WIDTH
 
#define WARP_TRANSFORM_TOTAL   FRAME_TOTAL + 8 * 2 + 5 * 8
 
#define TOT_TRANSFERS   CEIL(WARP_TRANSFORM_TOTAL, PACK_SIZE)
 
#define tcp   0
 
#define udp   1
 
#define PY_WRAP_WARPTRANSFORM_NUMPI   0
 
#define PY_WRAP_WARPTRANSFORM_FILENAME   1
 
#define TRMAT_DIM2   3
 
#define TRMAT_DIM1   2
 
#define NUM_STORE_ROWS   256
 
#define START_PROC   200
 
#define INTERPOLATION   0
 
#define TRANSFORM_TYPE   0
 
#define XF_USE_URAM   false
 
#define NPC1   XF_NPPC1
 
#define PTR_WIDTH   128
 
#define TYPE   XF_8UC1
 
#define CH_TYPE   XF_GRAY
 
#define INPUT_PTR_WIDTH   8
 
#define OUTPUT_PTR_WIDTH   64
 
#define NPIX   XF_NPPC1
 
#define WIDTH   FRAME_WIDTH
 
#define HEIGHT   FRAME_HEIGHT
 
#define IMGSIZE   FRAME_TOTAL
 
#define BITS_PER_10GBITETHRNET_AXI_PACKET   64
 
#define BYTES_PER_10GBITETHRNET_AXI_PACKET   (BITS_PER_10GBITETHRNET_AXI_PACKET/8)
 
#define IMG_PACKETS   IMGSIZE/(BYTES_PER_10GBITETHRNET_AXI_PACKET)
 
#define MIN_RX_LOOPS   IMG_PACKETS*(BITS_PER_10GBITETHRNET_AXI_PACKET/INPUT_PTR_WIDTH)
 
#define MIN_TX_LOOPS   IMG_PACKETS*(BITS_PER_10GBITETHRNET_AXI_PACKET/OUTPUT_PTR_WIDTH)
 
#define OUT_TYPE   TYPE
 

Functions

void delay (unsigned int mseconds)
 
void print_cFpZoo (void)
 
void resizeCropSquare (const cv::Mat &input, const cv::Mat &output, const cv::Size &dstSize, int interpolation=INTER_LINEAR)
 Resize an image and crop if necessary in order to keep a rectangle area in the middle of the image. More...
 
string prepareWarpTransformCommand (unsigned int rows, unsigned int cols, unsigned int channels, float *transform_matrix)
 
int main (int argc, char *argv[])
 
void warptTransformAccelArray (ap_uint< 8 > *img_in, float *transform, ap_uint< 64 > *img_out, int rows, int cols)
 Top-level accelerated function of the WarptTransform Application with array I/F used only for simulation/TB purposes. More...
 
void warpTransformAccelStream (hls::stream< ap_uint< 8 >> &img_in_axi_stream, hls::stream< ap_uint< 64 >> &img_out_axi_stream, int rows, int cols, float transform_matrix[9])
 Top-level accelerated function of the WarpTransform Application with array I/Fadd WARPTRANSFORM. More...
 
void fakeWarpTransformAccelStream (hls::stream< ap_axiu< 8, 0, 0, 0 > > &img_in_axi_stream, hls::stream< ap_axiu< 64, 0, 0, 0 > > &img_out_axi_stream, unsigned int min_rx_loops, unsigned int min_tx_loops, float transform_matrix[9])
 
void warp_transformAccelMem (membus_t *img_inp, membus_t *img_out, int rows, int cols, hls::stream< float > &sTxMatrix)
 Top-level accelerated function of the WarpTransform Application with memory mapped interfaces. More...
 

Detailed Description

This is the WarpTransform accelerated function from Vitis Vision Open Source Library.

Macro Definition Documentation

◆ BITS_PER_10GBITETHRNET_AXI_PACKET

#define BITS_PER_10GBITETHRNET_AXI_PACKET   64

Definition at line 121 of file xf_warp_transform_config.h.

◆ BUF_LEN

#define BUF_LEN   65540

Larger than maximum UDP packet size

Definition at line 55 of file config.h.

◆ BYTES_PER_10GBITETHRNET_AXI_PACKET

#define BYTES_PER_10GBITETHRNET_AXI_PACKET   (BITS_PER_10GBITETHRNET_AXI_PACKET/8)

Definition at line 122 of file xf_warp_transform_config.h.

◆ CEIL

#define CEIL (   a,
 
)    (((a) + (b-1)) / (b))

Ceiling function without using math.h

Definition at line 37 of file config.h.

◆ CH_TYPE

#define CH_TYPE   XF_GRAY

Definition at line 102 of file xf_warp_transform_config.h.

◆ DEBUG_LEVEL

#define DEBUG_LEVEL   dbgLevelNone

The level of debugging. 0->None, 1-> Light Debug, 2-> Medium Debug , 3-> Insane Debug

Definition at line 78 of file config.h.

◆ FRAME_HEIGHT

#define FRAME_HEIGHT   32

The maximum width of frame in pixels from 6x6 to 256x256 for debugging

Definition at line 43 of file config.h.

◆ FRAME_INTERVAL

#define FRAME_INTERVAL   (1000/30)

Definition at line 48 of file config.h.

◆ FRAME_TOTAL

#define FRAME_TOTAL   FRAME_HEIGHT * FRAME_WIDTH

Definition at line 82 of file config.h.

◆ FRAME_WIDTH

#define FRAME_WIDTH   32

The maximum height of frame in pixels

Definition at line 46 of file config.h.

◆ HEIGHT

#define HEIGHT   FRAME_HEIGHT

Definition at line 117 of file xf_warp_transform_config.h.

◆ IMG_PACKETS

#define IMG_PACKETS   IMGSIZE/(BYTES_PER_10GBITETHRNET_AXI_PACKET)

Definition at line 124 of file xf_warp_transform_config.h.

◆ IMGSIZE

#define IMGSIZE   FRAME_TOTAL

Definition at line 119 of file xf_warp_transform_config.h.

◆ INPUT_PTR_WIDTH

#define INPUT_PTR_WIDTH   8

Definition at line 103 of file xf_warp_transform_config.h.

◆ INPUT_TYPE_HOST

#define INPUT_TYPE_HOST   CV_8UC1

Keep it uncommented of you want the input to be from camera frames else, for images comment it For The OpenCV type fot th input image. TODO: We have to automatically fix it for every kernel

Definition at line 70 of file config.h.

◆ INTERPOLATION

#define INTERPOLATION   0

Definition at line 74 of file xf_warp_transform_config.h.

◆ MIN_RX_LOOPS

Definition at line 126 of file xf_warp_transform_config.h.

◆ MIN_TX_LOOPS

Definition at line 127 of file xf_warp_transform_config.h.

◆ NET_TYPE

#define NET_TYPE   udp

The network socket type: tcp or udp

Definition at line 73 of file config.h.

◆ NPC1

#define NPC1   XF_NPPC1

Definition at line 82 of file xf_warp_transform_config.h.

◆ NPIX

#define NPIX   XF_NPPC1

Definition at line 113 of file xf_warp_transform_config.h.

◆ NUM_STORE_ROWS

#define NUM_STORE_ROWS   256

Definition at line 69 of file xf_warp_transform_config.h.

◆ OUT_TYPE

#define OUT_TYPE   TYPE

Definition at line 130 of file xf_warp_transform_config.h.

◆ OUTPUT_PTR_WIDTH

#define OUTPUT_PTR_WIDTH   64

Definition at line 104 of file xf_warp_transform_config.h.

◆ PACK_SIZE

#define PACK_SIZE   1024

This is our custom MTU. We must use a multiple of 8 (Bytes per transaction)! 1450 4086 udp pack size; note that OSX limits < 8100 bytes

Definition at line 52 of file config.h.

◆ PTR_WIDTH

#define PTR_WIDTH   128

Definition at line 83 of file xf_warp_transform_config.h.

◆ PY_WRAP_WARPTRANSFORM_FILENAME

#define PY_WRAP_WARPTRANSFORM_FILENAME   1

Definition at line 92 of file config.h.

◆ PY_WRAP_WARPTRANSFORM_NUMPI

#define PY_WRAP_WARPTRANSFORM_NUMPI   0

Definition at line 91 of file config.h.

◆ START_PROC

#define START_PROC   200

Definition at line 72 of file xf_warp_transform_config.h.

◆ TB_SIM_CFP_VITIS

#define TB_SIM_CFP_VITIS

If defined, images will be shown in pop-up windows
For HOST TB uncomment this. For normal host execution keep it commented

Definition at line 64 of file config.h.

◆ tcp

#define tcp   0

Definition at line 88 of file config.h.

◆ TOT_TRANSFERS

#define TOT_TRANSFERS   CEIL(WARP_TRANSFORM_TOTAL, PACK_SIZE)

The total TxRx transfers for a predefined MTU=PACK_SIZE

Definition at line 86 of file config.h.

◆ TRANSFORM_TYPE

#define TRANSFORM_TYPE   0

Definition at line 77 of file xf_warp_transform_config.h.

◆ TRMAT_DIM1

#define TRMAT_DIM1   2

Definition at line 48 of file warp_transform_host.cpp.

◆ TRMAT_DIM2

#define TRMAT_DIM2   3

Definition at line 47 of file warp_transform_host.cpp.

◆ TYPE

#define TYPE   XF_8UC1

Definition at line 97 of file xf_warp_transform_config.h.

◆ udp

#define udp   1

Definition at line 89 of file config.h.

◆ WARP_TRANSFORM_TOTAL

#define WARP_TRANSFORM_TOTAL   FRAME_TOTAL + 8 * 2 + 5 * 8

Definition at line 83 of file config.h.

◆ WIDTH

#define WIDTH   FRAME_WIDTH

Definition at line 116 of file xf_warp_transform_config.h.

◆ WRITE_OUTPUT_FILE

#define WRITE_OUTPUT_FILE

If defined, output images will be written

Definition at line 58 of file config.h.

◆ XF_USE_URAM

#define XF_USE_URAM   false

Definition at line 78 of file xf_warp_transform_config.h.

Function Documentation

◆ delay()

void delay ( unsigned int  mseconds)

Definition at line 60 of file warp_transform_host.cpp.

61 {
62  clock_t goal = mseconds + clock();
63  while (goal > clock());
64 }
def clock()
Definition: common.py:174
Here is the call graph for this function:

◆ fakeWarpTransformAccelStream()

void fakeWarpTransformAccelStream ( hls::stream< ap_axiu< 8, 0, 0, 0 > > &  img_in_axi_stream,
hls::stream< ap_axiu< 64, 0, 0, 0 > > &  img_out_axi_stream,
unsigned int  min_rx_loops,
unsigned int  min_tx_loops,
float  transform_matrix[9] 
)
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Main testbench and user-application for WarpTransform on host. Client

Returns
O on success, 1 on fail

Definition at line 164 of file warp_transform_host.cpp.

164  {
165  if ((argc < 3) || (argc > 4)) { // Test for correct number of arguments
166  cerr << "Usage: " << argv[0] << " <Server> <Server Port> <optional input image>\n";
167  exit(1);
168  }
169 #endif // PY_WRAP
170 
171  //------------------------------------------------------
172  //-- STEP-1 : Socket and variables definition
173  //------------------------------------------------------
174 
175  #ifndef PY_WRAP
176  assert ((argc == 3) || (argc == 4));
177  string s_servAddress = argv[1]; // First arg: server address
178  char *s_servPort = argv[2];
179  #endif
180 
181  string servAddress = s_servAddress;
182  unsigned short servPort;
183  bool net_type = NET_TYPE;
184  if (net_type == udp) {
185  servPort = Socket::resolveService(s_servPort, "udp");
186  }
187  else if (net_type == tcp) {
188  servPort = atoi(s_servPort);
189  }
190  else {
191  cout << "ERROR: Invalid type of socket type provided: " << net_type << " Choosed one of (tcp=0 or udp=1)" << endl;
192  }
193 
194  unsigned char buffer[BUF_LEN]; // Buffer for echo string
195  unsigned int recvMsgSize; // Size of received message
196  string input_string;
197 #ifdef INPUT_FROM_CAMERA
198  int input_num;
199 #ifdef PY_WRAP
200 #if PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME
201  input_num = atoi(input_str);
202  input_string = "./cam"+to_string(input_num);
203 #endif // PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME
204 #else // !PY_WRAP
205  if (argc == 3) {
206  input_num = 0;
207  }
208  else if (argc == 4) {
209  input_num = atoi(argv[3]);
210  }
211  input_string = "./cam"+to_string(input_num);
212 #endif // PY_WRAP
213 #else // !INPUT_FROM_CAMERA
214 #ifdef PY_WRAP
215 #if PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME
216  input_string.assign(input_str);
217 #endif // PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME
218 #else // !PY_WRAP
219  if (argc == 3) {
220  // Give a default image
221  input_string.assign("../../../../../../ROLE/vision/hls/warp_transform/test/8x8.png");
222  }
223  else if (argc == 4) {
224  input_string.assign(argv[3]);
225  }
226 #endif // PY_WRAP
227 #endif // INPUT_FROM_CAMERA
228 
229 
230 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
231 
232  // ksize: aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...
233  // int ksize = WINDOW_SIZE ;
235  // float identity_tx_mat [9] = {1,0,0,0,1,0,0,0,0};
236  // float xtranslation_tx_mat [9] = {1,0,2,0,1,0,0,0,0};// 1 0 vx 0 1 vy 000
237  // float ytranslation_tx_mat [9] = {1,0,0,0,1,2,0,0,0};
238  // float reflection_tx_mat [9] = {-1,0,0,0,1,0,0,0,0};
239  // float yscale_tx_mat [9] = {2,0,0,0,1,0,0,0,0}; //cx 0 0 0 cy 0 000
240  // float xscale_tx_mat [9] = {1,0,0,0,2,0,0,0,0};
241  // float rotation_30degree_tx_mat [9] = {0.87,-0.5,0,0.5,0.87,0,0,0,0}; //cos -sin 0 sin cos 0 000
242  // float shearing_tx_mat [9] = {1,0.5,0,0,1,0,0,0,0}; //1 cx 0 cy 1 0 000
243  float transformation_matrix_float [9] = {1.5,0,0,0,1.8,0,0,0,0};
244  cv::Mat transformation_matrix(TRMAT_DIM1, TRMAT_DIM2, CV_32FC1, transformation_matrix_float);
246  string out_img_file;
247  string out_video_file;
248  // Define the codec and create VideoWriter object.The output is stored in 'outcpp.avi' file.
249  //#ifdef PY_WRAP
250  //out_video_file.assign(output_str);
251  //#else // !PY_WRAP
252  out_video_file.assign(input_string);
253  out_video_file += "_fpga_video_out.avi";
254  //#endif // PY_WRAP
255 #if CV_MAJOR_VERSION < 4
256  VideoWriter video(out_video_file,CV_FOURCC('M','J','P','G'),10, Size(FRAME_WIDTH,FRAME_HEIGHT));
257 #else
258  VideoWriter video(out_video_file,cv::VideoWriter::fourcc('M','J','P','G'),10, Size(FRAME_WIDTH,FRAME_HEIGHT));
259 #endif
260 
261 #endif // #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
262 
263  print_cFpZoo();
264 
265  try {
266 
267  //------------------------------------------------------
268  //-- STEP-2 : Initialize socket connection
269  //------------------------------------------------------
270 #if NET_TYPE == udp
271 #ifndef TB_SIM_CFP_VITIS
272  UDPSocket sock(servPort); // NOTE: It is very important to set port here in order to call
273  // bind() in the UDPSocket constructor
274 #else // TB_SIM_CFP_VITIS
275  UDPSocket sock; // NOTE: In HOST TB the port is already binded by warp_transform_host_fwd_tb.cpp
276 #endif // TB_SIM_CFP_VITIS
277 #else // tcp
278  TCPSocket sock(servAddress, servPort);
279 #endif // udp/tcp
280 
281 
282 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
283 
284 
285  //------------------------------------------------------------------------------------
286  //-- STEP-3 : Initialize a Greyscale OpenCV Mat either from image or from video/camera
287  //------------------------------------------------------------------------------------
288  Mat frame, send(FRAME_WIDTH, FRAME_HEIGHT, INPUT_TYPE_HOST, Scalar(0)), ocv_out_img;
289  vector < uchar > encoded;
290 
291 #ifdef INPUT_FROM_CAMERA
292 
293  VideoCapture cap(input_num); // Grab the camera
294  if (!cap.isOpened()) {
295  cerr << "OpenCV Failed to open camera " + input_num << endl;
296  exit(1);
297  }
298 
299 #else // INPUT_FROM_CAMERA
300 
301  VideoCapture cap(input_string); // Grab the image
302  if (!cap.isOpened()) {
303  cerr << "OpenCV Failed to open file " + input_string << endl;
304  exit(1);
305  }
306 
307 #endif // INPUT_FROM_CAMERA
308  std::string warptx_cmd = prepareWarpTransformCommand(FRAME_WIDTH, FRAME_HEIGHT, send.channels(), transformation_matrix_float);
309 
310  //frame = cv::imread(argv[3], cv::IMREAD_GRAYSCALE); // reading in the image in grey scale
311  unsigned int num_frame = 0;
312 
313  while (1) {
314  clock_t start_cycle_main = clock();
315  cap >> frame;
316  if (frame.empty()) break; // if input is an image, the loop will be executed once
317  if(frame.size().width==0) continue; //simple integrity check; skip erroneous data...
318  cout << " ___________________________________________________________________ " << endl;
319  cout << "/ \\" << endl;
320  cout << "INFO: Frame # " << ++num_frame << endl;
321 #if CV_MAJOR_VERSION < 4
322  cv::cvtColor(frame,frame,CV_BGR2GRAY);
323 #else
324  cv::cvtColor(frame,frame,cv::COLOR_BGR2GRAY);
325 #endif
326  resizeCropSquare(frame, send, Size(FRAME_WIDTH, FRAME_HEIGHT), INTER_LINEAR);
327  if ((frame.cols != FRAME_WIDTH) || (frame.rows != FRAME_HEIGHT)) {
328  cout << "WARNING: Input frame was resized from " << frame.cols << "x"
329  << frame.rows << " to " << send.cols << "x" << send.rows << endl;
330  }
331  imwrite("testimg.png", frame);
332 
333  assert(send.total() == FRAME_WIDTH * FRAME_HEIGHT);
334  // Ensure that the selection of MTU is a multiple of 8 (Bytes per transaction)
335  assert(PACK_SIZE % 8 == 0);
336 
337 #ifdef SHOW_WINDOWS
338  namedWindow("host_send", CV_WINDOW_NORMAL);
339  imshow("host_send", send);
340 
341 #endif // SHOW_WINDOWS
342 
343  // Ensure that the send Mat is in continuous memory space. Typically, imread or resize
344  // will return such a continuous Mat, but we should check it.
345  assert(send.isContinuous());
346 
347  unsigned int send_total = send.total();
348  unsigned int send_channels = send.channels();
349  unsigned int warptx_cmd_size = warptx_cmd.length();
350 #else // PY_WRAP == PY_WRAP_WARPTRANSFORM_NUMPI
352  unsigned int send_total = (unsigned int)total_size + warptx_cmd_size; //TBC!!!!!!!!!!!!!!!!!
353  unsigned int send_channels = 1; // FIXME: It is ok only for 1-d array, i.e. CV_8UC1
354  unsigned char * sendarr = input_img;
355 #endif // #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
356 
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;
360  assert(total_pack == TOT_TRANSFERS);
361 
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;
365 
366  //unsigned char * longbuf = new unsigned char[PACK_SIZE * total_pack];
367  unsigned char * longbuf = (unsigned char *) malloc (PACK_SIZE * total_pack_rx * sizeof (unsigned char));
368 
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;
380 
381 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
382 
383  //--------------------------------------------------------
384  //-- STEP-4 : RUN WARPTRANSFORM DETECTOR FROM OpenCV LIBRARY (SW)
385  //--------------------------------------------------------
386  clock_t start_cycle_warp_transform_sw = clock();
387  ocv_out_img.create(send.rows, send.cols, INPUT_TYPE_HOST); // create memory for opencv output image
388  ocv_ref(send, ocv_out_img, transformation_matrix);
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;
395 
396  //------------------------------------------------------
397  //-- STEP-5 : RUN WARPTRANSFORM DETECTOR FROM cF (HW)
398  //------------------------------------------------------
399 
400  //------------------------------------------------------
401  //-- STEP-5.1 : Preparation
402  //------------------------------------------------------
403 
404  // Anchor a pointer on cvMat raw data
405  unsigned char * sendarr_img = send.isContinuous()? send.data: send.clone().data;
406  // unsigned char * sendarr = send.isContinuous()? send.data: send.clone().data;
407  // warptx_cmd = warptx_cmd.append(sendarr_img, send_total * send_channels);
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);
411 
412 #endif // !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
413 
414  clock_t start_cycle_warp_transform_hw = clock();
415 
416  //------------------------------------------------------
417  //-- STEP-5.2 : TX Loop
418  //------------------------------------------------------
419  clock_t last_cycle_tx = clock();
420  unsigned int sending_now = PACK_SIZE;
421  for (unsigned int i = 0; i < total_pack; i++) {
422  if ( i == total_pack - 1 ) {
423  sending_now = bytes_in_last_pack;
424  }
425  #if NET_TYPE == udp
426  sock.sendTo( & sendarr[i * PACK_SIZE], sending_now, servAddress, servPort);
427  #else
428  sock.send( & sendarr[i * PACK_SIZE], sending_now);
429  #endif
430  //delay(500);
431  }
432 
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;
438 
439 
440  //------------------------------------------------------
441  //-- STEP-5.3 : RX Loop
442  //------------------------------------------------------
443  clock_t last_cycle_rx = clock();
444  unsigned int receiving_now = PACK_SIZE;
445  cout << "INFO: Expecting length of packs:" << total_pack_rx << " from " << servAddress << ":" << servPort << endl;
446  //unsigned char * longbuf = new unsigned char[PACK_SIZE * total_pack];
447  unsigned int loopi=0;
448  for (unsigned int i = 0; i < send_total; ) {
449  //cout << "DEBUG: i=" << i << ", loopi=" << loopi++ << endl;
450  //if ( i == total_pack - 1 ) {
451  // receiving_now = bytes_in_last_pack;
452  //}
453  #if NET_TYPE == udp
454  recvMsgSize = sock.recvFrom(buffer, BUF_LEN, servAddress, servPort);
455  #else
456  recvMsgSize = sock.recv(buffer, BUF_LEN);
457  #endif
458  if (recvMsgSize != receiving_now) {
459  cerr << "WARNING: at i=" << i << " received unexpected size pack:" << recvMsgSize << ". Expected: " <<
460  receiving_now << endl;
461  //continue;
462  }
463  memcpy( & longbuf[i], buffer, recvMsgSize);
464  //cout << "DEBUG: i=" << i << " recvMsgSize=" << recvMsgSize << endl;
465  i += recvMsgSize;
466  //delay(500);
467  }
468  cout << "INFO: Received packet from " << servAddress << ":" << servPort << endl;
469 
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;
475 
476  clock_t end_cycle_warp_transform_hw = next_cycle_rx;
477 
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;
484 
485 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
486 
487  frame = cv::Mat(FRAME_HEIGHT, FRAME_WIDTH, INPUT_TYPE_HOST, longbuf); // OR vec.data() instead of ptr
488  if (frame.size().width == 0) {
489  cerr << "receive failure!" << endl;
490  continue;
491  }
492 #ifdef SHOW_WINDOWS
493  namedWindow("host_recv", CV_WINDOW_NORMAL);
494  imshow("host_recv", frame);
495 #endif
496 
497  //------------------------------------------------------
498  //-- STEP-6 : Write output files and show in windows
499  //------------------------------------------------------
500 
501  ostringstream oss;
502 // oss << "cFp_Vitis E2E:" << "INFO: Effective FPS HW:" << (1 / duration_warp_transform_hw) <<
503 // " \tkbps:" << (PACK_SIZE * total_pack / duration_warp_transform_hw / 1024 * 8);
504  string windowName = "cFp_Vitis End2End"; //oss.str();
505 
506  //moveWindow(windowName, 0, 0);
507 #ifdef WRITE_OUTPUT_FILE
508  if (num_frame == 1) {
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)
512 
513  if (!strcpy(output_img_str, &out_img_file[0])) {
514  cerr << "ERROR: Cannot write to output image string." << endl;
515  }
516 #endif // defined(PY_WRAP) && (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
517  cout << "INFO: The output image file is stored at : " << out_img_file << endl;
518  // We save the image received from network after being processed by WarpTransform HW or HOST TB
519  imwrite(out_img_file, frame);
520  }
521  else if (num_frame > 1) {
522  // If the frame is empty, break immediately
523  if (frame.empty()) {
524  break;
525  }
526  cout << "INFO: The output video file is stored at : " << out_video_file << endl;
527  Mat tovideo;
528  if (frame.channels() != 1) {
529  tovideo = frame;
530  }
531  else {
532  cvtColor(frame, tovideo, COLOR_GRAY2BGR);
533  }
534  video.write(tovideo);
535  }
536 #endif // WRITE_OUTPUT_FILE
537  waitKey(FRAME_INTERVAL);
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
541  << endl;
542  //delete(longbuf);
543  free (longbuf);
544  } // while loop
545 
546  // When everything done, release the video capture and write object
547  cap.release();
548  video.release();
549 
550  // Closes all the windows
551  destroyAllWindows();
552 
553 #else // !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
554  //output_img = longbuf;
555  memcpy( output_img, longbuf, total_size);
556  free(longbuf);
557 #endif // !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_WARPTRANSFORM_FILENAME)
558 
559  // Destructor closes the socket
560  } catch (SocketException & e) {
561  cerr << e.what() << endl;
562  exit(1);
563  }
564 
565 #ifndef PY_WRAP
566  return 0;
567 #endif
568 }
cat GET request dos socat stdio tcp
Definition: commands.txt:1
void ocv_ref(cv::Mat img_gray, cv::Mat &ocv_out_img, float Th)
Definition: xf_ocv_ref.hpp:552
#define FRAME_INTERVAL
Definition: config.h:48
#define INPUT_TYPE_HOST
Definition: config.h:68
#define FRAME_HEIGHT
Definition: config.h:43
#define FRAME_WIDTH
Definition: config.h:46
#define TOT_TRANSFERS
Definition: config.h:70
#define udp
Definition: config.h:66
#define NET_TYPE
Definition: config.h:60
#define BUF_LEN
Definition: config.h:54
#define PACK_SIZE
Definition: config.h:51
#define TRMAT_DIM2
void print_cFpZoo(void)
string prepareWarpTransformCommand(unsigned int rows, unsigned int cols, unsigned int channels, float *transform_matrix)
void resizeCropSquare(const cv::Mat &input, const cv::Mat &output, const cv::Size &dstSize, int interpolation=INTER_LINEAR)
Resize an image and crop if necessary in order to keep a rectangle area in the middle of the image.
#define TRMAT_DIM1
def send(cFcluster cluster, node_id, data_obj, proto='udp', port=CF_DEFAULT_PORT)
Definition: comm.py:41

◆ prepareWarpTransformCommand()

string prepareWarpTransformCommand ( unsigned int  rows,
unsigned int  cols,
unsigned int  channels,
float *  transform_matrix 
)

Definition at line 103 of file warp_transform_host.cpp.

103  {
104  string out;
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];
109 
110  //init tx and img cmd
111  for (unsigned int k = 0; k < bytes_per_line; k++) {
112  value_cmd[k] = (char)0;
113  if (k != 0) {
114  tx_cmd[k] = (char)0;
115  img_cmd[k] = (char)0;
116  }
117  else {
118  tx_cmd[k] = (char)1;
119  img_cmd[k] = (char)2;
120  }
121  }
122  out = out.append(tx_cmd,bytes_per_line);
123 
124  //dump the even elements of the tx matrix
125  int off = 4;
126  for (int i = 0; i < 8; i++)
127  {
128  memcpy(value_cmd+off, (float*)transform_matrix+i, 4);
129  off += 4;
130  off = off % bytes_per_line;
131  if (i%2 && i!=0)
132  {
133  //dump matrix
134  out = out.append(value_cmd,bytes_per_line);
135  }
136  }
137  //dump last value
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);
142  //creating img mat cmd
143  memcpy(img_cmd+6, (char*)&rows, 2);
144  memcpy(img_cmd+4, (char*)&cols, 2);
145  img_cmd[1]=channels;
146  out = out.append(img_cmd,bytes_per_line);
147  return string(out);
148 }
out
Definition: test.py:12

◆ print_cFpZoo()

void print_cFpZoo ( void  )

Definition at line 66 of file warp_transform_host.cpp.

67 {
68  cout << " " << endl;
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;
78 }

◆ resizeCropSquare()

void resizeCropSquare ( const cv::Mat &  input,
const cv::Mat &  output,
const cv::Size &  dstSize,
int  interpolation = INTER_LINEAR 
)

Resize an image and crop if necessary in order to keep a rectangle area in the middle of the image.

Parameters
[in]inputA pointer to the cv::Mat input image
[out]outputA pointer to the cv::Mat output image
[in]SizeA pointer to the cv::Size of the output image (width, height)
[in]interpolationEnumerator for interpolation algorithm (imgproc.hpp)
Returns
Nothing.

Definition at line 91 of file warp_transform_host.cpp.

92 {
93  int h = input.rows;
94  int w = input.cols;
95  int min_size = min(h, w);
96  int x = w/2-min_size/2;
97  int y = h/2-min_size/2;
98  // printf("w=%d, h=%d, min_size=%d, x=%d, y=%d, width=%d, height=%d\n", w, h, min_size, x, y, width, height);
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);
101 }
string input
Definition: test.py:9
string output
Definition: test.py:10

◆ warp_transformAccelMem()

void warp_transformAccelMem ( membus_t img_inp,
membus_t img_out,
int  rows,
int  cols,
hls::stream< float > &  sTxMatrix 
)

Top-level accelerated function of the WarpTransform Application with memory mapped interfaces.

Returns
Nothing.

Definition at line 222 of file xf_warp_transform_accel.cpp.

228  {
229  // clang-format on
230  #pragma HLS INLINE off
231 
232  xf::cv::Mat<TYPE, HEIGHT, WIDTH, NPIX> imgInput(rows, cols);
233  // clang-format off
234  #pragma HLS stream variable=imgInput.data depth=4
235  // clang-format on
236 
237  #ifndef FAKE_WarpTransform
238  xf::cv::Mat<TYPE, HEIGHT, WIDTH, NPIX> imgOutput(rows, cols);
239  // clang-format off
240  #pragma HLS stream variable=imgOutput.data depth=4
241  // clang-format on
242  #endif
243 
244  // clang-format off
245  #pragma HLS DATAFLOW
246  // clang-format on
247 
248  // Copy transform data from global memory to local memory:
249  static float transform_matrix[TRANSFORM_MATRIX_DIM];
250 
251  //prepare the tx matrix
252  setUpTxMatrixFromStream(transform_matrix,sTxMatrix );
253 
254  // Feed a cv matrix from ddr memory
255  xf::cv::Array2xfMat<MEMDW_512, XF_8UC1, HEIGHT, WIDTH, NPIX>(img_inp, imgInput);
256 
257  #ifdef FAKE_WarpTransform
258  // Feed ddr memory from a cv matrix
259  xf::cv::xfMat2Array<MEMDW_512, XF_8UC1, HEIGHT, WIDTH, NPIX>(imgInput, img_out);
260  #else
261 
262  // Run xfOpenCV kernel:
264  XF_USE_URAM>(imgInput, imgOutput, transform_matrix);
265 
266  // Feed ddr memory from a cv matrix
267  xf::cv::xfMat2Array<MEMDW_512, XF_8UC1, HEIGHT, WIDTH, NPIX>(imgOutput, img_out);
268  #endif
269 
270 
271 }
#define XF_USE_URAM
#define NPC1
#define WIDTH
#define HEIGHT
#define TYPE
#define TRANSFORM_MATRIX_DIM
void setUpTxMatrixFromStream(float transform_matrix[9], hls::stream< float > &sTxMatrix)
#define TRANSFORM_TYPE
#define START_PROC
#define INTERPOLATION
#define NUM_STORE_ROWS
Here is the call graph for this function:
Here is the caller graph for this function:

◆ warpTransformAccelStream()

void warpTransformAccelStream ( hls::stream< ap_uint< 8 >> &  img_in_axi_stream,
hls::stream< ap_uint< 64 >> &  img_out_axi_stream,
int  rows,
int  cols,
float  transform_matrix[9] 
)

Top-level accelerated function of the WarpTransform Application with array I/Fadd WARPTRANSFORM.

Returns
Nothing.

Definition at line 129 of file xf_warp_transform_accel.cpp.

133  {
134  // clang-format on
135  #pragma HLS INLINE off
136 
137  xf::cv::Mat<TYPE, HEIGHT, WIDTH, NPIX> imgInput(rows, cols);
138  // clang-format off
139  #pragma HLS stream variable=imgInput.data depth=2
140  // clang-format on
141 
142  xf::cv::Mat<TYPE, HEIGHT, WIDTH, NPIX> imgOutput(rows, cols);
143  // clang-format off
144  #pragma HLS stream variable=imgOutput.data depth=2
145  // clang-format on
146 
147  // clang-format off
148  #pragma HLS DATAFLOW
149  // clang-format on
150  //FIXME: not static matrix
151  //float transform_matrix[9]={1.5,0,0,0,1.8,0,0,0,0};
152  // float transform_matrix[9]={0.87,-0.5,0,0.5,0.87,0,0,0,1};
153 
154  accel_utils accel_utils_obj;
155 
156  int dstMat_cols_align_npc = ((imgInput.cols + (NPIX - 1)) >> XF_BITSHIFT(NPIX)) << XF_BITSHIFT(NPIX);
157 
158  accel_utils_obj.hlsStrm2xfMat<INPUT_PTR_WIDTH, TYPE, HEIGHT, WIDTH, NPIX, (HEIGHT * WIDTH) / NPIX>(img_in_axi_stream, imgInput, dstMat_cols_align_npc);
159 
160  // Run xfOpenCV kernel:
162  XF_USE_URAM>(imgInput, imgOutput, transform_matrix);
163 
164  int srcMat_cols_align_npc = ((imgOutput.cols + (NPIX - 1)) >> XF_BITSHIFT(NPIX)) << XF_BITSHIFT(NPIX);
165 
166  accel_utils_obj.xfMat2hlsStrm<OUTPUT_PTR_WIDTH, TYPE, HEIGHT, WIDTH, NPIX, HEIGHT*((WIDTH + NPIX - 1) / NPIX)>(imgOutput, img_out_axi_stream,
167  srcMat_cols_align_npc);
168 }
#define NPIX
#define INPUT_PTR_WIDTH
#define OUTPUT_PTR_WIDTH
Here is the caller graph for this function:

◆ warptTransformAccelArray()

void warptTransformAccelArray ( ap_uint< 8 > *  img_in,
float *  transform,
ap_uint< 64 > *  img_out,
int  rows,
int  cols 
)

Top-level accelerated function of the WarptTransform Application with array I/F used only for simulation/TB purposes.

Returns
Nothing.

Definition at line 83 of file xf_warp_transform_accel.cpp.

84  {
85  const int pROWS = HEIGHT;
86  const int pCOLS = WIDTH;
87  const int pNPC1 = NPIX;
88 
89  xf::cv::Mat<XF_8UC1, HEIGHT, WIDTH, NPIX> imgInput(rows, cols);
90 // clang-format off
91  #pragma HLS stream variable=imgInput.data depth=2
92  // clang-format on
93  xf::cv::Mat<XF_8UC1, HEIGHT, WIDTH, NPIX> imgOutput(rows, cols);
94 // clang-format off
95  #pragma HLS stream variable=imgOutput.data depth=2
96 // clang-format on
97 
98 // clang-format off
99  #pragma HLS DATAFLOW
100  // clang-format on
101 
102  // Copy transform data from global memory to local memory:
103  float transform_matrix[9];
104 
105  for (unsigned int i = 0; i < 9; ++i) {
106 // clang-format off
107  #pragma HLS PIPELINE
108  // clang-format on
109  transform_matrix[i] = transform[i];
110  }
111 
112  xf::cv::Array2xfMat<INPUT_PTR_WIDTH, TYPE, HEIGHT, WIDTH, NPC1>(img_in, imgInput);
114  XF_USE_URAM>(imgInput, imgOutput, transform_matrix);
115  xf::cv::xfMat2Array<OUTPUT_PTR_WIDTH, XF_8UC1, HEIGHT, WIDTH, NPIX>(imgOutput, img_out);
116 }
Here is the caller graph for this function: