Boa tarde
Estou fazendo um projeto de python com arduino que a ideia é ler uma placa e identicar se ela foi cadastrada ou não no sistema para ativar o servo motor do arduino, porem estou com problemas para enquadrar area da placa. Alguem consegue me ajudar?
from PIL import Image
import numpy as np
import tkinter
import pytesseract
import cv2
import pandas as pd
from pyfirmata import Arduino,util
import time
def findPlace(contornos, imagem):
for c in contornos:
perimetro = cv2.arcLength(c, True)
if perimetro > 200 and perimetro < 600:
approx = cv2.approxPolyDP(c, 0.03 * perimetro, True)
if len(approx) == 4:
(x, y, lar, alt) = cv2.boundingRect(c)
cv2.rectangle(imagem, (x, y), (x + lar, y + alt), (255, 255, 255), 12)
roi = imagem[(y+10): y+alt, x: x+lar]
cv2.line(img, (x, y+alt), (x+lar, y+alt), (255, 255, 255), 200)
cv2.line(img, (x+lar, y), (x+lar, y+alt), (255, 255, 255), 200)
cv2.line(img, (x, y), (x, y+alt), (255, 255, 255), 200)
cv2.imwrite("imagem//roix.jpg", roi)
return imagem
def reconhecimentoOCR(path_img):
entrada = cv2.imread(path_img + ".jpg")
img = cv2.resize(entrada, None, fx=4, fy=4,
interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, img = cv2.threshold(img,30,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow("Limiar", img)
img = cv2.GaussianBlur(img, (5, 5), 0)
cv2.imshow("Desfoque", img)
cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.imwrite(path_img + '-ocr.jpg', img)
imagem = Image.open(path_img + '-ocr.jpg')
caracs = pytesseract.image_to_string(imagem, lang='mdt2')
caracs = caracs.replace(' ', '')
caracs = caracs.replace("-", "")
letras = caracs[:3]
num = caracs[3:]
num = num.replace("-", "")
letras = letras.replace("-", "")
num = num.replace('O', "0")
num = num.replace('Q', "0")
letras = letras.replace('0', "O")
num = num.replace('I', "1")
letras = letras.replace('1', "I")
num = num.replace('G', "6")
letras = letras.replace('6', "G")
num = num.replace('B', "8")
letras = letras.replace('8', "B")
num = num.replace('T', "1")
letras = letras.replace('1', "T")
num = num.replace('Z', "2")
letras = letras.replace('2', "Z")
num = num.replace('H', "11")
letras = letras.replace('11', "H")
num = num.replace('S', "5")
letras = letras.replace('5', "S")
placa_escrita = letras + '-' + num
plate_= placa_escrita[:8]
print (placa_escrita[:8])
if len(caracs) > 0:
print(caracs)
else:
texto = "Reconhecimento Falho"
return plate_
##############################################################
video = cv2.VideoCapture(0)
while(video.isOpened()):
ret, frame = video.read()
if(ret == False):
break
area = frame[220:370 , 200:440]
img_result = cv2.cvtColor(area, cv2.COLOR_BGR2GRAY)
ret, img_result = cv2.threshold(img_result, 90, 255, cv2.THRESH_BINARY)
img_result = cv2.GaussianBlur(img_result, (5, 5), 0)
img, contornos, hier = cv2.findContours(img_result, cv2.RETR_TREE,
cv2.CHAIN_APPROX_NONE)
cv2.imshow('FRAME', frame)
img3 = findPlace(contornos, area)
cv2.imshow('RES', area)
if cv2.waitKey(1) & 0xff == ord('q'):
break
video.release()
teste = reconhecimentoOCR('C://Users//Ana//Desktop//Test TCC//imagem//roix')
board = Arduino('COM3')
porteira = board.get_pin('d:9:s')
excelFile = 'placas.xlsx'
placasDB = pd.read_excel(excelFile)
print(placasDB)
placasDB['Placa']
value = teste
if value in placasDB['Placa'].values:
print ('Veiculo identificado:n')
print (placasDB[placasDB['Placa']==value])
porteira.write(90)
time.sleep(10)
porteira.write(0)
else:
print('Veiculo não identificada!')
cv2.destroyAllWindows()
Já tentei mudar os valores de referencia mas não funcionou tambem
New contributor
Ana Julia Maciel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.