示例#1
0
def main(args):

    start = time.time()

    video_reader = VideoReader(args.video_path)
    frame_counter = 0
    video_reader.start()
    width, height, fps, frame_count = video_reader.get_video_details()
    print(width, height, fps, frame_count)
    time.sleep(0.01)

    while video_reader.more():
        frame_start_time = time.time()
        frame = video_reader.read()
        convert_to_gray(frame)
        convert_to_rgb(frame)
        resize_image(frame)
        frame_duration = time.time() - frame_start_time
        frame_counter += 1
        cv2.imwrite(
            '{}/{}.jpg'.format(args.frames_op_path, frame_counter), frame)
        print('Completed processing frame no: {} in {}(s)'.format(
            frame_counter, frame_duration))
        time.sleep(0.001)

    duration = time.time() - start
    missed_frames = missing_frames(args.frames_op_path, frame_count)

    print('Total time taken: {}'.format(duration))
    print('Total number of frames {}'.format(frame_counter))
    print('Frames per sec: {}'.format(frame_counter/duration))
    print('Frames missed while processing: /n', missed_frames)
示例#2
0
def enqueue_image(uri, img):
    if isinstance(img, str):
        img = cv2.imread(img)
        if img is None:
            print("You have pushed an image with path: ", img,
                  "the path is invalid, skipped.")
            return

    # force resize here to avoid input image shape inconsistent
    # if the shape is consistent, it would not affect the data
    img = helper.resize_image(img, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH)
    data = cv2.imencode(".jpg", img)[1]

    img_encoded = helper.base64_encode_image(data)

    d = {"uri": uri, "image": img_encoded}

    inf = DB.info()

    try:
        if inf['used_memory'] >= inf['maxmemory'] * settings.input_threshold:
            raise redis.exceptions.ConnectionError
        DB.xadd("image_stream", d)
        print("Write to Redis successful")
    except redis.exceptions.ConnectionError:
        print("Redis queue is full, please wait for inference "
              "or delete the unprocessed records.")
        time.sleep(settings.interval_if_error)

    except redis.exceptions.ResponseError as e:
        print(e, "Redis memory is full, please dequeue or delete.")
        time.sleep(settings.interval_if_error)
示例#3
0
def resize_images(list_images, width):
    '''
    Resize images
    :param list_images: List of Images
    :param width: width final image resize
    :return:
    '''
    for img in list_images:
        image_resized = helper.resize_image(img, width)
        image_resized.save(img)
示例#4
0
def _load_data():
    input_list = []
    target_list = []
    for cat in range(len(_captcha_provider.chars)):
        char = _captcha_provider.chars[cat]
        char_images = dataset_manager.get_training_char_images(char)
        for image in char_images:
            target_list.append(cat)
            input_list.append(
                helper.resize_image(image, _std_height, _std_width).flatten())
    inputs = numpy.array(input_list, dtype=theano.config.floatX)
    targets = numpy.array(target_list, dtype=numpy.int64)
    return inputs, targets
示例#5
0
def _load_data():
    input_list = []
    target_list = []
    for cat in range(len(_captcha_provider.chars)):
        char = _captcha_provider.chars[cat]
        char_images = dataset_manager.get_training_char_images(char)
        for image in char_images:
            target_list.append(cat)
            input_list.append(helper.resize_image(
                image, _std_height, _std_width
            ).flatten())
    inputs = numpy.array(input_list, dtype=theano.config.floatX)
    targets = numpy.array(target_list, dtype=numpy.int64)
    return inputs, targets
示例#6
0
 def POST(self):
     i = web.input()
     uri = i['img']
     data_to_64 = re.search(r'base64,(.*)', uri).group(1)
     data = StringIO.StringIO()
     # data.write(base64.decodestring(data_to_64))
     data.write(data_to_64.decode('base64'))
     data.seek(0)
     im = resize_image(data)
     print np.asarray(im)
     X = np.asarray(im).flatten()
     label = classifier.predict(normalize_features(X))
     print label
     return label[0]
示例#7
0
    def get_classification(self, image):
        """Determines the color of the traffic light in the image

        Args:
            image (cv::Mat): image containing the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """
        #TODO implement light color prediction

        self.saver.restore(self.sess, tf.train.latest_checkpoint(MODEL_DIR))
        #print("graph: ",self.graph.get_operations())
        #print("self.saver: ", self.saver)

        #print("Image: ", image.shape)
        resized_img = helper.resize_image(image)
        print("resized shape: ", resized_img.shape)

        mean_pixel = np.array([104.006, 116.669, 122.679], dtype=np.float32)
        #preproc_img = helper.preprocess(resized_img, mean_pixel)
        expanded_img = np.expand_dims(resized_img, axis=0)
        print('--------------- Getting classification --------------')

        # Placeholders
        relu_op = self.graph.get_tensor_by_name('Classifier/Relu_2:0')
        summary_writer = tf.summary.FileWriter(LOG_DIR, graph=self.sess.graph)

        # softmax = tf.nn.softmax(logits)

        # with tf.Session() as sess:
        #     output = sess.run(softmax, feed_dict={logits: logit_data})

        predictions = self.sess.run(relu_op,
                                    feed_dict={
                                        "input_images:0": expanded_img,
                                        "keep_prob:0": 1.
                                    })

        print("Predictions: ", predictions)
        predictions = np.squeeze(predictions)  #squeeze array to 1 dim array
        softmax = helper.calc_softmax(predictions)
        max_index = np.argmax(softmax)
        print("Softmax: ", softmax)
        print("argmax: ", max_index)
        return max_index
        print('--------------- Classification complete -------------')
def predict(img):
    # data should be a numpy array of that has not been resized

    # helper.show_image(img)
    data = helper.resize_image(img,_std_height, _std_width).flatten()

    # load the saved model
    classifier = pickle.load(
        open(os.path.join(c.get('dataset'), 'best_model.pkl'), 'rb'))

    # compile a predictor function
    predict_model = theano.function(
        inputs=[classifier.input],
        outputs=classifier.logRegressionLayer.y_pred,
    )
    predicted_values = predict_model([data])
    print('Image is ' + _captcha_provider.chars[predicted_values])
    return _captcha_provider.chars[predicted_values]
示例#9
0
def predict(img):
    # data should be a numpy array of that has not been resized

    # helper.show_image(img)
    data = helper.resize_image(img, _std_height, _std_width).flatten()

    # load the saved model
    classifier = pickle.load(
        open(os.path.join(c.get('dataset'), 'best_model.pkl'), 'rb'))

    # compile a predictor function
    predict_model = theano.function(
        inputs=[classifier.input],
        outputs=classifier.logRegressionLayer.y_pred,
    )
    predicted_values = predict_model([data])
    print('Image is ' + _captcha_provider.chars[predicted_values])
    return _captcha_provider.chars[predicted_values]
示例#10
0
    def __process_frames(self):

        self.__log('Started processing frames')

        while not self.complete:

            if not self.q.empty():

                edisn_frame = self.q.get()

                if type(edisn_frame) is type(None):
                    self.__log('Processed last frame')
                    ray.get(
                        self.grayscale_converter.process_frame.remote(
                            ray.put(edisn_frame)))
                    # self.grayscale_converter.process_frame(edisn_frame)
                    self.complete = True
                    del edisn_frame

                else:
                    self.__log('Processing frame {}'.format(
                        edisn_frame.frame_number))

                    edisn_frame.resize_start_time = time.time()
                    edisn_frame.frame_resized = resize_image(
                        edisn_frame.frame_rgb)
                    edisn_frame.resize_end_time = time.time()

                    ray.get(
                        self.grayscale_converter.process_frame.remote(
                            ray.put(edisn_frame)))
                    # self.grayscale_converter.process_frame(edisn_frame)
                    del edisn_frame

            else:
                # self.__log('Queue empty. Sleeping for 1ms')
                time.sleep(0.001)
