資源簡介
Pythone+pyq5+opencv 多線程模式調用攝像頭。這樣你在識別人臉的時候,可以通過線程輸出結果,這時候攝像頭保持流暢視頻。

代碼片段和文件信息
“““
In?this?example?we?demonstrate?how?to?create?simple?camera?viewer?using?Opencv3?and?PyQt5
Author:?Berrouba.A
Last?edited:?21?Feb?2018
“““
#?import?system?module
import?sys
import?time
#?import?some?PyQt5?modules
from?PyQt5.QtWidgets?import?QApplication
from?PyQt5.QtWidgets?import?QWidget
from?PyQt5.QtGui?import?QImage
from?PyQt5.QtGui?import?QPixmap
from?PyQt5.QtCore?import?QTimer
from?PyQt5.QtCore?import?*
#?import?Opencv?module
import?cv2
from?ui_main_window?import?*
class?Mythread(QThread):
????#?定義信號定義參數為str類型
????breakSignal?=?pyqtSignal(QPixmap)
????def?__init__(self?parent=None):
????????super().__init__(parent)
????????#?下面的初始化方法都可以,有的python版本不支持
????????super(Mythread?self).__init__()
????????self.cap?=?cv2.VideoCapture(0)
????def?run(self):
????????while?True:
????????????
????????????ret?image?=?self.cap.read()
????????????#?convert?image?to?RGB?format
????????????image?=?cv2.cvtColor(image?cv2.COLOR_BGR2RGB)
????????????#?get?image?infos
????????????height?width?channel?=?image.shape
????????????step?=?channel?*?width
????????????#?create?QImage?from?image
????????????qImg?=?QImage(image.data?width?height?step?QImage.Format_RGB888)
????????????#?show?image?in?img_label
????????????self.breakSignal.emit(QPixmap.fromImage(qImg))
????????#self.ui.image_label.setPixmap(QPixmap.fromImage(qImg))
????????‘‘‘
????????for?i?in?range(2000000):
????????????#?發出信號
????????????print(12)
????????????a=[ii+1]
????????????self.breakSignal.emit(str(i))
????????????#?讓程序休眠
????????????time.sleep(0.5)
????????‘‘‘
class?Mythread2(QThread):
????#?定義信號定義參數為str類型
????breakSignal2?=?pyqtSignal(str)
????def?__init__(self?parent=None):
????????super().__init__(parent)
????????#?下面的初始化方法都可以,有的python版本不支持
????????super(Mythread2?self).__init__()
????def?run(self):
????????for?i?in?range(2000000):
????????????#?發出信號
????????????print(12)
????????????a=[ii+1]
????????????self.breakSignal2.emit(str(i))
????????????#?讓程序休眠
????????????time.sleep(0.5)
??
class?MainWindow(QWidget):
????#?class?constructor
????def?__init__(self):
????????#?call?QWidget?constructor
????????super().__init__()
????????self.ui?=?Ui_Form()
????????self.ui.setupUi(self)
????????#按鈕啟動現成
????????self.ui.control_bt.clicked.connect(self.controlTimer)
????????#?創建線程
????????self.thread?=?Mythread()
????????#?連接信號
????????self.thread.breakSignal.connect(self.chuli)
????????#?開始線程
??????????#?創建線程
????????self.thread2?=?Mythread2()
????????self.thread2.breakSignal2.connect(self.chuli2)
????????self.thread2.start()
????def?chuli(selfqImg):
????????self.ui.image_label.setPixmap(qImg)
???
????def?chuli2(selfmsg):
????????self.ui.control_bt.setText(msg)
????#?start/stop?timer
????def?controlTimer(self):
????????self.thread.start()
????
if?__name__?==?‘__main__‘:
????app?=?QApplication(sys.argv)
????#?create?and?show?mainWindow
????mainWindow?=?MainWindow()
????mainWindow.show()?
????sys.exit(app.exec_())
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????771499??2018-10-19?11:28??camView\build\main_window\ba
?????文件???????1036??2018-10-19?11:28??camView\build\main_window\main_window.exe.manifest
?????文件???????3159??2018-10-22?18:19??camView\main_window.py
?????文件????????981??2018-10-19?11:28??camView\main_window.spec
?????文件????????325??2018-02-22?13:17??camView\README.md
?????文件???????1344??2018-02-22?13:17??camView\ui_main_window.py
?????文件????????882??2018-02-22?13:17??camView\ui_main_window.ui
?????文件???????1365??2018-02-22?13:17??camView\__pycache__\ui_main_window.cpython-35.pyc
?????文件???????1217??2018-10-18?10:54??camView\__pycache__\ui_main_window.cpython-36.pyc
?????目錄??????????0??2018-10-19?11:28??camView\build\main_window
?????目錄??????????0??2018-10-19?11:26??camView\build
?????目錄??????????0??2018-10-19?11:26??camView\dist
?????目錄??????????0??2018-10-18?10:54??camView\__pycache__
?????目錄??????????0??2018-10-19?11:26??camView
-----------?---------??----------?-----??----
???????????????781808????????????????????14
- 上一篇:pcap-1.1.win32-py2.7.exe
- 下一篇:爬取瓜子二手車.py
評論
共有 條評論