{ "cells": [ { "metadata": { "ExecuteTime": { "end_time": "2024-08-28T07:38:11.097948Z", "start_time": "2024-08-28T07:38:10.051274Z" } }, "cell_type": "code", "source": [ "import json\n", "\n", "from ultralytics import YOLO\n", "import os\n", "\n", "from export_prediction_to_label_studio import export_one_yolo_output_to_label_studio_rectangles" ], "id": "688b209f5c7b833e", "outputs": [], "execution_count": 1 }, { "metadata": { "ExecuteTime": { "end_time": "2024-08-28T07:38:24.127255Z", "start_time": "2024-08-28T07:38:23.889160Z" } }, "cell_type": "code", "source": [ "%load_ext autoreload\n", "%autoreload 2" ], "id": "bcbe8835eff23931", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n" ] } ], "execution_count": 3 }, { "metadata": { "ExecuteTime": { "end_time": "2024-08-28T07:38:24.905478Z", "start_time": "2024-08-28T07:38:24.890872Z" } }, "cell_type": "code", "source": "yolo_model_loaded = YOLO(\"shared/yolov8n-fashionpedia-1.torchscript\", task='detect')", "id": "f427e4ba2cff784", "outputs": [], "execution_count": 4 }, { "metadata": { "ExecuteTime": { "end_time": "2024-08-28T07:38:31.339742Z", "start_time": "2024-08-28T07:38:31.325405Z" } }, "cell_type": "code", "source": "root_dataset_folder = '/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k'", "id": "f6b614baa08e709", "outputs": [], "execution_count": 5 }, { "metadata": { "ExecuteTime": { "end_time": "2024-08-28T07:42:03.489627Z", "start_time": "2024-08-28T07:39:47.352446Z" } }, "cell_type": "code", "source": [ "for root, dirs, files in os.walk(os.path.join(root_dataset_folder, 'images')):\n", " print(root)\n", " for a_file in files:\n", " if a_file.endswith('.jpg') or a_file.endswith('.jpeg') or a_file.endswith('.png'):\n", " result = yolo_model_loaded([os.path.join(root, a_file)], verbose=False)[0]\n", " current_folder = root.split('/')[-1]\n", " export_one_yolo_output_to_label_studio_rectangles(\n", " result,\n", " 'yolov8n-fashionpedia-1-torchscript-c98616b267958c24874073ce091a977ce25489f5',\n", " f'/data/local-files/?d=clothes_pattern/images/{current_folder}/{a_file}',\n", " root.replace('images/', 'pre-annotations/'),\n", " label_studio_from_name='clothes_pattern'\n", " )" ], "id": "66db1d64f1f26295", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/plisse_drape_fronce\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/jersey cotele\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/non_classe\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/rayure\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/motif repete\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/liberty\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/motif place\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/pied de poule\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/animal\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/pois\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/pois_monochrome\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/carreau\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/paillette\n", "/home/louis/vetement_ai/ai_that_look/pattern_detection/dataset/dataset-clothes-8-pattern-v0-labeled-15k/images/floral\n" ] } ], "execution_count": 8 }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "513093f0ca4ac231" }, { "metadata": { "ExecuteTime": { "end_time": "2024-08-07T14:10:47.957914Z", "start_time": "2024-08-07T14:10:47.920287Z" } }, "cell_type": "code", "source": [ "kids_output = [\n", " \"body\",\n", " \"chemise\",\n", " \"combinaison\",\n", " \"gigoteuse\",\n", " \"gilet\",\n", " \"jupe\",\n", " \"pantalon\",\n", " \"pyjama\",\n", " \"robe\",\n", " \"tshirt\"\n", "]\n", "\n", "attributes_to_transform_default = {\n", " \"shirt, blouse\": \"chemise\",\n", " \"top, t-shirt, sweatshirt\": \"tshirt\",\n", " \"sweater\": \"gilet\",\n", " \"cardigan\": \"gilet\",\n", " \"jacket\": \"gilet\",\n", " \"vest\": \"gilet\",\n", " \"pants\": \"pantalon\",\n", " \"shorts\": \"pantalon\",\n", " \"skirt\": \"jupe\",\n", " \"coat\": \"gilet\",\n", " \"dress\": \"robe\",\n", " \"jumpsuit\": \"combinaison\",\n", "}\n", "attributes_to_keep = {\n", " \"sleeve\": \"manche\",\n", " \"pocket\": \"poche\",\n", " \"buckle\": \"zip\",\n", " \"collar\": \"col\",\n", " \"lapel\": \"revers\",\n", " \"hood\": \"capuche\",\n", "}" ], "id": "aa14e86f94358cb1", "outputs": [], "execution_count": 27 }, { "metadata": { "ExecuteTime": { "end_time": "2024-08-07T14:53:59.250707Z", "start_time": "2024-08-07T14:53:57.334490Z" } }, "cell_type": "code", "source": [ "attributes_to_transform = {k: 'pyjama' for k in attributes_to_transform_default.keys()}\n", "\n", "attributes_to_label = {\n", " **attributes_to_transform,\n", " **attributes_to_keep,\n", "}\n", "\n", "assert len(set(attributes_to_transform.keys()).intersection(set(attributes_to_transform.values()))) == 0\n", "\n", "score_threshold = 0.5\n", "for root, dirs, files in os.walk(os.path.join(root_dataset_folder, 'pre-annotations', 'pyjama')):\n", " for a_file in files:\n", " if a_file.endswith('.json'):\n", " filepath = os.path.join(root, a_file)\n", " with open(filepath, 'r') as f:\n", " file_content_json = json.load(f)\n", "\n", " predictions = file_content_json['predictions']\n", " annotations = predictions[0]['result']\n", " annotations = [p for p in annotations if p['score'] >= score_threshold]\n", "\n", " updated_annotations = []\n", " for an_annotation in annotations:\n", " rectangle_labels = an_annotation['value']['rectanglelabels']\n", "\n", " updated_labels = []\n", " for a_label in rectangle_labels:\n", " if a_label in attributes_to_label.keys():\n", " updated_labels.append(attributes_to_label[a_label])\n", " elif a_label in attributes_to_label.values():\n", " # to make it idempotent\n", " updated_labels.append(a_label)\n", "\n", " if len(updated_labels) > 0:\n", " an_annotation['value']['rectanglelabels'] = updated_labels\n", " updated_annotations.append(an_annotation)\n", "\n", " with open(filepath, 'w') as f:\n", " json.dump({\n", " **file_content_json,\n", " 'predictions': [\n", " {\n", " **predictions[0],\n", " \"result\": updated_annotations\n", " }\n", " ]\n", " }, f)" ], "id": "4df42b4df9ca7f17", "outputs": [], "execution_count": 43 }, { "metadata": { "ExecuteTime": { "end_time": "2024-08-28T07:44:23.257797Z", "start_time": "2024-08-28T07:44:22.988904Z" } }, "cell_type": "code", "source": [ "# largest surface to specific label\n", "score_threshold = 0.5\n", "folder = 'rayure'\n", "for root, dirs, files in os.walk(os.path.join(root_dataset_folder, 'pre-annotations', folder)):\n", " for a_file in files:\n", " if a_file.endswith('.json'):\n", " filepath = os.path.join(root, a_file)\n", " with open(filepath, 'r') as f:\n", " file_content_json = json.load(f)\n", "\n", " predictions = file_content_json['predictions']\n", " annotations = predictions[0]['result']\n", " annotations = list(filter(lambda x: x[\"score\"] > 0.5, annotations))\n", " if len(annotations) > 0: \n", " only_annotation = max(annotations, key=lambda p: p['value']['width'] * p['value']['height'])\n", " only_annotation['value']['rectanglelabels'] = [folder]\n", " result = [only_annotation]\n", " else:\n", " result = []\n", "\n", " with open(filepath, 'w') as f:\n", " json.dump({\n", " **file_content_json,\n", " 'predictions': [\n", " {\n", " **predictions[0],\n", " \"result\": result\n", " }\n", " ]\n", " }, f)" ], "id": "bba8c29714c2ab7b", "outputs": [], "execution_count": 21 }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": "", "id": "262fe6ef762f57d1" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }