import picpac parser = argparse.ArgumentParser() # db path parser.add_argument("--db", default=None, help='') # output directory parser.add_argument("--out", default=None, help='') # file extention that's in the db parser.add_argument("--ext", default='.jpg', help='') args = parser.parse_args() assert args.db, 'please specify --db' assert args.out, 'please specify --out out_directory' db = picpac.Reader(args.db) sp.check_call('mkdir -p %s/images' % args.out, shell=True) sp.check_call('mkdir -p %s/labels' % args.out, shell=True) sp.check_call('mkdir -p %s/vis' % args.out, shell=True) for pk, _, _, fields in db: # pk is the serial number of image, integer image_path = '%s/images/%06d%s' % (args.out, pk, args.ext) label_path = '%s/labels/%06d.txt' % (args.out, pk) vis_path = '%s/vis/%06d.jpg' % (args.out, pk) image_buf = fields[0] # this is image buffer with open(image_path, 'wb') as f: f.write(image_buf)
anno = { "shapes": [ # annotation can contain multiple shapes { "type": "ellipse", "label": 1, "geometry": { "y": 0.384, "x": 0.419, "height": 0.377, "width": 0.281 } }, ] } db.append(buf, json.dumps(anno)) del db db = picpac.Reader('test.db') for label, _, _, fields in db: print("image size is %d" % len(fields[0])) if len(fields) == 1: print(" classification example, label = %f" % label) else: anno = json.loads(fields[1]) shapes = [s['type'] for s in anno['shapes']] print(" annotation example, annotation shapes are %s" % shapes) pass
for out_path, in_paths in DBS: print "Creating", out_path l = 1 try: os.remove(out_path) except: pass outb = picpac.Writer(out_path) for in_path in in_paths: # iterate one database inb = picpac.Reader(os.path.join('db', in_path)) cc = 0 for _, _, _, fields in inb: #print pid, label, len(fields) image_buf, anno_buf = fields arr = np.fromstring(image_buf, np.uint8) image = cv2.imdecode(arr, cv2.CV_LOAD_IMAGE_COLOR) # down size image image2 = cv2.resize(image, None, fx=RX, fy=RX) #print image.shape, '=>', image2.shape image_buf = cv2.imencode('.png', image2)[1].tostring() # original annotation doesn't have labels