Python/pyqt5/CODE/testui_0826/test_0826_01_main.py
2024-04-19 19:24:34 +08:00

144 lines
5.7 KiB
Python

import cv2
import sys, time, os
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from test_0826_ui_01 import Ui_MainWindow
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen
from PyQt5.QtCore import QRect, Qt,QCoreApplication
from PyQt5.QtCore import QDate,QTime
from PyQt5.QtWidgets import QApplication, QFileDialog, QLabel,QMainWindow, QWidget, QPushButton
class Camera(QtCore.QThread): #攝像頭
rawdata = QtCore.pyqtSignal(np.ndarray)
def __init__(self, parent=None):
super().__init__(parent)
#self.cam = cv2.VideoCapture(0)
self.cam = cv2.VideoCapture(0)
self.connect = True
def run(self): #運作攝像頭
while self.running and self.connect:
ret, img = self.cam.read()
if ret:
self.rawdata.emit(img)
def open(self): #打開攝像頭
self.running = True
def stop(self): #停止攝像頭
self.running = False
class readTime(QtCore.QThread): #讀取時間
sinOut = pyqtSignal(str) # 聲明一個帶字串參數的信號
def __init__(self, parent=None):
super(readTime, self).__init__(parent)
self.working = True # 設置工作狀態
def __del__(self):
self.working = False # 執行緒狀態改變與執行緒終止
self.wait()
def run(self):
while self.working:
result = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 讀取當下時間
self.sinOut.emit(f'{result}') # 傳送信号
self.msleep(1000) # 休眠1秒
def img_to_view(img):#原圖
img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # QT顏色顯示轉換
Ny, Nx, _ = img.shape
img = QtGui.QImage(img.data, Nx, Ny, QtGui.QImage.Format_RGB888) #須改格式
return img
def binary(img): #二值化
img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # QT顏色顯示轉換
img=cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
#ret, img1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) #二值
img1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 11, 2) #動態二值
#ret, img1 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) #二值
img1=cv2.cvtColor(img1, cv2.COLOR_GRAY2RGB)
Ny, Nx, _ = img1.shape
img1 = QtGui.QImage(img1.data, Nx, Ny, QtGui.QImage.Format_RGB888) #須改格式
return img1
def flip(img):#翻轉圖片
img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # QT顏色顯示轉換
img1=cv2.flip(img,1) #影像轉換
Ny, Nx, _ = img1.shape
img1 = QtGui.QImage(img1.data, Nx, Ny, QtGui.QImage.Format_RGB888) #須改格式
return img1
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None): #按鍵設定
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.view1.setScaledContents(True);
self.view2.setScaledContents(True);
self.ProcessCam = Camera() # 作動相機
if self.ProcessCam.connect:
self.ProcessCam.rawdata.connect(self.getRaw)
self.readDateTime = readTime() # 顯示當前時間
self.readDateTime.start()
self.readDateTime.sinOut.connect(self.settimeText) # 將signal和slot方法绑定
self.cambt.clicked.connect(self.cam1) # 按鍵cam
self.binarybt.clicked.connect(self.binary) # 按鍵binary
self.filpbt.clicked.connect(self.flip) # 按鍵binary
self.stopbt.clicked.connect(self.stop) # 按鍵stop
self.closebt.clicked.connect(self.close) # 按鍵close
def settimeText(self, str_in): #時間顯示
#str_in = f'<span style="color:#FF2D2D; font-size: 18px; font-weight: bold;">{str_in}</span>'
str_in = f'<span style=" background-color:red ; margin-right:3px ;color:blue; font-size: 18px ; font-weight: bold;">{str_in}</span>'
self.time.setText(str_in)
def stop(self,img_stop): #stop執行
img_stop = cv2.imread('stop.jpg')
self.ProcessCam.stop()
self.stop_show(img_stop)
def stop_show(self,img):
self.ProcessCam.stop()
self.img_original=img_to_view(img)
self.view1.setPixmap(QtGui.QPixmap.fromImage(self.img_original))
self.view2.setPixmap(QtGui.QPixmap.fromImage(self.img_original))
def close(self): #close執行
QtWidgets.QApplication.closeAllWindows()
def cam1(self): #cam執行
if self.ProcessCam.connect:
self.ProcessCam.open() #開啟相機
self.ProcessCam.start() #啟動相機
def getRaw(self, img):
self.cam_show(img)
def cam_show(self, img):
self.img_original=img_to_view(img)
self.view1.setPixmap(QtGui.QPixmap.fromImage(self.img_original))
def binary(self): #二值化 執行
self.ProcessCam.open()
self.ProcessCam.start()
self.ProcessCam.rawdata.connect(self.getRaw_binary)
def getRaw_binary(self, img):
self.show_binary(img)
def show_binary(self,img):
self.img_binary = binary(img)
self.view2.setPixmap(QtGui.QPixmap.fromImage(self.img_binary))
def flip(self): #翻轉圖片執行
self.ProcessCam.open()
self.ProcessCam.start()
self.ProcessCam.rawdata.connect(self.getRaw_flip)
def getRaw_flip(self, img):
self.show_flip(img)
def show_flip(self,img):
self.img_flip = flip(img)
self.view2.setPixmap(QtGui.QPixmap.fromImage(self.img_flip))
def keyPressEvent(self, event): #鍵盤執行
if event.key() == QtCore.Qt.Key_Q:
self.ProcessCam.stop()
QtWidgets.QApplication.closeAllWindows()
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())