当前位置:   article > 正文

yolov8+tensorrt+pyqt5+多线程+鼠标单机放大缩小多路视频监控系统+账户密码验证登录界面(持续开发完善中)_tensort+pyqt5

tensort+pyqt5

一.最近公司做多路检测项目,b站找了好多类似功能实现,源码都收费,好贵,无奈,自己搞

    1.废话不多说,先看demo吧

                

                  

                

 

        我这里算法用的是官方的yolov8s哈,各位大佬可以结合自己的算法转成engine再部署,另外我这里用的转化工具是参考github

             https://github.com/triple-Mu/YOLOv8-TensorRT 

        另外我的多线程中的推理部分也用的这个大神中间的部分代码,自己做了一些修改,特此鸣谢。

        另注:我这个还没写完哈,我只有一路是摄像头,其他都是视频文件模拟的,我的是显卡是3080,i5的12代cpu,后面还要加的功能很多,因为落地摄像头是rtsp流,可能还要加cuda硬编解码做优化。基本功能已完成,后面会持续优化而且代码也会有优化,现在只相当于草稿了,需要的留意更新吧。。。。。

v1.1.1

pyqt5 账户密码登录界面

  1. class loginUi(QMainWindow,login_ui):
  2. def __init__(self):
  3. QMainWindow.__init__(self)
  4. self.setupUi(self)
  5. self.pushButton.clicked.connect(self.checkLogin)
  6. self.pushButton_2.clicked.connect(self.exitSys)
  7. def exitSys(self):
  8. sys.exit()
  9. def checkLogin(self):
  10. self.name = self.lineEdit_2.text()
  11. self.passwd = self.lineEdit_3.text()
  12. self.getSql()
  13. if self.name == self.nameSave and self.passwdSave == self.passwd:
  14. self.myPlayer = mainUi()
  15. self.myPlayer.show()
  16. self.close()
  17. else:
  18. self.errorUi = errorLogin()
  19. self.errorUi.show()
  20. self.close()
  21. def getSql(self):
  22. with open("lo.txt",'r') as f:
  23. lines = f.readlines()
  24. self.nameSave = lines[0][:-1]
  25. self.passwdSave = lines[1]
  26. f.close()
  27. class errorLogin(QMainWindow,error_ui):
  28. def __init__(self):
  29. QMainWindow.__init__(self)
  30. self.setupUi(self)
  31. self.pushButton.clicked.connect(self.returnUi)
  32. def returnUi(self):
  33. self.log = loginUi()
  34. self.log.show()
  35. self.close()

v1.0.0

  1. class mainUi(QMainWindow,convert_ui):
  2. def __init__(self):
  3. QMainWindow.__init__(self)
  4. self.setupUi(self)
  5. self.pushButton.clicked.connect(self.openVideo)
  6. self.pushButton_2.clicked.connect(lambda :self.changeLabel(1))
  7. self.pushButton_3.clicked.connect(lambda: self.changeLabel(2))
  8. self.pushButton_4.clicked.connect(lambda: self.changeLabel(3))
  9. self.pushButton_5.clicked.connect(lambda: self.changeLabel(4))
  10. self.pushButton_6.clicked.connect(lambda: self.changeLabel(5))
  11. self.pushButton_7.clicked.connect(lambda: self.changeLabel(6))
  12. self.pushButton_8.clicked.connect(lambda: self.changeLabel(7))
  13. self.pushButton_9.clicked.connect(lambda: self.changeLabel(8))
  14. self.pushButton_10.clicked.connect(self.exitSys)
  15. self.countButton1 = 0
  16. self.countButton2 = 0
  17. self.countButton3 = 0
  18. self.countButton4 = 0
  19. self.countButton5 = 0
  20. self.countButton6 = 0
  21. self.countButton7 = 0
  22. self.countButton8 = 0
  23. def exitSys(self):
  24. sys.exit()
  25. def getArr(self,arr):
  26. img = QImage(arr, arr.shape[1], arr.shape[0],QImage.Format_BGR888)
  27. self.label.setPixmap(QPixmap(img))
  28. self.label.setScaledContents(True)
  29. def getArr2(self,arr):
  30. img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
  31. self.label_2.setPixmap(QPixmap(img))
  32. self.label_2.setScaledContents(True)
  33. def getArr3(self,arr):
  34. img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
  35. self.label_3.setPixmap(QPixmap(img))
  36. self.label_3.setScaledContents(True)
  37. def getArr4(self,arr):
  38. img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
  39. self.label_4.setPixmap(QPixmap(img))
  40. self.label_4.setScaledContents(True)
  41. def getArr5(self,arr):
  42. img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
  43. self.label_5.setPixmap(QPixmap(img))
  44. self.label_5.setScaledContents(True)
  45. def getArr6(self,arr):
  46. img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
  47. self.label_6.setPixmap(QPixmap(img))
  48. self.label_6.setScaledContents(True)
  49. def getArr7(self,arr):
  50. img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
  51. self.label_7.setPixmap(QPixmap(img))
  52. self.label_7.setScaledContents(True)
  53. def getArr8(self,arr):
  54. img = QImage(arr, arr.shape[1], arr.shape[0], QImage.Format_BGR888)
  55. self.label_8.setPixmap(QPixmap(img))
  56. self.label_8.setScaledContents(True)
  57. def openVideo(self):
  58. self.pushButton.setEnabled(False)
  59. self.pushButton.setText("请等待")
  60. self.imgGet = inferVideo1()
  61. self.imgGet.updateImg.connect(self.getArr)
  62. self.imgGet.start()
  63. self.imgGet2 = inferVideo2()
  64. self.imgGet2.updateImg1.connect(self.getArr2)
  65. self.imgGet2.start()
  66. self.imgGet3 = inferVideo3()
  67. self.imgGet3.updateImg2.connect(self.getArr3)
  68. self.imgGet3.start()
  69. self.imgGet4 = inferVideo4()
  70. self.imgGet4.updateImg3.connect(self.getArr4)
  71. self.imgGet4.start()
  72. self.imgGet5 = inferVideo5()
  73. self.imgGet5.updateImg4.connect(self.getArr5)
  74. self.imgGet5.start()
  75. self.imgGet6 = inferVideo6()
  76. self.imgGet6.updateImg5.connect(self.getArr6)
  77. self.imgGet6.start()
  78. self.imgGet7 = inferVideo7()
  79. self.imgGet7.updateImg6.connect(self.getArr7)
  80. self.imgGet7.start()
  81. self.imgGet8 = inferVideo8()
  82. self.imgGet8.updateImg7.connect(self.getArr8)
  83. self.imgGet8.start()
  84. self.pushButton.setStyleSheet("border-color : yellow")
  85. self.pushButton.setText("已播放")
  86. def returnPos(self):
  87. self.label.resize(501, 221)
  88. self.label.raise_()
  89. self.label_2.move(501, 0)
  90. self.label_2.resize(501, 221)
  91. self.label_2.raise_()
  92. self.label_3.move(0, 221)
  93. self.label_3.resize(501, 221)
  94. self.label_3.raise_()
  95. self.label_4.move(501, 221)
  96. self.label_4.resize(501, 221)
  97. self.label_4.raise_()
  98. self.label_5.move(0, 442)
  99. self.label_5.resize(501, 221)
  100. self.label_5.raise_()
  101. self.label_6.move(501, 442)
  102. self.label_6.resize(501, 221)
  103. self.label_6.raise_()
  104. self.label_7.move(0, 663)
  105. self.label_7.resize(501, 221)
  106. self.label_7.raise_()
  107. self.label_8.move(501, 663)
  108. self.label_8.resize(501, 221)
  109. self.label_8.raise_()
  110. def changeLabel(self,x):
  111. if x == 1:
  112. self.countButton1 += 1
  113. if self.countButton1 % 2 == 0:
  114. self.returnPos()
  115. else:
  116. self.label.resize(self.label.width() + 501, self.label.height() + 663)
  117. self.label.raise_()
  118. elif x == 2:
  119. self.countButton2 += 1
  120. if self.countButton2 % 2 == 0:
  121. self.returnPos()
  122. else:
  123. self.label_2.move(0, 0)
  124. self.label_2.resize(self.label_2.width() + 501, self.label_2.height() + 663)
  125. self.label_2.raise_()
  126. elif x == 3:
  127. self.countButton3 += 1
  128. if self.countButton3 % 2 == 0:
  129. self.returnPos()
  130. else:
  131. self.label_3.move(0, 0)
  132. self.label_3.resize(self.label_3.width() + 501, self.label_3.height() + 663)
  133. self.label_3.raise_()
  134. elif x == 4:
  135. self.countButton4 += 1
  136. if self.countButton4 % 2 == 0:
  137. self.returnPos()
  138. else:
  139. self.label_4.move(0, 0)
  140. self.label_4.resize(self.label_4.width() + 501, self.label_4.height() + 663)
  141. self.label_4.raise_()
  142. elif x == 5:
  143. self.countButton5 += 1
  144. if self.countButton5 % 2 == 0:
  145. self.returnPos()
  146. else:
  147. self.label_5.move(0, 0)
  148. self.label_5.resize(self.label_5.width() + 501, self.label_5.height() + 663)
  149. self.label_5.raise_()
  150. elif x == 6:
  151. self.countButton6 += 1
  152. if self.countButton6 % 2 == 0:
  153. self.returnPos()
  154. else:
  155. self.label_6.move(0, 0)
  156. self.label_6.resize(self.label_6.width() + 501, self.label_6.height() + 663)
  157. self.label_6.raise_()
  158. elif x == 7:
  159. self.countButton7 += 1
  160. if self.countButton7 % 2 == 0:
  161. self.returnPos()
  162. else:
  163. self.label_7.move(0, 0)
  164. self.label_7.resize(self.label_7.width() + 501, self.label_7.height() + 663)
  165. self.label_7.raise_()
  166. elif x == 8:
  167. self.countButton8 += 1
  168. if self.countButton8 % 2 == 0:
  169. self.returnPos()
  170. else:
  171. self.label_8.move(0, 0)
  172. self.label_8.resize(self.label_8.width() + 501, self.label_8.height() + 663)
  173. self.label_8.raise_()

   

  1. def det_postprocess(data: Tuple[Tensor, Tensor, Tensor, Tensor]):
  2. assert len(data) == 4
  3. num_dets, bboxes, scores, labels = data[0][0], data[1][0], data[2][
  4. 0], data[3][0]
  5. nums = num_dets.item()
  6. if nums == 0:
  7. return bboxes.new_zeros((0, 4)), scores.new_zeros((0, )), labels.new_zeros((0, ))
  8. bboxes = bboxes[:nums]
  9. scores = scores[:nums]
  10. labels = labels[:nums]
  11. return bboxes, scores, labels
  1. def capRun(Img,W,H,device,Engine):
  2. draw = Img.copy()
  3. bgr, ratio, dwdh = letterbox(Img, (W, H))
  4. tensor = blob(bgr, return_seg=False)
  5. dwdh = torch.asarray(dwdh * 2, dtype=torch.float32, device=device)
  6. tensor = torch.asarray(tensor, device=device)
  7. data = Engine(tensor)
  8. bboxes, scores, labels = det_postprocess(data)
  9. if bboxes.numel() == 0:
  10. # if no bounding box
  11. return draw
  12. bboxes -= dwdh
  13. bboxes /= ratio
  14. for (bbox, score, label) in zip(bboxes, scores, labels):
  15. bbox = bbox.round().int().tolist()
  16. cls_id = int(label)
  17. cls = CLASSES[cls_id]
  18. color = COLORS[cls]
  19. cv2.rectangle(draw, bbox[:2], bbox[2:], color, 2)
  20. cv2.putText(draw, f'{cls}:{score:.3f}', (bbox[0], bbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX,
  21. 0.75, [225, 255, 255], thickness=2)
  22. return draw
  1. class inferVideo1(QThread):
  2. updateImg = pyqtSignal(np.ndarray)
  3. def __init__(self):
  4. super(inferVideo1,self).__init__()
  5. self.device = torch.device("cuda:0")
  6. self.Engine = TRTModule("yolov8s.engine", self.device)
  7. self.H, self.W = self.Engine.inp_info[0].shape[-2:]
  8. self.Engine.set_desired(['num_dets', 'bboxes', 'scores', 'labels'])
  9. def run(self):
  10. cap = cv2.VideoCapture(0)
  11. if not cap.isOpened():
  12. print("Unable to open camera")
  13. else:
  14. while True:
  15. ret, Img = cap.read()
  16. if ret:
  17. draw = capRun(Img,self.W,self.H,self.device,self.Engine)
  18. self.updateImg.emit(draw)
  19. else:
  20. break
  21. cap.release()

上面是一个线程的代码哈,需要的自己复制粘贴8个就行。我的i5的12代 16g内存 完美运行。。。

需要源码的可以私信我哈。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/151042?site
推荐阅读
相关标签
  

闽ICP备14008679号