forked from AabidPatel/Vision-Based-Maze-Solving-Robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
118 lines (92 loc) · 3.71 KB
/
player.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
from vis_nav_game import Player, Action
import pygame
import cv2
class KeyboardPlayerPyGame(Player):
def __init__(self):
self.fpv = None
self.last_act = Action.IDLE
self.screen = None
self.keymap = None
super(KeyboardPlayerPyGame, self).__init__()
def reset(self):
self.fpv = None
self.last_act = Action.IDLE
self.screen = None
pygame.init()
self.keymap = {
pygame.K_LEFT: Action.LEFT,
pygame.K_RIGHT: Action.RIGHT,
pygame.K_UP: Action.FORWARD,
pygame.K_DOWN: Action.BACKWARD,
pygame.K_SPACE: Action.CHECKIN,
pygame.K_ESCAPE: Action.QUIT,
}
def act(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
self.last_act = Action.QUIT
return Action.QUIT
if event.type == pygame.KEYDOWN:
if event.key in self.keymap:
self.last_act |= self.keymap[event.key]
else:
self.show_target_images()
if event.type == pygame.KEYUP:
if event.key in self.keymap:
self.last_act ^= self.keymap[event.key]
return self.last_act
def show_target_images(self):
targets = self.get_target_images()
if targets is None or len(targets) <= 0:
return
hor1 = cv2.hconcat(targets[:2])
hor2 = cv2.hconcat(targets[2:])
concat_img = cv2.vconcat([hor1, hor2])
w, h = concat_img.shape[:2]
color = (0, 0, 0)
concat_img = cv2.line(concat_img, (int(h / 2), 0), (int(h / 2), w), color, 2)
concat_img = cv2.line(concat_img, (0, int(w / 2)), (h, int(w / 2)), color, 2)
w_offset = 25
h_offset = 10
font = cv2.FONT_HERSHEY_SIMPLEX
line = cv2.LINE_AA
size = 0.75
stroke = 1
cv2.putText(concat_img,"Front View",(h_offset, w_offset),font,size,color,stroke,line,)
cv2.putText(concat_img,"Right View",(int(h / 2) + h_offset, w_offset),font,size,color,stroke,line,)
cv2.putText(concat_img,"Back View",(h_offset, int(w / 2) + w_offset),font,size,color,stroke,line,)
cv2.putText(concat_img,"Left View",(int(h / 2) + h_offset, int(w / 2) + w_offset),font,size,color,stroke,line,)
cv2.imshow(f"KeyboardPlayer:target_images", concat_img)
cv2.waitKey(1)
def set_target_images(self, images):
super(KeyboardPlayerPyGame, self).set_target_images(images)
self.show_target_images()
def get_k(self):
k = self.get_camera_intrinsic_matrix()
return k
def see(self, fpv):
if fpv is None or len(fpv.shape) < 3:
return
self.fpv = fpv
if self.screen is None:
h, w, _ = fpv.shape
self.screen = pygame.display.set_mode((w, h))
def convert_opencv_img_to_pygame(opencv_image):
"""
Convert OpenCV images for Pygame.
see https://blanktar.jp/blog/2016/01/pygame-draw-opencv-image.html
"""
opencv_image = opencv_image[:, :, ::-1] # BGR->RGB
# (height,width,Number of colors) -> (width, height)
shape = opencv_image.shape[1::-1]
pygame_image = pygame.image.frombuffer(opencv_image.tobytes(), shape, "RGB")
return pygame_image
pygame.display.set_caption("KeyboardPlayer:fpv")
rgb = convert_opencv_img_to_pygame(fpv)
self.screen.blit(rgb, (0, 0))
pygame.display.update()
#K = self.get_k()
if __name__ == "__main__":
import vis_nav_game
vis_nav_game.play(the_player=KeyboardPlayerPyGame())