artbreguez commited on
Commit
f12f662
verified
1 Parent(s): 79ce677

Fazendo upload do app

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ import cv2
4
+ import re
5
+ from PIL import Image
6
+ import gradio as gr
7
+ import numpy as np
8
+
9
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolo-v5.pt')
10
+ model.conf = 0.80
11
+
12
+ processor = TrOCRProcessor.from_pretrained('microsoft/trocr-base-printed')
13
+ ocr = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-printed')
14
+
15
+ def extract_coordinates(img, model):
16
+ results = model(img)
17
+ cordinates = results.xyxy[0][:, :-1]
18
+ return cordinates
19
+
20
+ def read_plate_number(results, frame, cordinates):
21
+ plate_numbers = []
22
+ n = len(results)
23
+
24
+ for i in range(n):
25
+ row = cordinates[i]
26
+ if row[4] >= 0.5:
27
+ xmin, ymin, xmax, ymax = map(int, row[:4])
28
+ plate = frame[ymin:ymax, xmin:xmax]
29
+
30
+ pixel_values = processor(images=plate, return_tensors="pt").pixel_values
31
+ generated_ids = ocr.generate(pixel_values)
32
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
33
+
34
+ cleaned_text = clean_plate_number(generated_text)
35
+ plate_numbers.append(cleaned_text)
36
+
37
+ return plate_numbers
38
+
39
+ def clean_plate_number(text):
40
+ cleaned_text = re.sub(r'[^a-zA-Z0-9]', '', text)
41
+
42
+ if any(char.isalpha() for char in cleaned_text) and any(char.isdigit() for char in cleaned_text):
43
+ plate_number = cleaned_text[-7:]
44
+ return plate_number
45
+
46
+ return ""
47
+
48
+ def perform_ocr_on_image(image):
49
+ img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
50
+ results = model(img)
51
+ cordinates = extract_coordinates(img, model)
52
+
53
+ if len(cordinates) == 0:
54
+ return "Nenhuma placa encontrada."
55
+
56
+ plate_number = read_plate_number(results.pred[0], img, cordinates)
57
+
58
+ if plate_number:
59
+ return plate_number[0].lower()
60
+ else:
61
+ return "N茫o foi poss铆vel reconhecer a placa."
62
+
63
+ interface = gr.Interface(fn=perform_ocr_on_image,
64
+ inputs=gr.Image(type="pil"),
65
+ outputs="text",
66
+ title="Reconhecimento de Placas de Autom贸veis",
67
+ description="Envie uma imagem e receba o n煤mero da placa.")
68
+
69
+ interface.launch()