|
| 1 | +#include "YOLO.h" |
| 2 | + |
| 3 | +YOLO::YOLO(Net_config config) { |
| 4 | + this->confThreshold = config.confThreshold; |
| 5 | + this->nmsThreshold = config.nmsThreshold; |
| 6 | + this->xthr = config.xthr; |
| 7 | + this->ythr = config.xthr; |
| 8 | + this->objThreshold = config.objThreshold; |
| 9 | + |
| 10 | + this->net = readNet(config.modelpath); |
| 11 | + ifstream ifs("./costMap/class.txt"); |
| 12 | + std::string line; |
| 13 | + while (getline(ifs, line)) { |
| 14 | + printf("%s", line.c_str()); |
| 15 | + this->class_names.push_back(line); |
| 16 | + } |
| 17 | + this->num_class = class_names.size(); |
| 18 | + |
| 19 | + /*if (endsWith(config.modelpath, "6.onnx")) |
| 20 | + { |
| 21 | + anchors = (float*)anchors_1280; |
| 22 | + this->num_stride = 4; |
| 23 | + this->inpHeight = 1280; |
| 24 | + this->inpWidth = 1280; |
| 25 | + }*/ |
| 26 | + if (config.modelpath.find("ps")) { |
| 27 | + anchor_yolov5n[0][0][0] = 96; |
| 28 | + anchor_yolov5n[0][1][0] = 224; |
| 29 | + slot_stride = 32; |
| 30 | + this->inpHeight = 640; |
| 31 | + this->inpWidth = 640; |
| 32 | + } |
| 33 | + else { |
| 34 | + float anchor[3][3][2] = { 10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326 }; |
| 35 | + memcpy(anchor_yolov5n, anchor, sizeof(anchor)); |
| 36 | + this->num_stride = 3; |
| 37 | + this->inpHeight = 640; |
| 38 | + this->inpWidth = 640; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +Mat YOLO::resize_image(Mat srcimg, int* newh, int* neww, int* top, int* left) { |
| 43 | + int srch = srcimg.rows, srcw = srcimg.cols; |
| 44 | + *newh = this->inpHeight; |
| 45 | + *neww = this->inpWidth; |
| 46 | + Mat dstimg; |
| 47 | + if (this->keep_ratio && srch != srcw) { |
| 48 | + float hw_scale = (float)srch / srcw; |
| 49 | + if (hw_scale > 1) { |
| 50 | + *newh = this->inpHeight; |
| 51 | + *neww = int(this->inpWidth / hw_scale); |
| 52 | + resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA); |
| 53 | + *left = int((this->inpWidth - *neww) * 0.5); |
| 54 | + copyMakeBorder(dstimg, dstimg, 0, 0, *left, this->inpWidth - *neww - *left, BORDER_CONSTANT, 114); |
| 55 | + } |
| 56 | + else { |
| 57 | + *newh = (int)this->inpHeight * hw_scale; |
| 58 | + *neww = this->inpWidth; |
| 59 | + resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA); |
| 60 | + *top = (int)(this->inpHeight - *newh) * 0.5; |
| 61 | + copyMakeBorder(dstimg, dstimg, *top, this->inpHeight - *newh - *top, 0, 0, BORDER_CONSTANT, 114); |
| 62 | + } |
| 63 | + } |
| 64 | + else { |
| 65 | + resize(srcimg, dstimg, Size(*neww, *newh), INTER_AREA); |
| 66 | + } |
| 67 | + return dstimg; |
| 68 | +} |
| 69 | + |
| 70 | +void YOLO::slot_detect_1280_720(Mat& src, vector<parkingSpaceInPixel>& spaces) { |
| 71 | + spaces.clear(); |
| 72 | + cv::Mat frame = src.clone(); |
| 73 | + if ((frame.rows != 720) || (frame.cols != 1280)) { |
| 74 | + std::cerr << "input bird eye image is not 1280*720." << std::endl; |
| 75 | + return; |
| 76 | + } |
| 77 | + int startX = frame.cols / 2 - 340; |
| 78 | + int startY = frame.rows / 2 - 340; |
| 79 | + frame = frame(Rect(startX, startY, 680, 680)); |
| 80 | + // cv::imwrite("aa.jpg", frame); |
| 81 | + |
| 82 | + int newh = 0, neww = 0, padh = 0, padw = 0; |
| 83 | + Mat dstimg = this->resize_image(frame, &newh, &neww, &padh, &padw); |
| 84 | + Mat blob = blobFromImage(dstimg, 1 / 255.0, Size(this->inpWidth, this->inpHeight), Scalar(0, 0, 0), true, false); |
| 85 | + this->net.setInput(blob); |
| 86 | + vector<Mat> outs; |
| 87 | + this->net.forward(outs, this->net.getUnconnectedOutLayersNames()); |
| 88 | + int Input_w = frame.cols; |
| 89 | + int Input_h = frame.rows; |
| 90 | + int feat_width = dstimg.cols / slot_stride; |
| 91 | + int feat_height = dstimg.rows / slot_stride; |
| 92 | + int anchor_num = 2; |
| 93 | + int yoloOutNum = 1; |
| 94 | + int cls_num = this->num_class; |
| 95 | + float conf_thr = this->confThreshold; |
| 96 | + |
| 97 | + vector<SlotInfo> slots; |
| 98 | + get_yolo_slot_output(outs, feat_width, feat_height, slot_stride, anchor_num, cls_num, |
| 99 | + conf_thr, yoloOutNum, Input_w, Input_h, anchor_yolov5n, slots); |
| 100 | + slot_nms(slots, this->xthr, this->ythr); |
| 101 | + |
| 102 | + for (int iter = 0; iter < slots.size(); iter++) { |
| 103 | + int x1 = slots[iter].x1; |
| 104 | + int y1 = slots[iter].y1; |
| 105 | + int x2 = slots[iter].x2; |
| 106 | + int y2 = slots[iter].y2; |
| 107 | + float parkingSpaceWidth = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); |
| 108 | + float parkingSpaceLength = PARKING_SPACE_RATIO * parkingSpaceWidth; |
| 109 | + int x3 = slots[iter].cos2 * parkingSpaceLength + x2; |
| 110 | + int y3 = slots[iter].sin2 * parkingSpaceLength + y2; |
| 111 | + int x4 = slots[iter].cos2 * parkingSpaceLength + x1; |
| 112 | + int y4 = slots[iter].sin2 * parkingSpaceLength + y1; |
| 113 | + |
| 114 | +#if SHOW_PARKING_DEBUG |
| 115 | + float score = slots[iter].score; |
| 116 | + if (slots[iter].label == 0) { |
| 117 | + // cv::fillPoly(srcimg,ppt,npt,1,cv::Scalar(0,255,0),0); |
| 118 | + cv::line(frame, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0), 2); |
| 119 | + cv::line(frame, cv::Point(x1, y1), cv::Point(x4, y4), cv::Scalar(0, 255, 0), 2); |
| 120 | + cv::line(frame, cv::Point(x2, y2), cv::Point(x3, y3), cv::Scalar(0, 255, 0), 2); |
| 121 | + cv::circle(frame, cv::Point((x1 + x2) / 2, (y1 + y2) / 2), 3, cv::Scalar(255, 255, 0), 2); |
| 122 | + cv::putText(frame, std::to_string(score), cv::Point((x1 + x2) / 2, (y1 + y2) / 2 + 5), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0)); |
| 123 | + cv::putText(frame, std::to_string(1), cv::Point(x1, y1), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0)); |
| 124 | + cv::putText(frame, std::to_string(2), cv::Point(x2, y2), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0)); |
| 125 | + cv::putText(frame, std::to_string(3), cv::Point(x3, y3), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0)); |
| 126 | + } |
| 127 | + else if (slots[iter].label == 1) { |
| 128 | + cv::line(frame, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 0, 255), 2); |
| 129 | + cv::line(frame, cv::Point(x1, y1), cv::Point(x4, y4), cv::Scalar(0, 0, 255), 2); |
| 130 | + cv::line(frame, cv::Point(x2, y2), cv::Point(x3, y3), cv::Scalar(0, 0, 255), 2); |
| 131 | + cv::circle(frame, cv::Point((x1 + x2) / 2, (y1 + y2) / 2), 3, cv::Scalar(255, 255, 0), 2); |
| 132 | + cv::putText(frame, std::to_string(score), cv::Point((x1 + x2) / 2, (y1 + y2) / 2 + 5), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 0, 255)); |
| 133 | + // cv::fillPoly(srcimg,ppt,npt,1,cv::Scalar(0,0,255),0); |
| 134 | + } |
| 135 | + cv::imshow("yolo detect", frame); |
| 136 | +#endif // SHOW_PARKING_DEBUG |
| 137 | + |
| 138 | + |
| 139 | + parkingSpaceInPixel sp; |
| 140 | + sp.x1 = x1+ startX; |
| 141 | + sp.y1 = y1+ startY; |
| 142 | + sp.x2 = x2+ startX; |
| 143 | + sp.y2 = y2+ startY; |
| 144 | + sp.x3 = x3+ startX; |
| 145 | + sp.y3 = y3+ startY; |
| 146 | + sp.x4 = x4+ startX; |
| 147 | + sp.y4 = y4+ startY; |
| 148 | + |
| 149 | + sp.label = slots[iter].label; |
| 150 | + sp.score = slots[iter].score; |
| 151 | + spaces.push_back(sp); |
| 152 | + } |
| 153 | + /*SlotTrack tracker; |
| 154 | + tracker.process(frame, slots);*/ |
| 155 | +} |
0 commit comments