Exemplo n.º 1
0
def siamese_process_batch_rgb(lines, img_path, train=True):
    num = len(lines)
    batch = np.zeros((num, 16, 112, 112, 3), dtype='float32')
    btcof = np.zeros((num, 16, 112, 112, 3), dtype='float32')
    labels = np.zeros(num, dtype='int')
    for i in range(num):
        path = lines[i].split(' ')[0]
        label = lines[i].split(' ')[-1]
        symbol = lines[i].split(' ')[1]
        label = label.strip('\n')
        label = int(label)
        symbol = int(symbol) - 1
        # imgs = os.listdir(img_path+path)
        imgs = glob.glob(img_path + path + '/*.jpg')
        if imgs == []:
            imgs = glob.glob(img_path + path + '/*.png')
        imgs.sort(key=str.lower)
        if (symbol + 16) >= len(imgs):
            continue
        if train:
            crop_x = random.randint(0, 10)
            crop_y = random.randint(0, 10)
            tran_x = random.randint(0, 10)
            tran_y = random.randint(0, 10)
            is_flip = random.randint(0, 1)
            init_f = random.randint(0, 8)
            init_f = 0
            if (symbol + 16 + init_f) >= len(imgs):
                init_f = 0

            sc = random.uniform(0.2, 1)

            for j in range(16):
                img = imgs[symbol + j + init_f]
                # image = cv2.imread(img_path + path + '/' + img)
                image = cv2.imread(img)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

                im_of = np.load(img + '.npy')
                mag, ang = cv2.cartToPolar(im_of[..., 0], im_of[..., 1])
                im_of = np.dstack((im_of, mag))
                # im_of *= 16
                im_of = cv2.normalize(im_of, None, -1, 1, cv2.NORM_MINMAX)

                # Re-scale
                if doScale:
                    hh, ww = image.shape[0:2]
                    hh = int(hh * sc)
                    ww = int(ww * sc)
                    image = cv2.resize(image, (ww, hh))
                    im_of = cv2.resize(im_of, (ww, hh))

                # image = cv2.resize(image, (171, 128))
                if is_flip == 1 and doFlip:
                    image = cv2.flip(image, 1)
                    im_of = cv2.flip(im_of, 1)

                if doTrans:
                    hh, ww = image.shape[0:2]
                    M = np.float32([[1, 0, tran_x], [0, 1, tran_y]])
                    image = cv2.warpAffine(image, M, (ww, hh))
                    im_of = cv2.warpAffine(im_of, M, (ww, hh))

                if doNoisy:
                    noisy(image, 'gauss')
                    noisy(im_of, 'gauss')

                # cv2.imshow('opticalflow',im_of[crop_x:crop_x + 112, crop_y:crop_y + 112, :])
                # cv2.waitKey(1)

                batch[i][j][:][:][:] = image[crop_x:crop_x + 112,
                                             crop_y:crop_y + 112, :]
                btcof[i][j][:][:][:] = im_of[crop_x:crop_x + 112,
                                             crop_y:crop_y + 112, :]
            labels[i] = label
        else:
            for j in range(16):
                img = imgs[symbol + j]
                # image = cv2.imread(img_path + path + '/' + img)
                image = cv2.imread(img)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = cv2.resize(image, (171, 128))
                im_of = np.load(img + '.npy')
                mag, ang = cv2.cartToPolar(im_of[..., 0], im_of[..., 1])
                im_of = np.dstack((im_of, mag))
                im_of = cv2.normalize(im_of, None, -1, 1, cv2.NORM_MINMAX)
                batch[i][j][:][:][:] = image[8:120, 30:142, :]
                btcof[i][j][:][:][:] = im_of[8:120, 30:142, :]
            labels[i] = label
    return batch, btcof, labels
