Skip to content

Instantly share code, notes, and snippets.

@Aldhanekaa
Created February 23, 2023 03:19
Show Gist options
  • Save Aldhanekaa/04f46f0eeeae478a38e6349c85ddcc80 to your computer and use it in GitHub Desktop.
Save Aldhanekaa/04f46f0eeeae478a38e6349c85ddcc80 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import time
import torch
model = torch.hub.load('ultralytics/yolov5', 'custom', 'enson.engine',force_reload=True)
model1 = torch.hub.load('ultralytics/yolov5', 'custom', 'char.engine',force_reload=True)
cap = cv2.VideoCapture(0)
prev_frame_time = 0
new_frame_time = 0
while(cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
gray = frame
gray = cv2.resize(gray, (500, 300))
results = model(gray)
for _, det in enumerate(results.xyxy[0]):
# convert from tensor to numpy
box = det.detach().cpu().numpy()[:5]
# convert from float to integer
box = [int(x) for x in box]
x1, y1, x2, y2,name = box # crop the license plate image part
cropped = gray[y1:y2, x1:x2].copy()
label = "plate"
color = (0, 255, 255)
# draw a box on original image
cv2.rectangle(gray, (x1, y1), (x2, y2), (0, 255, 255), 2)
t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
img2 = cropped.copy()
results1=model1(img2)
results1.pandas().xyxy[0].sort_values('xmin')
yeni = str(results1.pandas().xyxy[0].sort_values('xmin').name.values)
clean_text2 = ""
for char in yeni:
if char in "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ":
clean_text2 += char
yeni = clean_text2
yeni = yeni.replace(" ", "")
font = cv2.FONT_HERSHEY_SIMPLEX
new_frame_time = time.time()
fps = 1/(new_frame_time-prev_frame_time)
prev_frame_time = new_frame_time
fps = int(fps)
fps = str(fps)
cv2.putText(gray, fps, (7, 70), font, 3, (100, 255, 0), 3, cv2.LINE_AA)
cv2.imshow('frame', gray)
if (len(yeni)<9) and (len(yeni)>6):
print(yeni)
print(len(yeni))
else:
continue
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
@Aldhanekaa
Copy link
Author

insert this into Vision2023 Folder and activate virtual environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment