def greengrass_object_classification_run()

in lambda-rpi-inference/greengrassObjectClassification.py [0:0]


def greengrass_object_classification_run(model):
    print("running inference loop")

    test_file = random.choice(test_files)
    print("Running inference on {0}".format(test_file))
    fname = os.path.split(test_file)[1]
    imgid = os.path.splitext(fname)[0] + '_' + str(int(time.time()))

    imgpayload = {}
    imgpayload['imgid'] = imgid 
    imgpayload['timestamp'] = str(int(time.time()))
    imgpayload['fab'] = fabid
    imgpayload['camera'] = cameraid
    print("Payload before image bytes: " + json.dumps(imgpayload))

    with open(test_file, "rb") as imageFile:
        imgpayload['bytes'] = base64.b64encode(imageFile.read())
        print(imgpayload['bytes'])

    topicPath="fabwafer/{0}/{1}/img/{2}".format(fabid, cameraid, imgid)
    print("Publishing to topic " + topicPath)
    client.publish(
        topic=topicPath,
        payload=json.dumps(imgpayload)
    )
    print("Published to topic " + topicPath)


    print("Calling inference model")
    im_frame = Image.open(test_file)
    im_frame = im_frame.resize((224,224), resample=Image.BILINEAR)
    imrgb = im_frame.convert("RGB")
    im2arr = np.array(imrgb)
    print("Data passed to model has shape " + str(im2arr.shape))
    response = int(predict(model, im2arr))
    print("Got inference response " + str(response))
    topicPath="fabwafer/{0}/{1}/prediction/{2}".format(fabid, cameraid, imgid)
    predpayload = {}
    predpayload['imgid'] = imgid 
    predpayload['timestamp'] = int(time.time())
    predpayload['fab'] = fabid
    predpayload['camera'] = cameraid
    predpayload['prediction'] = synsets[response]
    predpayload['probability'] = 50.0
    print("Prediction payload: " + json.dumps(predpayload))
    client.publish(
        topic=topicPath,
        payload=json.dumps(predpayload)
    )

    # Asynchronously schedule this function to be run again in 3 seconds
    Timer(interval, greengrass_object_classification_run, args=[model]).start()