Raspberry Pi
웹캠/ 얼굴인식 블로그
Ok_JH
2020. 10. 20. 16:05
- 기초
위키독스
온라인 책을 제작 공유하는 플랫폼 서비스
wikidocs.net
- 웹캠 영상 띄우기 (openCV 폴더에 파일 저장)
OpenCV에서 캡처한 영상을 pyQt5 윈도우에 보여주기
OpenCV에서 캡처한 영상을 pyQt5로 작성된 GUI에서 보여주는 방법을 다룹니다. 깃허브에 있는 코드를 수정하여 사용했습니다. 처음 실행하면 버튼 2개만 보입니다. start 버튼을 클릭하면 웹캠 영상이
webnautes.tistory.com
import cv2
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
class ShowVideo(QtCore.QObject):
flag = 0
camera = cv2.VideoCapture(0)
ret, image = camera.read()
height, width = image.shape[:2]
VideoSignal1 = QtCore.pyqtSignal(QtGui.QImage)
VideoSignal2 = QtCore.pyqtSignal(QtGui.QImage)
def __init__(self, parent=None):
super(ShowVideo, self).__init__(parent)
@QtCore.pyqtSlot()
def startVideo(self):
global image
run_video = True
while run_video:
ret, image = self.camera.read()
color_swapped_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
qt_image1 = QtGui.QImage(color_swapped_image.data,
self.width,
self.height,
color_swapped_image.strides[0],
QtGui.QImage.Format_RGB888)
self.VideoSignal1.emit(qt_image1)
if self.flag:
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_canny = cv2.Canny(img_gray, 50, 100)
qt_image2 = QtGui.QImage(img_canny.data,
self.width,
self.height,
img_canny.strides[0],
QtGui.QImage.Format_Grayscale8)
self.VideoSignal2.emit(qt_image2)
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(25, loop.quit) #25 ms
loop.exec_()
@QtCore.pyqtSlot()
def canny(self):
self.flag = 1 - self.flag
class ImageViewer(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ImageViewer, self).__init__(parent)
self.image = QtGui.QImage()
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawImage(0, 0, self.image)
self.image = QtGui.QImage()
def initUI(self):
self.setWindowTitle('Test')
@QtCore.pyqtSlot(QtGui.QImage)
def setImage(self, image):
if image.isNull():
print("Viewer Dropped frame!")
self.image = image
if image.size() != self.size():
self.setFixedSize(image.size())
self.update()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
thread = QtCore.QThread()
thread.start()
vid = ShowVideo()
vid.moveToThread(thread)
image_viewer1 = ImageViewer()
image_viewer2 = ImageViewer()
vid.VideoSignal1.connect(image_viewer1.setImage)
vid.VideoSignal2.connect(image_viewer2.setImage)
push_button1 = QtWidgets.QPushButton('Start')
push_button2 = QtWidgets.QPushButton('Canny')
push_button1.clicked.connect(vid.startVideo)
push_button2.clicked.connect(vid.canny)
vertical_layout = QtWidgets.QVBoxLayout()
horizontal_layout = QtWidgets.QHBoxLayout()
horizontal_layout.addWidget(image_viewer1)
horizontal_layout.addWidget(image_viewer2)
vertical_layout.addLayout(horizontal_layout)
vertical_layout.addWidget(push_button1)
vertical_layout.addWidget(push_button2)
layout_widget = QtWidgets.QWidget()
layout_widget.setLayout(vertical_layout)
main_window = QtWidgets.QMainWindow()
main_window.setCentralWidget(layout_widget)
main_window.show()
sys.exit(app.exec_())
- 얼굴인식
blog.naver.com/ljy9378/221438230814
8편 : 실시간 얼굴인식 카메라 | 실시간 카메라 영상에서 사람 얼굴 판별하기
실시간 얼굴인식 카메라 | 실시간 카메라 영상에서 사람 얼굴 판별하기프로젝트 목차0편 : 개요1편 : 라즈...
blog.naver.com