4th October 2024

The article under was contributed by Timothy Malche, an assistant professor within the Division of Laptop Purposes at Manipal College Jaipur.

A water meter measures the amount of water utilized by a residential or industrial constructing. It helps utility corporations and property house owners monitor water consumption for billing functions, leak detection, and useful resource administration. 

Water meters usually require studying to be carried out manually which is time-consuming and labour-intensive, particularly when studying is to be carried out by visiting distant places or giant numbers of meters to be monitored. That is the place laptop imaginative and prescient could be useful.

Moreover, manually coming into water meter info is susceptible to error. If a digit is mis-typed – attainable within the usually darkish and cramped environments through which many water meters are – it might take some time to rectify the error.

On this weblog submit we are going to learn to construct a pc imaginative and prescient system to learn knowledge from water meters and transmit the information remotely.

How the system works

The system operates through the use of a pc imaginative and prescient mannequin to detect and determine numbers from water meters. It consists of the next parts:

  • Digicam node captures photos of the water meter.
  • A pc imaginative and prescient mannequin constructed with Roboflow that analyzes the captured photos, then detects and extracts numbers from the water meter show.
  • Extracted knowledge is distributed to the Quibtro IoT platform through MQTT and knowledge is displayed.

The transmitted knowledge can then be used for varied functions, comparable to monitoring water utilization or producing payments.

How the system works

Constructing The Utility

To construct this utility, we are going to:

  • Construct an object detection mannequin.
  • Write inference script to detect numbers in water meter picture and ship knowledge through MQTT.
  • Setup Qubitro IoT server to obtain and visualize knowledge.

Step #1: Construct object detection mannequin

For this mission, we are going to use the water meter dataset from FESB workspace on Roboflow Universe. We’ll then prepare and deploy it utilizing the Roboflow.

The  water meter laptop imaginative and prescient mission dataset on Roboflow Universe consists of greater than 4K+ photos of water meters.

Water Meter Dataset on Roboflow Universe

The dataset is labeled for digits from Zero to 9, indicated by respective class labels. Following is an instance of sophistication labels.

Labeled picture from Water Meter Dataset

To construct the pc imaginative and prescient mannequin, I downloaded the dataset and uploaded it to my very own workspace.

Water meter dataset on my workspace

As soon as the dataset model is generated, I educated the mannequin utilizing Roboflow auto coaching. The next picture reveals the metrics of the educated mannequin. The educated mannequin is out there right here.

Modal metrics

Step #2: Write inference script to detect numbers in water meter picture and ship knowledge through MQTT

To run inference on a picture, we will use following script:

# import the inference-sdk
from inference_sdk import InferenceHTTPClient # initialize the consumer
CLIENT = InferenceHTTPClient(
    api_url="https://detect.roboflow.com",
    api_key="ROBOFLOW_API_KEY"
) # infer on a neighborhood picture
consequence = CLIENT.infer("water_meter.jpg", model_id="water-meter-monitoring/1")
print(consequence)

For the water_meter.jpg picture under:

Pattern check picture of water meter

The code generates the next output:

{'inference_id': 'a88ac8ce-e750-493a-b861-6f74734ec43c', 'time': 0.0358694110000215, 'picture': {'width': 416, 'top': 416}, 'predictions': [{'x': 258.0, 'y': 237.0, 'width': 30.0, 'height': 62.0, 'confidence': 0.9480071663856506, 'class': '8', 'class_id': 8, 'detection_id': '8a757580-2f8c-4732-bef4-9fe38b0ec665'}, {'x': 316.5, 'y': 236.0, 'width': 31.0, 'height': 64.0, 'confidence': 0.936906099319458, 'class': '7', 'class_id': 7, 'detection_id': '05b6b394-73ea-4900-baa3-068ed9b44155'}, {'x': 202.0, 'y': 237.0, 'width': 30.0, 'height': 58.0, 'confidence': 0.9148543477058411, 'class': '4', 'class_id': 4, 'detection_id': 'd7240eec-d1c2-4480-a1b4-03355260620c'}, {'x': 145.0, 'y': 236.5, 'width': 32.0, 'height': 59.0, 'confidence': 0.9080735445022583, 'class': '5', 'class_id': 5, 'detection_id': 'c74f9a86-2458-4537-94ea-103658e34d17'}, {'x': 87.0, 'y': 234.0, 'width': 30.0, 'height': 62.0, 'confidence': 0.8975856900215149, 'class': '5', 'class_id': 5, 'detection_id': '72c74e39-4029-428b-af2b-9c6772a0376c'}, {'x': 27.5, 'y': 229.0, 'width': 33.0, 'height': 54.0, 'confidence': 0.8934838771820068, 'class': '0', 'class_id': 0, 'detection_id': '33a9d3c9-14d8-4a09-b24a-47e8c85c4912'}, {'x': 371.5, 'y': 245.5, 'width': 29.0, 'height': 59.0, 'confidence': 0.883609414100647, 'class': '6', 'class_id': 6, 'detection_id': 'ae89df94-dad2-4738-95da-c2c05d074c4b'}]}

Within the output we will see that the detected digits, i.e. 8745506, are usually not in sequence as they seem on the water meter, i.e. 0554876. We will remedy this downside by making use of post-processing with coordinates. 

After detecting the digits, every detection could have a bounding field with coordinates (x, y, width, top). We will kind these bounding containers based mostly on their x coordinate, which represents the horizontal place of the digits within the picture. Sorting the bounding containers by the x coordinate will give us the digits within the left-to-right order, equivalent to their pure sequence on the meter. It may be carried out with the next code.

# Type the predictions by the x-coordinate
sorted_predictions = sorted(predictions, key=lambda p: p['x'])

Right here is the entire inference code that types the coordinates by their x values, permitting us to learn the numbers from left to proper:

from inference_sdk import InferenceHTTPClient
import os # Initialize the inference consumer
CLIENT = InferenceHTTPClient(
    api_url="https://detect.roboflow.com",
    api_key="ROBOFLOW_API_KEY"  # Add your API key right here
)
# Carry out inference on the captured picture
def perform_inference(image_path):
    if not image_path:
        print("No picture to course of.")
        return     # Carry out inference
    consequence = CLIENT.infer(image_path, model_id="water-meter-monitoring/1")     # Course of the inference consequence
    predictions = consequence['predictions']     # Type the predictions by the x-coordinate
    sorted_predictions = sorted(predictions, key=lambda p: p['x'])     # Extract the category labels and be part of them to type the digit sequence
    digit_sequence = ''.be part of([pred['class'] for pred in sorted_predictions])     # Print the digit sequence
    print("Detected digit sequence:", digit_sequence) # Major perform to seize picture and carry out inference
def major():
    # specify check picture supply
    image_path = "water_meter.jpg"     # Carry out inference on the captured picture
    perform_inference(image_path) # Run the principle perform
if __name__ == "__main__":
    major()

To create an automatic system, we have to make sure that the picture is captured mechanically at specified intervals (on a regular basis 9:00 AM in our instance) and that predictions are run on the captured picture. To realize this, we will use the next code which captures the picture from the webcam on a regular basis at 9:00 AM. Under is the ultimate code for our utility.

import cv2
import schedule
import time
from datetime import datetime
from inference_sdk import InferenceHTTPClient
import os
import paho.mqtt.consumer as mqtt
import json
import ssl # Qubitro MQTT consumer setup
broker_host = "dealer.qubitro.com"
broker_port = 8883
device_id = "" # Add your machine ID right here
device_token = "" # Add your machine token right here # Callback features for MQTT
def on_connect(consumer, userdata, flags, rc):
    if rc == 0:
        print("Related to Qubitro!")
        consumer.on_subscribe = on_subscribe
        consumer.on_message = on_message
    else:
        print("Failed to attach, go to: https://docs.qubitro.com/platform/mqtt/examples return code:", rc) def on_subscribe(mqttc, obj, mid, granted_qos):
    print("Subscribed, " + "qos: " + str(granted_qos)) def on_message(consumer, userdata, message):
    print("Acquired message =", str(message.payload.decode("utf-8"))) def on_publish(consumer, obj, publish):
    print("Printed: " + str(publish)) # Initialize the MQTT consumer
mqtt_client = mqtt.Consumer(client_id=device_id)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
mqtt_client.tls_set_context(context)
mqtt_client.username_pw_set(username=device_id, password=device_token)
mqtt_client.join(broker_host, broker_port, 60)
mqtt_client.on_connect = on_connect
mqtt_client.on_publish = on_publish
mqtt_client.loop_start() # Inference consumer setup
CLIENT = InferenceHTTPClient(
    api_url="https://detect.roboflow.com",
    api_key="ROBOFLOW_API_KEY"  # Add your API key right here
) # Seize a picture from the webcam
def capture_image_from_webcam():
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("Error: Couldn't open webcam.")
        return None     ret, body = cap.learn()
    if ret:
        image_path = f"webcam_image_{datetime.now().strftime('%Ypercentmpercentd_percentHpercentMpercentS')}.jpg"
        cv2.imwrite(image_path, body)
        print(f"Picture saved to {image_path}")
    else:
        print("Error: Couldn't learn body.")
        image_path = None
    
    cap.launch()
    return image_path # Carry out inference on the captured picture
