-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui,py
More file actions
157 lines (128 loc) · 6.2 KB
/
ui,py
File metadata and controls
157 lines (128 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import warnings
import absl.logging
import cv2
import sys
import numpy as np
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import QTimer
from test import process_frame, load_images, glasses_images, hats_images
# Suppress TensorFlow and absl logging warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Suppress TensorFlow logging (0 = all, 1 = INFO, 2 = WARN, 3 = ERROR)
absl.logging.set_verbosity(absl.logging.ERROR)
# Suppress the Google protobuf warning
warnings.filterwarnings("ignore", category=UserWarning, module='google.protobuf')
# Optional: Redirect stderr if the warnings are still appearing
import logging
logging.basicConfig(level=logging.ERROR)
sys.stderr = open(os.devnull, 'w')
class AccessoryApp(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.cap = cv2.VideoCapture(0)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(30)
self.current_glasses_index = 0
self.current_hat_index = 0
self.current_filter = None
self.current_frame = None
def initUI(self):
self.setWindowTitle('Accessory Overlay Live Feed')
self.setGeometry(100, 100, 1000, 700)
self.setStyleSheet("background-color: #2e2e2e; color: #f5f5f5;")
# Layouts
main_layout = QtWidgets.QHBoxLayout()
controls_layout = QtWidgets.QVBoxLayout()
# Video frame
self.video_frame = QtWidgets.QLabel(self)
self.video_frame.setFixedSize(800, 600)
self.video_frame.setStyleSheet("border: 2px solid #f5f5f5;")
# Buttons
self.glasses_button = QtWidgets.QPushButton('Toggle Glasses', self)
self.glasses_button.setStyleSheet("background-color: #808080; color: #ffffff; border-radius: 5px; padding: 10px;")
self.glasses_button.clicked.connect(self.toggle_glasses)
self.hat_button = QtWidgets.QPushButton('Toggle Hat', self)
self.hat_button.setStyleSheet("background-color: #808080; color: #ffffff; border-radius: 5px; padding: 10px;")
self.hat_button.clicked.connect(self.toggle_hat)
self.picture_button = QtWidgets.QPushButton('Take Picture', self)
self.picture_button.setStyleSheet("background-color: #808080; color: #ffffff; border-radius: 5px; padding: 10px;")
self.picture_button.clicked.connect(self.take_picture)
self.filter_button = QtWidgets.QPushButton('Change Filter', self)
self.filter_button.setStyleSheet("background-color: #808080; color: #ffffff; border-radius: 5px; padding: 10px;")
self.filter_button.clicked.connect(self.change_filter)
# Arrange layouts
controls_layout.addWidget(self.glasses_button)
controls_layout.addWidget(self.hat_button)
controls_layout.addWidget(self.filter_button)
controls_layout.addWidget(self.picture_button)
controls_layout.addStretch()
main_layout.addWidget(self.video_frame)
main_layout.addLayout(controls_layout)
self.setLayout(main_layout)
def toggle_glasses(self):
self.current_glasses_index = (self.current_glasses_index + 1) % len(glasses_images)
def toggle_hat(self):
self.current_hat_index = (self.current_hat_index + 1) % len(hats_images)
def change_filter(self):
if self.current_filter is None:
self.current_filter = 'greyscale'
elif self.current_filter == 'greyscale':
self.current_filter = 'vibrant'
else:
self.current_filter = None
def apply_filter(self, frame):
if self.current_filter == 'greyscale':
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
elif self.current_filter == 'vibrant':
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hsv[:, :, 1] = cv2.add(hsv[:, :, 1], 50)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
return frame
def take_picture(self):
if self.current_frame is not None:
filename = 'captured_image_with_overlay.png'
cv2.imwrite(filename, self.current_frame)
self.showMessage(f'Picture saved as {filename}')
def update_frame(self):
ret, frame = self.cap.read()
if ret:
glasses_image = glasses_images[self.current_glasses_index] if self.current_glasses_index < len(glasses_images) else None
hat_image = hats_images[self.current_hat_index] if self.current_hat_index < len(hats_images) else None
if glasses_image is not None and hat_image is not None:
frame = process_frame(frame, glasses_image, hat_image)
frame = self.apply_filter(frame)
self.current_frame = frame.copy()
# Convert frame to Qt image
if len(frame.shape) == 2: # Greyscale image
qt_image = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0], frame.shape[1], QtGui.QImage.Format_Grayscale8)
else:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
height, width, channel = rgb_frame.shape
bytes_per_line = 3 * width
qt_image = QtGui.QImage(rgb_frame.data, width, height, bytes_per_line, QtGui.QImage.Format_RGB888)
# Display frame
self.video_frame.setPixmap(QtGui.QPixmap.fromImage(qt_image))
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_G:
self.toggle_glasses()
elif event.key() == QtCore.Qt.Key_H:
self.toggle_hat()
elif event.key() == QtCore.Qt.Key_F:
self.change_filter()
elif event.key() == QtCore.Qt.Key_P:
self.take_picture()
def closeEvent(self, event):
self.cap.release()
cv2.destroyAllWindows()
def showMessage(self, message):
msg_box = QtWidgets.QMessageBox(self)
msg_box.setStyleSheet("background-color: #808080; color: #ffffff;")
msg_box.setText(message)
msg_box.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = AccessoryApp()
ex.show()
sys.exit(app.exec_())