artbreguez commited on
Commit
d163b9f
verified
1 Parent(s): 3548cce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -33
app.py CHANGED
@@ -1,36 +1,69 @@
1
- import gradio as gr
2
- import os
3
  import torch
 
 
 
4
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
-
7
- #subprocess.run(["mv","content/custom_data.yaml","./yolov5/data"])
8
-
9
-
10
- def load_model():
11
- '''
12
- Loading hub model & setting the preferences for the model
13
- '''
14
- model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolo-v5.pt')
15
- model.conf = 0.38
16
- model.dnn=True
17
- model.agnostic=True
18
- return model
19
-
20
- model=load_model()
21
- #, force_reload=True
22
- def detect(inp):
23
- #g = (size / max(inp.size)) #gain
24
- #im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
25
- results = model(inp,size=640) # inference
26
- results.render() # updates results.imgs with boxes and labels
27
- return Image.fromarray(results.ims[0])
28
-
29
-
30
- inp = gr.Image(type="pil", label="Original Image")
31
- output = gr.Image(type="pil", label="Output Image")
32
-
33
-
34
- io=gr.Interface(fn=detect, inputs=inp, outputs=output, title='Party Symbol Detection',examples=['Content/4.jpg','Content/10.jpg','Content/18.jpg'],theme='peach')
35
- io.launch(debug=True,share=False)
36
-
 
 
 
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()