3
3
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import threading
import datetime
class MainWindow(QWidget):
def __init__(self):
super().__init__()
thread=threading.Thread(target=run)
thread.daemon=True
thread.start()
self.makeUI()
def makeUI(self):
self.wtop=200
self.wleft=500
self.wwidth=800
self.wheight=600
self.setWindowTitle('나만의 필터 카메라')
self.setGeometry(self.wleft,self.wtop,self.wwidth,self.wheight)
self.btn_start=QPushButton('전원 버튼',self)
self.btn0=QPushButton('기본 필터',self)
self.btn1=QPushButton('흑백',self)
self.btn2=QPushButton('흑백 스케치',self)
self.btn3=QPushButton('컬러 스케치',self)
self.btn4=QPushButton('왜곡 1',self)
self.btn5=QPushButton('왜곡 2',self)
self.btn6=QPushButton('마스크 1',self)
self.btn7=QPushButton('마스크 2',self)
self.btn_cap=QPushButton('사진 촬영',self)
self.btn_ron=QPushButton('영상 촬영 ON',self)
self.btn_roff=QPushButton('영상 촬영 OFF',self)
self.btn_cap.clicked.connect(lambda: camera_function(-1))
self.btn_ron.clicked.connect(lambda: camera_function(-2))
self.btn_roff.clicked.connect(lambda: camera_function(-3))
hbox1=QHBoxLayout()
hbox1.addWidget(self.btn_cap)
hbox1.addWidget(self.btn_ron)
hbox1.addWidget(self.btn_roff)
widget1=QWidget()
widget1.setLayout(hbox1)
vbox1=QVBoxLayout()
vbox1.addWidget(webcam,stretch=5)
vbox1.addWidget(widget1,stretch=1)
widget2=QWidget()
widget2.setLayout(vbox1)
hbox2=QHBoxLayout()
hbox2.addWidget(self.btn_start)
hbox2.addWidget(self.btn0)
hbox2.addWidget(self.btn1)
hbox2.addWidget(self.btn2)
hbox2.addWidget(self.btn3)
hbox2.addWidget(self.btn4)
hbox2.addWidget(self.btn5)
hbox2.addWidget(self.btn6)
hbox2.addWidget(self.btn7)
widget3=QWidget()
widget3.setLayout(hbox2)
vbox2=QVBoxLayout()
vbox2.addWidget(widget2)
vbox2.addWidget(widget3)
self.setLayout(vbox2)
def camera_function(mode):
global state
if mode==-1:
state=-1
elif mode==1:
state=1
elif mode==0:
state=0
elif mode==-2:
state=-2
elif mode==-3:
state=-3
def run():
record=False
while True:
ret,image=camera.read()
height,width=image.shape[:2]
image=cv2.flip(image,1)
now=datetime.datetime.now().strftime('%Y.%m.%d_%H.%M.%S')
if state==1:
qt_image=QImage(image.data,width,height,image.strides[0],QImage.Format_BGR888)
pixmap=QPixmap.fromImage(qt_image)
webcam.setPixmap(pixmap)
elif state==-1:
cv2.imwrite('D:/openCV capture/'+str(now)+'.png',image)
print("here")
camera_function(1)
elif state==-2:
record=True
fourcc=cv2.VideoWriter_fourcc('m','p','4','v')
video=cv2.VideoWriter("D:/openCV capture/"+str(now)+".mp4",fourcc,20.0,
(width,height))
camera_function(1)
elif state==-3:
print('--- Record End ---')
record=False
video.release()
camera_function(1)
if record:
print('--- Recording~ ---')
video.write(image)
if __name__=='__main__':
state=1
app=QApplication(sys.argv)
camera=cv2.VideoCapture(0)
webcam=QLabel()
main_window=MainWindow()
main_window.show()
sys.exit(app.exec_())