本篇文章通过使用python实现对计算机摄像头的调用从而实现摄像监控的功能。
利用opencv的图像处理功能可以轻松对计算机摄像头的调用实现实时图像的采集效果。
最终将图片进行保存后生成监控的历史视频记录。
pipinstallPyQt5 pipinstallpython-opencv
若是没有安装相关的模块,可以选择上述pip的方式进行安装。
接下来我们将需要的python模块导入到代码块中。
#It'sjustanaliasforthecv2module. importcv2ascv_ #It'simportingalltheclassesfromtheQtGuimodule. fromPyQt5.QtGuiimport* #It'simportingalltheclassesfromtheQtCoremodule. fromPyQt5.QtCoreimport* #It'simportingalltheclassesfromtheQtWidgetsmodule. fromPyQt5.QtWidgetsimport* #It'simportingthesysmodule. importsys #It'simportingtheosmodule. importos #It'simportingthetimemodule. importtime #It'simportingthetracebackmodule. importtraceback
创建名称为CameraUI的python类,开发监控工具相关的页面组件及布局。
将控件按钮等对应的槽函数也放到该类中。
classCameraUI(QWidget): def__init__(self): super(CameraUI,self).__init__() self.init_ui() definit_ui(self): self.setWindowTitle('本地监控工具公众号:Python 集中营') self.setWindowIcon(QIcon('ico.png')) self.resize(600,400) self.label_view=QLabel() self.image_path=QLineEdit() self.image_path.setReadOnljavascripty(True) self.image_path.setPlaceholderText('视频流图片保存地址') self.image_path_btn=QPushButton() self.image_path_btn.setText('打开') self.image_path_btn.clicked.connect(self.image_path_btn_clk) self.video_path=QLineEdit() self.video_path.setReadOnly(True) self.video_path.setPlahttp://www.devze.comceholderText('监控视频保存地址') self.video_path_btn=QPushButton() self.video_path_btn.setText('打开') self.video_path_btn.clicked.connect(self.video_path_btn_clk) self.start_listen_btn=QPushButton() self.start_listen_btn.setText('开启监控') self.start_listen_btn.clicked.connect(self.start_listen_btn_clk) self.brohttp://www.devze.comwer=QTextBrowser() self.brower.setReadOnly(True) self.brower.setFont(QFont('宋体',8)) self.brower.setPlaceholderText('日志处理过程区域...') self.brower.ensureCursorVisible() hbox=QHBoxLayout() vbox_left=QVBoxLayout() vbox_right=QVBoxLayout() vbox_left.addwidget(self.label_view) vbox_right_grid1=QGridLayout() vbox_right_grid1.addWidget(self.image_path,0,0,1,2) vbox_right_grid1.addWidget(self.image_path_btn,0,2,1,1) vbox_right_grid1.addWidget(self.video_path,1,0,1,2) vbox_right_grid1.addWidget(self.video_path_btn,1,2,1,1) vbox_right_grid2=QGridLayout() vbox_right_grid2.addWidget(self.brower,0,0,1,3) vbox_right_grid2.addWidget(self.start_listen_btn,1,0,1,3) vbox_right.addLayout(vbox_right_grid1) vbox_right.addLayout(vbox_right_grid2) hbox.addLayout(vbox_left) hbox.addLayout(vbox_right) self.listen_threadwww.devze.com=ListenWorkThread(self) self.listen_thread.message.connect(self.show_message) self.listen_thre开发者_Go入门ad.finished.connect(lambda:self.starttjlPPR_listen_btn.setEnabled(True)) self.setLayout(hbox) defshow_message(self,text): cursor=self.brower.textCursor() cursor.movePosition(QTextCursor.End) self.brower.append(text) self.brower.setTextCursor(cursor) self.brower.ensureCursorVisible() defimage_path_btn_clk(self): dir=QFileDialog.getExistingDirectory(self,"选择文件夹",os.getcwd()) self.image_path.setText(dir) defvideo_path_btn_clk(self): dir=QFileDialog.getExistingDirectory(self,"选择文件夹",os.getcwd()) self.video_path.setText(dir) defstart_listen_btn_clk(self): self.listen_thread.start()
开发一个ListenWorkThread类并且继承自QThread的子线程。
将该线程作为业务类实现调用本地摄像头完成监控的效果。
classListenWorkThread(QThread): message=pyqtSignal(str) finished=pyqtSignal(bool) def__init__(self,parent=None): super(ListenWorkThread,self).__init__(parent) self.parent=parent self.working=True def__del__(self): self.working=False defrun(self): try: image_path=self.parent.image_path.text() video_path=self.parent.video_path.text() ifimage_path.strip()==''orvideo_path.strip()=='': self.message.emit('相关路径设置不能为空,请检查设置!') return self.cap=cv_.VideoCapture(0) self.cap.set(3,300) self.cap.set(4,400) ifself.cap.isOpened(): self.message.emit('摄像头已成功打开!') n=0 self.message.emit('正在进行视频 监控中....') start_time=time.clock() whileTrue: ret,img=self.cap.read() ifos.path.exists(os.path.join(image_path,str(n)+'.jpg')): os.remove(os.path.join(image_path,str(n)+'.jpg')) self.save_image(img,str(n),image_path) time.sleep(0.1) pixmap_=QPixmap(os.path.join(image_path,str(n)+'.jpg')) self.parent.label_view.setPixmap(pixmap_) n=n+1 self.finished.emit(True) exceptExceptionase: traceback.print_exc() self.message.emit('程序运行错误,请检查参数是否设置正确!') self.finished.emit(True) defsave_image(self,image=None,file_name=None,image_path=None): ifimageisnotNone: cv_.imwrite(os.path.join(image_path,file_name+'.jpg'),image)
以上整个业务子线程的逻辑就开发完成了,然后使用python模块的main函数调用。
将整个程序的运行加入到主体循环当中。
if__name__=='__main__': app=QApplication(sys.argv) main=CameraUI() main.show() sys.exit(app.exec_())
到此这篇关于Python+PyQt5自制监控小工具的文章就介绍到这了,更多相关Python PyQt5监控内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论