示例#11
0
def image_dequeue():
    image_ids = []
    images = []
    batch = None
    queue = DB.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1)
    for record in queue:
        record = json.loads(record.decode("utf-8"))
        image = helper.base64_decode_image(record["image"])
        image = helper.byte_to_mat(image, dtype=settings.IMAGE_DTYPE)
        image = helper.resize_image(image, settings.IMAGE_WIDTH,
                                    settings.IMAGE_HEIGHT,
                                    settings.IMAGE_MIN_SCALE)
        print(np.nonzero(image))
        image = helper.NHWC2HCHW(image)
        # check to see if the batch list is None
        if batch is None:
            batch = image
        # otherwise, stack the data
        else:
            batch = np.vstack([batch, image])
        images.append(image)
        image_ids.append(record["id"])
    DB.ltrim(settings.IMAGE_QUEUE, len(image_ids), -1)
    return image_ids, images, batch
示例#12
0
def predict(img):
    # data should be a numpy array that has not been resized
    data = helper.resize_image(img, _std_height, _std_width).flatten()
    predicted_values = _get_predict_model()([data])
    return _captcha_provider.chars[predicted_values]
示例#13
0
def predict(img):
    # data should be a numpy array that has not been resized
    data = helper.resize_image(img, _std_height, _std_width).flatten()
    predicted_values = _get_predict_model()([data])
    return _captcha_provider.chars[predicted_values]
示例#14
0
from sklearn.externals import joblib
from helper import resize_image

import numpy as np

im = resize_image('number5.png')
X = np.asarray(im).flatten()

classifier = joblib.load('model.pkl')
label = classifier.predict(X)

print label
示例#15
0
    def predict(self, data):
        """
        See https://github.com/mdai/model-deploy/blob/master/mdai/server.py for details on the
        schema of `data` and the required schema of the outputs returned by this function.
        """
        input_files = data["files"]
        input_annotations = data["annotations"]
        input_args = data["args"]

        outputs = []

        for file in input_files:
            if file["content_type"] != "application/dicom":
                continue

            arr, ds = read_image_dicom(BytesIO(file["content"]))
            if self.histogram_eq:
                arr = histogram_equalize(arr[:, :, 0]) * 255
                arr = np.stack((arr, ) * 3, -1)

            image = preprocess_image(arr.copy())
            image, scale = resize_image(image)

            if keras.backend.image_data_format() == "channels_first":
                image = image.transpose((2, 0, 1))

            # run network

            with self.graph1.as_default():
                with self.session1.as_default():
                    boxes, scores, labels = self.model.predict_on_batch(
                        np.expand_dims(image, axis=0), )[:3]

            # correct boxes for image scale
            boxes /= scale

            # select indices which have a score above the threshold
            indices = np.where(scores[0, :] > self.score_threshold)[0]

            # select those scores
            scores = scores[0][indices]

            # find the order with which to sort the scores
            scores_sort = np.argsort(-scores)[:100]

            # select detections
            image_boxes = boxes[0, indices[scores_sort], :]
            image_scores = scores[scores_sort]
            image_labels = labels[0, indices[scores_sort]]

            image_detections = np.concatenate(
                [
                    image_boxes,
                    np.expand_dims(image_scores, axis=1),
                    np.expand_dims(image_labels, axis=1),
                ],
                axis=1,
            )

            boxes[:, :, 2] -= boxes[:, :, 0]
            boxes[:, :, 3] -= boxes[:, :, 1]
            rsna_boxes = boxes[0, indices[scores_sort], :]

            selection = np.where(image_scores > self.score_threshold)[0]

            import gc

            gc.collect()

            if len(selection) == 0:
                output = {
                    "type": "NONE",
                    "study_uid": str(ds.StudyInstanceUID),
                    "series_uid": str(ds.SeriesInstanceUID),
                    "instance_uid": str(ds.SOPInstanceUID),
                    "frame_number": None,
                }
                outputs.append(output)
            else:
                for i in selection:
                    elem = rsna_boxes[i, :]
                    data = {
                        "x": int(elem[0]),
                        "y": int(elem[1]),
                        "width": int(elem[2]),
                        "height": int(elem[3]),
                    }
                    output = {
                        "type": "ANNOTATION",
                        "study_uid": str(ds.StudyInstanceUID),
                        "series_uid": str(ds.SeriesInstanceUID),
                        "instance_uid": str(ds.SOPInstanceUID),
                        "frame_number": None,
                        "class_index": 0,
                        "probability": 1.0,
                        "data": data,
                    }
                    outputs.append(output)
        return outputs