-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
224 lines (163 loc) · 6.39 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
import pygame
import cv2
import time
import hashlib
import requests
from escpos.printer import Usb
import qrcode
import numpy as np
from PIL import Image, ImageEnhance
from util import imageCombine, qrGenerate, saturate_contrast2
from qr import QR
HOST_URL = 'http://146.56.106.142/'
pygame.init()
SCREEN_WIDTH = 1300
SCREEN_HEIGHT = 1000
# surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.NOFRAME)
# surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.NOFRAME)
# surface = pygame.display.set_mode((480*2, 320*2))
surface = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
class ImgObject:
def __init__(self, path, size):
self.__img = pygame.image.load(path)
self.__size = size
self.__x, self.__y = self.__img.get_size()
def get_image(self):
return pygame.transform.scale(self.__img, (self.__x * self.__size, self.__y * self.__size))
BMO_TIMER_INIT = 15 * 10
TAKE_TIMER_INIT = 10 * 10
class BMO_generator:
def __init__(self):
self.timer = BMO_TIMER_INIT
self.__emotion = {
"open": ImgObject("./image/bmo_animation/emotion_0.png", 1.25),
"close_1": ImgObject("./image/bmo_animation/emotion_1.png", 1.25),
"close_2": ImgObject("./image/bmo_animation/emotion_2.png", 1.25),
"close": ImgObject("./image/bmo_animation/emotion_3.png", 1.25),
}
def next(self):
self.timer -= 1
if self.timer <= 0:
self.timer = BMO_TIMER_INIT
if self.timer == 4:
return self.__emotion["close_1"].get_image()
elif self.timer == 3:
return self.__emotion["close_2"].get_image()
elif self.timer == 2:
return self.__emotion["close"].get_image()
elif self.timer == 1:
return self.__emotion["close_1"].get_image()
return self.__emotion["open"].get_image()
class Cam_Object:
def __init__(self, frame):
self.__frame = frame
self.__size = 1
def setFrame(self, frame):
self.__frame = frame
def getImage(self):
image = pygame.image.frombuffer(
self.__frame.tobytes(), self.__frame.shape[1::-1], "BGR")
dx, dy = map(lambda x: int(self.__size * x), image.get_size())
image = pygame.transform.scale(image, (dx, dy))
image = pygame.transform.flip(image, True, False)
return image
def setSize(self, size: float):
self.__size = size
####################
# Init #
####################
# qr code init
qr = QR(HOST_URL)
# camera init
Camera = Cam_Object(None)
Camera.setSize(2.1)
webcam = cv2.VideoCapture(2)
timer = 0
img_saved = []
# time timer init
font1 = pygame.font.SysFont(None, 500)
# BMO animation setting
BMO = BMO_generator()
p = Usb(0x1fc9, 0x2016, in_ep=0x81, out_ep=0x01)
running = True
while running:
status, frame = webcam.read()
Camera.setFrame(frame)
# Key pressed
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: # SPACE : 사진 찍기
timer = 5 * 10
elif event.key == pygame.K_ESCAPE: # ESC : BMO 닫기
running = False
break
# 다음 screen까지 딜레이
pygame.time.delay(10)
surface.blit(BMO.next(), (0, 0))
# 타이머 0 이상일 시 ( 타이머 돌고 있을 경우 )
if timer > 0:
timer -= 1
surface.blit(Camera.getImage(), (0, 0))
if timer == 0: # 사진 찍혔을 때
if len(img_saved) <= 3:
surface.fill((255, 255, 255))
pygame.display.flip()
main_frame = imageCombine(
f'./image/frames/mario/mario_0{len(img_saved) + 1}.png', frame, len(img_saved) + 1)
img_saved.append(main_frame)
pygame.time.delay(500)
timer = 5 * 10
if len(img_saved) == 4:
timer = 1
else: # img_saved
# hashing
hash_object = hashlib.sha256(f"{time.time_ns()}".encode())
filename = hash_object.hexdigest()[:20]
# directory name define
FILE_DIRECTORY = './image/capture/' + filename + '.png'
with open(".last", "w") as f:
f.write(FILE_DIRECTORY)
# # IMG SAVE
# main_frame = imageCombine(f'./img/frames/mario/mario_0{i}.png', frame, i)
# img_saved.append(main_frame)
# img_saved.append(cv2.imread('./img/sscc.jpg'))
main_frame = cv2.vconcat(img_saved)
cv2.imwrite(FILE_DIRECTORY, main_frame)
print(FILE_DIRECTORY)
# IMG UPLOAD
try:
with open(FILE_DIRECTORY, 'rb') as f:
res = requests.post(
HOST_URL + "uploadfile/", files={'file': f})
if res.status_code == 200:
print(f"{res.status_code}, Image Upload Success")
else:
print(f"{res.status_code}, Error Occured")
except Exception as e:
print(f"Image upload failed: {e}")
# print
p.set(font="a", height=2, align="center")
for img_t in img_saved:
color_coverted = cv2.cvtColor(img_t, cv2.COLOR_BGR2RGB)
# i = Image.fromarray(color_coverted)
# gd = ImageEnhance.Brightness(pil_image)
# i = gd.enhance(0.5)
try:
p.image(Image.fromarray(color_coverted), fragment_height=3)
except:
print("PRINT ERROR OCCURED")
pygame.time.delay(5000)
p = Usb(0x1fc9, 0x2016, in_ep=0x81, out_ep=0x01)
break
img_saved = []
# QR
footer = qr.get_with_logo(HOST_URL+"images/"+filename)
p.image(footer, fragment_height=3)
# finish
p.cut()
else:
img1 = font1.render(f'{timer // 10}', True, (255, 255, 255))
surface.blit(img1, (SCREEN_WIDTH // 2 -
125, SCREEN_HEIGHT // 2 - 125))
pygame.display.flip()
pygame.quit()