Exemplo n.º 2
0
def process_batch_rgb(lines, img_path, train=True):
    num = len(lines)
    batch = np.zeros((num, 16, 112, 112, 3), dtype='float32')
    labels = np.zeros(num, dtype='int')
    for i in range(num):
        path = lines[i].split(' ')[0]
        label = lines[i].split(' ')[-1]
        symbol = lines[i].split(' ')[1]
        label = label.strip('\n')
        label = int(label)
        symbol = int(symbol) - 1
        # imgs = os.listdir(img_path+path)
        imgs = glob.glob(img_path + path + '/*.jpg')
        if imgs == []:
            imgs = glob.glob(img_path + path + '/*.png')
        imgs = [k for k in imgs if '.wav' not in k and 'flow' not in k]
        imgs.sort(key=str.lower)
        if (symbol + 16) >= len(imgs):
            continue
        if train:
            crop_x = random.randint(0, 10)
            crop_y = random.randint(0, 10)
            tran_x = random.randint(0, 10)
            tran_y = random.randint(0, 10)
            is_flip = random.randint(0, 1)
            init_f = random.randint(0, 8)
            init_f = 0
            if (symbol + 16 + init_f) >= len(imgs):
                init_f = 0

            sc = random.uniform(0.2, 1)

            for j in range(16):
                img = imgs[symbol + j + init_f]
                # image = cv2.imread(img_path + path + '/' + img)
                image = cv2.imread(img)
                # Re-scale
                if doScale:
                    hh, ww = image.shape[0:2]
                    hh = int(hh * sc)
                    ww = int(ww * sc)
                    image = cv2.resize(image, (ww, hh))
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = cv2.resize(image, (171, 128))
                if is_flip == 1 and doFlip:
                    image = cv2.flip(image, 1)

                if doTrans:
                    hh, ww = image.shape[0:2]
                    M = np.float32([[1, 0, tran_x], [0, 1, tran_y]])
                    image = cv2.warpAffine(image, M, (ww, hh))

                if doNoisy:
                    noisy(image, 'gauss')

                batch[i][j][:][:][:] = image[crop_x:crop_x + 112,
                                             crop_y:crop_y + 112, :]
            labels[i] = label
        else:
            for j in range(16):
                img = imgs[symbol + j]
                # image = cv2.imread(img_path + path + '/' + img)
                image = cv2.imread(img)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                image = cv2.resize(image, (171, 128))
                batch[i][j][:][:][:] = image[8:120, 30:142, :]
            labels[i] = label
    return batch, labels
Exemplo n.º 3
0
def siamese_process_batch(lines, img_path, train=True):
    num = len(lines)
    batch = np.zeros((num, 16, 112, 112, 3), dtype='float32')
    btcof = np.zeros((num, 16, 112, 112, 3), dtype='float32')
    labels = np.zeros(num, dtype='int')
    for i in range(num):
        path = lines[i].split(' ')[0]
        label = lines[i].split(' ')[-1]
        symbol = lines[i].split(' ')[1]
        label = label.strip('\n')
        label = int(label)
        symbol = int(symbol) - 1
        if not os.path.isfile(path + '/{:06d}'.format(symbol + 16) + '.jpg'):
            continue

        file_path = path + '/{:06d}'.format(symbol)
        flow_ext = ''
        use_npy = True
        if isfile(file_path + '.npy'):
            flow_ext = '.npy'
        elif isfile(file_path + '.npz'):
            flow_ext = '.npz'
        else:
            use_npy = False
            flow_ext = '_flow.jpg'

        if train:
            crop_x = random.randint(0, 10)
            crop_y = random.randint(0, 10)
            tran_x = random.randint(0, 10)
            tran_y = random.randint(0, 10)
            is_flip = random.randint(0, 1)

            sc = random.uniform(0.2, 1)

            for j in range(16):
                img_file = path + '/{:06d}'.format(symbol + j) + '.jpg'
                image = cv2.imread(img_file)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

                if use_npy:
                    im_of = np.load(file_path + flow_ext)
                else:
                    im_of = cv2.imread(file_path + flow_ext)
                if im_of.shape[2] == 2:
                    mag, ang = cv2.cartToPolar(im_of[..., 0], im_of[..., 1])
                    im_of = np.dstack((im_of, mag))
                # im_of *= 16
                im_of = cv2.normalize(im_of, None, -1, 1, cv2.NORM_MINMAX)
                # im_of = cv2.resize(im_of, (img_w, img_h))

                # Re-scale
                if doScale:
                    hh, ww = image.shape[0:2]
                    hh = int(hh * sc)
                    ww = int(ww * sc)
                    image = cv2.resize(image, (ww, hh))
                    im_of = cv2.resize(im_of, (ww, hh))

                # image = cv2.resize(image, (171, 128))
                if is_flip == 1 and doFlip:
                    image = cv2.flip(image, 1)
                    im_of = cv2.flip(im_of, 1)

                if doTrans:
                    hh, ww = image.shape[0:2]
                    M = np.float32([[1, 0, tran_x], [0, 1, tran_y]])
                    image = cv2.warpAffine(image, M, (ww, hh))
                    im_of = cv2.warpAffine(im_of, M, (ww, hh))

                if doNoisy:
                    noisy(image, 'gauss')
                    noisy(im_of, 'gauss')

                # cv2.imshow('opticalflow',im_of[crop_x:crop_x + 112, crop_y:crop_y + 112, :])
                # cv2.waitKey(1)

                image = cv2.resize(image[crop_x:, crop_y:, :],
                                   (kernel_w, kernel_h))
                im_of = cv2.resize(im_of[crop_x:, crop_y:, :],
                                   (kernel_w, kernel_h))
                batch[i][j][:][:][:] = image
                btcof[i][j][:][:][:] = im_of
            labels[i] = label
        else:
            for j in range(16):
                file_path = path + '/{:06d}'.format(symbol + j)
                image = cv2.imread(file_path + '.jpg')
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

                # if not os.path.isfile(file_path + flow_ext):
                #     print(file_path + flow_ext)
                if use_npy:
                    im_of = np.load(file_path + flow_ext)
                else:
                    im_of = cv2.imread(file_path + flow_ext)

                if im_of.shape[2] == 2:
                    mag, ang = cv2.cartToPolar(im_of[..., 0], im_of[..., 1])
                    im_of = np.dstack((im_of, mag))
                im_of = cv2.normalize(im_of, None, -1, 1, cv2.NORM_MINMAX)

                image = cv2.resize(image, (kernel_w, kernel_h))
                im_of = cv2.resize(im_of, (kernel_w, kernel_h))
                batch[i][j][:][:][:] = image
                btcof[i][j][:][:][:] = im_of
            labels[i] = label
    return batch, btcof, labels
