main.cc 27.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
/*
-- (c) Copyright 2018 Xilinx, Inc. All rights reserved.
--
-- This file contains confidential and proprietary information
-- of Xilinx, Inc. and is protected under U.S. and
-- international copyright and other intellectual property
-- laws.
--
-- DISCLAIMER
-- This disclaimer is not a license and does not grant any
-- rights to the materials distributed herewith. Except as
-- otherwise provided in a valid license issued to you by
-- Xilinx, and to the maximum extent permitted by applicable
-- law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND
-- WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES
-- AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
-- BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-
-- INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and
-- (2) Xilinx shall not be liable (whether in contract or tort,
-- including negligence, or under any other theory of
-- liability) for any loss or damage of any kind or nature
-- related to, arising under or in connection with these
-- materials, including for any direct, or any indirect,
-- special, incidental, or consequential loss or damage
-- (including loss of data, profits, goodwill, or any type of
-- loss or damage suffered as a result of any action brought
-- by a third party) even if such damage or loss was
-- reasonably foreseeable or Xilinx had been advised of the
-- possibility of the same.
--
-- CRITICAL APPLICATIONS
-- Xilinx products are not designed or intended to be fail-
-- safe, or for use in any application requiring fail-safe
-- performance, such as life-support or safety devices or
-- systems, Class III medical devices, nuclear facilities,
-- applications related to the deployment of airbags, or any
-- other applications that could lead to death, personal
-- injury, or severe property or environmental damage
-- (individually and collectively, "Critical
-- Applications"). Customer assumes the sole risk and
-- liability of any use of Xilinx products in Critical
-- Applications, subject only to applicable laws and
-- regulations governing limitations on product liability.
--
-- THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS
-- PART OF THIS FILE AT ALL TIMES.
*/

#include <algorithm>
#include <vector>
#include <atomic>
#include <queue>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <chrono>
#include <mutex>
#include <zconf.h>
#include <thread>
#include <sys/stat.h>
#include <dirent.h>

#include <dnndk/dnndk.h>

#include "utils.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

#define Port_host 12300
#define Port_dcam 12400
#define host_addr "192.168.2.1"
#define BUF_SIZE 1024


using namespace std;
using namespace cv;
using namespace std::chrono;


#define INPUT_NODE "layer0_conv"

chrono::system_clock::time_point start_time;

int Mutexnum = 0, Mutexdget = 0, Yolonum = 0;
int index_yolo = 0, index_co = 1; // file index
VideoCapture cam(cv::CAP_V4L);
int Sockfd_host, Sockfd_dcam, newSock_host, newSock_dcam; // socket


typedef pair<int, Mat> imagePair;
class paircomp {
public:
    bool operator()(const imagePair& n1, const imagePair& n2) const {
        if (n1.first == n2.first) {
            return (n1.first > n2.first);
        }

        return n1.first > n2.first;
    }
};

// 3d yolo result struct
typedef struct {
    char number[10];
    float x;
    float y;
    float w;
    float l;
    float yaw;
    float conf;
    float cls_conf;
    float cls_pred;
} dinfo;

// socket function
void send_result() {
    static int ind = 0;
    int now_size = 0, read_size;
    char file_name[256], msg_buf[BUF_SIZE], sync = '\0';
    FILE* image;
    unsigned int fsize;

    sprintf(file_name, "img_%03d_result.jpg", ind); // save file name

    image = fopen(file_name, "w");

    // recv file size
    if ((fsize = read(Sockfd_host, (char*)&msg_buf, sizeof(msg_buf) - 1)) < 0) {
        perror("read");
        exit(1);
    }
    msg_buf[fsize] = '\0';
    fsize = atoi(msg_buf);
    printf("%d\n", fsize);

    // recv image file
    while (1) {
        memset(msg_buf, 0, BUF_SIZE);
        read_size = read(Sockfd_host, (char*)&msg_buf, BUF_SIZE); // get image from socket
        fwrite(msg_buf, 1, read_size, image); // write image
        now_size += read_size;
        if (now_size == fsize)
            break;
        else if (now_size > fsize) {
            printf("file error\n");
            exit(1);
        }
        write(Sockfd_host, (char*)&sync, sizeof(sync)); // for sync
    }

    printf("Receive done : %s\n", file_name);

    if (write(Sockfd_host, (char*)&file_name, sizeof(file_name)) < 0) { // 종료 전송 
        perror("write");
        exit(1);
    }

    fclose(image);
    ind++;
    if (ind == 999) ind = 0; // max 1000 file
}

// input frames queue
queue<pair<int, Mat>> queueInput;

/**
 * @brief Feed input frame into DPU for process
 *
 * @param task - pointer to DPU Task for YOLO-v3 network
 * @param frame - pointer to input frame
 * @param mean - mean value for YOLO-v3 network
 *
 * @return none
 */
void setInputImageForYOLO(DPUTask* task, const Mat& frame, float* mean) {
    Mat img_copy;
    int height = dpuGetInputTensorHeight(task, INPUT_NODE);
    int width = dpuGetInputTensorWidth(task, INPUT_NODE);
    int size = dpuGetInputTensorSize(task, INPUT_NODE);
    int8_t* data = dpuGetInputTensorAddress(task, INPUT_NODE);

    image img_new = load_image_cv(frame);
    image img_yolo = letterbox_image(img_new, width, height);

    vector<float> bb(size);
    for (int b = 0; b < height; ++b) {
        for (int c = 0; c < width; ++c) {
            for (int a = 0; a < 3; ++a) {
                bb[b * width * 3 + c * 3 + a] = img_yolo.data[a * height * width + b * width + c];
            }
        }
    }

    float scale = dpuGetInputTensorScale(task, INPUT_NODE);

    for (int i = 0; i < size; ++i) {
        data[i] = int(bb.data()[i] * scale);
        if (data[i] < 0) data[i] = 127;
    }

    free_image(img_new);
    free_image(img_yolo);
}

