Пример #1
0
def crop(body, borehole_id, core_photo_id):
    core_photo_id_match = (
        tables.CorePhoto.query
        .filter(tables.CorePhoto.id == core_photo_id)
        .one_or_none()
    )

    if not core_photo_id_match:
        abort(404, f'Cropped Core photo id {core_photo_id} not found')

    cropped_core_photo_id_match = (
        tables.CroppedCorePhoto.query
        .filter(tables.CroppedCorePhoto.core_photo_id == core_photo_id)
        .one_or_none()
    )

    if cropped_core_photo_id_match:
        abort(409, f'Core photo id {core_photo_id} already cropped')

    schema = tables.CroppedCorePhotoSchema()
    core_photo = schema.dump(core_photo_id_match)

    file_path = pathlib.Path(core_photo['file_path'])
    cropped_file_path = pathlib.Path(
        file_path.parent, f'{file_path.stem}_crop.png')
    image_processing.crop(
        file_path.as_posix(), cropped_file_path.as_posix(), image_processing.points_to_numpy(body))
    # TODO: add error checking for image conversion
    core_photo['file_path'] = cropped_file_path.as_posix()
    core_photo['mime_type'] = 'image/png'
    core_photo['core_photo_id'] = core_photo_id

    cropped_core_photo = schema.load(core_photo)
    corner_schema = tables.CorePhotoCornerSchema(many=True)
    cropped_core_photo.core_photo_corners = corner_schema.load(body)
    sql.session.add(cropped_core_photo)
    sql.session.commit()

    return schema.dump(cropped_core_photo), 201
Пример #2
0
    def estimate_uniform_colors(self, image_hsv, uniform_loc):
        uniform_eh = image_processing.hsv2eh(
            image_processing.crop(image_hsv, uniform_loc))
        uniform_mask = cv2.imread('templates/uniform_mask.png',
                                  cv2.IMREAD_GRAYSCALE)

        uniform_masked = np.ma.masked_array(uniform_eh, uniform_mask == 0)
        values, counts = np.unique(uniform_masked.astype('uint16'),
                                   return_counts=True)

        uniform_pixels = np.count_nonzero(uniform_mask)

        uniform_values = []
        for v, c in zip(values, counts):
            if type(v) == np.ma.core.MaskedConstant:
                continue

            if c / uniform_pixels > 0.2:
                uniform_values.append(v)

        return uniform_values
Пример #3
0
 def create_thumb(self):
     if not self.pic:
         return
     image_processing.crop(self.pic, self.thumb, 150, 150)
Пример #4
0
 def create_thumb(self):
     if not self.cover:
         return
     image_processing.crop(self.cover, self.thumb, 400, 300)
Пример #5
0
    def play_game(self):
        self.create_debug_dir()

        logging.info('Starting game')

        logging.info('Entering arena')
        self.touch(config.arena_loc)
        time.sleep(3)

        logging.info('Playing match')
        self.touch(config.play_match_loc)
        time.sleep(3)

        logging.info('Finding an opponent')
        index = 0
        while True:
            if self.sign_in():
                return

            logging.info(f'Trying to find support screen')
            matched, score = self.match_template('templates/support.png',
                                                 config.support_loc)
            if matched:
                logging.info(f'Connection failed. Trying again ({score})')
                self.touch(config.support_ok_loc)
                time.sleep(3)

                logging.info('Playing match')
                self.touch(config.play_match_loc)

            logging.info(f'Trying to find no opponent screen')
            matched, score = self.match_template('templates/no_opponent.png',
                                                 config.no_opponent_loc)
            if matched:
                logging.info(f'Connection failed. Trying again ({score})')
                self.touch(config.no_opponent_ok_loc)
                time.sleep(3)

                logging.info('Playing match')
                self.touch(config.play_match_loc)

            logging.info(f'Trying to find bid screen')
            matched, score = self.match_template('templates/bid.png',
                                                 config.bid_loc)
            if matched:
                logging.info(f'Bid stage ({score})')
                time.sleep(5)
                break

            index += 1

            if index > 50:
                logging.info('Something\'s wrong. Restart the app')
                self.adb.restart_app()

                return

        logging.info('Game starated')

        photo_loc = [
            0, 0, config.screen_size[1],
            config.my_photo_loc[1] + config.my_photo_loc[3]
        ]

        self.frame_index = 0
        while True:
            image1 = self.adb.get_screen()

            logging.info('Trying to find game end screen')
            matched, score = self.match_template('templates/game_end.png',
                                                 config.game_end_loc,
                                                 image=image1)
            if matched:
                logging.info(f'Game ended ({score})')
                break

            logging.info('Trying to find time out screen')
            matched, score = self.match_template('templates/timeout.png',
                                                 config.timeout_loc,
                                                 mask=True,
                                                 threshold=0.95,
                                                 image=image1)
            if matched:
                logging.info(f'Timeout ({score})')
                break

            image2 = self.adb.get_screen()

            diff_image = image_processing.crop(
                image1, photo_loc) - image_processing.crop(image2, photo_loc)
            my_photo_diff = image_processing.crop(diff_image,
                                                  config.my_photo_loc)
            opponent_photo_diff = image_processing.crop(
                diff_image, config.opponent_photo_loc)

            if np.sum(my_photo_diff) != 0:
                logging.info(f'{self.frame_index} My turn to kick')
                diff_score = image_processing.diff_image(image1, image2)
                logging.debug(f'frame diff score: {diff_score}')
                if diff_score < 0.5:
                    image2 = self.adb.get_screen()
                    logging.debug(
                        f'Since frame was captured while camera is moving, re-captured'
                    )

                gray_image = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
                color_image = image2

                self.kick(gray_image, color_image)
            elif np.sum(opponent_photo_diff) != 0:
                logging.info(f'{self.frame_index} Opponent\'s turn to kick')
                #self.defend(gray_image, color_image)
            else:
                logging.info(f'{self.frame_index} In-progress')

            self.frame_index += 1

        while True:
            image = self.adb.get_screen()
            logging.info('Trying to find shootout')
            matched, score = self.match_template('templates/shootout.png',
                                                 config.shootout_loc,
                                                 mask=True,
                                                 image=image)
            if matched:
                logging.info(f'Shootout started ({score})')
                self.play_shootout()

            logging.info('Trying to find game end')
            matched, score = self.match_template('templates/game_end.png',
                                                 config.game_end_loc,
                                                 image=image)
            if matched:
                logging.info(f'Game ended ({score})')
                self.touch_box(config.game_end_loc)
                time.sleep(3)
                break

            time.sleep(0.5)

        time.sleep(3)

        logging.info('Trying to find relagation screen')
        matched, _ = self.match_template('templates/okay.png', config.okay_loc)
        if matched:
            logging.info('Relagation. Touch okay')
            self.touch_box(config.okay_loc)
        else:
            logging.info('Trying to find promotion package screen')
            matched, _ = self.match_template('templates/promotion_package.png',
                                             config.promotion_package_loc)
            if matched:
                logging.info('Promotion pakcage. Touch close')
                self.touch(config.promotion_package_close_loc)
                time.sleep(3)

            logging.info('Trying to find video watch screen')
            matched, _ = self.match_template('templates/watch_video.png',
                                             config.watch_video_loc)
            if matched:
                logging.info('Accepting video package')
                self.touch_box(config.watch_video_loc)

                logging.info('Playing video for 60 secs')
                time.sleep(60)

                logging.info('Finished playing video')

                found_close_button = False
                for index, loc in enumerate(config.video_close_locs):
                    matched, _ = self.match_template(
                        f'templates/video_close_{index}.png', loc)
                    if matched:
                        logging.info('Found close button')
                        self.touch(loc)
                        time.sleep(3)
                        found_close_button = True
                        break

                if not found_close_button:
                    logging.warning('Can\'t found video close button')
                    if self.debug:
                        cv2.imwrite(f'{self.debug_dir}\\video_error.png',
                                    self.adb.get_screen())

                    self.touch(config.free_collect_end_loc)
                    self.touch(config.video_package_close_loc)

                logging.info('Opening cards')
                if not self.open_cards():
                    return
            else:
                logging.info('There is no video watch')

        time.sleep(3)

        logging.info('Going back to the main screen')
        self.touch(config.go_back_loc)
        time.sleep(3)
Пример #6
0
def crop():
    image_processing.crop()
    return render_template("uploaded.html", file_path="img/temp_img_crop.jpeg")