dataturks解析

"""
根据大json写小json
"""
with open('pay.json') as f:
    datas = f.readlines()
    for data in datas:
        data = data.strip()
        j = json.loads(data)
        name = j['content'].split('/')[-1].replace('jpg','json')
        fw = open('drinks/{}'.format(name),'w')
        json.dump(j,fw)


"""
根据下载好的标注文件转换为txt
"""
for dir in os.listdir('drinks'):
    datas = json.load(open(os.path.join('drinks',dir)))
    content = '/home/lixuan/Arduino/hand_detection/landmarks/' + datas['content'].split('/')[-1].split('landmarks_')[-1]
    annotations = datas['annotation']
    if annotations == None:
        print(dir)
        continue
    # img = cv2.imread(content)
    # cv2.imwrite('/home/lixuan/桌面/multicenternetcopy/testimg/{}'.format(datas['content'].split('/')[-1].split('landmarks_')[-1]),img)
    f = open('/home/lixuan/Arduino/hand_detection/landmarks/' + datas['content'].split('/')[-1].split('landmarks_')[-1].replace('jpg','txt'),'w')
    # fl = open('train_data/category/' + datas['content'].split('/')[-1].replace('jpg','txt'), 'w')
    for annotation in annotations:
        label = '0'#annotation['label'][0]
        # if label == 'box':
        #     label = '0'
        # else:
        #     label = '1'
        # if label == '1':
        # fl.write(label + ' ')
        points = annotation['points']
        imageWidth = annotation['imageWidth']
        imageHeight = annotation['imageHeight']
        for point in points:
            point[0] = int(float(point[0]) * imageWidth)
            point[1] = int(float(point[1]) * imageHeight)
        points = np.array(points)
        # points = points[[2, 3, 0, 1], :]#landmark
        # points = points[[1, 0, 3, 2], :]#92
        for point in points:
            f.write(str(point[0]) + ' ' + str(point[1]) + ' ')

def func(listTemp, n):
    for i in range(0, len(listTemp), n):
        yield listTemp[i:i + n]

imglist = []
labellist = []
for dir in os.listdir('/home/lixuan/data/landmarks'):
    if 'txt' in dir:
        labellist.append(dir)
    else:
        imglist.append(dir)
import cv2
for imgname in imglist:
    img = cv2.imread('/home/lixuan/data/action/{}'.format(imgname))
    H,W,_ = img.shape
    Point = []
    with open('/home/lixuan/data/landmarks/{}'.format(imgname.replace('jpg','txt'))) as f:
        datas = f.readlines()[0].strip().split(' ')#[:-1]
        for i in range(0, len(datas), 2):
            b = datas[i:i + 2]
            Point.append(b)
    LL = func(Point,4)
    fw = open('/home/lixuan/data/landmarks/{}'.format(imgname.replace('jpg','txt')),'w')
    fl = open('/home/lixuan/data/category/{}'.format(imgname.replace('jpg','txt')))
    for cls in fl.readlines():
        cls = cls.strip().split(' ')
    for index,l in enumerate(LL):
        X = []
        Y = []
        for p in l:
            X.append(int(p[0]))
            Y.append(int(p[1]))
        x1 = min(X)
        y1 = min(Y)
        x2 = max(X)
        y2 = max(Y)
        x = ((x1 + x2) / 2) / W
        y = ((y1 + y2) / 2) / H
        w = (x2 - x1) / W
        h = (y2 - y1) / H
        fw.write(cls[index] + ' ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')
import json
import os
import cv2
import numpy as np

# for dir in os.listdir('json1'):
#     newname = dir.split('drink1_')[-1]
#     os.system('mv /home/lixuan/centernettrain/json1/{} /home/lixuan/centernettrain/json1/{}'.format(dir,newname))

# with open('/home/lixuan/lhq/centernet/train_data/txt标注样列/0ba0e56d-e078-4019-b8d0-74e22fb076ad_3包_40.txt') as f:
#     datas = f.readlines()[0].split(' ')
#     print(len(datas))
#     exit()

"""
根据大json写小json
"""
# with open('front_back_shelf_527.json') as f:
#     datas = f.readlines()
#     for data in datas:
#         data = data.strip()
#         j = json.loads(data)
#         name = j['content'].split('/')[-1].replace('jpg','json')
#         fw = open('drinks/{}'.format(name),'w')
#         json.dump(j,fw)
# exit()
"""
根据下载好的标注文件转换为txt
"""
# for dir in os.listdir('json'):
#     datas = json.load(open(os.path.join('json',dir)))
#     content = '/home/lixuan/桌面/landmark/' + datas['content'].split('/')[-1]
#     annotations = datas['annotation']
#     img = cv2.imread(content)
#     cv2.imwrite('train_data/txt标注样列/{}'.format(datas['content'].split('/')[-1]),img)
#     f = open('train_data/txt标注样列/' + datas['content'].split('/')[-1].replace('jpg','txt'),'w')
#     fl = open('train_data/category/' + datas['content'].split('/')[-1].replace('jpg','txt'), 'w')
#     for annotation in annotations:
#         label = annotation['label'][0]
#         if label == 'box':
#             label = '0'
#         else:
#             label = '1'
#         # if label == '1':
#         fl.write(label + ' ')
#         points = annotation['points']
#         imageWidth = annotation['imageWidth']
#         imageHeight = annotation['imageHeight']
#         for point in points:
#             point[0] = int(float(point[0]) * imageWidth)
#             point[1] = int(float(point[1]) * imageHeight)
#         points = np.array(points)
#         # points = points[[2, 3, 0, 1], :]#landmark
#         # points = points[[1, 0, 3, 2], :]#92
#         for point in points:
#             f.write(str(point[0]) + ' ' + str(point[1]) + ' ')


"""
bbox
"""
# for dir in os.listdir('drinks'):
#     datas = json.load(open(os.path.join('drinks',dir)))
#     content = '/home/lixuan/fromLX/coco/images/train/' + datas['content'].split('/')[-1]#.split('landmarks_')[-1]
#     annotations = datas['annotation']
#     if annotations == None:
#         print(dir)
#         continue
#     # img = cv2.imread(content)
#     # cv2.imwrite('/home/lixuan/桌面/multicenternetcopy/testimg/{}'.format(datas['content'].split('/')[-1].split('landmarks_')[-1]),img)
#     f = open('/home/lixuan/centernettrain/train_data/txt标注样列/' + datas['content'].split('/')[-1].replace('jpg','txt'),'w')#.split('landmarks_')[-1]
#     fl = open('./cate/' + datas['content'].split('/')[-1].replace('jpg','txt'),'w')
#     for annotation in annotations:
#         label = annotation['label'][0]
#         if label == 'front':
#             label = '1'
#         else:
#             label = '0'
#         # elif label == 'pay':
#         #     label = '1'
#         # elif label == 'handthing':
#         #     label = '2'
#         # elif label == 'handphone':
#         #     label = '3'
#         # elif label == 'handscanning':
#         #     label = '4'
#         # if label == '1':
#         fl.write(label + ' ')
#         points = annotation['points']
#         imageWidth = annotation['imageWidth']
#         imageHeight = annotation['imageHeight']
#         for point in points:
#             point[0] = int(float(point[0]) * imageWidth)
#             point[1] = int(float(point[1]) * imageHeight)
#         points = np.array(points)
#         # points = points[[2, 3, 0, 1], :]#landmark
#         # points = points[[1, 0, 3, 2], :]#92
#         for point in points:
#             f.write(str(point[0]) + ' ' + str(point[1]) + ' ')

# for dir in os.listdir('json'):
#     datas = json.load(open(os.path.join('json',dir)))
#     content = '/home/lixuan/lhq/new_wine/' + datas['content'].split('/')[-1]
#     annotations = datas['annotation']
#     # img = cv2.imread(content)
#     # cv2.imwrite('train_data/txt标注样列/{}'.format(datas['content'].split('/')[-1]),img)
#     f = open('train_data/txt标注样列/' + datas['content'].split('/')[-1].replace('jpg','txt'),'w')
#     for annotation in annotations:
#         label = annotation['label'][0]
#         f.write(label + ' ')
#         points = annotation['points']
#         imageWidth = annotation['imageWidth']
#         imageHeight = annotation['imageHeight']
#         for point in points:
#             point[0] = int(float(point[0]) * imageWidth)
#             point[1] = int(float(point[1]) * imageHeight)
#         points = np.array(points)
#         points = points[[2, 3, 0, 1], :]
#         for point in points:
#             f.write(str(point[0]) + ' ' + str(point[1]) + ' ')


"""
查看
"""
# imglist = []
# labellist = []
# for dir in os.listdir('/home/lixuan/centernettrain/drinks/txt标注样列'):
#     if 'txt' in dir:
#         labellist.append(dir)
#     else:
#         imglist.append(dir)
# import cv2
# for imgname in imglist:
#     img = cv2.imread('/home/lixuan/centernettrain/drinks/txt标注样列/{}'.format(imgname))
#     Point = []
#     with open('/home/lixuan/centernettrain/drinks/txt标注样列/{}'.format(imgname.replace('jpg','txt'))) as f:
#         datas = f.readlines()[0].strip().split(' ')#[:-1]
#         for i in range(0, len(datas), 2):
#             b = datas[i:i + 2]
#             Point.append(b)
#     for point in Point:
#         img = cv2.circle(img,(int(point[0]),int(point[1])),3,(255,0,0),2)
#         image = cv2.resize(img,(1000,800))
#         cv2.imshow('',image)
#         cv2.waitKey(0)
# print(imglist)
# print(labellist)
"""
bbox
"""

def func(listTemp, n):
    for i in range(0, len(listTemp), n):
        yield listTemp[i:i + n]

imglist = []
labellist = []

for dir in os.listdir('drinks'):
    datas = json.load(open(os.path.join('drinks',dir)))
    content = datas['content'].split('/')[-1]#.split('landmarks_')[-1]
    imglist.append(content)
    labellist.append(content.split('.')[0] + '.txt')

# for dir in os.listdir('/home/lixuan/fromLX/coco/images/train'):
#     if 'txt' in dir:
#         labellist.append(dir)
#     else:
#         imglist.append(dir)

import cv2
for imgname in imglist:
    if True:#imgname == '2_img115.jpg':
        try:
            img = cv2.imread('/home/lixuan/fromLX/coco/images/train/{}'.format(imgname))
            H,W,_ = img.shape
            Point = []
            with open('/home/lixuan/centernettrain/train_data/txt标注样列/{}'.format(imgname.replace('jpg','txt'))) as f:
                datas = f.readlines()[0].strip().split(' ')#[:-1]
                for i in range(0, len(datas), 2):
                    b = datas[i:i + 2]
                    Point.append([int(x) for x in b])
            LL = func(Point,4)
            fw = open('/home/lixuan/centernettrain/train_data/category/{}'.format(imgname.replace('jpg','txt')),'w')
            fl = open('./cate/{}'.format(imgname.replace('jpg','txt')))
            for cls in fl.readlines():
                cls = cls.strip().split(' ')
            for index,l in enumerate(LL):
                xs = []
                ys = []
                for p in l:
                    xs.append(p[0])
                    ys.append(p[1])
                x1 = min(xs)
                y1 = min(ys)
                x2 = max(xs)
                y2 = max(ys)
                x = ((x1 + x2) / 2) / W
                y = ((y1 + y2) / 2) / H
                w = (x2 - x1) / W
                h = (y2 - y1) / H
                fw.write(cls[index] + ' ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')
                # img = cv2.rectangle(img,(int(l[0][0]),int(l[0][1])),(int(l[2][0]),int(l[2][1])),(255,0,0),2)
                # image = cv2.resize(img,(1000,800))
                # cv2.imshow('',img)
                # cv2.waitKey(0)
        except Exception as e:
            print(e)
# print(imglist)
# print(labellist)

# image = cv2.imread('train_data/txt标注样列/landmark_6.jpg')
# with open('train_data/txt标注样列转为json/annotations/instances.json') as f:
#     Dict = json.load(f)
#     annotations = Dict['annotations']
#     font = cv2.FONT_HERSHEY_SIMPLEX
#     for annotation in annotations:
#         bbox = annotation['bbox']
#         category = annotation['category_id']
#         image = cv2.putText(image, str(category), (bbox[0], bbox[1]), font, 1.2, (255, 0,0), 2)
#     cv2.imwrite('test.jpg',image)

# image = cv2.imread('train_data/txt标注样列/landmark_6.jpg')
# with open('train_data/txt标注样列转为json/annotations/instances.json') as f:
#     Dict = json.load(f)
#     annotations = Dict['annotations']
#     font = cv2.FONT_HERSHEY_SIMPLEX
#     for annotation in annotations:
#         segmentation = annotation['segmentation'][0]
#         image = cv2.putText(image, '0', (segmentation[0], segmentation[1]), font, 1.2, (255, 0,0), 2)
#         image = cv2.putText(image, '1', (segmentation[2], segmentation[3]), font, 1.2, (255, 0, 0), 2)
#         image = cv2.putText(image, '2', (segmentation[4], segmentation[5]), font, 1.2, (255, 0, 0), 2)
#         image = cv2.putText(image, '3', (segmentation[6], segmentation[7]), font, 1.2, (255, 0, 0), 2)
#     cv2.imwrite('test.jpg',image)

# with open('train_data/txt标注样列转为json/annotations/instances.json') as f:
#     Dict = json.load(f)
#     images = Dict['images']
#     annotations = Dict['annotations']
#     for annotation in annotations:
#         print(annotation)

# import os
# base = '/home/lixuan/lhq/centernet/train_data/txt标注样列/landmark_12.txt'
# for i in range(12):
#     os.system('cp {} /home/lixuan/lhq/centernet/train_data/txt标注样列/landmark_{}.txt'.format(base,i))

# fw = open('2007_train.txt','w')
# for dir in os.listdir('dd'):
#     with open('dd/' + dir) as f:
#         fw.write(f.read() + '\n')
# from PIL import Image
# imgpath = '/home/lixuan/桌面/json/images/'
# f = open('winelabel.json')
# save_path = '/home/lixuan/lhq/new_wine/'
# for data in f.readlines():
#     data = data.strip()
#     data = json.loads(data)
#     imgname = data['content'].split('wine2/')[-1]
#     img = Image.open(imgpath + imgname)
#     img.save(save_path + imgname)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值