39 #include "../../../../../PracticalSockets/src/PracticalSockets.h"  
   40 #include "../include/config.h" 
   42 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_HARRIS_FILENAME) 
   43 #include "opencv2/opencv.hpp" 
   44 #include "../../../../../../ROLE/vision/hls/harris/include/xf_ocv_ref.hpp"   
   51 void delay(
unsigned int mseconds)
 
   53     clock_t goal = mseconds + 
clock();
 
   54     while (goal > 
clock());
 
   60     cout <<  
"...build with:                                            " << endl;
 
   61     cout <<  
" ██████╗███████╗██████╗    ███████╗ ██████╗  ██████╗      " << endl;
 
   62     cout <<  
"██╔════╝██╔════╝██╔══██╗   ╚══███╔╝██╔═══██╗██╔═══██╗     " << endl;
 
   63     cout <<  
"██║     █████╗  ██████╔╝     ███╔╝ ██║   ██║██║   ██║     " << endl;
 
   64     cout <<  
"██║     ██╔══╝  ██╔═══╝     ███╔╝  ██║   ██║██║   ██║     " << endl;
 
   65     cout <<  
"╚██████╗██║     ██║███████╗███████╗╚██████╔╝╚██████╔╝     " << endl;
 
   66     cout <<  
" ╚═════╝╚═╝     ╚═╝╚══════╝╚══════╝ ╚═════╝  ╚═════╝      " << endl;
 
   67     cout <<  
"A cloudFPGA project from IBM ZRL                    v1.0  " << endl;
 
   68     cout <<  
"Quantitative Finance Monte-Carlo European Pricing Engine  " << endl;
 
   86     int min_size = min(
h, w);
 
   87     int x = w/2-min_size/2;
 
   88     int y = 
h/2-min_size/2;
 
   90     cv::Mat crop_img = 
input(Rect(
x, 
y, min_size, min_size));
 
   91     resize(crop_img, 
output, Size(dstSize.width, dstSize.height), 0, 0, interpolation);
 
   94 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_HARRIS_FILENAME) 
  104                vector<Point>& hw_points) 
 
  107    for (
int j = 0; j < imgOutput.rows; j++) {
 
  108       for (
int i = 0; i < (imgOutput.cols); i++) {
 
  110           unsigned char pix = imgOutput.at<
unsigned char>(j,i);  
 
  115               if ((
tmp.x < in_img.cols) && (
tmp.y < in_img.rows) && (j > 0)) {
 
  116           hw_points.push_back(
tmp);
 
  121               if (j > 0) circle(out_img, Point(
x, 
y), 2, Scalar(0, 0, 255, 255), 1, 8, 0);
 
  130 #if PY_WRAP == PY_WRAP_HARRIS_FILENAME 
  131 void harris(
char *s_servAddress, 
char *s_servPort, 
char *input_str, 
char *output_img_str, 
char *output_points_str)
 
  132 #elif PY_WRAP == PY_WRAP_HARRIS_NUMPI 
  133 void harris(
int total_size, 
unsigned char *input_img, 
int total_size2, 
unsigned char *output_img, 
char *s_servAddress, 
char *s_servPort)
 
  141 int main(int argc, char * argv[]) {
 
  142     if ((argc < 3) || (argc > 4)) { 
 
  143         cerr << 
"Usage: " << argv[0] << 
" <Server> <Server Port> <optional input image>\n";
 
  153     assert ((argc == 3) || (argc == 4));
 
  154     string s_servAddress = argv[1]; 
 
  155     char *s_servPort = argv[2];
 
  158     string servAddress = s_servAddress;
 
  159     unsigned short servPort;
 
  161     if (net_type == 
udp) {
 
  162     servPort = Socket::resolveService(s_servPort, 
"udp");
 
  164     else if (net_type == 
tcp) {
 
  165     servPort = atoi(s_servPort);
 
  168     cout << 
"ERROR: Invalid type of socket type provided: " << net_type  << 
" Choosed one of (tcp=0 or udp=1)" << endl;
 
  172     unsigned int recvMsgSize; 
 
  174 #ifdef INPUT_FROM_CAMERA 
  177 #if PY_WRAP == PY_WRAP_HARRIS_FILENAME 
  178     input_num = atoi(input_str);
 
  179     input_string = 
"./cam"+to_string(input_num);
 
  185     else if (argc == 4) {
 
  186     input_num = atoi(argv[3]);
 
  188     input_string = 
"./cam"+to_string(input_num);
 
  192 #if PY_WRAP == PY_WRAP_HARRIS_FILENAME 
  193     input_string.assign(input_str);
 
  198         input_string.assign(
"../../../../../../ROLE/vision/hls/harris/test/8x8.png");
 
  200     else if (argc == 4) {
 
  201         input_string.assign(argv[3]);
 
  207 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_HARRIS_FILENAME) 
  216         Th = 41151168289701888.000000;
 
  218     string out_img_file, out_points_file;
 
  219     string out_video_file, out_video_points_file;
 
  224     out_video_file.assign(input_string);
 
  225     out_video_file += 
"_fpga_video_out.avi";
 
  226     out_video_points_file.assign(input_string);
 
  227     out_video_points_file += 
"_fpga_video_points_out.avi";
 
  229 #if CV_MAJOR_VERSION < 4 
  234     VideoWriter videop(out_video_points_file,cv::VideoWriter::fourcc(
'M',
'J',
'P',
'G'),10, Size(
FRAME_WIDTH,
FRAME_HEIGHT));
 
  247         #ifndef TB_SIM_CFP_VITIS 
  248         UDPSocket sock(servPort); 
 
  254     TCPSocket sock(servAddress, servPort);
 
  258 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_HARRIS_FILENAME) 
  265         vector < uchar > encoded;
 
  267     #ifdef INPUT_FROM_CAMERA 
  269         VideoCapture 
cap(input_num); 
 
  270         if (!
cap.isOpened()) {
 
  271             cerr << 
"OpenCV Failed to open camera " + input_num << endl;
 
  277     VideoCapture 
cap(input_string); 
 
  278         if (!
cap.isOpened()) {
 
  279             cerr << 
"OpenCV Failed to open file " + input_string << endl;
 
  289             clock_t start_cycle_main = 
clock();
 
  291             if (
frame.empty()) 
break; 
 
  292             if(
frame.size().width==0) 
continue; 
 
  293             cout << 
" ___________________________________________________________________ " << endl;
 
  294             cout << 
"/                                                                   \\" << endl;
 
  295         cout << 
"INFO: Frame # " << ++
num_frame << endl;
 
  296 #if CV_MAJOR_VERSION < 4 
  303             cout << 
"WARNING: Input frame was resized from " << 
frame.cols << 
"x"  
  304         << 
frame.rows << 
" to " << 
send.cols << 
"x" << 
send.rows << endl;
 
  312         namedWindow(
"host_send", CV_WINDOW_NORMAL);
 
  313             imshow(
"host_send", 
send);
 
  319         assert(
send.isContinuous());
 
  321         unsigned int send_total = 
send.total();
 
  322         unsigned int send_channels = 
send.channels();
 
  327         unsigned int send_channels = 1; 
 
  328         unsigned char * sendarr = input_img;
 
  333             unsigned int total_pack  = 1 + (send_total * send_channels - 1) / 
PACK_SIZE;
 
  334             unsigned int total_bytes = total_pack * 
PACK_SIZE;
 
  335             unsigned int bytes_in_last_pack = send_total * send_channels - (total_pack - 1) * 
PACK_SIZE;
 
  338             cout << 
"INFO: FPGA destination : " << servAddress << 
":" << servPort << endl;
 
  339         cout << 
"INFO: Network socket   : " << ((
NET_TYPE == 
tcp) ? 
"TCP" : 
"UDP") << endl;
 
  340         cout << 
"INFO: Total packets to send/receive = " << total_pack << endl;
 
  341             cout << 
"INFO: Total bytes to send/receive   = " << send_total * send_channels << endl;
 
  342         cout << 
"INFO: Total bytes in " << total_pack << 
" packets = "  << total_bytes << endl;
 
  343         cout << 
"INFO: Bytes in last packet          = " << bytes_in_last_pack << endl;
 
  344         cout << 
"INFO: Packet size (custom MTU)      = " << 
PACK_SIZE << endl;
 
  346 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_HARRIS_FILENAME)     
  351             clock_t start_cycle_harris_sw = 
clock();
 
  354         clock_t end_cycle_harris_sw = 
clock();
 
  355         double duration_harris_sw = (end_cycle_harris_sw - start_cycle_harris_sw) / 
 
  356                                     (
double) CLOCKS_PER_SEC;
 
  357         cout << 
"INFO: SW exec. time:" << duration_harris_sw << 
" seconds" << endl;
 
  358             cout << 
"INFO: Effective FPS SW:" << (1 / duration_harris_sw) << 
" \tkbps:" << 
 
  359                     (
PACK_SIZE * total_pack / duration_harris_sw / 1024 * 8) << endl;
 
  370             unsigned char * sendarr = 
send.isContinuous()? 
send.data: 
send.clone().data;
 
  372             clock_t start_cycle_harris_hw = 
clock();
 
  380             clock_t last_cycle_tx = 
clock();
 
  382             for (
unsigned int i = 0; i < total_pack; i++) {
 
  383                 if ( i == total_pack - 1 ) {
 
  384                     sending_now = bytes_in_last_pack;
 
  387         sock.sendTo( & sendarr[i * 
PACK_SIZE], sending_now, servAddress, servPort);
 
  389         sock.send( & sendarr[i * 
PACK_SIZE], sending_now);
 
  394             clock_t next_cycle_tx = 
clock();
 
  395             double duration_tx = (next_cycle_tx - last_cycle_tx) / (
double) CLOCKS_PER_SEC;
 
  396             cout << 
"INFO: Effective FPS TX:" << (1 / duration_tx) << 
" \tkbps:" << (
PACK_SIZE * 
 
  397                  total_pack / duration_tx / 1024 * 8) << endl;
 
  398             last_cycle_tx = next_cycle_tx;
 
  404 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_HARRIS_FILENAME) 
  405         clock_t last_cycle_rx = 
clock();
 
  408             cout << 
"INFO: Expecting length of packs:" << total_pack << 
" from " <<  servAddress << 
":" << servPort << endl;
 
  409             unsigned char * longbuf = 
new unsigned char[
PACK_SIZE * total_pack];
 
  410             for (
unsigned int i = 0; i < send_total; ) {
 
  416                 recvMsgSize = sock.recvFrom(buffer, 
BUF_LEN, servAddress, servPort);
 
  418         recvMsgSize = sock.recv(buffer, 
BUF_LEN);
 
  420         if (recvMsgSize != receiving_now) {
 
  421                     cerr << 
"WARNING: at i=" << i << 
" received unexpected size pack:" << recvMsgSize << 
". Expected: " << 
 
  422                             receiving_now << endl;
 
  425                 memcpy( & longbuf[i], buffer, recvMsgSize);
 
  430             cout << 
"INFO: Received packet from " << servAddress << 
":" << servPort << endl;
 
  432 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_HARRIS_FILENAME) 
  435         if (
frame.size().width == 0) {
 
  436                 cerr << 
"receive failure!" << endl;
 
  441             namedWindow(
"host_recv", CV_WINDOW_NORMAL);
 
  442             imshow(
"host_recv", 
frame);
 
  444             clock_t next_cycle_rx = 
clock();
 
  445             double duration_rx = (next_cycle_rx - last_cycle_rx) / (
double) CLOCKS_PER_SEC;
 
  446             cout << 
"INFO: Effective FPS RX:" << (1 / duration_rx) << 
" \tkbps:" << (
PACK_SIZE * 
 
  447                     total_pack / duration_rx / 1024 * 8) << endl;
 
  448             last_cycle_rx = next_cycle_rx;
 
  450         clock_t end_cycle_harris_hw = next_cycle_rx;
 
  452         double duration_harris_hw = (end_cycle_harris_hw - start_cycle_harris_hw) / 
 
  453                                     (
double) CLOCKS_PER_SEC;
 
  454         cout << 
"INFO: HW exec. time:" << duration_harris_hw << 
" seconds" << endl;
 
  455             cout << 
"INFO: Effective FPS HW:" << (1 / duration_harris_hw) << 
" \tkbps:" << 
 
  456                     (
PACK_SIZE * total_pack / duration_harris_hw / 1024 * 8) << endl;       
 
  462             out_img = 
send.clone();
 
  463             vector<Point> hw_points;
 
  469             oss << 
"cFp_Vitis E2E:" << 
"INFO: Effective FPS HW:" << (1 / duration_harris_hw) << 
 
  470                    " \tkbps:" << (
PACK_SIZE * total_pack / duration_harris_hw / 1024 * 8);
 
  471         string windowName = 
"cFp_Vitis End2End"; 
 
  487         namedWindow(windowName, CV_WINDOW_NORMAL);
 
  488         imshow(windowName, out_img);
 
  491 #ifdef WRITE_OUTPUT_FILE 
  493           out_img_file.assign(input_string);
 
  494           out_img_file += 
"_fpga_img_out_frame_" + to_string(
num_frame) + 
".png";
 
  495           out_points_file.assign(input_string);
 
  496           out_points_file += 
"_fpga_points_out_frame_" + to_string(
num_frame) + 
".png";
 
  497 #if defined(PY_WRAP) && (PY_WRAP == PY_WRAP_HARRIS_FILENAME) 
  499           if (!strcpy(output_img_str, &out_img_file[0])) {
 
  500           cerr << 
"ERROR: Cannot write to output image string." << endl;
 
  502           if (!strcpy(output_points_str, &out_points_file[0])) {
 
  503           cerr << 
"ERROR: Cannot write to output points string." << endl;
 
  506           cout << 
"INFO: The output image file is stored at  : " << out_img_file << endl; 
 
  507           cout << 
"INFO: The output points file is stored at : " << out_points_file << endl; 
 
  509           imwrite(out_img_file, out_img);
 
  510           imwrite(out_points_file, 
frame);
 
  517           cout << 
"INFO: The output video file is stored at  : " << out_video_file << endl;
 
  518           cout << 
"INFO: The output video -only points- file is stored at  : " << out_video_points_file << endl;
 
  519           Mat tovideo, tovideop;
 
  520           if (
frame.channels() != 1) {
 
  525         cvtColor(out_img, tovideo, COLOR_GRAY2BGR);
 
  526         cvtColor(
frame, tovideop, COLOR_GRAY2BGR);        
 
  528           video.write(tovideo);
 
  529           videop.write(tovideop);
 
  533             double duration_main = (
clock() - start_cycle_main) / (
double) CLOCKS_PER_SEC;
 
  534             cout << 
"INFO: Effective FPS E2E:" << (1 / duration_main) << endl;
 
  535             cout << 
"\\___________________________________________________________________/" << endl
 
  554     } 
catch (SocketException & e) {
 
  555         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)
void harris(ap_uint< 32 > *pi_rank, ap_uint< 32 > *pi_size, stream< NetworkWord > &siSHL_This_Data, stream< NetworkWord > &soTHIS_Shl_Data, stream< NetworkMetaStream > &siNrc_meta, stream< NetworkMetaStream > &soNrc_meta, ap_uint< 32 > *po_rx_ports)
Main process of the Harris Application directives.
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.
void delay(unsigned int mseconds)
void markPointsOnImage(Mat &imgOutput, Mat &in_img, Mat &out_img, vector< Point > &hw_points)
Mark the points found by Harris into the image.
def send(cFcluster cluster, node_id, data_obj, proto='udp', port=CF_DEFAULT_PORT)