void co_detect(DPUTask* task, Mat& frame1, int sWidth, int sHeight, VideoCapture& cam) {  // in postProcess function

    /*output nodes of YOLO-v3 */
    const vector<string> outputs_node = { "layer81_conv", "layer93_conv", "layer105_conv" };

    vector<vector<float>> boxes;
    for (size_t i = 0; i < outputs_node.size(); i++) {
        string output_node = outputs_node[i];
        int channel = dpuGetOutputTensorChannel(task, output_node.c_str());
        int width = dpuGetOutputTensorWidth(task, output_node.c_str());
        int height = dpuGetOutputTensorHeight(task, output_node.c_str());

        int sizeOut = dpuGetOutputTensorSize(task, output_node.c_str());
        int8_t* dpuOut = dpuGetOutputTensorAddress(task, output_node.c_str());
        float scale = dpuGetOutputTensorScale(task, output_node.c_str());
        vector<float> result(sizeOut);
        boxes.reserve(sizeOut);

        /* Store every output node results */
        get_output(dpuOut, sizeOut, scale, channel, height, width, result);

        /* Store the object detection frames as coordinate information  */
        detect(boxes, result, channel, height, width, i, sHeight, sWidth);
    }

    /* Restore the correct coordinate frame of the original image */
    correct_region_boxes(boxes, boxes.size(), frame1.cols, frame1.rows, sWidth, sHeight);

    /* Apply the computation for NMS */
    cout << "boxes size: " << boxes.size() << endl;
    vector<vector<float>> res = applyNMS(boxes, classificationCnt, NMS_THRESHOLD);

    float h = frame1.rows;
    float w = frame1.cols;
    Mat frame2;
    for (int i = 0; i < 3; i++)
        cam.grab();
    cam.read(frame2);

    // for socket variable
    char matchinfo[1024] = "", match_inframe_info[1024], match_n[10];
    int hostAddrlen, countnum;
    struct sockaddr_in hostAddr;
    hostAddrlen = sizeof(hostAddr);
    char tmpinfo[1024], count[5];
    char* receive_info = new char[1024];
    dinfo depthinfo;

    // read 3dyolo result from udp socket
    while (Mutexdget == 1)
        usleep(10);
    Mutexdget = 1; // lock
    // get result count in image
    if (recvfrom(Sockfd_dcam, &count, sizeof(count), 0, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrlen) < 0) {
        perror("recvfrom");
        exit(1);
    } // receive depth camera info
    countnum = atoi(count);
    for (int i = 0; i < countnum; i++) {
        if (recvfrom(Sockfd_dcam, &depthinfo, sizeof(depthinfo), 0, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrlen) < 0) {
            perror("recvfrom");
            exit(1);
        } // receive depth camera info
        sprintf(receive_info, "%s %f %f %f %f %f %f %f %f \n", depthinfo.number, depthinfo.x, depthinfo.y, depthinfo.w, depthinfo.l, depthinfo.yaw, depthinfo.conf, depthinfo.cls_conf, depthinfo.cls_pred);
        strcat(tmpinfo, receive_info);
    }
    Mutexdget = 0; // unlock

    for (size_t i = 0; i < res.size(); ++i) {
        float xmin = (res[i][0] - res[i][2] / 2.0) * w + 1.0;
        float ymin = (res[i][1] - res[i][3] / 2.0) * h + 1.0;
        float xmax = (res[i][0] + res[i][2] / 2.0) * w + 1.0;
        float ymax = (res[i][1] + res[i][3] / 2.0) * h + 1.0;

        //cout<<xmin<<" "<<ymin<<" "<<xmax<<" "<<ymax<<endl;

        Mat compare_part = frame1(Range((2 * ymin + 1 * ymax) / 3, (1 * ymin + 2 * ymax) / 3), Range((2 * xmin + 1 * xmax) / 3, (1 * xmin + 2 * xmax) / 3));

        unsigned nextx, nexty;
        double min = 100000000, error;
        for (int x = (2 * xmin + 1 * xmax) / 3; x < (1 * xmin + 2 * xmax) / 3; x += 2) {
            if (((xmax - xmin) > h / 2) || ((ymax - ymin) > w / 2))
                break; // too large object reject
            for (int y = (2 * ymin + 1 * ymax) / 3; y < (1 * ymin + 2 * ymax) / 3; y += 2) {
                error = 0;
                for (int k = -(int)(xmax - xmin) / 6; k < (int)(xmax - xmin) / 6; k++) {
                    for (int u = -(int)(ymax - ymin) / 6; u < (int)(ymax - ymin) / 6; u++) {
                        uchar b1 = compare_part.ptr<uchar>(u + (int)(ymax - ymin) / 6)[(k + (int)(xmax - xmin) / 6) * 3 + 0];
                        uchar g1 = compare_part.ptr<uchar>(u + (int)(ymax - ymin) / 6)[(k + (int)(xmax - xmin) / 6) * 3 + 1];
                        uchar r1 = compare_part.ptr<uchar>(u + (int)(ymax - ymin) / 6)[(k + (int)(xmax - xmin) / 6) * 3 + 2];
                        int yu = y + u;
                        int xk = x + k;
                        if (y + u < 0 || x + k < 0 || y + u > w || x + k > h) { // for padding
                            if (y + u < 0) yu = 0;
                            if (x + k < 0) xk = 0;
                            if (y + u > w) yu = w;
                            if (x + k > h) xk = h;
                        }
                        uchar b2 = frame2.ptr<uchar>(yu)[(xk) * 3 + 0];
                        uchar g2 = frame2.ptr<uchar>(yu)[(xk) * 3 + 1];
                        uchar r2 = frame2.ptr<uchar>(yu)[(xk) * 3 + 2];
                        error += (pow(b1 - b2, 2) + pow(g1 - g2, 2) + pow(r1 - r2, 2));
                    }
                }
                if (min > error) {
                    min = error;
                    nextx = x;
                    nexty = y;
                }
            }
        }
        // create result string
        sprintf(match_n, "MC%d-%d", index_co, i);
        sprintf(match_inframe_info, "%s %f %f %f %f\n", match_n, nextx - (xmax - xmin) / 2, nexty - (ymax - ymin) / 2, nextx + (xmax - xmin) / 2, nexty + (ymax - ymin) / 2);
        strcat(matchinfo, match_inframe_info);

        rectangle(frame2, Rect(Point((2 * xmin + 1 * xmax) / 3, (2 * ymin + 1 * ymax) / 3), Point((1 * xmin + 2 * xmax) / 3, (1 * ymin + 2 * ymax) / 3)), (0, 255, 0), 1);
        rectangle(frame2, Rect(Point(nextx - (xmax - xmin) / 2, nexty - (ymax - ymin) / 2), Point(nextx + (xmax - xmin) / 2, nexty + (ymax - ymin) / 2)), (0, 0, 255), 1);
    }
    // write tcp socket. to host
    strcat(matchinfo, tmpinfo);
    printf("%s\n", matchinfo);
    if (write(newSock_host, (char*)&matchinfo, sizeof(matchinfo)) < 0) {
        perror("write");
        exit(1);
    }

    string file_ind = "result_" + to_string(index_co) + ".jpg";
    imwrite(file_ind, frame2);
    cout << "co_detect1 : " << file_ind << endl;
    index_co += 2;
    imshow("Xilinx DPU", frame2);
    //if(waitKey(30) == 27) break;
    Yolonum++; // mutex update*/
}

