|
| 1 | +# Copyright (c) 2019 Anki, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License in the file LICENSE.txt or at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Data generation script to build a training and test dataset. |
| 16 | +
|
| 17 | +A sample dataset is included in the project ("dataset.zip"). Unzip the folder and use the |
| 18 | +--dataset_root_folder option to specify the file path and expand this dataset. |
| 19 | +
|
| 20 | +Use this script to build/expand the data needed to train the sign language recognition system. |
| 21 | +""" |
| 22 | + |
| 23 | +from concurrent.futures import CancelledError |
| 24 | +import curses |
| 25 | +import json |
| 26 | +import os |
| 27 | +import platform |
| 28 | +from pathlib import Path |
| 29 | +import random |
| 30 | +import sys |
| 31 | +import tempfile |
| 32 | +import time |
| 33 | + |
| 34 | +try: |
| 35 | + import numpy as np |
| 36 | +except ImportError as exc: |
| 37 | + sys.exit("Cannot import numpy: Do `pip3 install numpy` to install") |
| 38 | + |
| 39 | +try: |
| 40 | + from PIL import Image |
| 41 | +except ImportError: |
| 42 | + sys.exit("Cannot import from PIL: Do `pip3 install --user Pillow` to install") |
| 43 | + |
| 44 | +try: |
| 45 | + from scipy import ndimage |
| 46 | +except ImportError as exc: |
| 47 | + sys.exit("Cannot import scipy: Do `pip3 install scipy` to install") |
| 48 | + |
| 49 | +import anki_vector |
| 50 | +import util |
| 51 | + |
| 52 | + |
| 53 | +def data_capture(camera: anki_vector.camera.CameraComponent, stats: dict, root_folder: str) -> None: |
| 54 | + """Build an image dataset using the camera feed from Vector. |
| 55 | +
|
| 56 | + This method uses an image from the camera and generates a multiplier number of images by |
| 57 | + rotating the original image. The keystroke used to initiate the image capture and processing |
| 58 | + is used to label the image. |
| 59 | + """ |
| 60 | + |
| 61 | + try: |
| 62 | + # TODO: curses works well with Mac OS and Linux, explore msvcrt for Windows |
| 63 | + terminal = curses.initscr() |
| 64 | + curses.cbreak() |
| 65 | + curses.noecho() |
| 66 | + terminal.nodelay(True) |
| 67 | + |
| 68 | + # The number of images to generate using the image captured as a seed |
| 69 | + image_multiplier = 10 |
| 70 | + # The maximum amount of rotation by which to rotate the original image to generate more images |
| 71 | + min_rotation = -10 |
| 72 | + max_rotation = 10 |
| 73 | + |
| 74 | + print("------ capturing hand signs dataset, press ctrl+c to exit ------") |
| 75 | + while True: |
| 76 | + key = terminal.getch() |
| 77 | + if (ord("a") <= key <= ord("z")) or (key == ord(" ")): |
| 78 | + |
| 79 | + # Represents background images, filenames are switched to be prefixed with "background" instead of " " |
| 80 | + if key == ord(" "): |
| 81 | + key = "background" |
| 82 | + else: |
| 83 | + key = chr(key) |
| 84 | + |
| 85 | + # Pull image from camera |
| 86 | + original_image = camera.latest_image.raw_image |
| 87 | + if original_image: |
| 88 | + # Convert image to black and white |
| 89 | + black_white_image = original_image.convert("L") |
| 90 | + rotation_axes = [1, 1, 0] |
| 91 | + |
| 92 | + # Generate more images with random rotation |
| 93 | + for rotation in random.sample(range(min_rotation, max_rotation), image_multiplier): |
| 94 | + # Randomly define which axis to rotate the image by |
| 95 | + random.shuffle(rotation_axes) |
| 96 | + x_axis_rotation_enabled, y_axis_rotation_enabled = rotation_axes[:2] |
| 97 | + rotated_image_array = ndimage.rotate(black_white_image, |
| 98 | + rotation, |
| 99 | + axes=(x_axis_rotation_enabled, y_axis_rotation_enabled), |
| 100 | + reshape=False) |
| 101 | + |
| 102 | + # Convert to a 200*200 image |
| 103 | + rotated_image = Image.fromarray(rotated_image_array) |
| 104 | + cropped_image = util.crop_image(rotated_image, util.NetworkConstants.IMAGE_WIDTH, util.NetworkConstants.IMAGE_HEIGHT) |
| 105 | + |
| 106 | + # Save the image |
| 107 | + image_filename = key + "_" + str(stats.get(key, 0)) + ".png" |
| 108 | + stats[key] = stats.get(key, 0) + 1 |
| 109 | + cropped_image.save(os.path.join(root_folder, image_filename)) |
| 110 | + |
| 111 | + # Character |
| 112 | + print(f"Recorded images for {key}\n\r") |
| 113 | + except (CancelledError, KeyboardInterrupt): |
| 114 | + pass |
| 115 | + finally: |
| 116 | + curses.nocbreak() |
| 117 | + curses.echo() |
| 118 | + curses.endwin() |
| 119 | + |
| 120 | + |
| 121 | +def main(): |
| 122 | + stats = {} |
| 123 | + |
| 124 | + args = util.parse_command_args() |
| 125 | + if not args.dataset_root_folder: |
| 126 | + args.dataset_root_folder = str(Path(tempfile.gettempdir(), "dataset")) |
| 127 | + print(f"No data folder defined, saving to {args.dataset_root_folder}") |
| 128 | + os.makedirs(args.dataset_root_folder, exist_ok=True) |
| 129 | + time.sleep(2) |
| 130 | + |
| 131 | + # Read existing stats or set new stats up |
| 132 | + if os.path.isfile(os.path.join(args.dataset_root_folder, "stats.json")): |
| 133 | + with open(os.path.join(args.dataset_root_folder, "stats.json"), "r") as stats_file: |
| 134 | + stats = json.load(stats_file) |
| 135 | + else: |
| 136 | + stats = {} |
| 137 | + |
| 138 | + with anki_vector.Robot(args.serial) as robot: |
| 139 | + try: |
| 140 | + # Add a rectangular overlay describing the portion of image that is used after cropping. |
| 141 | + # TODO: The rectangle overlay should feed in a full rect, not just a size |
| 142 | + frame_of_interest = anki_vector.util.RectangleOverlay(util.NetworkConstants.IMAGE_WIDTH, util.NetworkConstants.IMAGE_HEIGHT) |
| 143 | + robot.viewer.overlays.append(frame_of_interest) |
| 144 | + robot.camera.init_camera_feed() |
| 145 | + robot.viewer.show() |
| 146 | + data_capture(robot.camera, stats, args.dataset_root_folder) |
| 147 | + finally: |
| 148 | + with open(os.path.join(args.dataset_root_folder, "stats.json"), "w") as stats_file: |
| 149 | + # Save the stats of expanded dataset |
| 150 | + json.dump(stats, stats_file) |
| 151 | + |
| 152 | + # Reset the terminal |
| 153 | + print(f"Data collection done!\nData stored in {args.dataset_root_folder}") |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == '__main__': |
| 157 | + main() |
0 commit comments