def perform_inference(image_path):
    if not image_path:
        print("No picture to course of.")
        return None     consequence = CLIENT.infer(image_path, model_id="water-meter-monitoring/1")
    predictions = consequence['predictions']
    sorted_predictions = sorted(predictions, key=lambda p: p['x'])
    digit_sequence = ''.be part of([pred['class'] for pred in sorted_predictions])
    print("Detected digit sequence:", digit_sequence)
    return digit_sequence # Job to seize picture, carry out inference, and ship knowledge to Qubitro
def daily_task():
    print("Operating each day process...")
    image_path = capture_image_from_webcam()
    digit_sequence = perform_inference(image_path)
    
    if digit_sequence:
        payload = {
            "reading-digits": digit_sequence,
        }
        mqtt_client.publish(device_id, payload=json.dumps(payload)) # Schedule the each day process at 9:00 AM
schedule.each().day.at("11:47").do(daily_task) # Maintain the script operating to examine for scheduled duties
whereas True:
    schedule.run_pending()
    time.sleep(60) 

The code captures photos at specified intervals utilizing the schedule library and performs predictions on these photos. The capture_image_from_webcam perform captures the picture, and perform_inference processes it for digit detection. The script runs repeatedly, checking for scheduled duties to execute. 

We’re additionally sending our prediction outcomes, within the daily_task() perform, through MQTT to Quibtro IoT & Information Administration platform. For this the machine id and token must be specified within the following strains.

device_id = ""  # Add your machine ID right here
device_token = ""  # Add your machine token right here

We’ll learn to get hold of this within the subsequent step.

Step #3: Setup Qubitro IoT server to obtain and visualize knowledge

We’ll now arrange Qubitro to obtain knowledge and visualize it. Qubitro is an Web of Issues (IoT) platform that permits customers to attach, handle, and analyze IoT gadgets and knowledge. It supplies instruments for machine administration, knowledge visualization, and real-time monitoring, making it simpler to deploy and scale IoT options. The platform helps varied communication protocols, together with MQTT, for safe and environment friendly knowledge switch.

First, signup and login to your Qubitro account. Subsequent, Click on on “New Undertaking” to create a brand new mission. Specify the title and outline of your mission.

A screenshot of a computer Description automatically generated
Create mission dialog field

As soon as the mission is created, add a “New Supply” within the mission, an MQTT protocol based mostly finish level in our instance.

Including an MQTT machine

Subsequent, specify the main points to your machine, i.e. title and the kind of machine. 

Creating an MQTT machine

As soon as the machine is created, you could find the machine id and token within the “Settings” tab of your machine (as proven in following determine). Use this info within the code from step 2.

Subsequent, create a dashboard to your machine the place the information can be seen. Go to the dashboard tab of your machine and click on the hyperlink “Go to dashboard”.

Subsequent, specify the main points (title and tags) to your dashboard:

Creating dashboard

Subsequent, we have to widgets to the dashboard. This widget will present the information despatched by the machine operating the code from step 2. Click on on “New Widget”.

Including a brand new widget

Choose the widget sort as “Stat” and click on “Add level”.

Widget configuration

Within the “Add level” dialog field, choose your mission, machine, and worth variable. If the worth dropdown is empty, run the pattern code supplied by Qubitro, utilizing “reading-digits” as the important thing within the payload. This may populate the worth within the “join knowledge level” dialog. You could find the pattern “code snippet” below the “Overview” tab of your machine.

Information level configuration

Lastly, whenever you run code (given within the step 2), you will note the final worth from the inference script within the Quibtro dashboard as proven within the following determine.

A white background with black text Description automatically generated
Dashboard displaying final worth acquired

Conclusion

On this mission, we automated the  water meter studying utilizing laptop imaginative and prescient. We educated a pc imaginative and prescient mannequin utilizing Roboflow for digit detection. The detected readings have been then securely transmitted to Qubitro IoT platform for distant monitoring and evaluation. This strategy showcases the combination of Roboflow’s highly effective laptop imaginative and prescient capabilities with IoT options, highlighting the helpful utility of Laptop Imaginative and prescient in sensible, real-world situations.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.