-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_screen.py
214 lines (174 loc) · 8.02 KB
/
main_screen.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import cv2
import sys
import requests
import numpy as np
import barcode_recognition as barR
import text_extraction as txtE
import txtE_screen
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt, pyqtSignal
class MainScreen(QWidget):
def __init__(self):
super().__init__()
self.image_paths = []
self.text_list = []
self.info_screen = None
self.initUI()
def initUI(self):
# 위젯 사이즈 설정
self.widget_width = 375
self.widget_height = 600
self.setFixedSize(self.widget_width, self.widget_height)
background_image_path = "./img/bookself.png"
self.background_image = QPixmap(background_image_path)
self.background_label = QLabel(self)
self.background_label.setPixmap(self.background_image)
self.background_label.setGeometry(0, 0, self.widget_width, self.widget_height)
# 책 각 위치에 출력하는 부분
self.grid_layout = QGridLayout(self)
self.grid_layout.setContentsMargins(50, 10, 10, 10)
self.grid_layout.setSpacing(0)
self.create_search_btn()
def create_search_btn(self):
# 버튼 생성 및 스타일 설정
button_size = 50
button_x = 20
button_y = self.widget_height - button_size - 20
btn = QPushButton(self)
btn.setGeometry(button_x, button_y, button_size, button_size)
btn.setStyleSheet(" border-radius: 50%;")
btn_image_path = "./img/search_icon.png"
btn_image = QPixmap(btn_image_path)
btn_image = btn_image.scaled(button_size, button_size, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
icon = QIcon(btn_image)
btn.setIcon(icon)
btn.setIconSize(btn.size())
btn.clicked.connect(self.barcode_recognition)
self.setWindowTitle('BOOKSCANNER')
def barcode_recognition(self):
self.hide()
barR.barcorde_recognition()
self.close()
self.show_info_screen()
def show_info_screen(self):
print('Open InfoScreen')
self.info_screen = InfoScreen(self.image_paths)
self.info_screen.closed.connect(self.update_image_paths)
self.info_screen.closed.connect(self.show_main_screen)
self.info_screen.show()
def update_image_paths(self):
self.image_paths = self.info_screen.image_paths
def show_main_screen(self):
# Clear grid layout
while self.grid_layout.count():
child = self.grid_layout.takeAt(0)
if child.widget():
child.widget().deleteLater()
self.create_search_btn()
for row in range(4):
self.grid_layout.setRowMinimumHeight(row, self.widget_height // 4)
for col in range(3):
self.grid_layout.setColumnMinimumWidth(col, self.widget_width // 3)
index = row * 3 + col
if index < len(self.image_paths):
if self.image_paths[row * 3 + col]:
image_path = self.image_paths[row * 3 + col]
self.create_bookimg_button(image_path, row, col)
else:
continue
self.show()
def create_bookimg_button(self, image_path, row, col):
self.bookimg_btn = QPushButton(self)
self.bookimg_btn.setFixedSize(70, 100)
pixmap = QPixmap(image_path).scaled(self.bookimg_btn.size())
self.bookimg_btn.setIcon(QIcon(pixmap))
self.bookimg_btn.setIconSize(self.bookimg_btn.size())
self.bookimg_btn.clicked.connect(self.show_custom)
self.grid_layout.addWidget(self.bookimg_btn, row, col)
def show_custom(self):
print('open MainScreen')
# self.main_screen = pdf.CustomWidget()
# self.main_screen = txtE.text_extraction()
self.txtE_screen = txtE_screen.TextDetectionApp()
self.txtE_screen.show()
# self.hide()
class InfoScreen(QWidget):
closed = pyqtSignal()
def __init__(self, image_paths):
super().__init__()
self.image_paths = image_paths
self.initUI()
def initUI(self):
# Book information from barcode recognition
title, author, isbn, publisher, pubdata, discount, description, image_url = barR.book_info()
self.setStyleSheet("background-color: white;")
# Layout
main_layout = QHBoxLayout()
layout1 = QGridLayout()
layout = QGridLayout()
main_layout.addLayout(layout1)
main_layout.addLayout(layout)
self.setLayout(main_layout)
# Load and display image
image_data = requests.get(image_url).content
image = QPixmap()
image.loadFromData(image_data)
image = image.scaledToWidth(image.width() // 3)
# Save image
cap_cnt = len(self.image_paths) + 1
image_array = np.frombuffer(image_data, np.uint8)
save_image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
save_image_path = f"./img/books/image{cap_cnt}.jpg"
cv2.imwrite(save_image_path, save_image)
self.image_paths.append(save_image_path)
print(self.image_paths)
# Display image
self.image_label = QLabel(self)
self.image_label.setPixmap(image)
self.image_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 이미지를 왼쪽 상단에 정렬
layout1.addWidget(self.image_label, 0, 0, Qt.AlignLeft | Qt.AlignTop)
# Display title
self.title_label = QLabel(title, self)
self.title_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 텍스트를 왼쪽 상단에 정렬
layout.addWidget(self.title_label, 0, 1, Qt.AlignLeft | Qt.AlignTop)
# Display author
self.author_label = QLabel(author, self)
self.author_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 텍스트를 왼쪽 상단에 정렬
layout.addWidget(self.author_label, 1, 1, Qt.AlignLeft | Qt.AlignTop)
# Display isbn
self.isbn_label = QLabel(isbn, self)
self.isbn_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 텍스트를 왼쪽 상단에 정렬
layout.addWidget(self.isbn_label, 2, 1, Qt.AlignLeft | Qt.AlignTop)
# Display publisher
self.publisher_label = QLabel(publisher, self)
self.publisher_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 텍스트를 왼쪽 상단에 정렬
layout.addWidget(self.publisher_label, 3, 1, Qt.AlignLeft | Qt.AlignTop)
# # Display pubdata
# self.pubdata_label = QLabel(pubdata, self)
# self.pubdata_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 텍스트를 왼쪽 상단에 정렬
# layout.addWidget(self.pubdata_label, 4, 1, Qt.AlignLeft | Qt.AlignTop)
# Display discount
self.discount_label = QLabel(discount, self)
self.discount_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 텍스트를 왼쪽 상단에 정렬
layout.addWidget(self.discount_label, 5, 1, Qt.AlignLeft | Qt.AlignTop)
# Display description
self.description_scroll_area = QScrollArea()
self.description_scroll_area.setWidgetResizable(True)
self.description_label = QLabel(description, self)
self.description_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) # 텍스트를 왼쪽 상단에 정렬
self.description_label.setWordWrap(True)
self.description_scroll_area.setWidget(self.description_label)
layout.addWidget(self.description_scroll_area, 6, 1, Qt.AlignLeft | Qt.AlignTop)
self.setLayout(layout)
self.setWindowTitle('Book Info')
self.setStyleSheet("background-color: white;")
def closeEvent(self, event):
self.emit_closed_signal()
def emit_closed_signal(self):
self.closed.emit()
if __name__ == "__main__":
app = QApplication(sys.argv)
main_screen = MainScreen()
main_screen.show()
sys.exit(app.exec_())