monitoring my gas counter with a webcam
During my certification I have learned about DocumentAI and I am eager to put it into practical use. So, here is my project: I will install a webcam to watch my gas counter and take a picture every day. Will use DocumentAI to find out its read and store it in a table.
Here is the prototype:
Webcam monitoring my gas counter |
OK, first we have the physical setup. I use an old Ubuntu laptop (will be a Raspberry Pi soon) with a very new and good webcam:
On Ubuntu, I issue the command to grab the picture from the webcam:
$ fswebcam -r 4000x3000 -d /dev/video0 test2.jpg
--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 4000x3000 to 2304x1296.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'test2.jpg'.
When I upload it to DocumentAI, it does recognize the counter's number, in this case 9153.486 m³
Now, this deserves to be automated for daily uploads and a statistic. First thing to do is
sudo apt install python3-pip
Then, follow https://cloud.google.com/document-ai/docs/process-documents-client-libraries. In the end, my python script docai.py looks like this:
from google.api_core.client_options import ClientOptions
from google.cloud import documentai
# TODO(developer): Uncomment these variables before running the sample.
project_id = 'XXXXXXXXX'
location = 'us' # Format is 'us' or 'eu'
processor_id = 'x1x1x1x1x1' # Create processor before running sample
file_path = '/home/thorsten/test.jpg'
mime_type = 'image/jpeg' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types
def quickstart(
project_id: str, location: str, processor_id: str, file_path: str, mime_type: str
):
# You must set the api_endpoint if you use a location other than 'us', e.g.:
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
client = documentai.DocumentProcessorServiceClient(client_options=opts)
# The full resource name of the processor, e.g.:
# projects/project_id/locations/location/processor/processor_id
name = client.processor_path(project_id, location, processor_id)
# Read the file into memory
with open(file_path, "rb") as image:
image_content = image.read()
# Load Binary Data into Document AI RawDocument Object
raw_document = documentai.RawDocument(content=image_content, mime_type=mime_type)
# Configure the process request
request = documentai.ProcessRequest(name=name, raw_document=raw_document)
result = client.process_document(request=request)
# For a full list of Document object attributes, please reference this page:
# https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.Document
document = result.document
# Read the text recognition output from the processor
print("The document contains the following text:")
print(document.text)
quickstart(project_id,location,processor_id,file_path,mime_type)
as you can see, it expects a file /home/thorsten/test.jpg and OCRs it.
If I run it, it outputs all text it can read from the image:
CE
TRENAMIEN
koom
schroder
mi
TUN
BK-G4
C4 2002
(
SEA
18777
o SWH-AG
U9165012 m³
Elster Handel GmbH Mainz
way
Bel GASGERUCH:
Durchschn
Tindavon unten maches
stadtwerke
heldelberg.
SUNITS WAS A
KAZANHADINE
Manchma
2022-10-05 20:01 (CEST)
Using the grep utility, I can limit the output to all lines that contain m³:
$ python3 docai.py | grep m³
U9165012 m³
Comments
Post a Comment