Upload New File

parent ea6d45b1
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QImage, QPixmap
import cv2
from tkinter import *
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import numpy as np
import time
import DetectChars
import DetectPlates
print(classification_report(y_test,preds_test))
SCALAR_BLACK = (0.0, 0.0, 0.0)
SCALAR_WHITE = (255.0, 255.0, 255.0)
SCALAR_YELLOW = (0.0, 255.0, 255.0)
SCALAR_GREEN = (0.0, 255.0, 0.0)
SCALAR_RED = (0.0, 0.0, 255.0)
showSteps = False
class Ui_MainWindow(object):
global video_path,save_image,classes,g_frame
video_path=""
g_frame=""
classes = ["Car","Van","Number Plate","Bicycle","MotorCycles","Lorry","HeavyLorry","Bus","Hand Tractors","Land Vehicle","JCB","Threewheel"]
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1025, 1029)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.centralwidget.setStyleSheet("background-color: #b3b3b3;")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 10, 981, 45))
font = QtGui.QFont()
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setAutoFillBackground(False)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget , clicked=lambda : self.start_video())
self.pushButton_3.setGeometry(QtCore.QRect(20, 880, 501, 81))
self.pushButton_3.setObjectName("pushButton_3")
self.toolButton = QtWidgets.QToolButton(self.centralwidget , clicked=lambda : self.chooseVideo())
self.toolButton.setGeometry(QtCore.QRect(20, 630, 160, 41))
self.toolButton.setObjectName("toolButton")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 700, 181, 41))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget , clicked=lambda : self.capture_image())
self.pushButton_2.setGeometry(QtCore.QRect(360, 630, 160, 41))
self.pushButton_2.setObjectName("pushButton_2")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(230, 700, 291, 41))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItems(classes)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 780, 191, 41))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_view = QtWidgets.QLabel(self.centralwidget, )
self.label_view.setFrameShape(QtWidgets.QFrame.WinPanel)
self.label_view.setGeometry(QtCore.QRect(20, 130, 480, 480))
self.label_view.setLineWidth(5)
self.label_view.setObjectName("graphicsView")
self.label_view_1 = QtWidgets.QLabel(self.centralwidget)
self.label_view_1.setGeometry(QtCore.QRect(520, 130, 480, 480))
self.label_view_1.setFrameShape(QtWidgets.QFrame.WinPanel)
self.label_view_1.setLineWidth(5)
self.label_view_1.setObjectName("graphicsView_2")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(230, 780, 291, 41))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget , clicked=lambda : self.capture_type())
self.pushButton.setGeometry(QtCore.QRect(190, 630, 160, 41))
self.pushButton.setObjectName("pushButton")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(20, 79, 471, 31))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setAlignment(QtCore.Qt.AlignCenter)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(520, 80, 481, 31))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setAlignment(QtCore.Qt.AlignCenter)
self.label_6.setObjectName("label_6")
self.label_view_2 = QtWidgets.QLabel(self.centralwidget)
self.label_view_2.setGeometry(QtCore.QRect(530, 630, 471, 331))
self.label_view_2.setFrameShape(QtWidgets.QFrame.WinPanel)
self.label_view_2.setLineWidth(5)
self.label_view_2.setText("")
self.label_view_2.setObjectName("label_view_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1025, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Vehicle Type & Number Plate Detection"))
self.label.setText(_translate("MainWindow", "Vehicle Type & Number Plate Detection"))
self.pushButton_3.setText(_translate("MainWindow", "Start"))
self.toolButton.setText(_translate("MainWindow", "Choose Video"))
self.label_2.setText(_translate("MainWindow", "Vehicle Type"))
self.pushButton_2.setText(_translate("MainWindow", "Number Plate Capture"))
self.label_3.setText(_translate("MainWindow", "Vehicle Number Plate"))
self.pushButton.setText(_translate("MainWindow", "Type Capture"))
self.label_5.setText(_translate("MainWindow", "Original Video"))
self.label_6.setText(_translate("MainWindow", "Detection Video"))
def chooseVideo(self):
global video_path
root = tk.Tk()
root.withdraw()
filetypes = (
('MP4 files', '*.mp4'),
('MOV files', '*.mov'),
('WMV files', '*.wmv'),
('AVI files', '*.avi'),
)
file_path = filedialog.askopenfilename(
filetypes=filetypes
)
if(file_path!=""):
video_path = file_path
def start_video(self):
root = tk.Tk()
root.withdraw()
if(video_path==""):
messagebox.showwarning("Required!", "Choose Video!")
else:
self.select_img(video_path)
def select_img(self,path):
t1=run_video(self,path)
t1.run()
def capture_image(self):
global save_image
global g_frame
root = tk.Tk()
root.withdraw()
if(g_frame==""):
messagebox.showwarning("Required!", "Start Video!")
else:
cv2.imwrite('test.jpg', g_frame)
blnKNNTrainingSuccessful = DetectChars.loadKNNDataAndTrainKNN()
if blnKNNTrainingSuccessful == False:
messagebox.showwarning("error: KNN traning was not successful!")
return False
imgOriginalScene = cv2.imread('test.jpg')
if imgOriginalScene is None:
messagebox.showwarning("error: image not read from file")
return False
listOfPossiblePlates = DetectPlates.detectPlatesInScene(imgOriginalScene)
listOfPossiblePlates = DetectChars.detectCharsInPlates(listOfPossiblePlates)
if len(listOfPossiblePlates) == 0:
messagebox.showwarning("no license plates were detected")
else:
listOfPossiblePlates.sort(key = lambda possiblePlate: len(possiblePlate.strChars), reverse = True)
licPlate = listOfPossiblePlates[0]
if len(licPlate.strChars) == 0:
print("no characters were detected")
return False
print("license plate read from image = " + licPlate.strChars)
self.lineEdit.setText(licPlate.strChars)
p2fRectPoints = cv2.boxPoints(licPlate.rrLocationOfPlateInScene)
image = cv2.rectangle(imgOriginalScene,(int(p2fRectPoints[0][0]),int(p2fRectPoints[0][1])),(int(p2fRectPoints[2][0]),int(p2fRectPoints[2][1])), (0,0,255), 2)
img = cv2.resize(image, (471,331))
frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = QImage(frame, frame.shape[1],frame.shape[0],frame.strides[0],QImage.Format_RGB888)
self.label_view_2.setPixmap(QtGui.QPixmap.fromImage(image))
cv2.waitKey(0)
def capture_type(self):
global g_frame
root = tk.Tk()
root.withdraw()
if g_frame.size == 0:
messagebox.showwarning("Required!", "Start Video!")
else:
net = cv2.dnn.readNetFromDarknet("yolov3_training.cfg",r"yolov3_training_10000.weights")
layer_names = net.getLayerNames()
outputlayers = net.getUnconnectedOutLayersNames()
colors=np.random.uniform(0,255,size=(len(classes),3))
font= cv2.FONT_HERSHEY_PLAIN
starting_time= time.time()
frame_id = 0
height, width, channels= g_frame.shape
#detecting objects
blob= cv2.dnn.blobFromImage (g_frame, 1/255,(320,320),(0,0,0),swapRB = True,crop= False) #reduce 416 to 320
net.setInput (blob)
outs = net.forward (outputlayers)
#print (outs[1])
#Showing info on screen/ get confidence score of algorithm in detecting an object in blob
class_ids=[]
confidences=[]
boxes= []
for out in outs:
for detection in out:
scores = detection [5:]
class_id= np.argmax (scores)
confidence = scores[class_id]
if confidence > 0.3:
center_x= int (detection [0] *width)
center_y=int (detection [1] *height)
w = int (detection [2] *width)
h = int (detection [3] *height)
x=int (center_x - w/2)
y=int (center_y - h/2)
boxes.append([x, y,w,h]) #put all rectangle areas
confidences.append (float (confidence)) #how confidence was that object detected and show that percentage
class_ids.append (class_id) #name of the object tha was detected
indexes = cv2.dnn. NMSBoxes (boxes, confidences, 0.4,0.6)
for i in range (len (boxes)):
if i in indexes:
x,y,w,h = boxes[i]
label = str(classes[class_ids[i]])
if("Number Plate"!=label):
self.comboBox.setCurrentText(label)
confidence = str(round(confidences[i],2))
color = colors[i]
cv2.rectangle(g_frame,(x,y),(x+w,y+h),color,2)
cv2.putText(g_frame,label + " " + confidence, (x,y+400),font,2,color,2)
elapsed_time = time.time () - starting_time
fps=frame_id/elapsed_time
cv2.putText (g_frame, "FPS: "+str (round (fps, 2)), (10,50), font, 2, (0,0,0),1)
img = cv2.resize(g_frame, (480,480))
frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = QImage(frame, frame.shape[1],frame.shape[0],frame.strides[0],QImage.Format_RGB888)
self.label_view_1.setPixmap(QtGui.QPixmap.fromImage(image))
cv2.waitKey (1) #wait 1ms the loop will start again and we will process the next frame
def run_video(self,path):
global g_frame
global save_image
cap = cv2.VideoCapture(path)
while(cap.isOpened()):
_, img = cap.read()
if img is None:
break
g_frame=img
img = cv2.resize(img, (480,480))
frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = QImage(frame, frame.shape[1],frame.shape[0],frame.strides[0],QImage.Format_RGB888)
save_image=image
self.label_view.setPixmap(QtGui.QPixmap.fromImage(image))
cv2.waitKey(10)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment