import boto3 import paho.mqtt.client as mqtt import time def on_message(mosq, obj, msg): global th_abort img = msg.payload start = img[0:2] end = img[-2:] if start == b'\xff\xd8' and end == b'\xff\xd9': # we have a complete jpeg awsCheck(img) if msg.payload.decode("utf-8")=='awsReadText_close': th_abort = True def on_connect(client, obj, flags, rc): client.subscribe(topic_in, 0) def on_subscribe(client, userdata, mid, granted_qos): print ('Subscribed:', userdata, mid, granted_qos) print ('We are here, waiting...') def send_mqtt_message(topic, msg): for i in range(5): result, mid = client.publish(topic, msg, 0) if result == 0: break def awsCheck(img): response = awsclient.detect_text(Image={'Bytes': img}) textDetections=response['TextDetections'] print ('Matching texts') for text in textDetections: print ('Detected text:' + text['DetectedText']) print ('Confidence: ' + "{:.2f}".format(text['Confidence']) + "%") print ('Id: {}'.format(text['Id'])) if 'ParentId' in text: print ('Parent Id: {}'.format(text['ParentId'])) print ('Type:' + text['Type']) if text['Type'] == 'LINE' and float(text['Confidence']) >= 90.0 : client.publish(topic_out, 'Detected text:' + text['DetectedText']) client = mqtt.Mosquitto() topic_in = "aws_in" topic_out = "aws_out" awsclient=boto3.client('rekognition','eu-west-1') client.on_connect = on_connect client.on_message = on_message client.on_subscribe = on_subscribe resp = client.connect('127.0.0.1', 1883, 60) client.loop_start() th_abort = False while not th_abort: time.sleep(0.1) client.loop_stop() client.disconnect() print ("awsReadText: stopped...") exit()