Exemplo n.º 4
0
def process_batch_opt_flow(lines, img_path, train=True):
    num = len(lines)
    btcof = np.zeros((num, 16, 112, 112, 3), dtype='float32')
    labels = np.zeros(num, dtype='int')
    for i in range(num):
        path = lines[i].split(' ')[0]
        label = lines[i].split(' ')[-1]
        symbol = lines[i].split(' ')[1]
        label = label.strip('\n')
        label = int(label)
        symbol = int(symbol) - 1
        # imgs = os.listdir(img_path+path)
        imgs = glob.glob(img_path + path + '/*.jpg')
        if imgs == []:
            imgs = glob.glob(img_path + path + '/*.png')
        imgs.sort(key=str.lower)
        if (symbol + 16) >= len(imgs):
            continue
        if train:
            crop_x = random.randint(0, 10)
            crop_y = random.randint(0, 10)
            tran_x = random.randint(0, 10)
            tran_y = random.randint(0, 10)
            is_flip = random.randint(0, 1)
            init_f = random.randint(0, 8)
            init_f = 0
            if (symbol + 16 + init_f) >= len(imgs):
                init_f = 0

            sc = random.uniform(0.2, 1)

            for j in range(16):
                img_file = imgs[symbol + j + init_f]
                im_of = np.load(img_file + '.npy')
                im_of *= 16
                im_of = cv2.normalize(im_of, None, 0, 255, cv2.NORM_MINMAX)

                # Re-scale
                if doScale:
                    hh, ww = im_of.shape[0:2]
                    hh = int(hh * sc)
                    ww = int(ww * sc)
                    im_of = cv2.resize(im_of, (ww, hh))

                # image = cv2.resize(image, (171, 128))
                if is_flip == 1 and doFlip:
                    im_of = cv2.flip(im_of, 1)

                if doTrans:
                    hh, ww = im_of.shape[0:2]
                    M = np.float32([[1, 0, tran_x], [0, 1, tran_y]])
                    im_of = cv2.warpAffine(im_of, M, (ww, hh))

                if doNoisy:
                    noisy(im_of, 'gauss')

                btcof[i][j][:][:][:] = im_of[crop_x:crop_x + 112,
                                             crop_y:crop_y + 112, :]
            labels[i] = label
        else:
            for j in range(16):
                img_file = imgs[symbol + j]
                # image = cv2.imread(img_path + path + '/' + img_file)
                im_of = np.load(img_file + '.npy')
                btcof[i][j][:][:][:] = im_of[8:120, 30:142, :]
            labels[i] = label
    return btcof, labels
Exemplo n.º 5
0
def siamese_process_batch(lines, img_path, train=True):
    num = len(lines)
    batch = np.zeros((num, 16, kernel_w, kernel_h, 3), dtype='float32')
    btcof = np.zeros((num, 16, kernel_w, kernel_h, 3), dtype='float32')
    labels = np.zeros(num, dtype='int')
    for i in range(num):
        path = lines[i].split(' ')[0]
        label = lines[i].split(' ')[-1]
        symbol = lines[i].split(' ')[1]
        label = label.strip('\n')
        label = int(label)
        symbol = int(symbol) - 1
        # imgs = os.listdir(img_path+path)
        imgs = glob.glob(img_path + path + '/*.jpg')
        if imgs == []:
            imgs = glob.glob(img_path + path + '/*.png')
        imgs.sort(key=str.lower)
        if (symbol + 16) >= len(imgs):
            continue
        if train:
            # crop_x = random.randint(0, 15)
            # crop_y = random.randint(0, 58)
            # is_flip = random.randint(0, 1)
            crop_x = random.randint(0, 10)
            crop_y = random.randint(0, 10)
            tran_x = random.randint(0, 10)
            tran_y = random.randint(0, 10)
            is_flip = random.randint(0, 1)

            sc = random.uniform(0.2, 1)

            for j in range(16):
                # crop_x = random.randint(0, 15)  # add detection noise
                # crop_y = random.randint(0, 58)
                img = imgs[symbol + j]
                # image = cv2.imread(img_path + path + '/' + img)
                image = cv2.imread(img)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                # image = cv2.resize(image, (img_w, img_h))

                im_of = np.load(img + '.npy') if isfile(
                    img + '.npy') else np.load(img[:-4] + '.npz')['flow']
                if im_of.shape[2] == 2:
                    mag, ang = cv2.cartToPolar(im_of[..., 0], im_of[..., 1])
                    im_of = np.dstack((im_of, mag))
                # im_of *= 16
                im_of = cv2.normalize(im_of, None, -1, 1, cv2.NORM_MINMAX)
                # im_of = cv2.resize(im_of, (img_w, img_h))

                image = cv2.resize(image, (kernel_w, kernel_h))
                im_of = cv2.resize(im_of, (kernel_w, kernel_h))
                batch[i][j][:][:][:] = image
                btcof[i][j][:][:][:] = im_of
                continue

                # Re-scale
                if doScale:
                    hh, ww = image.shape[0:2]
                    hh = int(hh * sc)
                    ww = int(ww * sc)
                    image = cv2.resize(image, (ww, hh))
                    im_of = cv2.resize(im_of, (ww, hh))

                if is_flip == 1 and doFlip:
                    image = cv2.flip(image, 1)
                    im_of = cv2.flip(im_of, 1)

                if doTrans:
                    hh, ww = image.shape[0:2]
                    M = np.float32([[1, 0, tran_x], [0, 1, tran_y]])
                    image = cv2.warpAffine(image, M, (ww, hh))
                    im_of = cv2.warpAffine(im_of, M, (ww, hh))

                if doNoisy:
                    noisy(image, 'gauss')
                    noisy(im_of, 'gauss')

                batch[i][j][:][:][:] = image[crop_x:crop_x + kernel_w,
                                             crop_y:crop_y + kernel_h, :]
                btcof[i][j][:][:][:] = im_of[crop_x:crop_x + kernel_w,
                                             crop_y:crop_y + kernel_h, :]
            labels[i] = label
        else:
            for j in range(16):
                img = imgs[symbol + j]
                # image = cv2.imread(img_path + path + '/' + img)
                image = cv2.imread(img)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                im_of = np.load(img + '.npy') if isfile(
                    img + '.npy') else np.load(img[:-4] + '.npz')['flow']
                if im_of.shape[2] == 2:
                    mag, ang = cv2.cartToPolar(im_of[..., 0], im_of[..., 1])
                    im_of = np.dstack((im_of, mag))
                im_of = cv2.normalize(im_of, None, -1, 1, cv2.NORM_MINMAX)

                image = cv2.resize(image, (kernel_w, kernel_h))
                im_of = cv2.resize(im_of, (kernel_w, kernel_h))
                batch[i][j][:][:][:] = image
                btcof[i][j][:][:][:] = im_of
                continue

                image = cv2.resize(image, (img_w, img_h))
                im_of = cv2.resize(im_of, (img_w, img_h))
                h_start = (img_h - kernel_h) // 2
                h_end = h_start + kernel_h
                w_start = (img_w - kernel_w) // 2
                w_end = w_start + kernel_w
                batch[i][j][:][:][:] = image[h_start:h_end, w_start:w_end, :]
                btcof[i][j][:][:][:] = im_of[h_start:h_end, w_start:w_end, :]
            labels[i] = label
    return batch, btcof, labels