728x90
반응형
더보기
PyQt5와 다양한 라이브러리 경험을 위한 Toy 프로젝트이다.
Baseline
PyQt5를 처음 사용하므로 ChatGPT의 도움을 받자.
이 코드를 복사하기 이전에 아나콘다를 통해 가상 환경을 만든다.
python==3.9.13
CUDA 버전은 11.6 이므로
pip install torch torchvsion --index-url https://download.pytorch.org/whl/cu116
torch==1.13.1+cu116
torchvision==0.14.1+cu116
opencv-python==4.7.0.72
import sys
import cv2
import torch
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
# Load the face landmarks detection model
# model = torch.hub.load('pytorch/vision', 'faster_rcnn_resnet50_fpn', pretrained=True)
# model.eval()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Set up the UI
self.setWindowTitle("Face Landmarks Detection")
self.setGeometry(100, 100, 640, 480)
# Set up the video capture
self.video = cv2.VideoCapture(0)
self.video.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
self.video.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# Set up the timer to update the video feed
self.timer = QTimer()
self.timer.timeout.connect(self.update_frame)
self.timer.start(1)
# Set up the buttons
self.button1 = QPushButton("Button 1", self)
self.button1.setGeometry(10, 10, 100, 30)
self.button1.clicked.connect(self.button1_clicked)
self.button2 = QPushButton("Button 2", self)
self.button2.setGeometry(120, 10, 100, 30)
self.button2.clicked.connect(self.button2_clicked)
def update_frame(self):
# Read a frame from the video capture
ret, frame = self.video.read()
# Convert the frame to a QImage
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
pixmap = QPixmap.fromImage(image)
self.label.setPixmap(pixmap)
def button1_clicked(self):
# Perform the first image processing operation
# with torch.no_grad():
# outputs = model(image)
print('btn1 clk')
# TODO: Add code to process the outputs and display the result
def button2_clicked(self):
# Perform the second image processing operation
# TODO: Add code to perform the operation and display the result
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
코드를 잠깐 살펴보면 PyQt5는 메인 클래스 내부에서 객체를 선언하고 객체 에벤트에 connect함수로 동작을 지정할 수 있나 보다. 또한 OpenCV로 Frame을 읽어서 QImage로 변환하는 작업을 수행하는 데, PyQt5 출력에 필요한 연산인 것 같다.
당장 필요한 패키지는 모두 설치했으니 실행해보자.
chatGPT가 준 위 코드를 그대로 실행하면 label이 선언되지 않았다고 오류가 날 것 이다. 또한 웹캠 정보를 지속적으로 읽어야 하기 때문에 추가적인 코드 수정을 해주었다. 거기에 레이아웃 배치를 손 보면 다음과 같이 베이스 라인이 완성된다.
본인은 웹캠이 없어서 옛날에 쓰던 폴더 스마트폰을 DroidCam 어플을 이용해 웹캠으로 사용했다.
728x90
반응형
'Projects > Face Toy Project' 카테고리의 다른 글
[GUI 기반 Face Toy Project -6] real time face alignment opencv (0) | 2023.04.11 |
---|---|
[GUI 기반 Face Toy Project -5] face alignment opencv (0) | 2023.04.10 |
[GUI 기반 Face Toy Project -4] real time face swap (0) | 2023.04.10 |
[GUI 기반 Face Toy Project -3] 들로네 삼각 변환(Delaunay triangulation) (0) | 2023.04.07 |
GUI 기반 Face Toy Project -2 (0) | 2023.04.05 |
댓글