Esempio n. 1
0
    def run(self):
        super().run()

        search_query = self.args['query']
        num_of_images = self.args['num_of_images']

        folder = '{}_{}'.format(num_of_images, orig_time)

        if not os.path.exists('../data/raw/{}'.format(search_query)):
            os.mkdir('../data/raw/{}'.format(search_query))
        if not os.path.exists('../data/raw/{}/{}'.format(search_query,
                                                         folder)):
            os.mkdir('../data/raw/{}/{}'.format(search_query, folder))

        photos = flickr.walk(text=search_query,
                             tag_mode='all',
                             tags=search_query,
                             extras='url_c',
                             per_page=num_of_images,
                             sort='relevance')
        progress_bar = ProgressBar(num_of_images, log=self.log)

        for i, photo in enumerate(photos):
            try:
                url = photo.get('url_c')
                img = download_image_from_url(url)
                cv2.imwrite(
                    '../data/raw/{}/{}/img_{}_{}.png'.format(
                        search_query, folder, i, int(time.time())), img)
            except:
                pass
            if progress_bar.increment(): break
Esempio n. 2
0
    def run(self):
        super().run()
        query = args['query']

        options = os.listdir('../data/preprocessed/' + query)
        ind = -1
        if len(options) == 1:
            ind = 0
        else:
            for i, option in enumerate(options):
                print('({}) {}'.format(i, option))
            ind = input('Enter your option index: ')
            while not ind.isdigit() or int(ind) < 0 or int(ind) >= len(options):
                ind = input('Please enter a valid option: ')
            ind = int(ind)
        img_dir = '../data/preprocessed/' + query + '/' + options[ind]
        
        img_names = os.listdir(img_dir)
        progress_bar = ProgressBar(len(img_names), log=self.log)

        if not os.path.exists('../data/loaded/' + query):
            os.mkdir('../data/loaded/{}'.format(query))

        images = []

        while True:
            img = cv2.imread(img_dir + '/' + img_names[progress_bar.i])
            images.append(img)
            if progress_bar.increment(): break

        timer = Timer(log=self.log)

        images = timer.time(lambda:np.array(images), 'Converting to numpy array')

        timer.time(lambda:np.save('../data/loaded/{}/{}'.format(query, options[ind] + '.npy'), images), 'Saving numpy array to file')
    def run(self):
        super().run()
        query = args['query']

        options = os.listdir('../data/raw/' + query)
        ind = -1
        if len(options) == 1:
            ind = 0
        else:
            for i, option in enumerate(options):
                print('({}) {}'.format(i, option))
            ind = input('Enter your option index: ')
            while not ind.isdigit() or int(ind) < 0 or int(ind) >= len(options):
                ind = input('Please enter a valid option: ')
            ind = int(ind)
        img_dir = '../data/raw/' + query + '/' + options[ind]
        
        img_names = os.listdir(img_dir)
        progress_bar = ProgressBar(len(img_names), self.log)

        if not os.path.exists('../data/preprocessed/' + query):
            os.mkdir('../data/preprocessed/' + query)
        if not os.path.exists('../data/preprocessed/' + query + '/' + options[ind]):
            os.mkdir('../data/preprocessed/' + query + '/' + options[ind])

        while True:
            img = cv2.imread(img_dir + '/' + img_names[progress_bar.i])
            dim = min(img.shape[:2])
            if img.shape[0] == dim:
                lrg_dim = img.shape[1]
                img = img[:, int((lrg_dim - dim) / 2): int((lrg_dim + dim) / 2)]
            else:
                lrg_dim = img.shape[0]
                img = img[int((lrg_dim - dim) / 2): int((lrg_dim + dim) / 2), :]
            img = cv2.resize(img, (200, 200))
            cv2.imwrite('../data/preprocessed/' + query + '/' + options[ind] + '/' + img_names[progress_bar.i], img)
            if progress_bar.increment(): break