void co_detect2(DPUTask* task, Mat& frame1, int sWidth, int sHeight, VideoCapture& cam) {  // in postProcess function

    /*output nodes of YOLO-v3 */
    const vector<string> outputs_node = { "layer81_conv", "layer93_conv", "layer105_conv" };

    vector<vector<float>> boxes;
    for (size_t i = 0; i < outputs_node.size(); i++) {
        string output_node = outputs_node[i];
        int channel = dpuGetOutputTensorChannel(task, output_node.c_str());
        int width = dpuGetOutputTensorWidth(task, output_node.c_str());
        int height = dpuGetOutputTensorHeight(task, output_node.c_str());

        int sizeOut = dpuGetOutputTensorSize(task, output_node.c_str());
        int8_t* dpuOut = dpuGetOutputTensorAddress(task, output_node.c_str());
        float scale = dpuGetOutputTensorScale(task, output_node.c_str());
        vector<float> result(sizeOut);
        boxes.reserve(sizeOut);

        /* Store every output node results */
        get_output(dpuOut, sizeOut, scale, channel, height, width, result);

        /* Store the object detection frames as coordinate information  */
        detect(boxes, result, channel, height, width, i, sHeight, sWidth);
    }

    /* Restore the correct coordinate frame of the original image */
    correct_region_boxes(boxes, boxes.size(), frame1.cols, frame1.rows, sWidth, sHeight);

    /* Apply the computation for NMS */
    cout << "boxes size: " << boxes.size() << endl;
    vector<vector<float>> res = applyNMS(boxes, classificationCnt, NMS_THRESHOLD);

    float h = frame1.rows;
    float w = frame1.cols;
    Mat frame2;
    for (int i = 0; i < 3; i++)
        cam.grab();
    cam.read(frame2);
    char matchinfo[1024] = "", match_inframe_info[1024], match_n[10];

    int hostAddrlen, countnum;
    struct sockaddr_in hostAddr;
    hostAddrlen = sizeof(hostAddr);
    char tmpinfo[1024], count[5];
    char* receive_info = new char[1024];
    dinfo depthinfo;

    while (Mutexdget == 1)
        usleep(10);
    Mutexdget = 1; // lock
    if (recvfrom(Sockfd_dcam, &count, sizeof(count), 0, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrlen) < 0) {
        perror("recvfrom");
        exit(1);
    } // receive depth camera info
    countnum = atoi(count);
    for (int i = 0; i < countnum; i++) {
        if (recvfrom(Sockfd_dcam, &depthinfo, sizeof(depthinfo), 0, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrlen) < 0) {
            perror("recvfrom");
            exit(1);
        } // receive depth camera info
        sprintf(receive_info, "%s %f %f %f %f %f %f %f %f \n", depthinfo.number, depthinfo.x, depthinfo.y, depthinfo.w, depthinfo.l, depthinfo.yaw, depthinfo.conf, depthinfo.cls_conf, depthinfo.cls_pred);
        strcat(tmpinfo, receive_info);
    }
    Mutexdget = 0; // unlock

    for (size_t i = 0; i < res.size(); ++i) {
        float xmin = (res[i][0] - res[i][2] / 2.0) * w + 1.0;
        float ymin = (res[i][1] - res[i][3] / 2.0) * h + 1.0;
        float xmax = (res[i][0] + res[i][2] / 2.0) * w + 1.0;
        float ymax = (res[i][1] + res[i][3] / 2.0) * h + 1.0;

        //cout<<xmin<<" "<<ymin<<" "<<xmax<<" "<<ymax<<endl;

        Mat compare_part = frame1(Range((2 * ymin + 1 * ymax) / 3, (1 * ymin + 2 * ymax) / 3), Range((2 * xmin + 1 * xmax) / 3, (1 * xmin + 2 * xmax) / 3));

        unsigned nextx, nexty;
        double min = 100000000, error;
        for (int x = (2 * xmin + 1 * xmax) / 3; x < (1 * xmin + 2 * xmax) / 3; x += 2) { // for padding
            if (((xmax - xmin) > h / 2) || ((ymax - ymin) > w / 2))
                break; // too large object reject
            for (int y = (2 * ymin + 1 * ymax) / 3; y < (1 * ymin + 2 * ymax) / 3; y += 2) {
                error = 0;
                for (int k = -(int)(xmax - xmin) / 6; k < (int)(xmax - xmin) / 6; k++) {
                    for (int u = -(int)(ymax - ymin) / 6; u < (int)(ymax - ymin) / 6; u++) {
                        uchar b1 = compare_part.ptr<uchar>(u + (int)(ymax - ymin) / 6)[(k + (int)(xmax - xmin) / 6) * 3 + 0];
                        uchar g1 = compare_part.ptr<uchar>(u + (int)(ymax - ymin) / 6)[(k + (int)(xmax - xmin) / 6) * 3 + 1];
                        uchar r1 = compare_part.ptr<uchar>(u + (int)(ymax - ymin) / 6)[(k + (int)(xmax - xmin) / 6) * 3 + 2];
                        int yu = y + u;
                        int xk = x + k;
                        if (y + u < 0 || x + k < 0 || y + u > w || x + k > h) {
                            if (y + u < 0) yu = 0;
                            if (x + k < 0) xk = 0;
                            if (y + u > w) yu = w;
                            if (x + k > h) xk = h;
                        }
                        uchar b2 = frame2.ptr<uchar>(yu)[(xk) * 3 + 0];
                        uchar g2 = frame2.ptr<uchar>(yu)[(xk) * 3 + 1];
                        uchar r2 = frame2.ptr<uchar>(yu)[(xk) * 3 + 2];
                        error += (pow(b1 - b2, 2) + pow(g1 - g2, 2) + pow(r1 - r2, 2));
                    }
                }
                if (min > error) {
                    min = error;
                    nextx = x;
                    nexty = y;
                }
            }
        }
        sprintf(match_n, "MC%d-%d", index_co, i);
        sprintf(match_inframe_info, "%s %f %f %f %f\n", match_n, nextx - (xmax - xmin) / 2, nexty - (ymax - ymin) / 2, nextx + (xmax - xmin) / 2, nexty + (ymax - ymin) / 2);
        strcat(matchinfo, match_inframe_info);

        rectangle(frame2, Rect(Point((2 * xmin + 1 * xmax) / 3, (2 * ymin + 1 * ymax) / 3), Point((1 * xmin + 2 * xmax) / 3, (1 * ymin + 2 * ymax) / 3)), (0, 0, 255), 1);
        rectangle(frame2, Rect(Point(nextx - (xmax - xmin) / 2, nexty - (ymax - ymin) / 2), Point(nextx + (xmax - xmin) / 2, nexty + (ymax - ymin) / 2)), (0, 0, 255), 1);
    }
    strcat(matchinfo, tmpinfo);
    printf("%s\n", matchinfo);
    if (write(newSock_host, (char*)&matchinfo, sizeof(matchinfo)) < 0) {
        perror("write");
        exit(1);
    }

    string file_ind = "result_" + to_string(index_co) + ".jpg";
    imwrite(file_ind, frame2);
    cout << "co_detect2 : " << file_ind << endl;
    index_co += 2;
    imshow("Xilinx DPU", frame2);
    //if(waitKey(30) == 27) break;
    delete receive_info;
    Yolonum++; // mutex update*/
}

/**
 * @brief Post process after the runing of DPU for YOLO-v3 network
 *
 * @param task - pointer to DPU task for running YOLO-v3
 * @param frame
 * @param sWidth
 * @param sHeight
 *
 * @return none
 */
void postProcess(DPUTask* task, Mat& frame, int sWidth, int sHeight) {

    /*output nodes of YOLO-v3 */
    const vector<string> outputs_node = { "layer81_conv", "layer93_conv", "layer105_conv" };

    vector<vector<float>> boxes;
    for (size_t i = 0; i < outputs_node.size(); i++) {
        string output_node = outputs_node[i];
        int channel = dpuGetOutputTensorChannel(task, output_node.c_str());
        int width = dpuGetOutputTensorWidth(task, output_node.c_str());
        int height = dpuGetOutputTensorHeight(task, output_node.c_str());

        int sizeOut = dpuGetOutputTensorSize(task, output_node.c_str());
        int8_t* dpuOut = dpuGetOutputTensorAddress(task, output_node.c_str());
        float scale = dpuGetOutputTensorScale(task, output_node.c_str());
        vector<float> result(sizeOut);
        boxes.reserve(sizeOut);

        /* Store every output node results */
        get_output(dpuOut, sizeOut, scale, channel, height, width, result);

        /* Store the object detection frames as coordinate information  */
        detect(boxes, result, channel, height, width, i, sHeight, sWidth);
    }

    /* Restore the correct coordinate frame of the original image */
    correct_region_boxes(boxes, boxes.size(), frame.cols, frame.rows, sWidth, sHeight);

    /* Apply the computation for NMS */
    cout << "boxes size: " << boxes.size() << endl;
    vector<vector<float>> res = applyNMS(boxes, classificationCnt, NMS_THRESHOLD);

    float h = frame.rows;
    float w = frame.cols;
    char yoloinfo[1024] = "YOLO-\n", yolo_inframe_info[1024], yolo_num[10];
    for (size_t i = 0; i < res.size(); ++i) {

        float xmin = (res[i][0] - res[i][2] / 2.0) * w + 1.0;
        float ymin = (res[i][1] - res[i][3] / 2.0) * h + 1.0;
        float xmax = (res[i][0] + res[i][2] / 2.0) * w + 1.0;
        float ymax = (res[i][1] + res[i][3] / 2.0) * h + 1.0;

        //cout<<res[i][res[i][4] + 6]<<" ";
        //cout<<xmin<<" "<<ymin<<" "<<xmax<<" "<<ymax<<endl;

    // write yolo result
        sprintf(yolo_num, "YC%d-%d", index_yolo, i);
        sprintf(yolo_inframe_info, "%s %f %f %f %f\n", yolo_num, xmin, ymin, xmax, ymax); // yolo info
        strcat(yoloinfo, yolo_inframe_info);

        if (res[i][res[i][4] + 6] > CONF) {
            int type = res[i][4];

            if (type == 0) {
                rectangle(frame, cvPoint(xmin, ymin), cvPoint(xmax, ymax), Scalar(0, 0, 255), 1, 1, 0);
            }
            else if (type == 1) {
                rectangle(frame, cvPoint(xmin, ymin), cvPoint(xmax, ymax), Scalar(255, 0, 0), 1, 1, 0);
            }
            else {
                rectangle(frame, cvPoint(xmin, ymin), cvPoint(xmax, ymax), Scalar(0, 255, 255), 1, 1, 0);
            }
        }
    }
    int hostAddrlen, countnum;
    struct sockaddr_in hostAddr;
    hostAddrlen = sizeof(hostAddr);
    char receive_info[1024], count[5];
    dinfo depthinfo;

    while (Mutexdget == 1)
        usleep(10);
    Mutexdget = 1; // lock

    if (recvfrom(Sockfd_dcam, &count, sizeof(count), 0, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrlen) < 0) {
        perror("recvfrom");
        exit(1);
    } // receive depth camera info
    countnum = atoi(count);
    for (int i = 0; i < countnum; i++) {
        if (recvfrom(Sockfd_dcam, &depthinfo, sizeof(depthinfo), 0, (struct sockaddr*)&hostAddr, (socklen_t*)&hostAddrlen) < 0) {
            perror("recvfrom");
            exit(1);
        } // receive depth camera info
        sprintf(receive_info, "%s %f %f %f %f %f %f %f %f \n", depthinfo.number, depthinfo.x, depthinfo.y, depthinfo.w, depthinfo.l, depthinfo.yaw, depthinfo.conf, depthinfo.cls_conf, depthinfo.cls_pred);
        strcat(yoloinfo, receive_info);
    }
    Mutexdget = 0; // unlock 

    printf("%s\n", yoloinfo);
    if (write(newSock_host, (char*)&yoloinfo, sizeof(yoloinfo)) < 0) {
        perror("write");
        exit(1);
    }
}

