def main(): client = get_mqtt_client() # if MQTT_USER != "" and MQTT_PWD != "": client.username_pw_set(MQTT_USER, MQTT_PWD) client.connect(MQTT_BROKER, port=MQTT_PORT) time.sleep(4) # Wait for connection setup to complete client.loop_start() # Open camera camera = WebcamVideoStream(src=VIDEO_SOURCE).start() # time.sleep(2) # Webcam light should come on if using one while True: frame = camera.read() font = cv2.FONT_HERSHEY_SIMPLEX datet = datetime.datetime.now().strftime("%d.%m.%Y, %H:%M:%S") frame = cv2.putText(frame, datet, (10, 20), font, 0.7, (255, 255, 255), 1, cv2.LINE_AA) # cv2.imshow('frame', frame) np_array_RGB = opencv2matplotlib(frame) # Convert to RGB image = Image.fromarray(np_array_RGB) # PIL image byte_array = pil_image_to_byte_array(image) client.publish(MQTT_TOPIC_CAMERA, byte_array, qos=MQTT_QOS) now = get_now_string() print(f"published frame on topic: {MQTT_TOPIC_CAMERA} at {now}")
def on_message(client, userdata, msg): now = get_now_string() print("message on " + str(msg.topic) + f" at {now}") try: image = byte_array_to_pil_image(msg.payload) image = image.convert("RGB") save_file_path = CAPTURES_DIRECTORY + f"capture_{now}.jpg" image.save(save_file_path) # Additional code to save thumbnail to db db_conn = sqlite_connect(DB) cursor = db_conn.cursor() db_insert_blob = """ INSERT INTO {table_name} (name, data) VALUES (?, ?) """.format(table_name=DB_TABLE) image.thumbnail(size=THUMBNAIL_SIZE) binary_data = pil_image_to_byte_array(image) data_tuple = (save_file_path, binary_data) # Execute the query cursor.execute(db_insert_blob, data_tuple) db_conn.commit() print(f"Saved {save_file_path} and inserted to db") cursor.close() except Exception as exc: print(exc)
def on_message(client, userdata, msg): now = get_now_string() print("message on " + str(msg.topic) + f" at {now}") try: image = byte_array_to_pil_image(msg.payload) # PIL image image = image.rotate(ROTATE_ANGLE) # Apply rotation byte_array = pil_image_to_byte_array(image) client.publish(MQTT_PUBLISH_TOPIC, byte_array, qos=MQTT_QOS) print(f"published processed frame on topic: {MQTT_PUBLISH_TOPIC} at {now}") except Exception as exc: print(exc)
def main(): client = get_mqtt_client() client.connect(MQTT_BROKER, port=MQTT_PORT) time.sleep(4) # Wait for connection setup to complete client.loop_start() # Open camera camera = WebcamVideoStream(src=VIDEO_SOURCE).start() time.sleep(2) # Webcam light should come on if using one while True: frame = camera.read() np_array_RGB = opencv2matplotlib(frame) # Convert to RGB image = Image.fromarray(np_array_RGB) # PIL image byte_array = pil_image_to_byte_array(image) client.publish(MQTT_TOPIC_CAMERA, byte_array, qos=MQTT_QOS) now = get_now_string() print(f"published frame on topic: {MQTT_TOPIC_CAMERA} at {now}") time.sleep(1 / FPS)