{"id":8096,"date":"2023-11-02T10:26:50","date_gmt":"2023-11-02T18:26:50","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=8096"},"modified":"2025-04-24T17:04:41","modified_gmt":"2025-04-24T17:04:41","slug":"create-and-visualize-image-data-with-kangas-for-computer-vision-tasks","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks\/","title":{"rendered":"Create and visualize image data with Kangas for computer vision tasks"},"content":{"rendered":"\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks\">\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rE3bf23O8Ulf4szSw0-EMg.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo by <a class=\"af nb\" href=\"https:\/\/unsplash.com\/@genny_taylor?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener ugc nofollow\">Genny Dimitrakopoulou<\/a> on <a class=\"af nb\" href=\"https:\/\/unsplash.com\/photos\/owiIxBvzjBo?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener ugc nofollow\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"8e9c\">Visualizing data to carry out a detailed EDA, especially for image data, is critical. In <strong class=\"be nx\">computer vision<\/strong> datasets, if we can view and compare the images across different views with their relevant metadata and transformations within a single and well-designed UI, we are one step ahead in solving a CV task. We<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"cdbb\">In this article, we will explore the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Creating DataGrid with images.<\/li>\n\n\n\n<li>Adding image metadata.<\/li>\n\n\n\n<li>Adding bounding boxes<\/li>\n\n\n\n<li>Adding image masks<\/li>\n\n\n\n<li>Loading and visualizing image data from hugging face.<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading oj ok fr be ol om on gr oo op oq gu or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" id=\"fb77\">Creating DataGrid with images.<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"13f3\">There are various ways we can construct a Kangas DataGrid. Check out this tutorial on<a class=\"af nb\" href=\"https:\/\/heartbeat.comet.ml\/constructing-and-visualizing-kangas-datagrid-on-kangas-ui-f63d4350ab61\" target=\"_blank\" rel=\"noopener ugc nofollow\"> Constructing and visualizing Kangas DataGrid on Kangas UI<\/a>.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"5b14\">We can store images on a DataGrid using the Kangas <code class=\"cw pk pl pm pn b\">Image()<\/code> class or as Pillow images(PIL).<\/p>\n\n\n\n<h2 class=\"wp-block-heading po ok fr be ol pp pq pr oo ps pt pu or nk pv pw px no py pz qa ns qb qc qd qe bj\" id=\"0421\">Loading images with Image() class<\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"9bc1\">Here we will use an image of a cat and load it using the <code class=\"cw pk pl pm pn b\">Image()<\/code> class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"15d9\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">image = kg.Image(<span class=\"hljs-string\">'\/content\/cats-dogs\/cat1.jpeg'<\/span>)\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'TYPE OF IMAGE: '<\/span>, <span class=\"hljs-built_in\">type<\/span>(image))\n<span class=\"hljs-built_in\">print<\/span>()\nimage.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:418\/1*elu5dcdt5I2n5boUvm8Iow.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Store Image with Kangas Image() class<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading po ok fr be ol pp pq pr oo ps pt pu or nk pv pw px no py pz qa ns qb qc qd qe bj\" id=\"dbbd\">Load image as Pillow (PIL) image with Kangas<\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"1c57\">Kangas provides a <code class=\"cw pk pl pm pn b\">to_pil()<\/code> method that converts the image into a Pillow image.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"8b09\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">image = kg.Image(<span class=\"hljs-string\">'\/content\/cats-dogs\/cat1.jpeg'<\/span>).to_pil()\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'TYPE OF IMAGE: '<\/span>, <span class=\"hljs-built_in\">type<\/span>(image))\n<span class=\"hljs-built_in\">print<\/span>()\nimage.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:461\/1*b8s5NyWclmO76X5YpVJdXA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Store Image as PIL image with Kangas<\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"f75f\">The image data passed into the <code class=\"cw pk pl pm pn b\">Image()<\/code> class can take a variety of formats. Let\u2019s try to load an image as a NumPy matrix and a TensorFlow Tensor. View more formats on the Creating an Image Asset<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"ab95\"><a class=\"af nb\" href=\"https:\/\/github.com\/comet-ml\/kangas\/wiki\/Image#__init__\" target=\"_blank\" rel=\"noopener ugc nofollow\">See other formats<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"4243\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\"><span class=\"hljs-comment\"># Image<\/span>\nimage = kg.Image(<span class=\"hljs-string\">'\/content\/cats-dogs\/cat1.jpeg'<\/span>).to_pil()<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"74a6\">Image data as NumPy matrix:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"14ea\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">from<\/span> numpy <span class=\"hljs-keyword\">import<\/span> asarray\n\nnumpy_img_data = asarray(image)\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">f\"Type of data: <span class=\"hljs-subst\">{<span class=\"hljs-built_in\">type<\/span>(numpy_img_data)}<\/span> data:  of Shape: <span class=\"hljs-subst\">{numpy_img_data.shape}<\/span>\"<\/span>)\n\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'TYPE OF IMAGE: '<\/span>, <span class=\"hljs-built_in\">type<\/span>(image))\n<span class=\"hljs-built_in\">print<\/span>()\nimage.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:533\/1*bkgsPfoN2wCcnfr1OP79SQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"e4da\">Image data as TensorFlow tensor:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"5959\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> tensorflow <span class=\"hljs-keyword\">as<\/span> tf\n\ntensor_img_data = tf.convert_to_tensor(image)\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">f\"Type of data: <span class=\"hljs-subst\">{<span class=\"hljs-built_in\">type<\/span>(tensor_img_data)}<\/span> data:  of Shape: <span class=\"hljs-subst\">{tensor_img_data.shape}<\/span>\"<\/span>)\n\n<span class=\"hljs-built_in\">print<\/span>(<span class=\"hljs-string\">'TYPE OF IMAGE: '<\/span>, <span class=\"hljs-built_in\">type<\/span>(image))\n<span class=\"hljs-built_in\">print<\/span>()\nimage.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:687\/1*7g1RnD2-QGkgfr9pTNOIpg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading po ok fr be ol pp pq pr oo ps pt pu or nk pv pw px no py pz qa ns qb qc qd qe bj\" id=\"ef65\">Adding images to a DataGrid<\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"1467\">We can load the image to a DataGrid and visualize it with the UI.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"3452\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">image = kg.Image(<span class=\"hljs-string\">'\/content\/cats-dogs\/cat1.jpeg'<\/span>, name=<span class=\"hljs-string\">'cat.jpeg'<\/span>) <span class=\"hljs-comment\"># provide name to be used as filename in metadata<\/span>\ndg = kg.DataGrid(name=<span class=\"hljs-string\">'Cat'<\/span>) <span class=\"hljs-comment\"># provide name to be used as the datagrid name<\/span>\ndg.extend([{<span class=\"hljs-string\">'image'<\/span>: image}])\n\n<span class=\"hljs-comment\"># Fire up the UI<\/span>\ndg.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*oa4yxs40cvKYxnd2qHWwjQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Kangas UI<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"db4e\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> glob\n\n<span class=\"hljs-comment\">#instantiate DataGrid<\/span>\ndg = DataGrid(name=<span class=\"hljs-string\">\"Images\"<\/span>, columns=[<span class=\"hljs-string\">\"image_file_path\"<\/span>, <span class=\"hljs-string\">\"image\"<\/span>, <span class=\"hljs-string\">'height'<\/span>, <span class=\"hljs-string\">'width'<\/span>, <span class=\"hljs-string\">\"label\"<\/span>])\n\n<span class=\"hljs-comment\"># append data to DataGrid<\/span>\n<span class=\"hljs-keyword\">for<\/span> file <span class=\"hljs-keyword\">in<\/span> glob.iglob(<span class=\"hljs-string\">\"\/*\/*\/*.jpeg\"<\/span>):\n  file_name = os.path.basename(file) <span class=\"hljs-comment\"># file_name to metadata<\/span>\n  image = Image.<span class=\"hljs-built_in\">open<\/span>(file)\n  image_w = image.width\n  image_h = image.height\n  label = os.path.splitext(file_name)[<span class=\"hljs-number\">0<\/span>][:<span class=\"hljs-number\">3<\/span>]\n  dg.append([file, kg.Image(file, name=file_name), image_w, image_h, label])<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*433aiR1E04WgKHFTH4p2tA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Kangas UI: Image data<\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading oj ok fr be ol om on gr oo op oq gu or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" id=\"1cd1\">Adding Image metadata<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"3340\">When we create images, we can specify more data describing various attributes of the images we are working with. By default, Kangas logs out the <code class=\"cw pk pl pm pn b\">name(as <\/code>filename) and the<code class=\"cw pk pl pm pn b\">assetId<\/code>.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"f67a\"><em class=\"qt\">We can visualize this on the UI. Click on the image, and a pop window will appear. Locate the \u201cMetadata\u201d section and toggle the dropdown.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*JlgJjoGIX8f7mBpCUPi5CA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image Metadata<\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"1efa\"><strong class=\"be nx\">Note <\/strong>that the <code class=\"cw pk pl pm pn b\">extension <\/code>value is derived from the <code class=\"cw pk pl pm pn b\">filename <\/code>value. If the <code class=\"cw pk pl pm pn b\">filename(name argument)<\/code>is not provided, the value would be the file path.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"85cc\">The <code class=\"cw pk pl pm pn b\"><a class=\"af nb\" href=\"https:\/\/github.com\/comet-ml\/kangas\/wiki\/Image#__init__\" target=\"_blank\" rel=\"noopener ugc nofollow\">Image()<\/a><\/code><a class=\"af nb\" href=\"https:\/\/github.com\/comet-ml\/kangas\/wiki\/Image#__init__\" target=\"_blank\" rel=\"noopener ugc nofollow\"> class<\/a> provides more optional arguments that we can provide to give more information about the image.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"af94\">Image annotations like bounding boxes and masks are included as metadata.<\/p>\n\n\n\n<h2 class=\"wp-block-heading po ok fr be ol pp pq pr oo ps pt pu or nk pv pw px no py pz qa ns qb qc qd qe bj\" id=\"1062\">Adding bounding boxes to image with Kangas<\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"5975\">In object detection tasks, bounding boxes are essential as they help us determine the spatial location of a particular object.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The bounding box can be represented by the x and y coordinates of the upper-left and lower-right corners of the rectangle.<\/li>\n\n\n\n<li>We can also represent the bounding box with the (x, y)-axis coordinates of the bounding box center and the width and height of the box.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*WOkBhqQjEX7UMNejqXxagQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Two ways to represent a bounding box<\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"1dbf\">The Kangas <code class=\"cw pk pl pm pn b\"><a class=\"af nb\" href=\"https:\/\/github.com\/comet-ml\/kangas\/wiki\/Image#add_bounding_boxes\" target=\"_blank\" rel=\"noopener ugc nofollow\">add_bounding_boxes()<\/a><\/code> method can receive either of the above representations. We can pass the following to the method:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong class=\"be nx\">label<\/strong>: a label for the boxes.<\/li>\n\n\n\n<li><strong class=\"be nx\">boxes<\/strong>: list or tuples of exactly 2 points (top-left, bottom-right) or 4 ints (x, y, width, height).<\/li>\n\n\n\n<li><strong class=\"be nx\">score<\/strong>: the score relevant to the bounding box.<\/li>\n\n\n\n<li><strong class=\"be nx\">layer_name<\/strong>: the layer for the label and bounding boxes.<\/li>\n\n\n\n<li><strong class=\"be nx\">id<\/strong>: box ids.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"1124\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">img2_aug = kg.Image(<span class=\"hljs-string\">'\/content\/cat_dog.jpg'<\/span>)\n\nbox1 = [(<span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">65<\/span>), (<span class=\"hljs-number\">210<\/span>, <span class=\"hljs-number\">280<\/span>)]\nbox2 = [(<span class=\"hljs-number\">222<\/span>, <span class=\"hljs-number\">23<\/span>),(<span class=\"hljs-number\">448<\/span>, <span class=\"hljs-number\">281<\/span>)]\nimgs_bbox = img2_aug.add_bounding_boxes(<span class=\"hljs-string\">\"Cat\"<\/span>, box1, score=<span class=\"hljs-number\">0.89<\/span>, layer_name=<span class=\"hljs-string\">\"Truth\"<\/span>, <span class=\"hljs-built_in\">id<\/span>=<span class=\"hljs-string\">'cat01'<\/span>)\nimgs_bbox = img2_aug.add_bounding_boxes(<span class=\"hljs-string\">\"Dog\"<\/span>, box2, score=<span class=\"hljs-number\">0.89<\/span>, layer_name=<span class=\"hljs-string\">\"Truth\"<\/span>, <span class=\"hljs-built_in\">id<\/span>=<span class=\"hljs-string\">'dog01'<\/span>)\n\nimg_dg = kg.DataGrid(name=<span class=\"hljs-string\">'cat_dog'<\/span>)\nimg_dg.extend([[imgs_bbox]])\n\nimg_dg.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*H68e5aGm6V8MTVGv5J8JAg.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Kangas UI: Adding bounding boxes<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading po ok fr be ol pp pq pr oo ps pt pu or nk pv pw px no py pz qa ns qb qc qd qe bj\" id=\"d2fd\">Image Masking with Kangas<\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"d2cf\">Image masking lets us focus on the parts of the image we are most interested in. For instance, if we build a computer vision model to recognize faces, we can mask the rest of the image and focus on the actual faces.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"c9c6\">While we can do image masking in various ways, for example, using OpenCV, Kangas has the <code class=\"cw pk pl pm pn b\">add_mask()<\/code> method we can use when creating an image.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"c4e7\">With this method, a mask can be:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A 2D array or <code class=\"cw pk pl pm pn b\">np.array<\/code> with int values.<\/li>\n\n\n\n<li>An image filename.<\/li>\n\n\n\n<li>A Pillow or <code class=\"cw pk pl pm pn b\">kangas.Image image<\/code>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"c499\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">image = kg.Image(<span class=\"hljs-string\">'\/content\/cat_dog.jpg'<\/span>)\nmask = PIL.Image.<span class=\"hljs-built_in\">open<\/span>(<span class=\"hljs-string\">'\/content\/mask_truth.png'<\/span>)\nimage.add_mask({<span class=\"hljs-number\">1<\/span>: <span class=\"hljs-string\">'Cat_vs_dog'<\/span>}, mask, layer_name=<span class=\"hljs-string\">'Truth'<\/span>)\ndg = kg.DataGrid(name=<span class=\"hljs-string\">\"model1\"<\/span>)\ndg.append([image])\ndg.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rd6hOfoAnlakVT1GPQS_Iw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Adding mask: Kangas UI<\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading oj ok fr be ol om on gr oo op oq gu or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" id=\"54ef\">Loading and visualizing image data from hugging face.<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"f35d\">We can visualize and apply built-in functions from Kangas UI on <a class=\"af nb\" href=\"https:\/\/huggingface.co\/datasets\" target=\"_blank\" rel=\"noopener ugc nofollow\">hugging face datasets<\/a> with Kangas. Hugging Face contains a collection of datasets we can use to build various models.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"447c\">In this section, we will see how to create DataGrid from hugging face datasets and visualize them on the Kangas UI. We will grab the <a class=\"af nb\" href=\"https:\/\/huggingface.co\/datasets\/keremberke\/license-plate-object-detection\" target=\"_blank\" rel=\"noopener ugc nofollow\">license plate object detection dataset<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rCB1CZKXlhK3Mn9xzrh7Kg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">License plate object detection dataset from hugging face<\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"3ac0\">First, import the datasets library to enable us to grab the dataset:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"99a7\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">%pip install datasets<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"ecc3\">Next, import the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"7753\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">import<\/span> datasets\n<span class=\"hljs-keyword\">from<\/span> datasets <span class=\"hljs-keyword\">import<\/span> load_datasets, list_datasets\n<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"71a2\">For the simplicity of this article, we will only split 20 examples from the training set of the dataset.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"73bb\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">license_dataset = load_dataset(<span class=\"hljs-string\">'keremberke\/license-plate-object-detection'<\/span>, <span class=\"hljs-string\">'full'<\/span>, split=<span class=\"hljs-string\">'train[:20]'<\/span>)\nlicense_dataset<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:547\/1*vvqQdi_jiaQ6uBPBoYb6Iw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"94c4\">We have 5 features, and we have split 20 examples.<br>\nLet\u2019s load the datasets into a Kangas DataGrid and visualize it in the UI:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"e913\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">dg_obj_detection = kg.DataGrid(licence_plate_dataset, name=<span class=\"hljs-string\">'Licence_plate_obj_detection'<\/span>)\n\ndg_obj_detection.show()<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*DjEXVvjaoAdtND9C8P4-Ag.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">License plate object detection dataset: Kangas UI<\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"0a82\">The image annotations are stored as dictionary objects in the \u2018<em class=\"qt\">objects\u2019 <\/em>column. Let\u2019s see the annotations of the first image:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"0f11\" class=\"qi ok fr pn b bf qj qk l ql qm\" data-selectable-paragraph=\"\">annotations = license_dataset[<span class=\"hljs-number\">0<\/span>][<span class=\"hljs-string\">'objects'<\/span>]\n\nannotations, <span class=\"hljs-built_in\">type<\/span>(annotations)<\/span><\/pre>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:439\/1*FCZK9J_rXI6kuhH2eG5YwA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">image annotations<\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"d3a6\">We can also view this clearly by clicking the object column of any of the images:<\/p>\n\n\n\n<figure class=\"wp-block-image mk ml mm mn mo mp mh mi paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*r64nG6KxGz5msoBbodaXkA.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image annotations: Kangas UI<\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading oj ok fr be ol om on gr oo op oq gu or os ot ou ov ow ox oy oz pa pb pc pd pe bj\" id=\"d7f9\">Final thoughts<\/h1>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp pf nf ng gs pg ni nj nk ph nm nn no pi nq nr ns pj nu nv nw fk bj\" id=\"cd7a\">This article taught us how to create and visualize image data on the Kangas UI. We can visualize image data more efficiently, quickly, and with few lines of code without complicated functions and libraries.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph nc nd fr be b gp ne nf ng gs nh ni nj nk nl nm nn no np nq nr ns nt nu nv nw fk bj\" id=\"0440\">While this is an overview, there is still more that we can do with Kangas. Be sure to explore more on the <a class=\"af nb\" href=\"https:\/\/github.com\/comet-ml\/kangas\/tree\/main\/examples\/hugging_face\/object_detection\" target=\"_blank\" rel=\"noopener ugc nofollow\">Kangas repo<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visualizing data to carry out a detailed EDA, especially for image data, is critical. In computer vision datasets, if we can view and compare the images across different views with their relevant metadata and transformations within a single and well-designed UI, we are one step ahead in solving a CV task. We In this article, [&hellip;]<\/p>\n","protected":false},"author":108,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[7],"tags":[],"coauthors":[206],"class_list":["post-8096","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create and visualize image data with Kangas for computer vision tasks - Comet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create and visualize image data with Kangas for computer vision tasks\" \/>\n<meta property=\"og:description\" content=\"Visualizing data to carry out a detailed EDA, especially for image data, is critical. In computer vision datasets, if we can view and compare the images across different views with their relevant metadata and transformations within a single and well-designed UI, we are one step ahead in solving a CV task. We In this article, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T18:26:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:04:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rE3bf23O8Ulf4szSw0-EMg.jpeg\" \/>\n<meta name=\"author\" content=\"Brian Mutea\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brian Mutea\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Create and visualize image data with Kangas for computer vision tasks - Comet","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks","og_locale":"en_US","og_type":"article","og_title":"Create and visualize image data with Kangas for computer vision tasks","og_description":"Visualizing data to carry out a detailed EDA, especially for image data, is critical. In computer vision datasets, if we can view and compare the images across different views with their relevant metadata and transformations within a single and well-designed UI, we are one step ahead in solving a CV task. We In this article, [&hellip;]","og_url":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-11-02T18:26:50+00:00","article_modified_time":"2025-04-24T17:04:41+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rE3bf23O8Ulf4szSw0-EMg.jpeg","type":"","width":"","height":""}],"author":"Brian Mutea","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Brian Mutea","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks\/"},"author":{"name":"Brian Mutea","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/45acdda6535e03a9542e665f23953c3b"},"headline":"Create and visualize image data with Kangas for computer vision tasks","datePublished":"2023-11-02T18:26:50+00:00","dateModified":"2025-04-24T17:04:41+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks\/"},"wordCount":859,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rE3bf23O8Ulf4szSw0-EMg.jpeg","articleSection":["Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks\/","url":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks","name":"Create and visualize image data with Kangas for computer vision tasks - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rE3bf23O8Ulf4szSw0-EMg.jpeg","datePublished":"2023-11-02T18:26:50+00:00","dateModified":"2025-04-24T17:04:41+00:00","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rE3bf23O8Ulf4szSw0-EMg.jpeg","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*rE3bf23O8Ulf4szSw0-EMg.jpeg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/create-and-visualize-image-data-with-kangas-for-computer-vision-tasks#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"Create and visualize image data with Kangas for computer vision tasks"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/45acdda6535e03a9542e665f23953c3b","name":"Brian Mutea","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/0008644e1041f4f2e48e3566c59bc055","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/11\/1652705747012-96x96.jpg","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/11\/1652705747012-96x96.jpg","caption":"Brian Mutea"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/brianmuteakgmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=8096"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8096\/revisions"}],"predecessor-version":[{"id":15461,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8096\/revisions\/15461"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=8096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=8096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=8096"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=8096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}