示例#1
0
    def run_episode(self):
        (s, r, d) = self.mario_env.reset()
        counter = 0
        while True:
            time.sleep(
                THREAD_DELAY
            )  # so that multiple agents can run in parallel (more then cpus)
            try:
                im = self.get_screenshot(s)
                im = self.prepare_image(im)
                a = self.agent.act(im)
                s_, r, done = self.mario_env.step(a)
                im_ = utils.get_screenshot(s_)
                im_ = utils.prepare_image(im_)
                if not done:
                    self.agent.train(im, a, r, im_)
                s = s_
            except:
                print(
                    'some error occured during image processing use default action'
                )
                print(sys.exc_info()[0])
                a = 0

            if counter == 300:
                (s, r, d) = self.mario_env.reset()
                counter = 0
                global frames
                frames = 0

            counter += 1

            if done or self.stop_signal:
                break
示例#2
0
def predict():
    """initialize the data dictionary that will be returned from the
        view
    
    :rtype: `flask.json`
    """
    global model, graph
    data = {"success": False}

    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))

            # preprocess the image and prepare it for classification
            image = prepare_image(image=image, target_size=TARGET_SIZE)

            # classify the input image and then initialize the list
            # of predictions to return to the client
            #run predictions asynchronously and get output with collect()
            with graph.as_default():
                preds = model.predict(image)
            data["predictions"] = preds

            # indicate that the request was a success
            data["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(data)
示例#3
0
    def run(self) -> None:
        while True:
            # remove stale memes
            for file in Path('images/').glob('*'):
                if arrow.get(
                        file.stat().st_mtime) > arrow.now().shift(hours=+1):
                    os.remove(file)

            # get next meme
            post = next(self._reddit_d.subreddit('memes').new(limit=1))
            skip = False
            for img in self._images:
                if img.caption == post.title:
                    os.remove(img.path)
                    skip = True
            if skip:
                continue

            # download the maymay
            r = requests.get(post.url, stream=True)
            if r.status_code != 200:
                time.sleep(self._timeout / 2)
                continue
            r.raw.decode_content = True
            tmp_path = utils.save_image(r.raw)
            path = utils.prepare_image(tmp_path)
            self.images.append(_Image(path, post.title))

            time.sleep(self._timeout)
示例#4
0
def image_feature_recognition(image_path):
    display_ui_image(image_path, 224, 224)

    image224 = prepare_image(image_path, 224, 224)
    image299 = prepare_image(image_path, 299, 299)
    image331 = prepare_image(image_path, 331, 331)

    set_text('')
    add_text(VGG16_predict(image224))
    add_text(VGG19_predict(image224))
    add_text(ResNet50_predict(image224))
    add_text(InceptionV3_predict(image299))
    add_text(Xception_predict(image299))
    add_text(MobileNet_predict(image224))
    add_text(MobileNetV2_predict(image224))
    add_text(DenseNet_predict(image224))
    add_text(NASNetLarge_predict(image331))
    add_text(NASNetMobile_predict(image224))
示例#5
0
    def do_GET(self):

        ### determine manual override
        manual_override = real_controller.manual_override()

        if (not manual_override):

            ## Look
            bmp = take_screenshot()
            vec = prepare_image(bmp)

            ## Think
            joystick = model.y.eval(session=sess,
                                    feed_dict={
                                        model.x: [vec],
                                        model.keep_prob: 1.0
                                    })[0]

        else:
            joystick = real_controller.read()
            joystick[
                1] *= -1  # flip y (this is in the config when it runs normally)

        ## Act

        ### calibration
        output = [
            int(joystick[0] * 80),
            int(joystick[1] * 80),
            int(round(joystick[2])),
            int(round(joystick[3])),
            int(round(joystick[4])),
        ]

        ### print to console
        if (manual_override):
            cprint("Manual: " + str(output), 'yellow')
        else:
            cprint("AI: " + str(output), 'green')

        ### respond with action
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write(output)
        return
示例#6
0
    def act(self, screenshot_path, reward, done):
        action = None
        im = utils.get_screenshot(screenshot_path)
        if im is not None:
            prepared_image = utils.prepare_image(im)
            action = self.model.predict(prepared_image, batch_size=1)[0]
            action = action[0]
        else:
            logger.error(
                'If you want image from clipboard, provide image in clipboard')

        if action is None:
            logger.error(
                'could not predict next action set action=0 | screenshot path: {}'
                .format(screenshot_path))
            return 0
        return action
示例#7
0
def seek(net, filename):
    """
    Loading image and return list of Result objects
    """

    full_img = pl.imread(filename)
    #img = full_img
    img = full_img[:full_img.shape[0] // 4, :full_img.shape[1] // 4]

    if img.dtype == np.uint8:
        img = img.astype(np.float32) / 255.0

    img_size = float(img.shape[0] * img.shape[1])
    lum = get_lum(img)
    edges = canny(lum, sigma=config.seek.lum_sigma)

    fill = ndimage.binary_fill_holes(edges)
    fill = remove_small_objects(fill, 100)
    labels, n = ndimage.label(fill)

    for y, x in (t for t in ndimage.find_objects(labels) if t is not None):
        # where = np.argwhere(labeled == i)
        # s - start, e- end
        width = float(x.start - x.stop)
        height = float(y.start - y.stop)

        size = width * height / img_size
        ratio = width / height

        # print '%5.2f%% %5.2f' % (size * 100, ratio)

        if not (config.seek.size.in_range_close(size)
                and config.seek.ratio.in_range_close(ratio)):
            continue

        shape = img[y, x]

        prepared_img = prepare_image(shape, size=config.image.size)

        result = net.activate(prepared_img)

        yield Result(shape,
                     sorted(((image_names[i], x)
                             for i, x in enumerate(result)),
                            key=lambda a: -a[1]))
def predict():
    """initialize the data dictionary that will be returned from the
        view
    
    :rtype: `flask.json`
    """
    global model, graph
    data = {"success": False}

    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))

            # preprocess the image and prepare it for classification
            image = prepare_image(image=image, target_size=TARGET_SIZE)

            # classify the input image and then initialize the list
            # of predictions to return to the client
            #run predictions asynchronously and get output with collect()
            with graph.as_default():
                preds = model.predict(image)
            print(flask.request.args)
            if flask.request.args.get("top"):
                top = flask.request.args.get("top")
                results = imagenet_utils.decode_predictions(preds, top=top)
            else:
                results = imagenet_utils.decode_predictions(
                    preds, top=DEFAULT_PREDICTION_NUMBER)
            data["predictions"] = []

            # loop over the results and add them to the list of
            # returned predictions
            for (imagenetID, label, prob) in results[0]:
                r = {"label": label, "probability": float(prob)}
                data["predictions"].append(r)

            # indicate that the request was a success
            data["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(data)
示例#9
0
def predict():
    data = {"success": False}
    if request.method == "POST":
        if request.files.get("image"):
            image = request.files["image"].read()
            try:
                image = prepare_image(image)
            except Exception:
                logging.exception("Error in preparing image")
                return jsonify(data), 400
            im_id = str(uuid4())
            im_dict = {"im_id": im_id, "image": b64_encoding(image)}
            # send image to the redis queue
            db.rpush(IMAGE_QUEUE, json.dumps(im_dict))
            logging.info("New image added to queue with id: %s", im_id)
            while True:
                # start polling
                output = db.get(im_id)
                if output is not None:
                    # image processed, try to get predictions
                    try:
                        output = output.decode("utf-8")
                        data["predictions"] = json.loads(output)
                        logging.info("Got predictions for image with id: %s",
                                     im_id)
                    except Exception:
                        logging.exception(
                            "Error in getting predictions for image with id: %s",
                            im_id)
                        return jsonify(data), 400
                    finally:
                        db.delete(im_id)
                        logging.info("Deleting image from queue with id: %s",
                                     im_id)
                    break
                time.sleep(CONSUMER_SLEEP)
            data["success"] = True
            logging.info("Send result for image with id: %s", im_id)
            return jsonify(data), 200
    logging.warning("Invalid request with file %s", request.files)
    return jsonify(data), 400
示例#10
0
def predict(image_path, model_path, top_k, class_names_path):
    image = utils.prepare_image(image_path)
    model = utils.load_model(model_path)

    # Predict
    predictions = model.predict(image)[0]

    # Get indexes of top k predictions
    # Sort indexes, in reverse (descending) order
    sorted_indexes = np.argsort(predictions)[::-1]
    top_k_indexes = sorted_indexes[0:top_k]

    # Extract probabilites and class names based on indexes
    probabilities = [predictions[i] for i in top_k_indexes]
    if class_names_path != None:
        # +1 because class_names key range is from 1 to 102
        class_names = utils.load_class_names(class_names_path, top_k_indexes)
    else:
        # Put indexes for class names
        class_names = [i for i in range(1, 103)]
    return class_names, probabilities
            score = mad(x, y)
            return score.mean()
        else:
            with torch.no_grad():
                score = mad(x, y)
            return score


if __name__ == '__main__':
    from PIL import Image
    import argparse
    from utils import prepare_image

    parser = argparse.ArgumentParser()
    parser.add_argument('--ref', type=str, default='images/r0.png')
    parser.add_argument('--dist', type=str, default='images/r1.png')
    args = parser.parse_args()

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    ref = prepare_image(Image.open(args.ref).convert("L"),
                        repeatNum=1).to(device)
    dist = prepare_image(Image.open(args.dist).convert("L"),
                         repeatNum=1).to(device)

    model = MAD(channels=1).to(device)

    score = model(dist, ref, as_loss=False)
    print('score: %.4f', score.item())
    # score: 168
            return score


if __name__ == '__main__':
    from PIL import Image
    import argparse
    from utils import prepare_image

    parser = argparse.ArgumentParser()
    parser.add_argument('--ref', type=str, default='images/r0.png')
    parser.add_argument('--dist', type=str, default='images/r1.png')
    args = parser.parse_args()

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    ref = prepare_image(Image.open(args.ref).convert("RGB")).to(device)
    dist = prepare_image(Image.open(args.dist).convert("RGB")).to(device)

    model = SSIM(channels=3)

    score = model(dist, ref, as_loss=False)
    print('score: %.4f' % score.item())
    # score: 0.6717

    # model = SSIM(channels=1)
    # score = 0
    # for i in range(3):
    #     ref1 = ref[:,i,:,:].unsqueeze(1)
    #     dist1= dist[:,i,:,:].unsqueeze(1)
    #     score = score + model(ref1, dist1).item()
    # print('score: %.4f' % score)
示例#13
0
    def do_GET(self):
        global gas
        ### determine manual override
        manual_override = real_controller.manual_override()

        if (not manual_override):

            ## Look
            image = take_screenshot()
            vec = prepare_image(image)


            ## Think
            joystick = model.y.eval(session=sess, feed_dict={model.x: [vec], model.keep_prob: 1.0})[0]
            steer = joystick[0]
            print steer
            gas = 1 - gas
            # gas = 1
            # joystick[1] =  gas # slow down a bit
            ####### amplification
            # steer = amp(int(steer * 80))
            steer = int(steer * 80)
            # print steer
            Y_axis = 0


        else:
            joystick = real_controller.read()
            joystick[1] *= -1 # flip y (this is in the config when it runs normally)
            steer = int(joystick[0] * 80)
            gas = int(joystick[2])
            Y_axis = int(joystick[1] * 80)
        # joystick[1] =  0 # Y axis
        # joystick[3] =  0 # X button
        # joystick[4] =  0 # RB button



        ## Act


        output = [
            steer,
            gas,
            Y_axis
            # joystick[0], # LEFT RIGHT ANALOG
            # joystick[1], #  UP DOWN ANALOG
            # joystick[2], # A
            # joystick[3], # X
            # joystick[4], # RB
        ]

        ### print to console
        if (manual_override):
            cprint("Manual: " + str(output), 'yellow')
        else:
            cprint("AI: " + str(output), 'green')
        # TODO: include other buttons as in controller.c (input-bot)
        json_output = ControllerState()
        json_output.X_AXIS = steer
        json_output.Y_AXIS = Y_axis
        json_output.A_BUTTON = gas

        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()
        self.wfile.write(json.dumps(json_output.__dict__))
        return
示例#14
0
def predict():
    # initialize the data dictionary that will be returned from the
    # view
    data = {"success": False}

    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format and prepare it for
            # classification
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))
            image = prepare_image(image, (IMAGE_WIDTH, IMAGE_HEIGHT))

            # reading the task choice
            task = flask.request.form["task"]

            # ensure our NumPy array is C-contiguous as well,
            # otherwise we won't be able to serialize it
            # image = image.copy(order="C")

            # generate an ID for the classification then add the
            # classification ID + image to the queue
            k = str(uuid.uuid4())
            d = {"id": k, "image": image, "type": task}
            db.rpush(IMAGE_QUEUE, json.dumps(d))

            # keep looping until our model server returns the output
            # predictions
            while True:
                # attempt to grab the output predictions
                output = db.get(k)

                # check to see if our model has classified the input
                # image
                if output is not None:
                    # add the output predictions to our data
                    # dictionary so we can return it to the client

                    if task == 'IMC':
                        output = output.decode("utf-8")
                        data["predictions"] = json.loads(output)
                        data["type"] = 'IMC'
                    elif task == 'IMT':
                        output = output.decode("utf-8")
                        data["result"] = str.replace(
                            str.replace(output, '\n', '<br>'), 'temp.jpeg', '')
                        data["type"] = 'IMT'
                    elif task == 'ODT':
                        output = base64_encode_image(output)
                        data["result"] = output
                        data["type"] = 'ODT'
                    # delete the result from the database and break
                    # from the polling loop
                    db.delete(k)
                    break

                # sleep for a small amount to give the model a chance
                # to classify the input image
                time.sleep(CLIENT_SLEEP)

            # indicate that the request was a success
            data["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(data)
示例#15
0
文件: bilinear.py 项目: Amabitur/CUDA
# собираем ядро
mod = compiler.SourceModule(open("kernel.cu", "r").read())
bilinear_interpolation_kernel = mod.get_function("interpolate")

x_out = np.array([i for i in range(M2)] * N2)
y_out = np.array([i for i in range(N2)] * M2)

start = driver.Event()
stop = driver.Event()

#подготовка текстуры
print("Считаем на ГПУ...")
start.record()

prep_image = prepare_image(image)
tex = mod.get_texref("tex")
tex.set_filter_mode(driver.filter_mode.LINEAR)
tex.set_address_mode(0, driver.address_mode.CLAMP)
tex.set_address_mode(1, driver.address_mode.CLAMP)
driver.matrix_to_texref(prep_image, tex, order="C")

bilinear_interpolation_kernel(driver.Out(result),
                              driver.In(x_out),
                              driver.In(y_out),
                              np.int32(M1),
                              np.int32(N1),
                              np.int32(M2),
                              np.int32(N2),
                              block=block,
                              grid=grid,
示例#16
0
    cord = cords[tvname]
    img_cords = (slice(cord[1], cord[1] + cord[3]),
                 slice(cord[0], cord[0] + cord[2]))

    for fname in names:
        # load file
        name = test_dir_name + fname
        img = imread(name)

        # get logo
        box = img[img_cords]

        fig = pl.figure('logo_%s' % fname.split('.')[0].replace(' ', '_'))

        # create image
        prepared_img = prepare_image(box, size=config.image.size)

        # simulate
        result = net.activate(prepared_img)

        # result
        min_result = min(result)
        delta = max(result) - min_result
        result = sorted(((image_names[i], x)
                         for i, x in enumerate(result)),
                        key=lambda a: -a[1])

        fig = pl.figure()

        fig.add_subplot(1, 2, 1).imshow(img)
示例#17
0
import utils
import Parameters
import model
import numpy as np
import matplotlib.pyplot as plt

_, parameters = Parameters.load_last()

classes = [
    "without_mask",
    "with_mask",
]

Test_Image = r'tdataset\prepared\dev\1-0185.png'
real_image = utils.prepare_image(Test_Image, image_size=64)

image = real_image.copy()
image_flatten = image.reshape(1, -1).T
image = image_flatten / 255.
p, _ = model.predict(image, np.array([[0]]), parameters)

plt.imshow(real_image, interpolation='nearest')
plt.title(f'predict: {classes[int(np.squeeze(p))]}')
plt.axis('off')
plt.show()
            landmark_file = open(landmark)
            lines = landmark_file.readlines()

            landmark = []
            for line in lines:
                elem = line.strip()
                elem = elem.split('  ')
                landmark.append([float(i) for i in elem])

            landmarks.append(landmark)

            maximum = np.amax(landmark, axis=0)
            minimum = np.amin(landmark, axis=0)
            bb = np.concatenate([minimum, maximum], 0)

            new_im, _ = prepare_image(im, bb, shape=(224, 224), convert_shape=True)

            # cv2.imshow(f'{frame}', new_im)
            # cv2.waitKey(1)
            # cv2.destroyAllWindows()

            new_im = cv2.cvtColor(new_im, cv2.COLOR_BGR2RGB)
            new_im = new_im.astype(np.float32)/255
            cropped_frames.append(new_im)

        label_file = open(os.path.join(label_dir, subject, folder, label_list[0]))
        label = label_file.read()
        label = label.strip()
        label = int(float(label))
        features = intermediate_layer_model.predict(np.asarray(cropped_frames))
        # print(features.shape)
示例#19
0
 def predict_btn_listener(event):
     app.enable_buttons(False)
     app.save_image(utils.get_cwd() + "/.keras/images/orig_image.ps")
     utils.prepare_image(utils.get_cwd() + "/.keras/images/orig_image.ps")
     model.predict(utils.get_cwd() + "/.keras/images/prepared_image.png", predict_btn_callback)
示例#20
0
        targets = np.zeros((0, 2))

        for frame in frames:
            counter += 1
            frame_dir = os.path.join(dataset_dir, folder, subject, frame)
            im = cv2.imread(frame_dir)

            landmarks = subject_info['frames'][f'{frame[:-4]}']['landmarks']
            arousal = subject_info['frames'][f'{frame[:-4]}']['arousal']
            valence = subject_info['frames'][f'{frame[:-4]}']['valence']
            maximum = np.amax(landmarks, axis=0)
            minimum = np.amin(landmarks, axis=0)
            bb = np.concatenate([minimum, maximum], 0)
            try:
                new_im, _ = prepare_image(cv2.cvtColor(im, cv2.COLOR_BGR2RGB),
                                          bb,
                                          shape=(224, 224),
                                          convert_shape=True)
            except:
                print('error')
                print(subject)
                print(frame)
            input_images = np.vstack(
                (input_images, new_im[np.newaxis, :, :, :]))

            targets = np.vstack((targets, np.array([[valence, arousal]])))

        # for i in range(input_images.shape[0]):
        #     pic = input_images[i, :, :, :].astype(np.uint8)
        #     cv2.imshow(f'frame', pic)
        #     cv2.waitKey(50)
        input_images = input_images / 255