1+ import os
2+ import cv2
3+ import json
4+ import pandas as pd
5+ import numpy as np
6+ from glob import glob
7+ from tqdm import tqdm
8+ from IPython import embed
9+ import base64
10+ from labelme import utils
11+ image_path = "./images/"
12+ csv_file = "./train_labels.csv"
13+ annotations = pd .read_csv (csv_file ,header = None ).values
14+ total_csv_annotations = {}
15+ for annotation in annotations :
16+ key = annotation [0 ].split (os .sep )[- 1 ]
17+ value = np .array ([annotation [1 :]])
18+ if key in total_csv_annotations .keys ():
19+ total_csv_annotations [key ] = np .concatenate ((total_csv_annotations [key ],value ),axis = 0 )
20+ else :
21+ total_csv_annotations [key ] = value
22+ for key ,value in total_csv_annotations .items ():
23+ height ,width = cv2 .imread (image_path + key ).shape [:2 ]
24+ labelme_format = {
25+ "version" :"3.6.16" ,
26+ "flags" :{},
27+ "lineColor" :[0 ,255 ,0 ,128 ],
28+ "fillColor" :[255 ,0 ,0 ,128 ],
29+ "imagePath" :key ,
30+ "imageHeight" :height ,
31+ "imageWidth" :width
32+ }
33+ with open (image_path + key ,"rb" ) as f :
34+ imageData = f .read ()
35+ imageData = base64 .b64encode (imageData ).decode ('utf-8' )
36+ #img = utils.img_b64_to_arr(imageData)
37+ labelme_format ["imageData" ] = imageData
38+ shapes = []
39+ for shape in value :
40+ label = shape [- 1 ]
41+ s = {"label" :label ,"line_color" :None ,"fill_color" :None ,"shape_type" :"rectangle" }
42+ points = [
43+ [shape [0 ],shape [1 ]],
44+ [shape [2 ],shape [3 ]]
45+ ]
46+ s ["points" ] = points
47+ shapes .append (s )
48+ labelme_format ["shapes" ] = shapes
49+ json .dump (labelme_format ,open ("%s/%s/" % (image_path ,key .replace (".jpg" ,".json" )),"w" ),ensure_ascii = False , indent = 2 )
0 commit comments