def classify_process():
    print("* Loading model...")
    model = ResNet50(weights="imagenet")
    print("* Model loaded")

    while True:

        messages = sqs.receive()
        images = []
        if not messages:
            continue

        for msg in messages:
            body = json.loads(msg['Body'])
            s3_path = body['Message']
            img = s3.get(s3_path)
            images.append(img)

        # images = db.lrange(settings.IMAGE_QUEUE, 0,
        #                    settings.BATCH_SIZE - 1)

        image_ids = []
        image_batch = None

        for img in images:

            img = json.loads(img.decode("utf-8"))
            image = helpers.base64_decode_image(
                img["image"], settings.IMAGE_DTYPE,
                (1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
                 settings.IMAGE_CHANS))

            if image_batch is None:
                image_batch = image

            else:
                image_batch = np.vstack([image_batch, image])

            image_ids.append(img["id"])

        if len(image_ids) > 0:

            print("* Batch size: {}".format(image_batch.shape))
            predicts = model.predict(image_batch)
            results = imagenet_utils.decode_predictions(predicts)

            for (image_id, result_set) in zip(image_ids, results):

                output = []

                for _, label, prob in result_set:
                    r = {"label": label, "probability": float(prob)}
                    output.append(r)

                db.set(image_id, json.dumps(output))

            # db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)
        sqs.delete(messages)

        time.sleep(settings.SERVER_SLEEP)
Example #2
0
def run_mnist_model_server():
    """
    The model server
    
    Pull image from the Redis, decode 
    send to the model, predict
    return the response to the redis
    
    Images are tracked using is Image IDs
    """
    print "* Loading MNIST Model..."
    mnist_model = load_model(settings.MNIST_MODEL_PATH)
    print "* MNIST Model Loaded!"

    while True:
        # continues listening...
        # grab a list of image (equal to the batch size from the
        # redis db
        queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE)
        imageIDs = []
        batch = None

        # for each image in the queue
        for q in queue:
            q = json.loads(q.decode("utf-8"))
            # decode the image
            this_image = helpers.base64_decode_image(
                q['image'],
                settings.IMAGE_TYPE,
                shape=(1, 1, settings.IMAGE_WIDTH, settings.IMAGE_HEIGHT))
            # stack up the image to the current
            # batch
            if batch is None:
                batch = this_image
            else:
                batch = np.vstack([batch, this_image])

            # add the id
            imageIDs.append(q['id'])

        # start to put the result back to the db
        if len(imageIDs) > 0:
            print "* Batch size: {}".format(batch.shape)

            # get the prediction results
            this_preds = mnist_model.predict(batch)

            for (each_id, each_pred) in zip(imageIDs, this_preds):
                output = [{"label": np.argmax(each_pred)}]
                # push the results to db
                # imageID as the key
                db.set(each_id, json.dumps(output))

            # remove the set of images from the queue
            db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)

        # wait for the redis to receive new images to predict
        time.sleep(settings.SERVER_SLEEP)
Example #3
0
def ocr_process():
    # 载入ocr模型
    model = ocr.Ocr(text_process=False)
    print("load model done")
    while True:
        # 尝试从redis数据库取出一队图像数据
        queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1)
        # 用于放置每幅图像的id
        imageIDs = []
        batch = None

        # 遍历图像数据队列
        for q in queue:
            # 解码图像数据
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(q["image"],
                                                settings.IMAGE_DTYPE,
                                                q["shape"])

            # 检查数据批是否为空
            if batch is None:
                batch = image

            # 不为空则将数据堆叠起来,形成一批数据
            else:
                batch = np.vstack([batch, image])

            # 更新图像id列表
            imageIDs.append(q["id"])

        # 检查是否需要进行模型预测
        if len(imageIDs) > 0:
            # 处理数据块
            results = []
            print("* Batch size: {}".format(batch.shape))
            for img in batch:
                preds = model.predict(img)
                results.append(preds)

            for (imageID, resultSet) in zip(imageIDs, results):
                # 初始化预测结果
                output = []

                # 初始化文本检测结果
                boxes = []
                for box in resultSet[1]:
                    boxes.append([box[xy] for xy in range(8)])
                r = {"words": resultSet[0], "boxes": boxes}
                output.append(r)

                # 存储检测与识别结果
                db.set(imageID, json.dumps(output, ensure_ascii=False))
            db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)

        # 延时
        time.sleep(settings.SERVER_SLEEP)
Example #4
0
def face_process():
    #vgg_features = VGGFace(include_top=False, input_shape=(224, 224, 3), pooling='avg') ## doesn't seem to be required.
    print("* Loading model")
    model = VGGFace(model='vgg16')
    print("* Model loaded")

    while True:
        queue = db.lrange(settings.FACE_QUEUE, 0, settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = None

        for q in queue:
            # deserialize the object and obtain the input image
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(
                q["image"], settings.IMAGE_DTYPE,
                (1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
                 settings.IMAGE_CHANS))

            # 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])

            # update the list of image IDs
            imageIDs.append(q["id"])

        # check to see if we need to process the batch
        if len(imageIDs) > 0:
            # classify the batch
            print("* Batch size: {}".format(batch.shape))
            preds = model.predict(batch)
            results = utils.decode_predictions(preds)
            #print(results) ## this comes back with something so its the below structure giving me an issue.
            # [[["b'A.J._Buckley'", 0.9768057], ["b'David_Denman'", 0.0013909286], ["b'Carmine_Giovinazzo'", 0.0010687601], ["b'Robert_Buckley'", 0.00093060045], ["b'Eddie_Cahill'", 0.00044030472]]]

            for (imageID, resultSet) in zip(imageIDs, results):
                print("imageID", imageID, resultSet)
                ## imageID 63350aef-0ec3-4e1d-be99-3db36013a6d7
                output = []
                for (label, prob) in resultSet:
                    r = {"label": label, "probability": float(prob)}
                    output.append(r)
                db.set(imageID, json.dumps(output))

            # remove the set of images from our queue
            db.ltrim(settings.FACE_QUEUE, len(imageIDs), -1)

        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
def classify_process():
    # load the pre-trained Keras model (here we are using a model
    # pre-trained on ImageNet and provided by Keras, but you can
    # substitute in your own networks just as easily)
    print("* Loading model...")
    model = ResNet50(weights="imagenet")
    print("* Model loaded")
    # continually pool for new images to classify
    while True:
        # attempt to grab a batch of images from the database, then
        # initialize the image IDs and batch of images themselves
        queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = None
        # loop over the queue
        for q in queue:
            # deserialize the object and obtain the input image
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(
                q["image"], settings.IMAGE_DTYPE,
                (1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
                 settings.IMAGE_CHANS))
            # 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])
            # update the list of image IDs
            imageIDs.append(q["id"])
        # check to see if we need to process the batch
        if len(imageIDs) > 0:
            # classify the batch
            print("* Batch size: {}".format(batch.shape))
            preds = model.predict(batch)
            results = decode_predictions(preds)
            # loop over the image IDs and their corresponding set of
            # results from our model
            for (imageID, resultSet) in zip(imageIDs, results):
                # initialize the list of output predictions
                output = []
                # loop over the results and add them to the list of
                # output predictions
                for (imagenetID, label, prob) in resultSet:
                    r = {"label": label, "probability": float(prob)}
                    output.append(r)
                # store the output predictions in the database, using
                # the image ID as the key so we can fetch the results
                db.set(imageID, json.dumps(output))
            # remove the set of images from our queue
            db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)
        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
Example #6
0
def classify_process():
    #to be replaced
    model = ResNet50(weights="imagenet")

    while True:
        # get batch
        queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = None

        for q in queue:

            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(
                q["image"], settings.IMAGE_DTYPE,
                (1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
                 settings.IMAGE_CHANS))

            if batch is None:
                batch = image

            else:
                batch = np.vstack([batch, image])

            imageIDs.append(q["id"])

        if len(imageIDs) > 0:
            print("* Batch size: {}".format(batch.shape))
            preds = model.predict(batch)
            results = imagenet_utils.decode_predictions(preds)

            for (imageID, resultSet) in zip(imageIDs, results):

                output = []

                for (imagenetID, label, prob) in resultSet:
                    r = {
                        "label": label,
                        "point_x": float(prob[0]),
                        "point_x": float(prob[1])
                    }
                    output.append(r)

                db.set(imageID, json.dumps(output))

            db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)

        time.sleep(settings.SERVER_SLEEP)
def classify_process(stream_batch, batch_id):
    # Covert to List[Row]
    stream_batch = stream_batch.collect()

    image_ids = []
    batch = None
    # loop over the queue
    for q in stream_batch:
        # deserialize the object and obtain the input image
        image = helpers.base64_decode_image(q["image"])
        image = helpers.byte_to_mat(image, dtype=settings.IMAGE_DTYPE)
        image = helpers.image_preprocess(image, settings.IMAGE_WIDTH, settings.IMAGE_HEIGHT)
        # 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])

        # update the list of image IDs
        image_ids.append(q["id"])

    # check to see if we need to process the batch
    if len(image_ids) > 0:
        # classify the batch
        batch = np.expand_dims(batch, axis=0)
        print("* Batch size: {}".format(batch.shape))
        # Output is [1, 4, 1000]
        results = model.predict(batch)[0]

        # loop over the image IDs and their corresponding set of
        # results from our model
        for (image_id, resultSet) in zip(image_ids, results):
            # initialize the list of output predictions
            output = {}
            # loop over the results and add them to the list of
            # output predictions
            # Top 1
            max_index = np.argmax(resultSet)
            output["Top-1"] = str(max_index)
            output["id"] = image_id
            print("* Predict result " + str(output))
            # store the output predictions in the database, using
            # the image ID as the key so we can fetch the results
            DB.lpush(settings.PREDICT_QUEUE, json.dumps(output))

        # remove the set of images from our queue
        DB.ltrim(settings.IMAGE_QUEUE, len(image_ids), -1)
Example #8
0
def detect_process():
    # continually pool for new images to detect
    while True:
        # attempt to grab a batch of images from the database, then
        # initialize the image IDs and batch of images themselves
        queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = None

        # loop over the queue
        for q in queue:
            # deserialize the object and obtain the input image
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(
                q["image"], settings.IMAGE_DTYPE,
                (1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
                 settings.IMAGE_CHANS))
            #rfid = q["rfid"]

            # 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])

            # update the list of image IDs
            imageIDs.append(q["id"])

        # check to see if we need to process the batch
        if len(imageIDs) > 0:
            # detect the batch
            print("* Batch size: {}".format(batch.shape))
            rfid = './data/tmp/rfid.csv'
            results = sync_bibnumber_api(batch, rfid)
            # loop over the image IDs and their corresponding set of
            # results from our model
            for (imageID, resultSet) in zip(imageIDs, results):
                db.set(imageID, json.dumps(resultSet))

            # remove the set of images from our queue
            db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)

        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
Example #9
0
def tag_process():
    print("* Loading model .....")
    model = VGG16(weights="vgg16_weights_tf_dim_ordering_tf_kernels.h5")
    print("* Model loaded")

    while True:
        queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = None
        for q in queue:
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(
                q["image"], settings.IMAGE_DTYPE,
                (1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
                 settings.IMAGE_CHANS))

            if batch is None:
                batch = image
            else:
                batch = np.vstack([batch, image])

            imageIDs.append(q["id"])

        if len(imageIDs) > 0:
            print("* Batch size : {}".format(batch.shape))
            preds = model.predict(batch)
            results = imagenet_utils.decode_predictions(
                preds, top=settings.VGG_TOP_CLASSES)

            for (imageID, resultSet) in zip(imageIDs, results):
                tags = []
                for (_, label, prob) in resultSet:
                    tags.append(label)

                db.set(imageID, json.dumps(tags))

            db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)

        time.sleep(settings.SERVER_SLEEP)
Example #10
0
def start_classify():

    model = load_model()
    print("Done LOADING!")

    while True:
        queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = None

        for q in queue:
            q = json.loads(q.decode("utf-8"))
            #print(type(q["image"]))
            image = base64_decode_image(q["image"], settings.IMAGE_DTYPE)

            if batch is None:
                batch = image
            else:
                batch = np.vstack([batch, image])

            imageIDs.append(q["id"])

        if len(imageIDs) > 0:
            print("* Batch size: {}".format(batch.shape))
            results = [im_detect(model, image)]

            for (imageID, ans) in zip(imageIDs, results):
                #print(ans)
                output = []
                scores = ans[0]
                boxes = ans[1]
                r = {"labels": scores.tolist(), "boxes": boxes.tolist()}
                output.append(r)

                db.set(imageID, json.dumps(output))
            db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)

        time.sleep(settings.SERVER_SLEEP)
Example #11
0
def classify_process(model_path):
    # load the pre-trained OpenVINO model
    print("* Loading model...")
    model = InferenceModel()
    model.load_openvino(model_path,
                        weight_path=model_path[:model_path.rindex(".")] + ".bin")
    print("* Model loaded")

    # continually pool for new images to classify
    while True:
        # attempt to grab a batch of images from the database, then
        # initialize the image IDs and batch of images themselves
        queue = DB.lrange(settings.IMAGE_QUEUE, 0,
                          settings.BATCH_SIZE - 1)
        image_ids = []
        batch = None

        # loop over the queue
        start_time = time.time()
        for q in queue:
            # deserialize the object and obtain the input image
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(q["image"])
            image = helpers.byte_to_mat(image, dtype=settings.IMAGE_DTYPE)
            image = helpers.image_preprocess(image, settings.IMAGE_WIDTH, settings.IMAGE_HEIGHT)
            # 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])

            # update the list of image IDs
            image_ids.append(q["id"])
        print("* Pop from redis %d ms" % int(round((time.time() - start_time) * 1000)))
        # check to see if we need to process the batch
        if len(image_ids) > 0:
            # classify the batch
            batch = np.expand_dims(batch, axis=0)
            print("* Batch size: {}".format(batch.shape))
            # Output is [1, 4, 1000]
            results = model.predict(batch)[0]
            print("* Predict a batch %d ms" % int(round((time.time() - start_time) * 1000)))
            # loop over the image IDs and their corresponding set of
            # results from our model
            for (imageID, resultSet) in zip(image_ids, results):
                # initialize the list of output predictions
                output = {}
                # loop over the results and add them to the list of
                # output predictions
                # Top 1
                max_index = np.argmax(resultSet)
                output["Top-1"] = str(max_index)
                output["id"] = imageID
                print("* Predict result " + str(output))
                # store the output predictions in the database, using
                # the image ID as the key so we can fetch the results
                DB.lpush(settings.PREDICT_QUEUE, json.dumps(output))

            # remove the set of images from our queue
            print("* Total time used is %d ms" % int(round((time.time() - start_time) * 1000)))
            DB.ltrim(settings.IMAGE_QUEUE, len(image_ids), -1)

        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
Example #12
0
def classify_process(model_path):
    print("* Loading model...")
    model = InferenceModel()
    model.load_openvino(model_path=model_path,
                        weight_path=model_path[:model_path.rindex(".")] +
                        ".bin")
    print("* Model loaded")
    # continually poll for new images to classify
    while True:
        image_ids = []
        batch = None
        # loop over the queue
        start_time = time.time()
        count = 0
        while count < 4:
            # Get message or None or timeout
            record = pub.get_message()
            if record and record['type'] == 'message':
                data = json.loads(record['data'].decode("utf-8"))
                image = helpers.base64_decode_image(data["image"])
                image = helpers.byte_to_mat(image, dtype=settings.IMAGE_DTYPE)
                image = helpers.image_preprocess(image, settings.IMAGE_WIDTH,
                                                 settings.IMAGE_HEIGHT)
                # 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])
                # update the list of image IDs
                image_ids.append(data["id"])
                count += 1
        print("* Pop from redis %d ms" %
              int(round((time.time() - start_time) * 1000)))
        # check to see if we need to process the batch
        if len(image_ids) > 0:
            # classify the batch
            batch = np.expand_dims(batch, axis=0)
            print("* Batch size: {}".format(batch.shape))
            # Output is [1, 4, 1000]
            results = model.predict(batch)[0]
            print("* Predict a batch %d ms" %
                  int(round((time.time() - start_time) * 1000)))
            # loop over the image IDs and their corresponding set of
            # results from our model
            for (imageID, resultSet) in zip(image_ids, results):
                # initialize the list of output predictions
                output = {}
                # loop over the results and add them to the list of
                # output predictions
                # Top 1
                max_index = np.argmax(resultSet)
                output["Top-1"] = str(max_index)
                output["id"] = imageID
                print("* Predict result " + str(output))
                # store the output predictions in the database, using
                # the image ID as the key so we can fetch the results
                DB.lpush(settings.PREDICT_QUEUE, json.dumps(output))

            # remove the set of images from our queue
            print("* Total time used is %d ms" %
                  int(round((time.time() - start_time) * 1000)))

        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
Example #13
0
def classify_process():
    print("* Loading model...")
    img_size = 64
    model = WideResNet(img_size, depth=16, k=8)()
    model.load_weights("checkpoints168/weights.48-3.62.hdf5")
    print("* Model loaded")

    while True:
        body = db.lpop(settings.IMAGE_QUEUE)
        if body is not None:
            body = json.loads(body.decode("utf-8"))
            image = helpers.base64_decode_image(body["image"], body["height"])

            bounding_boxes = demo_mtcnn.detect_face(image)
            margin = 0.4
            faces = np.empty((len(bounding_boxes), 64, 64, 3))
            for i, bounding_box in enumerate(bounding_boxes):
                x = int(bounding_box[0])
                y = int(bounding_box[1])
                w = int(bounding_box[2])
                h = int(bounding_box[3])
                img_h, img_w, _ = np.shape(image)
                _x = max(int(x - margin * w), 0)
                _y = max(int(y - margin * h), 0)
                _x2 = min(int(x + w + margin * w), img_w - 1)
                _y2 = min(int(y + h + margin * h), img_h - 1)
                # cv2.rectangle(image, (x, y), (x+w, y+h), (0, 155, 255), 2)
                faces[i, :, :, :] = cv2.resize(
                    image[_y:_y2 + 1, _x:_x2 + 1, :], (64, 64))

                # cv2.rectangle(image, (_x, _y), (_x2, _y2), (0, 155, 255), 2)
                # cv2.imshow("",faces[0])

                # key  = cv2.waitKey(-1)
                # if key==27:
                #     break
            if len(faces) > 0:

                results = model.predict(faces)
                # results = imagenet_utils.decode_predictions(preds)
                predicted_genders = results[0]
                ages = np.arange(0, 101).reshape(101, 1)
                predicted_ages = results[1].dot(ages).flatten()
                predicted_ages = predicted_ages.tolist()
                predicted_genders = [
                    gender.tolist() for gender in predicted_genders
                ]
                print(type(predicted_genders[0]))
                print(type(predicted_ages))

                # for i, bounding_box in enumerate(bounding_boxes):
                # 	label = "{}, {}".format(int(predicted_ages[i]),"M" if predicted_genders[i][0] < 0.5 else "F")
                # 	demo_mtcnn.draw_label(image, (int(bounding_box[0]), int(bounding_box[1])), label)
                # cv2.imshow("",image)
                # cv2.waitKey(1000)
                print("done")

            # image_out = helpers.base64_encode_image(image).decode('utf-8')

            # db.set(body["id"], json.dumps(image_out))
            data = {
                "boudingboxes": bounding_boxes.tolist(),
                "ages": predicted_ages,
                "genders": predicted_genders
            }
            db.set(body["id"], json.dumps(data))
        else:
            print("No data")
        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)
Example #14
0
 def receive_img(self, topic, dtype, shape):
     img = self._consume(topic, is_img=True)
     return base64_decode_image(img, dtype=dtype, shape=shape)
Example #15
0
def classify_process():
	# load the pre-trained Keras model (here we are using a model
	# pre-trained on ImageNet and provided by Keras, but you can
	# substitute in your own networks just as easily)
	print("* Loading model...")
	base_model = tf.keras.applications.MobileNetV2(input_shape=(settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH, settings.IMAGE_CHANS),
                                               include_top=False,
                                               weights='imagenet')
	model = Sequential([base_model, GlobalAveragePooling2D(), Dense(497, activation='softmax')])
	model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
	model.load_weights(settings.CHECKPOINT_PATH)
	print("* Model loaded")

	# continually pool for new images to classify
	while True:
		# attempt to grab a batch of images from the database, then
		# initialize the image IDs and batch of images themselves
		queue = db.lrange(settings.IMAGE_QUEUE, 0,
			settings.BATCH_SIZE - 1)
		imageIDs = []
		batch = None

		# loop over the queue
		for q in queue:
			# deserialize the object and obtain the input image
			q = json.loads(q.decode("utf-8"))
			image = helpers.base64_decode_image(q["image"],
				settings.IMAGE_DTYPE,
				(1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
					settings.IMAGE_CHANS))
			# 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])

			# update the list of image IDs
			imageIDs.append(q["id"])

		# check to see if we need to process the batch
		if len(imageIDs) > 0:
			# classify the batch
			print("* Batch size: {}".format(batch.shape))
			preds = model.predict(batch)
			results = helpers.decode_predictions(preds)
			
			# loop over the image IDs and their corresponding set of
			# results from our model
			for (imageID, resultSet) in zip(imageIDs, results):
				# initialize the list of output predictions
				output = []

				# loop over the results and add them to the list of
				# output predictions
				label, prob = resultSet
				r = {"label": label, "probability": float(prob)}
				output.append(r)

				# store the output predictions in the database, using
				# the image ID as the key so we can fetch the results
				db.set(imageID, json.dumps(output))

			# remove the set of images from our queue
			db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)

		# sleep for a small amount
		time.sleep(settings.SERVER_SLEEP)
def classify_process():
	# load the pre-trained Keras model (here we are using a model
	# pre-trained on ImageNet and provided by Keras, but you can
	# substitute in your own networks just as easily)
	print("* Loading model...")
	model = ResNet50(weights="imagenet")
	print("* Model loaded")
 
	# continually pool for new images to classify
	while True:
		# attempt to grab a batch of images from the database, then
		# initialize the image IDs and batch of images themselves
		queue = db.lrange(settings.IMAGE_QUEUE, 0,
			settings.BATCH_SIZE - 1)
		imageIDs = []
		batch = None
 
		# loop over the queue
		for q in queue:
			# deserialize the object and obtain the input image
			q = json.loads(q.decode("utf-8"))
			image = helpers.base64_decode_image(q["image"],
				settings.IMAGE_DTYPE,
				(1, settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
					settings.IMAGE_CHANS))
 
			# 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])
 
			# update the list of image IDs
			imageIDs.append(q["id"])
 
		# check to see if we need to process the batch
		if len(imageIDs) > 0:
			# classify the batch
			print("* Batch size: {}".format(batch.shape))
			preds = model.predict(batch)
			results = imagenet_utils.decode_predictions(preds)
 
			# loop over the image IDs and their corresponding set of
			# results from our model
			for (imageID, resultSet) in zip(imageIDs, results):
				# initialize the list of output predictions
				output = []
 
				# loop over the results and add them to the list of
				# output predictions
				for (imagenetID, label, prob) in resultSet:
					r = {"label": label, "probability": float(prob)}
					output.append(r)
 
				# store the output predictions in the database, using
				# the image ID as the key so we can fetch the results
				db.set(imageID, json.dumps(output))
 
			# remove the set of images from our queue
			db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1)
 
		# sleep for a small amount
		time.sleep(settings.SERVER_SLEEP)
