detection_bbox.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVUTIL_DETECTION_BBOX_H
  19. #define AVUTIL_DETECTION_BBOX_H
  20. #include "rational.h"
  21. #include "avassert.h"
  22. #include "frame.h"
  23. typedef struct AVDetectionBBox {
  24. /**
  25. * Distance in pixels from the left/top edge of the frame,
  26. * together with width and height, defining the bounding box.
  27. */
  28. int x;
  29. int y;
  30. int w;
  31. int h;
  32. #define AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE 64
  33. /**
  34. * Detect result with confidence
  35. */
  36. char detect_label[AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE];
  37. AVRational detect_confidence;
  38. /**
  39. * At most 4 classifications based on the detected bounding box.
  40. * For example, we can get max 4 different attributes with 4 different
  41. * DNN models on one bounding box.
  42. * classify_count is zero if no classification.
  43. */
  44. #define AV_NUM_DETECTION_BBOX_CLASSIFY 4
  45. uint32_t classify_count;
  46. char classify_labels[AV_NUM_DETECTION_BBOX_CLASSIFY][AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE];
  47. AVRational classify_confidences[AV_NUM_DETECTION_BBOX_CLASSIFY];
  48. } AVDetectionBBox;
  49. typedef struct AVDetectionBBoxHeader {
  50. /**
  51. * Information about how the bounding box is generated.
  52. * for example, the DNN model name.
  53. */
  54. char source[256];
  55. /**
  56. * Number of bounding boxes in the array.
  57. */
  58. uint32_t nb_bboxes;
  59. /**
  60. * Offset in bytes from the beginning of this structure at which
  61. * the array of bounding boxes starts.
  62. */
  63. size_t bboxes_offset;
  64. /**
  65. * Size of each bounding box in bytes.
  66. */
  67. size_t bbox_size;
  68. } AVDetectionBBoxHeader;
  69. /*
  70. * Get the bounding box at the specified {@code idx}. Must be between 0 and nb_bboxes.
  71. */
  72. static av_always_inline AVDetectionBBox *
  73. av_get_detection_bbox(const AVDetectionBBoxHeader *header, unsigned int idx)
  74. {
  75. av_assert0(idx < header->nb_bboxes);
  76. return (AVDetectionBBox *)((uint8_t *)header + header->bboxes_offset +
  77. idx * header->bbox_size);
  78. }
  79. /**
  80. * Allocates memory for AVDetectionBBoxHeader, plus an array of {@code nb_bboxes}
  81. * AVDetectionBBox, and initializes the variables.
  82. * Can be freed with a normal av_free() call.
  83. *
  84. * @param nb_bboxes number of AVDetectionBBox structures to allocate
  85. * @param out_size if non-NULL, the size in bytes of the resulting data array is
  86. * written here.
  87. */
  88. AVDetectionBBoxHeader *av_detection_bbox_alloc(uint32_t nb_bboxes, size_t *out_size);
  89. /**
  90. * Allocates memory for AVDetectionBBoxHeader, plus an array of {@code nb_bboxes}
  91. * AVDetectionBBox, in the given AVFrame {@code frame} as AVFrameSideData of type
  92. * AV_FRAME_DATA_DETECTION_BBOXES and initializes the variables.
  93. */
  94. AVDetectionBBoxHeader *av_detection_bbox_create_side_data(AVFrame *frame, uint32_t nb_bboxes);
  95. #endif