TIF_E41200296/main.py

499 lines
20 KiB
Python

import cv2
import torch
from super_gradients.training import models
import math
from sort import *
import skfuzzy as fuzz
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import *
import sys
class MyWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 1080, 720)
self.setWindowTitle("Image Changer")
# self.label = QtWidgets.QPushButton()
# self.label.setText("Test")
# layout = QtWidgets.QVBoxLayout()
# layout.addWidget(self.label)
# self.setLayout(layout)
self.UIComponents()
# def changeText(self, text):
# self.label.setText(text)
def UIComponents(self):
pixmap = QPixmap("image/traffict.png")
smaller_pixmap = pixmap.scaled(1080, 720)
self.bg = QtWidgets.QLabel(self)
self.bg.setPixmap(smaller_pixmap)
# self.bg.setGeometry(0, 0, 2560, 1440)
p2 = QPixmap("image/1.png")
p2 = p2.scaled(200, 80)
self.t2 = QtWidgets.QLabel(self)
self.t2.setGeometry(200, 520, 200, 200)
self.t2.setPixmap(p2)
p1 = QPixmap("image/1.png")
p1 = p1.scaled(200, 80)
self.t1 = QtWidgets.QLabel(self)
self.t1.setGeometry(600, 520, 200, 200)
self.t1.setPixmap(p1)
p3 = QPixmap("image/1.png")
p3 = p3.scaled(200, 80)
self.t3 = QtWidgets.QLabel(self)
self.t3.setGeometry(600, 260, 200, 200)
self.t3.setPixmap(p3)
self.label3 = QtWidgets.QLabel(self)
self.label3.setGeometry(610, 260, 80, 50)
self.label3.setStyleSheet("background-color: green")
self.label3.setFont(QFont('Times', 18))
self.label3.setAlignment(Qt.AlignCenter)
self.label1 = QtWidgets.QLabel(self)
self.label1.setGeometry(610, 665, 80, 50)
self.label1.setStyleSheet("background-color: green")
self.label1.setFont(QFont('Times', 18))
self.label1.setAlignment(Qt.AlignCenter)
self.label2 = QtWidgets.QLabel(self)
self.label2.setGeometry(310, 665, 80, 50)
self.label2.setStyleSheet("background-color: green")
self.label2.setFont(QFont('Times', 18))
self.label2.setAlignment(Qt.AlignCenter)
self.labelMotor = QtWidgets.QLabel(self)
self.labelMotor.setText(" Jumlah Motor : 0")
self.labelMotor.setStyleSheet("background-color:black")
self.labelMotor.setGeometry(20, 20, 120, 40)
self.labelMobil = QtWidgets.QLabel(self)
self.labelMobil.setText(" Jumlah Mobil : 0")
self.labelMobil.setStyleSheet("background-color:black")
self.labelMobil.setGeometry(20, 70, 120, 40)
self.labelSimpang1 = QtWidgets.QLabel(self)
self.labelSimpang1.setText(" Simpang 1")
self.labelSimpang1.setStyleSheet("background-color:black")
self.labelSimpang1.setGeometry(610, 558, 75, 20)
self.labelSimpang2 = QtWidgets.QLabel(self)
self.labelSimpang2.setText(" Simpang 2")
self.labelSimpang2.setStyleSheet("background-color:black")
self.labelSimpang2.setGeometry(319, 558, 75, 20)
self.labelSimpang3 = QtWidgets.QLabel(self)
self.labelSimpang3.setText(" Simpang 3")
self.labelSimpang3.setStyleSheet("background-color:black")
self.labelSimpang3.setGeometry(610, 400, 75, 20)
self.label1.hide()
self.label2.hide()
self.label3.hide()
def textMotor(self, count):
text = " Jumlah Motor : " + str(count)
self.labelMotor.setText(text)
def textMobil(self, count):
text = " Jumlah Mobil : " + str(count)
self.labelMobil.setText(text)
def textCount1(self, string):
self.label1.setText(string)
def textCount2(self, string):
self.label2.setText(string)
def textCount3(self, string):
self.label3.setText(string)
def trafficGreen1(self):
pixmap = QPixmap("image/3.png")
pixmap = pixmap.scaled(200,80)
self.t1.setPixmap(pixmap)
def trafficRed1(self):
pixmap = QPixmap("image/1.png")
pixmap = pixmap.scaled(200,80)
self.t1.setPixmap(pixmap)
def trafficYellow1(self):
pixmap = QPixmap("image/2.png")
pixmap = pixmap.scaled(200,80)
self.t1.setPixmap(pixmap)
def trafficGreen2(self):
pixmap = QPixmap("image/3.png")
pixmap = pixmap.scaled(200,80)
self.t2.setPixmap(pixmap)
def trafficRed2(self):
pixmap = QPixmap("image/1.png")
pixmap = pixmap.scaled(200,80)
self.t2.setPixmap(pixmap)
def trafficYellow2(self):
pixmap = QPixmap("image/2.png")
pixmap = pixmap.scaled(200,80)
self.t2.setPixmap(pixmap)
def trafficGreen3(self):
pixmap = QPixmap("image/3.png")
pixmap = pixmap.scaled(200, 80)
self.t3.setPixmap(pixmap)
def trafficRed3(self):
pixmap = QPixmap("image/1.png")
pixmap = pixmap.scaled(200, 80)
self.t3.setPixmap(pixmap)
def trafficYellow3(self):
pixmap = QPixmap("image/2.png")
pixmap = pixmap.scaled(200, 80)
self.t3.setPixmap(pixmap)
def window():
app = QApplication(sys.argv)
win = MyWindow()
win.show()
traffictRun(win)
sys.exit(app.exec_())
device = torch.device("cpu")
model = models.get('yolo_nas_l', pretrained_weights="coco").to(device)
cap = cv2.VideoCapture("Video_Skripsi/simpang 1.mp4")
cap2 = cv2.VideoCapture("Video_Skripsi/simpang 4.mp4")
cap3 = cv2.VideoCapture("Video_Skripsi/simpang 3.mp4")
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
"fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
"diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
"teddy bear", "hair drier", "toothbrush"
]
def countTimeTraffict(motor, mobil):
x_motor = np.arange(0, 100, 1)
x_mobil = np.arange(0, 30, 1)
x_traffict = np.arange(0, 120, 1)
motor_sedikit = fuzz.trapmf(x_motor, [0, 0, 20, 38])
motor_sedang = fuzz.trapmf(x_motor, [20, 38, 63, 80])
motor_banyak = fuzz.trapmf(x_motor, [63, 80, 100, 100])
mobil_sedikit = fuzz.trapmf(x_mobil, [0, 0, 4, 8])
mobil_sedang = fuzz.trapmf(x_mobil, [4, 8, 12, 16])
mobil_banyak = fuzz.trapmf(x_mobil, [12, 16, 30, 30])
traffict_cepat = fuzz.trapmf(x_traffict, [0, 0, 20, 40])
traffict_sedang = fuzz.trimf(x_traffict, [20, 50, 70])
traffict_lambat = fuzz.trimf(x_traffict, [50, 70, 100])
traffict_sangat_lambat = fuzz.trapmf(x_traffict, [80, 100, 120, 120])
mt_sedikit = fuzz.interp_membership(x_motor, motor_sedikit, motor)
mt_sedang = fuzz.interp_membership(x_motor, motor_sedang, motor)
mt_banyak = fuzz.interp_membership(x_motor, motor_banyak, motor)
mb_sedikit = fuzz.interp_membership(x_mobil, mobil_sedikit, mobil)
mb_sedang = fuzz.interp_membership(x_mobil, mobil_sedang, mobil)
mb_banyak = fuzz.interp_membership(x_mobil, mobil_banyak, mobil)
drjt_motor = mt_sedikit, mt_sedang, mt_banyak
drjt_mobil = mb_sedikit, mb_sedang, mb_banyak
# Rule 1 sedikit, sedikit = cepat
rl_1 = np.fmin(drjt_motor[0], drjt_mobil[0])
# Rule 2 sedikit, sedang = sedang
rl_2 = np.fmin(drjt_motor[0], drjt_mobil[1])
# Rule 3 sedikit, banyak = lambat
rl_3 = np.fmin(drjt_motor[0], drjt_motor[2])
# Rule 4 sedang, sedikit = sedang,
rl_4 = np.fmin(drjt_motor[1], drjt_mobil[0])
# Rule 5 sedang, sedang = sedang
rl_5 = np.fmin(drjt_motor[1], drjt_mobil[1])
# Rule 6 sedang, banyak = lambat
rl_6 = np.fmin(drjt_motor[1], drjt_mobil[2])
# Rule 7 banyak, sedikit = lambat
rl_7 = np.fmin(drjt_motor[2], drjt_mobil[0])
# Rule 8 banyak, sedang = lambat
rl_8 = np.fmin(drjt_motor[2], drjt_motor[1])
# Rule 9 banyak, banyak =sangat lambat
rl_9 = np.fmin(drjt_motor[2], drjt_mobil[2])
pk_traffict_cepat = rl_1
pk_traffict_sedang = np.fmax(rl_2, np.fmax(rl_4, rl_5))
pk_traffict_lambat = np.fmax(rl_3, np.fmax(rl_6, np.fmax(rl_7, rl_8)))
pk_traffict_sangat_lambat = rl_9
pk_traffict_cepat = np.fmin(pk_traffict_cepat, traffict_cepat)
pk_traffict_sedang = np.fmin(pk_traffict_sedang, traffict_sedang)
pk_traffict_lambat = np.fmin(pk_traffict_lambat, traffict_lambat)
pk_traffict_sangat_lambat = np.fmin(pk_traffict_sangat_lambat, traffict_sangat_lambat)
komposisi = np.fmax(pk_traffict_cepat,
np.fmax(pk_traffict_sedang, np.fmax(pk_traffict_lambat, pk_traffict_sangat_lambat)))
traffictTime = fuzz.defuzz(x_traffict, komposisi, 'centroid')
return round(traffictTime)
def countVehiclesSimpang1(frame):
roi_y1 = 188
roi_x1 = 722
roi_y2 = 1076
roi_x2 = 1383
mobilcount = 0
motorcount = 0
frame = frame[roi_y1:roi_y2, roi_x1:roi_x2]
result = list(model.predict(frame, conf=0.35, iou=0.1, fuse_model=False))[0]
bbox_xyxys = result.prediction.bboxes_xyxy.tolist()
confidences = result.prediction.confidence
labels = result.prediction.labels.tolist()
for (bbox_xyxy, confidence, cls) in zip(bbox_xyxys, confidences, labels):
bbox = np.array(bbox_xyxy)
x1, y1, x2, y2 = bbox[0], bbox[1], bbox[2], bbox[3]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
classname = int(cls)
if classname == 2 or classname == 5 or classname == 6 or classname == 7:
mobilcount += 1
class_name = classNames[classname]
conf = math.ceil((confidence * 100)) / 100
label = f'{class_name} {conf}'
# print("Frame N", count, x1,y1,x2,y2)
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
c2 = x1 + t_size[0], y1 - t_size[1] - 3
cv2.rectangle(frame, (x1, y1), c2, (255, 0, 255), -1, cv2.LINE_AA)
cv2.putText(frame, label, (x1, y1 - 2), 0, 1, (255, 255, 255), thickness=1, lineType=cv2.LINE_AA)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
if classname == 1 or classname == 3:
motorcount += 1
class_name = classNames[classname]
conf = math.ceil((confidence * 100)) / 100
label = f'{class_name} {conf}'
# print("Frame N", count, x1,y1,x2,y2)
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
c2 = x1 + t_size[0], y1 - t_size[1] - 3
cv2.rectangle(frame, (x1, y1), c2, (255, 0, 255), -1, cv2.LINE_AA)
cv2.putText(frame, label, (x1, y1 - 2), 0, 1, (255, 255, 255), thickness=1, lineType=cv2.LINE_AA)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
resize_frame = cv2.resize(frame, (0,0), fx=0.4, fy=0.4)
cv2.imshow("Output", resize_frame)
return mobilcount, motorcount
def countVehiclesSimpang2(frame):
roi_y1 = 320
roi_x1 = 740
roi_y2 = 1076
roi_x2 = 1067
frame = frame[roi_y1:roi_y2, roi_x1:roi_x2]
motorcount = 0
mobilcount = 0
result = list(model.predict(frame, conf=0.35, iou=0.1, fuse_model=False))[0]
bbox_xyxys = result.prediction.bboxes_xyxy.tolist()
confidences = result.prediction.confidence
labels = result.prediction.labels.tolist()
for (bbox_xyxy, confidence, cls) in zip(bbox_xyxys, confidences, labels):
bbox = np.array(bbox_xyxy)
x1, y1, x2, y2 = bbox[0], bbox[1], bbox[2], bbox[3]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
classname = int(cls)
if classname == 2 or classname == 5 or classname == 6 or classname == 7:
mobilcount += 1
class_name = classNames[classname]
conf = math.ceil((confidence * 100)) / 100
label = f'{class_name} {conf}'
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
c2 = x1 + t_size[0], y1 - t_size[1] - 3
cv2.rectangle(frame, (x1, y1), c2, (255, 0, 255), -1, cv2.LINE_AA)
cv2.putText(frame, label, (x1, y1 - 2), 0, 1, (255, 255, 255), thickness=1, lineType=cv2.LINE_AA)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
if classname == 1 or classname == 3:
motorcount += 1
class_name = classNames[classname]
conf = math.ceil((confidence * 100)) / 100
label = f'{class_name} {conf}'
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
c2 = x1 + t_size[0], y1 - t_size[1] - 3
cv2.rectangle(frame, (x1, y1), c2, (255, 0, 255), -1, cv2.LINE_AA)
cv2.putText(frame, label, (x1, y1 - 2), 0, 1, (255, 255, 255), thickness=1, lineType=cv2.LINE_AA)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
resize_frame = cv2.resize(frame, (0, 0), fx=0.4, fy=0.4)
cv2.imshow("Output", resize_frame)
return mobilcount, motorcount
def countVehiclesSimpang3(frame):
roi_y1 = 262
roi_x1 = 900
roi_y2 = 1076
roi_x2 = 1380
frame = frame[roi_y1:roi_y2, roi_x1:roi_x2]
motorcount = 0
mobilcount = 0
result = list(model.predict(frame, conf=0.35, iou=0.1, fuse_model=False))[0]
bbox_xyxys = result.prediction.bboxes_xyxy.tolist()
confidences = result.prediction.confidence
labels = result.prediction.labels.tolist()
for (bbox_xyxy, confidence, cls) in zip(bbox_xyxys, confidences, labels):
bbox = np.array(bbox_xyxy)
x1, y1, x2, y2 = bbox[0], bbox[1], bbox[2], bbox[3]
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
classname = int(cls)
if classname == 2 or classname == 5 or classname == 6 or classname == 7:
mobilcount += 1
class_name = classNames[classname]
conf = math.ceil((confidence * 100)) / 100
label = f'{class_name} {conf}'
# print("Frame N", count, x1,y1,x2,y2)
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
c2 = x1 + t_size[0], y1 - t_size[1] - 3
cv2.rectangle(frame, (x1, y1), c2, (255, 0, 255), -1, cv2.LINE_AA)
cv2.putText(frame, label, (x1, y1 - 2), 0, 1, (255, 255, 255), thickness=1, lineType=cv2.LINE_AA)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
if classname == 1 or classname == 3:
motorcount += 1
class_name = classNames[classname]
conf = math.ceil((confidence * 100)) / 100
label = f'{class_name} {conf}'
# print("Frame N", count, x1,y1,x2,y2)
t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
c2 = x1 + t_size[0], y1 - t_size[1] - 3
cv2.rectangle(frame, (x1, y1), c2, (255, 0, 255), -1, cv2.LINE_AA)
cv2.putText(frame, label, (x1, y1 - 2), 0, 1, (255, 255, 255), thickness=1, lineType=cv2.LINE_AA)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
resize_frame = cv2.resize(frame, (0, 0), fx=0.4, fy=0.4)
cv2.imshow("Output", resize_frame)
return mobilcount, motorcount
def traffictRun(win):
count = 0
captureimagetimesimpang1 = 5
captureimagetimesimpang2 = 0
captureimagetimesimpang3 = 0
traffict_time1 = 0
traffict_time2 = 0
traffict_time3 = 0
countDown1 = 0
countDown2 = 0
countDown3 = 0
while cap.isOpened() or cap2.isOpened() or cap3.isOpened():
ret1, frame1 = cap.read()
ret2, frame2 = cap2.read()
ret3, frame3 = cap3.read()
count += 1
if count % 24 == 0:
print(count / 24)
if ret1:
resize_frame = cv2.resize(frame1, (0, 0), fx = 0.2, fy = 0.2)
cv2.imshow("Video Simpang 1", resize_frame)
if count == captureimagetimesimpang1 * cap.get(cv2.CAP_PROP_FPS):
mobilcount, motorcount = countVehiclesSimpang1(frame1)
win.textMotor(motorcount)
win.textMobil(mobilcount)
print(f" Simpang 1 Jumlah Mobil : {mobilcount}")
print(f"Simpang 1 Jumlah Motor : {motorcount}")
traffict_time1 = countTimeTraffict(motorcount, mobilcount)
captureimagetimesimpang2 = captureimagetimesimpang1 + traffict_time1
countDown1 = 0
win.label1.show()
win.trafficRed2()
win.trafficRed3()
win.trafficGreen1()
print("Simpang 1 Hijau")
if count == (captureimagetimesimpang1 - 2) * cap2.get(cv2.CAP_PROP_FPS):
win.trafficYellow1()
win.trafficYellow3()
if count % 24 == 0:
countDown1 += 1
win.textCount1(str(traffict_time1 - countDown1))
if traffict_time1 - countDown1 == 0:
win.label1.hide()
if ret2:
resize_frame = cv2.resize(frame2, (0, 0), fx=0.2, fy=0.2)
cv2.imshow("Video Simpang 2", resize_frame)
if count == captureimagetimesimpang2 * cap2.get(cv2.CAP_PROP_FPS):
mobilcount, motorcount = countVehiclesSimpang2(frame2)
win.textMotor(motorcount)
win.textMobil(mobilcount)
print(f" Simpang 2 Jumlah Mobil : {mobilcount}")
print(f" Simpang 2 Jumlah Motor : {motorcount}")
traffict_time2 = countTimeTraffict(motorcount, mobilcount)
captureimagetimesimpang3 = captureimagetimesimpang2 + traffict_time2
countDown2 = 0
win.label2.show()
win.trafficRed1()
win.trafficRed3()
win.trafficGreen2()
print("Simpang 2 Hijau")
if count == (captureimagetimesimpang2 - 2) * cap2.get(cv2.CAP_PROP_FPS):
win.trafficYellow2()
win.trafficYellow1()
if count % 24 == 0:
countDown2 += 1
win.textCount2(str(traffict_time2 - countDown2))
if traffict_time2 - countDown2 == 0:
win.label2.hide()
if ret3:
resize_frame = cv2.resize(frame3, (0, 0), fx=0.2, fy=0.2)
cv2.imshow("Video Simpang 3", resize_frame)
if count == captureimagetimesimpang3 * cap3.get(cv2.CAP_PROP_FPS):
mobilcount, motorcount = countVehiclesSimpang3(frame3)
win.textMotor(motorcount)
win.textMobil(mobilcount)
print(f" Simpang 3 Jumlah Mobil : {mobilcount}")
print(f" Simpang 3 Jumlah Motor : {motorcount}")
traffict_time3 = countTimeTraffict(motorcount, mobilcount)
captureimagetimesimpang1 = captureimagetimesimpang3 + traffict_time3
countDown3 = 0
win.label3.show()
win.trafficRed2()
win.trafficRed1()
win.trafficGreen3()
print("Simpang 3 Hijau")
if count == (captureimagetimesimpang3 - 2) * cap2.get(cv2.CAP_PROP_FPS):
win.trafficYellow3()
win.trafficYellow2()
if count % 24 == 0:
countDown3 += 1
win.textCount3(str(traffict_time3 - countDown3))
if traffict_time3 - countDown3 == 0:
win.label3.hide()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.waitKey(1)
window()
cap.release()
cv2.destroyAllWindows()