def main():
    print("* Loading models...")
    input_img = Input(
        (settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH, settings.IMAGE_CHANS),
        name='img')
    model = get_unet(input_img, n_filters=16, dropout=0.05, batchnorm=True)
    model.compile(optimizer=Adam(),
                  loss="binary_crossentropy",
                  metrics=["accuracy"])
    model.load_weights('face-segmentation.h5')
    net = cv2.dnn.readNetFromCaffe('deploy.prototxt.txt',
                                   'res10_300x300_ssd_iter_140000.caffemodel')
    print("* Models loaded")

    while True:
        # attempt to grab a batch of images from the database, then
        # initialize the image IDs and batch of images themselves
        queue = db.lrange(settings.SEGMENT_IMAGE_QUEUE, 0,
                          settings.BATCH_SIZE - 1)
        imageIDs = []
        batch = []
        confidences = []
        # loop over the queue
        for q in queue:
            # deserialize the object and obtain the input image
            q = json.loads(q.decode("utf-8"))
            image = helpers.base64_decode_image(q["image"],
                                                settings.IMAGE_DTYPE,
                                                q["shape"])
            # check to see if the batch list is None
            batch.append(image)
            # update the list of image IDs
            imageIDs.append(q["id"])
            # add to list of confidence attributes
            confidences.append(q["confidence"])

        # check to see if we need to process the batch
        if len(imageIDs) > 0:
            print('* ImageIDs: {}'.format(imageIDs))
            faces = {}
            for i in range(len(batch)):
                print("* Batch size: {}".format(batch[i][0].shape))
                image = np.array(batch[i][0], dtype="uint8")
                (h, w) = image.shape[:2]
                if h > 1000 or w > 1000:
                    blob = cv2.dnn.blobFromImage(image,
                                                 mean=(104.0, 117.0, 123.0),
                                                 swapRB=True)
                    net.setInput(blob)
                    detections = net.forward()
                else:
                    blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)),
                                                 1.0, (300, 300),
                                                 (104.0, 117.0, 123.0),
                                                 swapRB=True)
                    net.setInput(blob)
                    detections = net.forward()

                if detections.shape[2] == 0:
                    faces[i] = np.array([[[[0., 1., 1., 0., 0., 1., 1.]]]])
                else:
                    faces[i] = detections

            X = []
            X_positions = []
            numFaces = []
            for imageNum, detections in faces.items():
                image = batch[imageNum][0]
                confidence = confidences[imageNum]
                (h, w) = image.shape[:2]
                index = 0
                # loop over the detections
                for i in range(0, detections.shape[2]):
                    # extract the confidence (i.e., probability) associated with the prediction
                    conf = detections[0, 0, i, 2]
                    # filter out weak detections by ensuring the `confidence` is greater than the minimum confidence
                    if conf > confidence:
                        # compute the (x, y)-coordinates of the bounding box for the object
                        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                        (startX, startY, endX, endY) = box.astype("int")
                        if startX > image.shape[
                                1] - 10 or startY > image.shape[0] - 10:
                            continue
                        w_box = endX - startX
                        h_box = endY - startY
                        transpose_x, transpose_y = w_box * 0.75, h_box * 0.75
                        x_img = math.floor(startX - transpose_x) if math.floor(
                            startX - transpose_x) >= 0 else 0
                        y_img = math.floor(startY - transpose_y) if math.floor(
                            startY - transpose_y) >= 0 else 0
                        w_img = math.floor(
                            w_box + 2 * transpose_x) if x_img + math.floor(
                                w_box + 2 * transpose_x
                            ) <= image.shape[1] else image.shape[1] - x_img
                        h_img = math.floor(
                            h_box + 2 * transpose_y) if y_img + math.floor(
                                h_box + 2 * transpose_y
                            ) <= image.shape[0] else image.shape[0] - y_img
                        X_positions.append([x_img, y_img, w_img, h_img])
                        # 2d array of cropped image
                        roi_color = image[y_img:h_img + y_img,
                                          x_img:w_img + x_img]
                        X.append(
                            resize(
                                roi_color,
                                (settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH),
                                mode='constant',
                                preserve_range=True))
                        index += 1
                if (index == 0):
                    index = 1
                    box = [0., 0., 1., 1.] * np.array([w, h, w, h])
                    (startX, startY, endX, endY) = box.astype("int")
                    w_box = endX - startX
                    h_box = endY - startY
                    transpose_x, transpose_y = w_box * 0.75, h_box * 0.75
                    x_img = math.floor(startX - transpose_x) if math.floor(
                        startX - transpose_x) >= 0 else 0
                    y_img = math.floor(startY - transpose_y) if math.floor(
                        startY - transpose_y) >= 0 else 0
                    w_img = math.floor(
                        w_box + 2 * transpose_x) if x_img + math.floor(
                            w_box + 2 * transpose_x
                        ) <= image.shape[1] else image.shape[1] - x_img
                    h_img = math.floor(
                        h_box + 2 * transpose_y) if y_img + math.floor(
                            h_box + 2 * transpose_y
                        ) <= image.shape[0] else image.shape[0] - y_img
                    X_positions.append([x_img, y_img, w_img, h_img])
                    # 2d array of cropped image
                    roi_color = image[y_img:h_img + y_img, x_img:w_img + x_img]
                    X.append(
                        resize(roi_color,
                               (settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH),
                               mode='constant',
                               preserve_range=True))
                numFaces.append(index)

            X = np.array(X).astype(np.float32)
            preds_test = (model.predict(X, verbose=1) > 0.8).astype(np.uint8)
            index = 0
            masks = []
            for imageNum, detections in faces.items():
                upsampled_mask = np.zeros(
                    (batch[imageNum][0].shape[0], batch[imageNum][0].shape[1]),
                    dtype=np.uint8)
                for i in range(index, index + numFaces[imageNum]):
                    coords = X_positions[i]
                    section = resize(np.squeeze(preds_test[i]),
                                     (coords[3], coords[2]),
                                     mode='constant',
                                     preserve_range=True,
                                     order=0)
                    upsampled_mask[coords[1]:coords[3] + coords[1],
                                   coords[0]:coords[2] +
                                   coords[0]] += section.astype(np.uint8)
                masks.append(upsampled_mask)
                index += numFaces[imageNum]

            for imageID, i in zip(imageIDs, range(len(imageIDs))):
                db.set(imageID, json.dumps(masks[i].tolist()))
            # remove the set of images from our queue
            db.ltrim(settings.SEGMENT_IMAGE_QUEUE, len(imageIDs), -1)
        # sleep for a small amount
        time.sleep(settings.SERVER_SLEEP)