Exemplo n.º 1
0
def telemetry(sid, data):
    if data:
        # The current steering angle of the car
        steering_angle = data["steering_angle"]
        # The current throttle of the car
        throttle = data["throttle"]
        # The current speed of the car
        speed = data["speed"]
        # The current image from the center camera of the car
        imgString = data["image"]
        image = Image.open(BytesIO(base64.b64decode(imgString)))
        image_array = np.asarray(image)
        image_array = dat.process_image(image_array)
        steering_angle = float(
            model.predict(image_array[None, :, :, :], batch_size=1))

        throttle = controller.update(float(speed))

        print(steering_angle, throttle)
        send_control(steering_angle, throttle)

        # save frame
        if args.image_folder != '':
            timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
            image_filename = os.path.join(args.image_folder, timestamp)
            image.save('{}.jpg'.format(image_filename))
    else:
        # NOTE: DON'T EDIT THIS.
        sio.emit('manual', data={}, skip_sid=True)
Exemplo n.º 2
0
def main(video):

    data = DataSet()
    model = load_model('data/savedmodels/inception.model.hdf5')
    file = video[2:-8]
    print(file)

    images = glob.glob(
        os.path.join('data', 'frames', 'test', file, video, '*.jpg'))

    path = os.path.join('data', 'frames', 'test', file, video)
    print(path)
    nb_images = len(images)
    print(nb_images)
    class_label_predictions = {}

    for i, label in enumerate(data.classes):
        class_label_predictions[i] = 0

    for sample in range(nb_images):
        print('-' * 80)

        image = images[sample]

        print(image)
        image_arr = process_image(image, (299, 299, 3))
        image_arr = np.expand_dims(image_arr, axis=0)

        predictions = model.predict(image_arr)

        label_predictions = {}
        for i, label in enumerate(data.classes):
            label_predictions[label] = predictions[0][i]
            class_label_predictions[i] += (predictions[0][i] / nb_images)

        sorted_lps = sorted(label_predictions.items(),
                            key=operator.itemgetter(1),
                            reverse=True)

        for i, class_prediction in enumerate(sorted_lps):
            if i > 4:
                break
            print(i)
            print("%s: %.2f" % (class_prediction[0], class_prediction[1]))
            i += 1
    print('-' * 80)
    print('-' * 80)
    class_sorted_lps = sorted(class_label_predictions.items(),
                              key=operator.itemgetter(1),
                              reverse=True)
    for i, class_prediction in enumerate(class_sorted_lps):
        if i > 4:
            break
        print(i)
        print("%s: %.2f" %
              (data.classes[class_prediction[0]], class_prediction[1]))
        i += 1

    return data.classes[class_sorted_lps[0][0]]
Exemplo n.º 3
0
def predict(image_path, model, topk, gpu):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    if gpu == True and torch.cuda.is_available():
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")

    # TODO: Implement the code to predict the class from an image file
    model.to(device)

    model.eval()

    # Importing the Image and proccesing it
    image = process_image(image_path)

    # Image to a Tensor
    image = torch.from_numpy(np.array([image])).float()

    image = image.to(device)

    # Calculating the output
    output = model.forward(image)

    # Probabilities of the image
    ps = torch.exp(output)

    # This last part of this code I got it from a guy named najeebhassan
    # I found his way more efficient and effective
    probability = torch.topk(ps, topk)[0].tolist()[0]
    index = torch.topk(ps, topk)[1].tolist()[0]

    ind = []
    for i in range(len(model.class_to_idx.items())):
        ind.append(list(model.class_to_idx.items())[i][0])

    # transfer index to label
    classes = []
    for i in range(topk):
        classes.append(ind[index[i]])

    return probability, classes
Exemplo n.º 4
0
def web_cam(model_to_test):
    cap = cv2.VideoCapture(0)
    detector = FaceDetector()
    selector = ImageSelector()

    while True:
        ret, frame = cap.read()
        font = cv2.FONT_HERSHEY_COMPLEX
        faces, start_xs, start_ys, end_xs, end_ys = detector.get_all_faces(
            frame)

        for face, start_x, start_y, end_x, end_y in zip(
                faces, start_xs, start_ys, end_xs, end_ys):
            try:
                face = cv2.resize(face, (RESNET_IMG_WIDTH, RESNET_IMG_HEIGHT))
                face = process_image(face,
                                     mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
            except:
                break
            face = np.expand_dims(face, 0).astype(np.float16)
            prediction, prob = model_to_test.get_database_prediction(face)
            if prob > 0.5:
                name = selector.get_name_by_index(prediction.item())
            else:
                name = 'Unknown'

            cv2.rectangle(frame, (start_x, start_y), (end_x, end_y),
                          (0, 0, 255), 2)
            cv2.putText(frame, f"{name}:{prob * 100: .2f}%",
                        (start_x, start_y), font, 0.5, (255, 0, 0), 1)

        cv2.imshow("Camera", frame)

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    cv2.destroyAllWindows()
    cap.release()
Exemplo n.º 5
0
def main():
    # Initialize the job, which will
    # load and verify all input parameters
    Job.initialize()

    defense_name = Job.function
    defense_options = dict(Job.options)
    defense_class = DEFENSE_MAP[defense_name]

    input_file_paths = list(map(lambda f: f.path, Job.input_files))
    input_dataset, is_temp_dataset = get_or_create_dataset(input_file_paths)

    output_dataset = build_image_dataset(
        Job.make_output_filepath(input_dataset.path.name))

    for item in input_dataset:
        input_image = recreate_image(item.data)

        output_image = defense_class.apply(input_image, **defense_options)
        output_image_arr = process_image(output_image)

        output_dataset.add_item(name=item.name,
                                data=output_image_arr,
                                label=item.label,
                                prediction=-1)

    output_item = output_dataset[0]
    output_image = recreate_image(output_item.data)
    output_image_path = Job.make_output_filepath(output_item.name)
    output_image.save(output_image_path)

    Job.add_output_file(str(output_dataset.path), is_extra=True)
    Job.add_output_file(output_image_path,
                        is_modified=True,
                        tags={"mlsploit-visualize": "image"})

    Job.commit_output()

    if is_temp_dataset:
        os.remove(input_dataset.path)
Exemplo n.º 6
0
    default=False
)

args = parser.parse_args()

# validate and convert the image file
image_filename = args.input
if not os.path.isfile(image_filename):
    print('Unable to find image file', image_filename)
    exit()

# BUGBUG: useful for testing, but probably don't want to assume the
# image is from the image dataset
image_category = image_filename.split(os.sep)[-2]

image_data = data.process_image(args.input)

# create the network
device = 'cuda' if args.gpu else 'cpu'
model = network.load_network(args.checkpoint, device)

# predict
probs, classes = network.predict(image_data, model, device, topk=args.top_k)

# Load the category to name mapping if provided
cat_to_name = None
if args.category_names and os.path.isfile(args.category_names):
    with open(args.category_names, 'r') as f:
        cat_to_name = json.load(f)

# output results
Exemplo n.º 7
0
    def __getitem__(self, item) -> (torch.Tensor, torch.Tensor, torch.Tensor):
        """
        Return 3 images: the anchor face; the positive face, which is the same person as the anchor;
        and the negative face, which is from a different person from the anchor
        :param item: int
            Ignored, it is sampled randomly
        :return: (torch.Tensor, torch.Tensor, torch.Tensor)
            The anchor image face, the positive image face, and the negative image face
        """
        negative_file = ''
        while True:
            while True:
                anchor_name = np.random.choice(list(self.identities.keys()))

                if len(self.identities[anchor_name]) > 1:
                    if len(self.identities[anchor_name]) == 2:
                        anchor_file = self.identities[anchor_name][0]
                        positive_file = self.identities[anchor_name][1]

                    else:
                        anchor_file, positive_file = np.random.choice(
                            self.identities[anchor_name], 2)
                        if anchor_file == positive_file:
                            continue

                    negative_name = np.random.choice(
                        list(self.identities.keys()))
                    if negative_name == anchor_name:
                        continue

                    negative_file = np.random.choice(
                        self.identities[negative_name])
                    break

            anchor_image = cv2.imread(self.images_path + anchor_file).astype(
                np.float32)
            positive_image = cv2.imread(self.images_path +
                                        positive_file).astype(np.float32)
            negative_image = cv2.imread(self.images_path +
                                        negative_file).astype(np.float32)

            anchor_x1, anchor_y1, anchor_x2, anchor_y2 = self.boxes[
                anchor_file]
            positive_x1, positive_y1, positive_x2, positive_y2 = self.boxes[
                positive_file]
            negative_x1, negative_y1, negative_x2, negative_y2 = self.boxes[
                negative_file]

            anchor_image = anchor_image[anchor_y1:anchor_y2,
                                        anchor_x1:anchor_x2]
            positive_image = positive_image[positive_y1:positive_y2,
                                            positive_x1:positive_x2]
            negative_image = negative_image[negative_y1:negative_y2,
                                            negative_x1:negative_x2]

            try:
                anchor_image = cv2.resize(anchor_image,
                                          (self.width, self.height))
                positive_image = cv2.resize(positive_image,
                                            (self.width, self.height))
                negative_image = cv2.resize(negative_image,
                                            (self.width, self.height))
            except:
                continue

            anchor_image = process_image(anchor_image,
                                         mean=[0.485, 0.456, 0.406],
                                         std=[0.229, 0.224, 0.225])
            positive_image = process_image(positive_image,
                                           mean=[0.485, 0.456, 0.406],
                                           std=[0.229, 0.224, 0.225])
            negative_image = process_image(negative_image,
                                           mean=[0.485, 0.456, 0.406],
                                           std=[0.229, 0.224, 0.225])

            anchor_image = torch.from_numpy(anchor_image)
            positive_image = torch.from_numpy(positive_image)
            negative_image = torch.from_numpy(negative_image)

            return anchor_image, positive_image, negative_image