1.废话不多说,先看demo吧
我这里算法用的是官方的yolov8s哈,各位大佬可以结合自己的算法转成engine再部署,另外我这里用的转化工具是参考github:
https://github.com/triple-Mu/YOLOv8-TensorRT
另外我的多线程中的推理部分也用的这个大神中间的部分代码,自己做了一些修改,特此鸣谢。
另注:我这个还没写完哈,我只有一路是摄像头,其他都是视频文件模拟的,我的是显卡是3080,i5的12代cpu,后面还要加的功能很多,因为落地摄像头是rtsp流,可能还要加cuda硬编解码做优化。基本功能已完成,后面会持续优化而且代码也会有优化,现在只相当于草稿了,需要的留意更新吧。。。。。
v1.1.1
pyqt5 账户密码登录界面
class loginUi(QMainWindow,login_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.checkLogin)
self.pushButton_2.clicked.connect(self.exitSys)
def exitSys(self):
sys.exit()
def checkLogin(self):
self.name = self.lineEdit_2.text()
self.passwd = self.lineEdit_3.text()
self.getSql()
if self.name == self.nameSave and self.passwdSave == self.passwd:
self.myPlayer = mainUi()
self.myPlayer.show()
self.close()
else:
self.errorUi = errorLogin()
self.errorUi.show()
self.close()
def getSql(self):
with open("lo.txt",'r') as f:
lines = f.readlines()
self.nameSave = lines[0][:-1]
self.passwdSave = lines[1]
f.close()
class errorLogin(QMainWindow,error_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.returnUi)
def returnUi(self):
self.log = loginUi()
self.log.show()
self.close()
v1.0.0
class mainUi(QMainWindow,convert_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.openVideo)
self.pushButton_2.clicked.connect(lambda :self.changeLabel(1))
self.pushButton_3.clicked.connect(lambda: self.changeLabel(2))
self.pushButton_4.clicked.connect(lambda: self.changeLabel(3))
self.pushButton_5.clicked.connect(lambda: self.changeLabel(4))
self.pushButton_6.clicked.connect(lambda: self.changeLabel(5))
self.pushButton_7.clicked.connect(lambda: self.changeLabel(6))
self.pushButton_8.clicked.connect(lambda: self.changeLabel(7))
self.pushButton_9.clicked.connect(lambda: self.changeLabel(8))
self.pushButton_10.clicked.connect(self.exitSys)
self.countButton1 = 0
self.countButton2 = 0
self.countButton3 = 0
self.countButton4 = 0
self.countButton5 = 0
self.countButton6 = 0
self.countButton7 = 0
self.countButton8 = 0
def exitSys(self):
sys.exit()
def getArr(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0],QImage.Format_BGR888)
self.label.setPixmap(QPixmap(img))
self.label.setScaledContents(True)
def getArr2(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
self.label_2.setPixmap(QPixmap(img))
self.label_2.setScaledContents(True)
def getArr3(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
self.label_3.setPixmap(QPixmap(img))
self.label_3.setScaledContents(True)
def getArr4(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
self.label_4.setPixmap(QPixmap(img))
self.label_4.setScaledContents(True)
def getArr5(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
self.label_5.setPixmap(QPixmap(img))
self.label_5.setScaledContents(True)
def getArr6(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
self.label_6.setPixmap(QPixmap(img))
self.label_6.setScaledContents(True)
def getArr7(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
self.label_7.setPixmap(QPixmap(img))
self.label_7.setScaledContents(True)
def getArr8(self,arr):
img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
self.label_8.setPixmap(QPixmap(img))
self.label_8.setScaledContents(True)
def openVideo(self):
self.pushButton.setEnabled(False)
self.pushButton.setText("请等待")
self.imgGet = inferVideo1()
self.imgGet.updateImg.connect(self.getArr)
self.imgGet.start()
self.imgGet2 = inferVideo2()
self.imgGet2.updateImg1.connect(self.getArr2)
self.imgGet2.start()
self.imgGet3 = inferVideo3()
self.imgGet3.updateImg2.connect(self.getArr3)
self.imgGet3.start()
self.imgGet4 = inferVideo4()
self.imgGet4.updateImg3.connect(self.getArr4)
self.imgGet4.start()
self.imgGet5 = inferVideo5()
self.imgGet5.updateImg4.connect(self.getArr5)
self.imgGet5.start()
self.imgGet6 = inferVideo6()
self.imgGet6.updateImg5.connect(self.getArr6)
self.imgGet6.start()
self.imgGet7 = inferVideo7()
self.imgGet7.updateImg6.connect(self.getArr7)
self.imgGet7.start()
self.imgGet8 = inferVideo8()
self.imgGet8.updateImg7.connect(self.getArr8)
self.imgGet8.start()
self.pushButton.setStyleSheet("border-color : yellow")
self.pushButton.setText("已播放")
def returnPos(self):
self.label.resize(501, 221)
self.label.raise_()
self.label_2.move(501, 0)
self.label_2.resize(501, 221)
self.label_2.raise_()
self.label_3.move(0, 221)
self.label_3.resize(501, 221)
self.label_3.raise_()
self.label_4.move(501, 221)
self.label_4.resize(501, 221)
self.label_4.raise_()
self.label_5.move(0, 442)
self.label_5.resize(501, 221)
self.label_5.raise_()
self.label_6.move(501, 442)
self.label_6.resize(501, 221)
self.label_6.raise_()
self.label_7.move(0, 663)
self.label_7.resize(501, 221)
self.label_7.raise_()
self.label_8.move(501, 663)
self.label_8.resize(501, 221)
self.label_8.raise_()
def changeLabel(self,x):
if x == 1:
self.countButton1 += 1
if self.countButton1 % 2 == 0:
self.returnPos()
else:
self.label.resize(self.label.width() + 501, self.label.height() + 663)
self.label.raise_()
elif x == 2:
self.countButton2 += 1
if self.countButton2 % 2 == 0:
self.returnPos()
else:
self.label_2.move(0, 0)
self.label_2.resize(self.label_2.width() + 501, self.label_2.height() + 663)
self.label_2.raise_()
elif x == 3:
self.countButton3 += 1
if self.countButton3 % 2 == 0:
self.returnPos()
else:
self.label_3.move(0, 0)
self.label_3.resize(self.label_3.width() + 501, self.label_3.height() + 663)
self.label_3.raise_()
elif x == 4:
self.countButton4 += 1
if self.countButton4 % 2 == 0:
self.returnPos()
else:
self.label_4.move(0, 0)
self.label_4.resize(self.label_4.width() + 501, self.label_4.height() + 663)
self.label_4.raise_()
elif x == 5:
self.countButton5 += 1
if self.countButton5 % 2 == 0:
self.returnPos()
else:
self.label_5.move(0, 0)
self.label_5.resize(self.label_5.width() + 501, self.label_5.height() + 663)
self.label_5.raise_()
elif x == 6:
self.countButton6 += 1
if self.countButton6 % 2 == 0:
self.returnPos()
else:
self.label_6.move(0, 0)
self.label_6.resize(self.label_6.width() + 501, self.label_6.height() + 663)
self.label_6.raise_()
elif x == 7:
self.countButton7 += 1
if self.countButton7 % 2 == 0:
self.returnPos()
else:
self.label_7.move(0, 0)
self.label_7.resize(self.label_7.width() + 501, self.label_7.height() + 663)
self.label_7.raise_()
elif x == 8:
self.countButton8 += 1
if self.countButton8 % 2 == 0:
self.returnPos()
else:
self.label_8.move(0, 0)
self.label_8.resize(self.label_8.width() + 501, self.label_8.height() + 663)
self.label_8.raise_()
def det_postprocess(data: Tuple[Tensor, Tensor, Tensor, Tensor]):
assert len(data) == 4
num_dets, bboxes, scores, labels = data[0][0], data[1][0], data[2][
0], data[3][0]
nums = num_dets.item()
if nums == 0:
return bboxes.new_zeros((0, 4)), scores.new_zeros((0, )), labels.new_zeros((0, ))
bboxes = bboxes[:nums]
scores = scores[:nums]
labels = labels[:nums]
return bboxes, scores, labels
def capRun(Img,W,H,device,Engine):
draw = Img.copy()
bgr, ratio, dwdh = letterbox(Img, (W, H))
tensor = blob(bgr, return_seg=False)
dwdh = torch.asarray(dwdh * 2, dtype=torch.float32, device=device)
tensor = torch.asarray(tensor, device=device)
data = Engine(tensor)
bboxes, scores, labels = det_postprocess(data)
if bboxes.numel() == 0:
# if no bounding box
return draw
bboxes -= dwdh
bboxes /= ratio
for (bbox, score, label) in zip(bboxes, scores, labels):
bbox = bbox.round().int().tolist()
cls_id = int(label)
cls = CLASSES[cls_id]
color = COLORS[cls]
cv2.rectangle(draw, bbox[:2], bbox[2:], color, 2)
cv2.putText(draw, f'{cls}:{score:.3f}', (bbox[0], bbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX,
0.75, [225, 255, 255], thickness=2)
return draw
class inferVideo1(QThread):
updateImg = pyqtSignal(np.ndarray)
def __init__(self):
super(inferVideo1,self).__init__()
self.device = torch.device("cuda:0")
self.Engine = TRTModule("yolov8s.engine", self.device)
self.H, self.W = self.Engine.inp_info[0].shape[-2:]
self.Engine.set_desired(['num_dets', 'bboxes', 'scores', 'labels'])
def run(self):
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Unable to open camera")
else:
while True:
ret, Img = cap.read()
if ret:
draw = capRun(Img,self.W,self.H,self.device,self.Engine)
self.updateImg.emit(draw)
else:
break
cap.release()
上面是一个线程的代码哈,需要的自己复制粘贴8个就行。我的i5的12代 16g内存 完美运行。。。
需要源码的可以私信我哈。