forked from karauri14/safetyplus-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
107 lines (84 loc) · 3.4 KB
/
ui.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
import cv2
import numpy as np
import pygame.mixer
import time
import threading
MAX_WIDTH = 1824
MAX_HEIGHT = 984
SIGN_HEIGHT = (int)(MAX_HEIGHT / 3)
SIGN_WIDTH = SIGN_HEIGHT
GUIDE_WIDTH = MAX_WIDTH - SIGN_WIDTH
GUIDE_HEIGHT = MAX_HEIGHT
TEXT_WIDTH = GUIDE_WIDTH
TEXT_HEIGHT = MAX_HEIGHT - 98
LIST_MARGIN = 123
LIST_HEIGHT = 190
LIST_WIDTH = 495
def soundPlay():
pygame.mixer.music.play(1)
time.sleep(10)
pygame.mixer.music.stop()
def windowInit(lang):
#cv2.namedWindow('drive', cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('drive', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('drive', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
pygame.init()
pygame.mixer.music.load(lang + '/voice/turn.mp3')
images = {}
images['REFUEL'] = cv2.imread(lang + '/img/refuel.png') #full size
images['LEFT'] = cv2.imread('./img/turn_left.png') #main size
images['RIGHT'] = cv2.imread('./img/turn_right.png') #main size
images['STOP'] = cv2.imread(lang + '/img/stop.png') #sign size
images['SLOW'] = cv2.imread(lang + '/img/slow.png') #sign size
images['OVER'] = cv2.imread(lang + '/img/overtaking.png') #sign size
images['ATTENTION'] = cv2.imread(lang + '/img/attention.png') #text size
images['DOOR'] = cv2.imread(lang + '/img/door.png') #full size
images['BACK'] = cv2.imread(lang + '/img/back.png') #full size
images['DANGER'] = cv2.imread(lang + '/img/danger.png') #text size
return images
def pastePicture(background, src, x, y):
row, col, channel = src.shape
graySrc = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(graySrc, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
roi = background[0 + y : row + y, 0 + x : col + x]
bg = cv2.bitwise_and(roi, roi, mask = mask_inv)
srcFg = cv2.bitwise_and(src, src, mask = mask)
dst = cv2.add(bg, srcFg)
background[0 + y : row + y, 0 + x : col + x] = dst
def makeWindow(base, images, stateString):
turn, text, stop, slow, over = stateString.split(',')
#main area
if turn == 'left':
pastePicture(base, images['LEFT'], SIGN_WIDTH, 0)
soundThread = threading.Thread(target = soundPlay)
soundThread.start()
elif turn == 'right':
pastePicture(base, images['RIGHT'], SIGN_WIDTH, 0)
soundThread = threading.Thread(target = soundPlay)
soundThread.start()
#text area
if text == 'danger':
pastePicture(base, images['DANGER'], SIGN_WIDTH, TEXT_HEIGHT)
else :
pastePicture(base, images['ATTENTION'], SIGN_WIDTH, TEXT_HEIGHT)
#sign area
if stop == 'stop':
pastePicture(base, images['STOP'], 0, 0)
if over == 'over':
pastePicture(base, images['OVER'], 0, SIGN_HEIGHT)
if slow == 'slow':
pastePicture(base, images['SLOW'], 0, SIGN_HEIGHT * 2)
cv2.imshow('drive', base)
def langSelectWindow(bgOrigin, frame, lang):
bg = bgOrigin.copy()
if lang == 'en':
pastePicture(bg, frame, LIST_WIDTH, LIST_MARGIN + LIST_HEIGHT * 0)
elif lang == 'zh-tw':
pastePicture(bg, frame, LIST_WIDTH, LIST_MARGIN + LIST_HEIGHT * 1)
elif lang == 'zh-cn':
pastePicture(bg, frame, LIST_WIDTH, LIST_MARGIN + LIST_HEIGHT * 2)
elif lang == 'ko':
pastePicture(bg, frame, LIST_WIDTH, LIST_MARGIN + LIST_HEIGHT * 3)
cv2.imshow('drive', bg)
cv2.waitKey(1)