程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pytq5 and opencv acquire camera, save pictures and process pictures in Python environment

編輯:Python

Camera window locked , No more windows to save images , All the codes are as follows :

import sysimport cv2from PyQt5 import QtCore, QtGui,QtWidgetsimport osclass Ui_MainWindow(QtWidgets.QWidget): def __init__(self, parent=None): super(Ui_MainWindow, self).__init__(parent) self.timer_camera = QtCore.QTimer() self.cap = cv2.VideoCapture() self.CAM_NUM = 0 self.set_ui() self.slot_init() self.__flag_work = 0 self.x =0 self.count = 0 def set_ui(self): self.__layout_main = QtWidgets.QHBoxLayout() self.__layout_fun_button = QtWidgets.QVBoxLayout() self.__layout_data_show = QtWidgets.QVBoxLayout() # Button to add  self.button_analyse_image = QtWidgets.QPushButton(u' Start analyzing images ') self.button_save_image = QtWidgets.QPushButton(u' Save image ') self.button_manual_model = QtWidgets.QPushButton(u' Manual mode ') self.button_stop_analyse = QtWidgets.QPushButton(u' Stop analyzing ') self.button_auto_model = QtWidgets.QPushButton(u' Automatic analysis ') self.button_open_camera = QtWidgets.QPushButton(u' Turn on camera ') self.button_close = QtWidgets.QPushButton(u' sign out ') # Minimum height  self.button_analyse_image.setMinimumHeight(40) self.button_save_image.setMinimumHeight(40) self.button_manual_model.setMinimumHeight(40) self.button_stop_analyse.setMinimumHeight(40) self.button_auto_model.setMinimumHeight(40) self.button_open_camera.setMinimumHeight(40) self.button_close.setMinimumHeight(40) self.button_analyse_image.setMinimumHeight(40) self.button_close.move(100,1000) # Information display  self.label_show_camera = QtWidgets.QLabel() self.label_move = QtWidgets.QLabel() self.label_move.setFixedSize(250, 250) self.label_show_cameras = QtWidgets.QLabel() # Window size  self.label_show_camera.setFixedSize(480, 320) #setFixedSize () Method sets the fixed size of the window , Then set the layout of the controls in the window to vertical layout  self.label_show_camera.setAutoFillBackground(False) # Display button  self.__layout_fun_button.addWidget(self.button_analyse_image) self.__layout_fun_button.addWidget(self.button_save_image) self.__layout_fun_button.addWidget(self.button_manual_model) self.__layout_fun_button.addWidget(self.button_stop_analyse) self.__layout_fun_button.addWidget(self.button_auto_model) self.__layout_fun_button.addWidget(self.button_open_camera) self.__layout_fun_button.addWidget(self.button_close) self.__layout_fun_button.addWidget(self.label_move) self.__layout_main.addLayout(self.__layout_fun_button) self.__layout_main.addWidget(self.label_show_camera) self.setLayout(self.__layout_main) self.label_move.raise_() self.setWindowTitle(u' Detection window ') def slot_init(self): self.button_open_camera.clicked.connect(self.button_open_camera_click) self.timer_camera.timeout.connect(self.show_camera) self.button_close.clicked.connect(self.close) def button_open_camera_click(self): if self.timer_camera.isActive() == False: flag = self.cap.open(self.CAM_NUM) if flag == False: msg = QtWidgets.QMessageBox.warning(self, u"Warning", u" Please check whether the camera is connected to the computer correctly ", buttons=QtWidgets.QMessageBox.Ok, defaultButton=QtWidgets.QMessageBox.Ok) else: self.timer_camera.start(30) self.button_open_camera.setText(u' Turn off camera ') else: self.timer_camera.stop() self.cap.release() self.label_show_camera.clear() self.button_open_camera.setText(u' Turn on camera ') def show_camera(self): flag, self.image = self.cap.read() show = cv2.resize(self.image, (480, 360)) # Camera window size  show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) ui = Ui_MainWindow() ui.show() sys.exit(app.exec_())

I don't know how to change this part , No replaceable module found , To achieve the purpose of adjusting the position in the window .

 # Window size  self.label_show_camera.setFixedSize(480, 320) #setFixedSize () Method sets the fixed size of the window , Then set the layout of the controls in the window to vertical layout  self.label_show_camera.setAutoFillBackground(False)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved