39 #include "PracticalSockets.h"
43 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_SOBEL_FILENAME) || (PY_WRAP == PY_WRAP_SOBEL_NUMPI)
44 #include "opencv2/opencv.hpp"
45 #include "../../../../../../ROLE/vision/hls/sobel/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);
97 #if PY_WRAP == PY_WRAP_SOBEL_FILENAME
98 void sobel(
char *s_servAddress,
char *s_servPort,
char *input_str,
char *output_img_str,
char *output_points_str)
99 #elif PY_WRAP == PY_WRAP_SOBEL_NUMPI
100 void sobel(
int total_size,
unsigned char *input_img,
int total_size2,
unsigned char *output_img,
char *s_servAddress,
char *s_servPort)
108 int main(int argc, char * argv[]) {
109 if ((argc < 3) || (argc > 4)) {
110 cerr <<
"Usage: " << argv[0] <<
" <Server> <Server Port> <optional input image>\n";
120 assert ((argc == 3) || (argc == 4));
121 string s_servAddress = argv[1];
122 char *s_servPort = argv[2];
125 string servAddress = s_servAddress;
126 unsigned short servPort;
128 if (net_type ==
udp) {
129 servPort = Socket::resolveService(s_servPort,
"udp");
131 else if (net_type ==
tcp) {
132 servPort = atoi(s_servPort);
135 cout <<
"ERROR: Invalid type of socket type provided: " << net_type <<
" Choosed one of (tcp=0 or udp=1)" << endl;
139 unsigned int recvMsgSize;
141 #ifdef INPUT_FROM_CAMERA
144 #if PY_WRAP == PY_WRAP_SOBEL_FILENAME
145 input_num = atoi(input_str);
146 input_string =
"./cam"+to_string(input_num);
152 else if (argc == 4) {
153 input_num = atoi(argv[3]);
155 input_string =
"./cam"+to_string(input_num);
159 #if PY_WRAP == PY_WRAP_SOBEL_FILENAME
160 input_string.assign(input_str);
165 input_string.assign(
"../../../../../../ROLE/vision/hls/sobel/test/8x8.png");
167 else if (argc == 4) {
168 input_string.assign(argv[3]);
174 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_SOBEL_FILENAME)
179 string out_video_file;
184 out_video_file.assign(input_string);
185 out_video_file +=
"_fpga_video_out.avi";
187 #if CV_MAJOR_VERSION < 4
203 #ifndef TB_SIM_CFP_VITIS
204 UDPSocket sock(servPort);
210 TCPSocket sock(servAddress, servPort);
214 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_SOBEL_FILENAME)
221 vector < uchar > encoded;
223 #ifdef INPUT_FROM_CAMERA
225 VideoCapture
cap(input_num);
226 if (!
cap.isOpened()) {
227 cerr <<
"OpenCV Failed to open camera " + input_num << endl;
233 VideoCapture
cap(input_string);
234 if (!
cap.isOpened()) {
235 cerr <<
"OpenCV Failed to open file " + input_string << endl;
245 clock_t start_cycle_main =
clock();
247 if (
frame.empty())
break;
248 if(
frame.size().width==0)
continue;
249 cout <<
" ___________________________________________________________________ " << endl;
250 cout <<
"/ \\" << endl;
251 cout <<
"INFO: Frame # " << ++
num_frame << endl;
252 #if CV_MAJOR_VERSION < 4
259 cout <<
"WARNING: Input frame was resized from " <<
frame.cols <<
"x"
260 <<
frame.rows <<
" to " <<
send.cols <<
"x" <<
send.rows << endl;
267 namedWindow(
"host_send", CV_WINDOW_NORMAL);
268 imshow(
"host_send",
send);
274 assert(
send.isContinuous());
276 unsigned int send_total =
send.total();
277 unsigned int send_channels =
send.channels();
282 unsigned int send_channels = 1;
283 unsigned char * sendarr = input_img;
286 unsigned int total_pack = 1 + (send_total * send_channels - 1) /
PACK_SIZE;
287 unsigned int total_bytes = total_pack *
PACK_SIZE;
288 unsigned int bytes_in_last_pack = send_total * send_channels - (total_pack - 1) *
PACK_SIZE;
292 unsigned char * longbuf = (
unsigned char *) malloc (
PACK_SIZE * total_pack *
sizeof (
unsigned char));
302 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_SOBEL_FILENAME)
307 clock_t start_cycle_sobel_sw =
clock();
310 clock_t end_cycle_sobel_sw =
clock();
311 double duration_sobel_sw = (end_cycle_sobel_sw - start_cycle_sobel_sw) /
312 (
double) CLOCKS_PER_SEC;
326 unsigned char * sendarr =
send.isContinuous()?
send.data:
send.clone().data;
331 clock_t start_cycle_sobel_hw =
clock();
336 clock_t last_cycle_tx =
clock();
338 for (
unsigned int i = 0; i < total_pack; i++) {
339 if ( i == total_pack - 1 ) {
340 sending_now = bytes_in_last_pack;
343 sock.sendTo( & sendarr[i *
PACK_SIZE], sending_now, servAddress, servPort);
345 sock.send( & sendarr[i *
PACK_SIZE], sending_now);
350 clock_t next_cycle_tx =
clock();
351 double duration_tx = (next_cycle_tx - last_cycle_tx) / (
double) CLOCKS_PER_SEC;
354 last_cycle_tx = next_cycle_tx;
360 clock_t last_cycle_rx =
clock();
364 unsigned int loopi=0;
365 for (
unsigned int i = 0; i < send_total; ) {
371 recvMsgSize = sock.recvFrom(buffer,
BUF_LEN, servAddress, servPort);
373 recvMsgSize = sock.recv(buffer,
BUF_LEN);
375 if (recvMsgSize != receiving_now) {
376 cerr <<
"WARNING: at i=" << i <<
" received unexpected size pack:" << recvMsgSize <<
". Expected: " <<
377 receiving_now << endl;
380 memcpy( & longbuf[i], buffer, recvMsgSize);
387 clock_t next_cycle_rx =
clock();
388 double duration_rx = (next_cycle_rx - last_cycle_rx) / (
double) CLOCKS_PER_SEC;
391 last_cycle_rx = next_cycle_rx;
393 clock_t end_cycle_sobel_hw = next_cycle_rx;
395 double duration_sobel_hw = (end_cycle_sobel_hw - start_cycle_sobel_hw) /
396 (
double) CLOCKS_PER_SEC;
401 #if !defined(PY_WRAP) || (PY_WRAP == PY_WRAP_SOBEL_FILENAME)
404 if (
frame.size().width == 0) {
405 cerr <<
"receive failure!" << endl;
409 namedWindow(
"host_recv", CV_WINDOW_NORMAL);
410 imshow(
"host_recv",
frame);
420 string windowName =
"cFp_Vitis End2End";
423 #ifdef WRITE_OUTPUT_FILE
425 out_img_file.assign(input_string);
426 out_img_file +=
"_fpga_image_out_frame_" + to_string(
num_frame) +
".png";
427 #if defined(PY_WRAP) && (PY_WRAP == PY_WRAP_SOBEL_FILENAME)
429 if (!strcpy(output_img_str, &out_img_file[0])) {
430 cerr <<
"ERROR: Cannot write to output image string." << endl;
433 cout <<
"INFO: The output image file is stored at : " << out_img_file << endl;
435 imwrite(out_img_file,
frame);
442 cout <<
"INFO: The output video file is stored at : " << out_video_file << endl;
444 if (
frame.channels() != 1) {
448 cvtColor(
frame, tovideo, COLOR_GRAY2BGR);
450 video.write(tovideo);
454 double duration_main = (
clock() - start_cycle_main) / (
double) CLOCKS_PER_SEC;
476 }
catch (SocketException & e) {
477 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 sobel(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, stream< DmCmd > &soMemWrCmdP0, stream< DmSts > &siMemWrStsP0, stream< Axis< 512 > > &soMemWriteP0, membus_t *lcl_mem0, membus_t *lcl_mem1)
Main process of the Sobel 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)
def send(cFcluster cluster, node_id, data_obj, proto='udp', port=CF_DEFAULT_PORT)
Common utility functions headers, i.e. timing, printing wrappers.