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)
def predict(): #initialize data object data = {"success": False} #ensure an image was property if flask.request.method == 'POST': if flask.request.files.get("image"): image = flask.request.files['image'].read() image = Image.open(io.BytesIO(image)) image = prepare_image( image, (settings.IMAGE_WIDTH, settings.IMAGE_HEIGHT)) image = image.copy(order="C") k = str(uuid.uuid4()) image = helper.base64_encode_image(image) d = {"id": k, "image": image} db.rpush(settings.IMAGE_QUEUE, json.dumps(d)) while True: output = db.get(k) if output is not None: output = output.decode("utf-8") data["predictions"] = json.loads(output) db.delete(k) break time.sleep(settings.CLIENT_SLEEP) data["success"] = True return flask.jsonify(data)
def image_enqueue_as_stream(image_path): with open(image_path, "rb") as imageFile: # generate an ID for the classification then add the # classification ID + image to the queue k = str(uuid.uuid4()) image = helper.base64_encode_image(imageFile.read()) d = {"id": str(k), "path": image_path, "image": image} DB.xadd(settings.IMAGE_STREAMING, d)
def image_enqueue(image_path): with open(image_path, "rb") as imageFile: # generate an ID for the classification then add the # classification ID + image to the queue k = str(uuid.uuid4()) image = helper.base64_encode_image(imageFile.read()) d = {"id": k, "path": image_path, "image": image} DB.rpush(settings.IMAGE_QUEUE, json.dumps(d))
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, (settings.IMAGE_WIDTH, settings.IMAGE_HEIGHT)) # 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": helper.base64_encode_image(image)} db.rpush(settings.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 output = output.decode("utf-8") data["predictions"] = json.loads(output) # 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(settings.CLIENT_SLEEP) # indicate that the request was a success data["success"] = True # return the data dictionary as a JSON response return flask.jsonify(data)