|
import cv2 |
|
from time import time |
|
from alpr import * |
|
import torch |
|
import cv2 |
|
import numpy as np |
|
import tensorflow.compat.v1 as tf |
|
import os |
|
import streamlit as st |
|
from PIL import Image |
|
import streamlit as st |
|
|
|
def load_image(image_file): |
|
img = Image.open(image_file) |
|
return img |
|
|
|
|
|
st.subheader("Image") |
|
image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
submit = st.button('Generate') |
|
|
|
if submit: |
|
image = load_image(image_file) |
|
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yoloocrv2_1.pt') |
|
model.cpu() |
|
model.conf = 0.5 |
|
license = DetectLicensePlate() |
|
counter = dict() |
|
frame = np.array(image)[...,::-1] |
|
try: |
|
plate_img = alpr(frame,license) |
|
results = model(plate_img*255) |
|
control = max(results.pandas().xyxy[0].sort_values('ymin').iloc[:,1].values) |
|
if control > 50: |
|
name = results.pandas().xyxy[0].sort_values('ymin') |
|
ind = [ix for ix,i in enumerate(name.iloc[:,1]) if i>50][0] |
|
upper_f_2 = name.iloc[:ind].sort_values("xmin").iloc[:,-1][:2] |
|
upper_sort = name.iloc[:ind].sort_values("xmin").iloc[:,-1][2:] |
|
bottom_sort = name.iloc[ind:].sort_values("xmin").iloc[:,-1] |
|
upper_name = "".join([i for i in upper_sort]) |
|
upper_f_name = "".join([i for i in upper_f_2]) |
|
bottom_name = "".join([i for i in bottom_sort]) |
|
if "1" in upper_name: |
|
upper_name= upper_name.replace("1","I") |
|
if "6" in upper_name: |
|
upper_name= upper_name.replace("6","G") |
|
if "0" in upper_name: |
|
upper_name= upper_name.replace("0","O") |
|
|
|
name = upper_f_name + upper_name + bottom_name |
|
if name not in counter and name != '': |
|
counter[name] = 1 |
|
if name in counter and name != '': |
|
counter[name] += 1 |
|
plate_name = list((sorted(counter.items(), key=lambda item: item[1])))[-1][0] |
|
st.write(plate_name) |
|
|
|
else: |
|
|
|
|
|
decoder = results.pandas().xyxy[0].sort_values('xmin').iloc[:,0].values |
|
compare = list(decoder[2:]) |
|
maks = None |
|
for i in range(len(compare)): |
|
if i == len(compare) - 1: |
|
break |
|
if maks == None: |
|
maks = abs(compare[i] - compare[i + 1]) |
|
w_index = (maks, i + 1) |
|
if abs(compare[i] - compare[i + 1]) > maks: |
|
maks = abs(compare[i] - compare[i + 1]) |
|
w_index = (maks, i + 1) |
|
|
|
name = results.pandas().xyxy[0].sort_values('xmin').iloc[:, -1] |
|
name = "".join([i for i in name]) |
|
if name not in counter and name != '': |
|
counter[name] = 1 |
|
if name in counter and name !='': |
|
counter[name] +=1 |
|
plate_name = list((sorted(counter.items(),key = lambda item:item[1])))[-1][0] |
|
|
|
mid_chars = str(plate_name[2:int(w_index[1] + 2)]) |
|
|
|
if "6" in mid_chars: |
|
mid_chars = mid_chars.replace("6", "G") |
|
if "1" in mid_chars: |
|
mid_chars = mid_chars.replace("1", "I") |
|
if "0" in mid_chars: |
|
mid_chars = mid_chars.replace("0", "O") |
|
|
|
new_plate_name = plate_name.replace(plate_name[2:int(w_index[1] + 2)], mid_chars) |
|
|
|
|
|
st.write(new_plate_name) |
|
|
|
|
|
except Exception as e: |
|
|
|
counter.clear() |
|
st.write("Plaka Bulunamadı") |
|
|
|
|