/**
 * @brief Thread entry for running YOLO-v3 network on DPU for acceleration
 *
 * @param task - pointer to DPU task for running YOLO-v3
 * @param img
 *
 * @return none
 */
thread runYOLO(DPUTask* task, Mat& img, VideoCapture& cam) {
    /* mean values for YOLO-v3 */
    float mean[3] = { 0.0f, 0.0f, 0.0f };

    int height = dpuGetInputTensorHeight(task, INPUT_NODE);
    int width = dpuGetInputTensorWidth(task, INPUT_NODE);

    /* feed input frame into DPU Task with mean value */
    setInputImageForYOLO(task, img, mean);

    /* invoke the running of DPU for YOLO-v3 */
    dpuRunTask(task);
    thread t(co_detect, task, ref(img), width, height, ref(cam));
    postProcess(task, img, width, height);
    Yolonum++;
    return t;
}
thread runYOLO2(DPUTask* task, Mat& img, VideoCapture& cam) {
    /* mean values for YOLO-v3 */
    float mean[3] = { 0.0f, 0.0f, 0.0f };

    int height = dpuGetInputTensorHeight(task, INPUT_NODE);
    int width = dpuGetInputTensorWidth(task, INPUT_NODE);

    /* feed input frame into DPU Task with mean value */
    setInputImageForYOLO(task, img, mean);

    /* invoke the running of DPU for YOLO-v3 */
    dpuRunTask(task);
    thread t(co_detect2, task, ref(img), width, height, ref(cam));
    postProcess(task, img, width, height);
    Yolonum++;
    return t;
}

/**
 * @brief Entry for running YOLO-v3 neural network for ADAS object detection
 *
 */
void Close(int signo)
{
    close(Sockfd_host);
    close(Sockfd_dcam);
    printf("\ndone\n");
    exit(0);
}
int main(const int argc, const char** argv) {
    int     len;
    struct sockaddr_in      Addr, dcamAddr, hostAddr;

    signal(SIGINT, Close);

    if (argc != 1) {
        cout << "Usage : ./yolo " << endl;
        return -1;
    }

    // get current path
    /*char cwd[256];
    if(getcwd(cwd, 256) == NULL) {
         perror("getcwd");
         exit(1);
    }*/

    /* socket for host - tcp */
    if ((Sockfd_host = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }

    cout << "~~";

    bzero((char*)&Addr, sizeof(Addr));
    Addr.sin_family = PF_INET;
    Addr.sin_addr.s_addr = htonl(INADDR_ANY);
    Addr.sin_port = htons(Port_host);

    if (bind(Sockfd_host, (struct sockaddr*)&Addr, sizeof(Addr)) < 0) {
        perror("bind");
        exit(1);
    }
    listen(Sockfd_host, 5);

    len = sizeof(hostAddr);
    newSock_host = accept(Sockfd_host, (struct sockaddr*)&hostAddr, (socklen_t*)&len);
    if (newSock_host < 0) {
        perror("accept");
        exit(1);
    }

    /*socket for depth camera - udp*/
    if ((Sockfd_dcam = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        exit(1);
    }

    bzero((char*)&dcamAddr, sizeof(dcamAddr));
    dcamAddr.sin_family = PF_INET;
    dcamAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    dcamAddr.sin_port = htons(Port_dcam);

    if (bind(Sockfd_dcam, (struct sockaddr*)&dcamAddr, sizeof(dcamAddr)) < 0) {
        perror("bind");
        exit(1);
    }

    /*
        if(write(Sockfd, (char*)&cwd, sizeof(cwd)) < 0) { // cwd 전송
            perror("write");
            exit(1);
        }*/

    if (!cam.isOpened()) {
        cout << "Fail to open webcam" << endl;
        exit(-1);
    }
    string file_ind;

    // init Yolo2 before while 
    dpuOpen();
    DPUKernel* kernel = dpuLoadKernel("yolo");
    DPUTask* task = dpuCreateTask(kernel, 0);
    Mat img;
    thread t1, t2;
    cam.read(img);
    t2 = runYOLO2(task, img, cam);
    file_ind = "result_" + to_string(index_yolo) + ".jpg";
    imwrite(file_ind, img);
    cout << "Yolo2 : " << file_ind << endl;
    index_yolo += 2;
    while (1) {
        file_ind = "result_" + to_string(index_yolo) + ".jpg";
        // run yolo1
        for (int i = 0; i < 5; i++) // buffer claer
            cam.grab();
        cam.read(img);
        t1 = runYOLO(task, img, cam);
        if (Mutexnum + 1 < Yolonum) // create mutex
            t2.join();
        imwrite(file_ind, img);
        cout << "Yolo1 : " << file_ind << endl;
        index_yolo += 2;
        imshow("Xilinx DPU", img);
        if (waitKey(30) == 27) break;
        // run yolo2
        for (int i = 0; i < 5; i++) // buffer claer
            cam.grab();
        cam.read(img);
        t2 = runYOLO2(task, img, cam);
        if (Mutexnum + 1 < Yolonum) // create mutex
            t1.join();
        file_ind = "result_" + to_string(index_yolo) + ".jpg";
        imwrite(file_ind, img);
        cout << "Yolo2 : " << file_ind << endl;
        index_yolo += 2;
        imshow("Xilinx DPU", img);
        if (waitKey(30) == 27) break;
    }
    dpuDestroyTask(task);
    /* Destroy DPU Kernels & free resources */
    dpuDestroyKernel(kernel);

    /* Dettach from DPU driver & free resources */
    dpuClose();
    return 0;
}