falba commited on
Commit
598a04f
·
verified ·
1 Parent(s): 46800ad

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
5
+ from qreader import QReader
6
+ import cv2
7
+ import json
8
+ import ast
9
+ from datetime import datetime
10
+
11
+
12
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-stage1")
13
+ model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-stage1")
14
+ qreader = QReader()
15
+
16
+
17
+ def yolo_and_trocr(image_input, save):
18
+ try:
19
+ # YOLO instanciated from the trained model
20
+ yolo = YOLO('/content/best.pt')
21
+
22
+ # Creating results
23
+ results = yolo(image_input, conf=0.5, iou=0.7)
24
+ res = results[0].plot()[:, :, [2,1,0]]
25
+ boxes = results[0].boxes.xyxy
26
+ image = Image.fromarray(res)
27
+ texts = []
28
+
29
+ # Texts and cropped images get saved in the lists.
30
+ for i in boxes:
31
+ img_cropped = image.crop(tuple(i.tolist()))
32
+ # TrOCR model is run to detect text in image
33
+ pixel_values = processor(img_cropped, return_tensors="pt").pixel_values
34
+ generated_ids = model.generate(pixel_values)
35
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
36
+ texts.append(generated_text)
37
+ text = texts[0]
38
+ text = f"{text[:5]}.{text[5:]}" # fix decimals
39
+
40
+
41
+ # Reading the QR code from the image
42
+ qr_code = cv2.cvtColor(cv2.imread(image_input), cv2.COLOR_BGR2RGB)
43
+ decoded_text = qreader.detect_and_decode(image=qr_code)
44
+ if len(decoded_text) == 0:
45
+ decoded_text = "No QR code detected"
46
+ else:
47
+ decoded_text = decoded_text[0]
48
+
49
+ # Saving the info in a dictionary
50
+ if save:
51
+ data_dict = ast.literal_eval(decoded_text)
52
+ with open(f"{data_dict['Address']}.json", "w") as file:
53
+ current_datetime = datetime.now()
54
+ timestamp = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
55
+ data_dict['Last_Reading'] = {f'{timestamp}': f'{text}'}
56
+ json.dump(data_dict, file, indent=4)
57
+
58
+
59
+ return image, text, decoded_text
60
+
61
+ except Exception as e:
62
+ return "", f"Your input is invalid: {str(e)}", f"Try Again: Make sure the meter and QR code are clearly captured"
63
+
64
+ app = gr.Interface(
65
+ fn=yolo_and_trocr,
66
+ inputs=[gr.File(label="Input: Water Meter Image"), gr.Checkbox(label="Save")],
67
+ outputs=[gr.Image(label='Output: Water Meter Photo'), gr.Textbox(label="Output: Water Meter Reading"), gr.Textbox(label="Output: QR Code Detection")],
68
+ title="Water Meter Reading with YOLO and OCR"
69
+ )
70
+
71
+ app.launch()