Spaces:
Runtime error
Runtime error
File size: 2,766 Bytes
400f36b c751ec5 eefab63 c328d49 c751ec5 400f36b f37a2e7 400f36b 1db6c06 53efe8a 400f36b 80e52d7 c751ec5 c328d49 cf419b3 c328d49 cf419b3 c328d49 a6be14a cc95f94 c751ec5 cc95f94 c751ec5 f14aab1 c751ec5 cf419b3 75017d0 10870c1 c751ec5 cf419b3 53efe8a c751ec5 cf419b3 c328d49 8985a80 175b437 af793ae 256c5df af793ae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import easyocr as ocr #OCR
import streamlit as st #Web App
import numpy as np #Image Processing
import torch
import cv2
import pandas as pd
import os
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont, ImageOps
#title
st.title("Lector de Matrículas 🚘")
#subtitle
st.markdown("## Usando `YoloV5` y `EasyOCR`")
#image uploader
image = st.file_uploader(label="",type=['png','jpg','jpeg'])
def get_optimal_font_scale(text, width):
for scale in reversed(range(0, 60, 1)):
textSize = cv2.getTextSize(text, fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=scale/10, thickness=1)
new_width = textSize[0][0]
if (new_width <= width):
return scale/10
return 1
@st.cache
def load_models():
reader = ocr.Reader(['es'],model_storage_directory='.')
plate_detector = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
return reader, plate_detector
def detect(img, reader, plate_detector):
p = 0.03
model = plate_detector
results = model(img)
coor = results.pandas().xyxy[0]
coor = coor.astype({'xmin':'int', 'ymin':'int', 'xmax': 'int', 'ymax': 'int'})
if len(coor) == 0:
img = Image.fromarray(img)
I = ImageDraw.Draw(img)
myFont = ImageFont.truetype('MESPREG.ttf', 200)
I.text((100, 10), "No se detecta matricula", fill=(242, 190, 0), font=myFont)
img = img[:, :, ::-1]
return img, "No se detecta matricula"
crop = img[coor["ymin"][0]-int(img.shape[1]*p):coor["ymax"][0]+int(img.shape[1]*p), coor["xmin"][0]-int(img.shape[0]*p):coor["xmax"][0]+int(img.shape[0]*p)]
#Read text
result = reader.readtext(crop, allowlist="BCDFGHJKLMNPQRSTVWXYZ1234567890")
matricula = ""
for detection in result:
if detection[2] > 0.5:
matricula = matricula + ' ' + detection[1]
plate = matricula.split()
plate.sort(key=len, reverse = True)
plate = ''.join(plate)[:7].upper()
plate = plate[0:4].replace("L","4") + plate[4:]
img = Image.fromarray(img)
I = ImageDraw.Draw(img)
myFont = ImageFont.truetype('MESPREG.ttf', 300)
I.text((100, 10), plate, fill=(242, 190, 0), font=myFont)
img = np.array(img)
img = img[:, :, ::-1]
return img, plate
reader, plate_detector = load_models() #load model
if image is not None:
input_image = Image.open(image) #read image
input_image = ImageOps.exif_transpose(input_image)
input_image = np.array(input_image)
input_image = input_image[:, :, ::-1].copy()
with st.spinner("🤖 ¡Trabajando! "):
result, matricula = detect(input_image, reader, plate_detector)
st.write("Matrícula:",matricula)
st.image(result)
else:
st.write("Sube una imagen")
|