ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / yolo / yolo-image.cpp
| 1 | #define STB_IMAGE_IMPLEMENTATION |
| 2 | #include "stb_image.h" |
| 3 | #define STB_IMAGE_WRITE_IMPLEMENTATION |
| 4 | #include "stb_image_write.h" |
| 5 | |
| 6 | #include "yolo-image.h" |
| 7 | |
| 8 | static void draw_box(yolo_image & a, int x1, int y1, int x2, int y2, float r, float g, float b) |
| 9 | { |
| 10 | if (x1 < 0) x1 = 0; |
| 11 | if (x1 >= a.w) x1 = a.w-1; |
| 12 | if (x2 < 0) x2 = 0; |
| 13 | if (x2 >= a.w) x2 = a.w-1; |
| 14 | |
| 15 | if (y1 < 0) y1 = 0; |
| 16 | if (y1 >= a.h) y1 = a.h-1; |
| 17 | if (y2 < 0) y2 = 0; |
| 18 | if (y2 >= a.h) y2 = a.h-1; |
| 19 | |
| 20 | for (int i = x1; i <= x2; ++i){ |
| 21 | a.data[i + y1*a.w + 0*a.w*a.h] = r; |
| 22 | a.data[i + y2*a.w + 0*a.w*a.h] = r; |
| 23 | |
| 24 | a.data[i + y1*a.w + 1*a.w*a.h] = g; |
| 25 | a.data[i + y2*a.w + 1*a.w*a.h] = g; |
| 26 | |
| 27 | a.data[i + y1*a.w + 2*a.w*a.h] = b; |
| 28 | a.data[i + y2*a.w + 2*a.w*a.h] = b; |
| 29 | } |
| 30 | for (int i = y1; i <= y2; ++i){ |
| 31 | a.data[x1 + i*a.w + 0*a.w*a.h] = r; |
| 32 | a.data[x2 + i*a.w + 0*a.w*a.h] = r; |
| 33 | |
| 34 | a.data[x1 + i*a.w + 1*a.w*a.h] = g; |
| 35 | a.data[x2 + i*a.w + 1*a.w*a.h] = g; |
| 36 | |
| 37 | a.data[x1 + i*a.w + 2*a.w*a.h] = b; |
| 38 | a.data[x2 + i*a.w + 2*a.w*a.h] = b; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void draw_box_width(yolo_image & a, int x1, int y1, int x2, int y2, int w, float r, float g, float b) |
| 43 | { |
| 44 | for (int i = 0; i < w; ++i) { |
| 45 | draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | bool save_image(const yolo_image & im, const char *name, int quality) |
| 50 | { |
| 51 | uint8_t *data = (uint8_t*)calloc(im.w*im.h*im.c, sizeof(uint8_t)); |
| 52 | for (int k = 0; k < im.c; ++k) { |
| 53 | for (int i = 0; i < im.w*im.h; ++i) { |
| 54 | data[i*im.c+k] = (uint8_t) (255*im.data[i + k*im.w*im.h]); |
| 55 | } |
| 56 | } |
| 57 | int success = stbi_write_jpg(name, im.w, im.h, im.c, data, quality); |
| 58 | free(data); |
| 59 | if (!success) { |
| 60 | fprintf(stderr, "Failed to write image %s\n", name); |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool load_image(const char *fname, yolo_image & img) |
| 67 | { |
| 68 | int w, h, c; |
| 69 | uint8_t * data = stbi_load(fname, &w, &h, &c, 3); |
| 70 | if (!data) { |
| 71 | return false; |
| 72 | } |
| 73 | c = 3; |
| 74 | img.w = w; |
| 75 | img.h = h; |
| 76 | img.c = c; |
| 77 | img.data.resize(w*h*c); |
| 78 | for (int k = 0; k < c; ++k){ |
| 79 | for (int j = 0; j < h; ++j){ |
| 80 | for (int i = 0; i < w; ++i){ |
| 81 | int dst_index = i + w*j + w*h*k; |
| 82 | int src_index = k + c*i + c*w*j; |
| 83 | img.data[dst_index] = (float)data[src_index]/255.; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | stbi_image_free(data); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | static yolo_image resize_image(const yolo_image & im, int w, int h) |
| 92 | { |
| 93 | yolo_image resized(w, h, im.c); |
| 94 | yolo_image part(w, im.h, im.c); |
| 95 | float w_scale = (float)(im.w - 1) / (w - 1); |
| 96 | float h_scale = (float)(im.h - 1) / (h - 1); |
| 97 | for (int k = 0; k < im.c; ++k){ |
| 98 | for (int r = 0; r < im.h; ++r) { |
| 99 | for (int c = 0; c < w; ++c) { |
| 100 | float val = 0; |
| 101 | if (c == w-1 || im.w == 1){ |
| 102 | val = im.get_pixel(im.w-1, r, k); |
| 103 | } else { |
| 104 | float sx = c*w_scale; |
| 105 | int ix = (int) sx; |
| 106 | float dx = sx - ix; |
| 107 | val = (1 - dx) * im.get_pixel(ix, r, k) + dx * im.get_pixel(ix+1, r, k); |
| 108 | } |
| 109 | part.set_pixel(c, r, k, val); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | for (int k = 0; k < im.c; ++k){ |
| 114 | for (int r = 0; r < h; ++r){ |
| 115 | float sy = r*h_scale; |
| 116 | int iy = (int) sy; |
| 117 | float dy = sy - iy; |
| 118 | for (int c = 0; c < w; ++c){ |
| 119 | float val = (1-dy) * part.get_pixel(c, iy, k); |
| 120 | resized.set_pixel(c, r, k, val); |
| 121 | } |
| 122 | if (r == h-1 || im.h == 1) continue; |
| 123 | for (int c = 0; c < w; ++c){ |
| 124 | float val = dy * part.get_pixel(c, iy+1, k); |
| 125 | resized.add_pixel(c, r, k, val); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return resized; |
| 130 | } |
| 131 | |
| 132 | static void embed_image(const yolo_image & source, yolo_image & dest, int dx, int dy) |
| 133 | { |
| 134 | for (int k = 0; k < source.c; ++k) { |
| 135 | for (int y = 0; y < source.h; ++y) { |
| 136 | for (int x = 0; x < source.w; ++x) { |
| 137 | float val = source.get_pixel(x, y, k); |
| 138 | dest.set_pixel(dx+x, dy+y, k, val); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | yolo_image letterbox_image(const yolo_image & im, int w, int h) |
| 145 | { |
| 146 | int new_w = im.w; |
| 147 | int new_h = im.h; |
| 148 | if (((float)w/im.w) < ((float)h/im.h)) { |
| 149 | new_w = w; |
| 150 | new_h = (im.h * w)/im.w; |
| 151 | } else { |
| 152 | new_h = h; |
| 153 | new_w = (im.w * h)/im.h; |
| 154 | } |
| 155 | yolo_image resized = resize_image(im, new_w, new_h); |
| 156 | yolo_image boxed(w, h, im.c); |
| 157 | boxed.fill(0.5); |
| 158 | embed_image(resized, boxed, (w-new_w)/2, (h-new_h)/2); |
| 159 | return boxed; |
| 160 | } |
| 161 | |
| 162 | static yolo_image tile_images(const yolo_image & a, const yolo_image & b, int dx) |
| 163 | { |
| 164 | if (a.w == 0) { |
| 165 | return b; |
| 166 | } |
| 167 | yolo_image c(a.w + b.w + dx, (a.h > b.h) ? a.h : b.h, a.c); |
| 168 | c.fill(1.0f); |
| 169 | embed_image(a, c, 0, 0); |
| 170 | embed_image(b, c, a.w + dx, 0); |
| 171 | return c; |
| 172 | } |
| 173 | |
| 174 | static yolo_image border_image(const yolo_image & a, int border) |
| 175 | { |
| 176 | yolo_image b(a.w + 2*border, a.h + 2*border, a.c); |
| 177 | b.fill(1.0f); |
| 178 | embed_image(a, b, border, border); |
| 179 | return b; |
| 180 | } |
| 181 | |
| 182 | yolo_image get_label(const std::vector<yolo_image> & alphabet, const std::string & label, int size) |
| 183 | { |
| 184 | size = size/10; |
| 185 | size = std::min(size, 7); |
| 186 | yolo_image result(0,0,0); |
| 187 | for (int i = 0; i < (int)label.size(); ++i) { |
| 188 | int ch = label[i]; |
| 189 | yolo_image img = alphabet[size*128 + ch]; |
| 190 | result = tile_images(result, img, -size - 1 + (size+1)/2); |
| 191 | } |
| 192 | return border_image(result, (int)(result.h*.25)); |
| 193 | } |
| 194 | |
| 195 | void draw_label(yolo_image & im, int row, int col, const yolo_image & label, const float * rgb) |
| 196 | { |
| 197 | int w = label.w; |
| 198 | int h = label.h; |
| 199 | if (row - h >= 0) { |
| 200 | row = row - h; |
| 201 | } |
| 202 | for (int j = 0; j < h && j + row < im.h; j++) { |
| 203 | for (int i = 0; i < w && i + col < im.w; i++) { |
| 204 | for (int k = 0; k < label.c; k++) { |
| 205 | float val = label.get_pixel(i, j, k); |
| 206 | im.set_pixel(i + col, j + row, k, rgb[k] * val); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |