forked from Sanderi44/AR-Drone-Fire-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
videocontrol.py
75 lines (71 loc) · 1.75 KB
/
videocontrol.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
#!/usr/bin/env python
import cv2
import pygame
import numpy
import libardrone
running = True
flying = False
path = 'tcp://192.168.1.1:5555'
drone = libardrone.ARDrone()
drone.reset()
drone.speed = 0.5
pygame.init()
W, H = 640, 360
hud_color = (255, 0, 0)
screen = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()
stream = cv2.VideoCapture(path)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
drone.land()
drone.reset()
elif event.key == pygame.K_SPACE:
if flying == False:
drone.takeoff()
flying = True
else:
drone.land()
flying = False
elif event.key == pygame.K_w:
drone.move_forward()
elif event.key == pygame.K_s:
drone.move_backward()
elif event.key == pygame.K_a:
drone.move_left()
elif event.key == pygame.K_d:
drone.move_right()
elif event.key == pygame.K_RIGHT:
drone.turn_right()
elif event.key == pygame.K_LEFT:
drone.turn_left()
elif event.key == pygame.K_UP:
drone.move_up()
elif event.key == pygame.K_DOWN:
drone.move_down()
if running == False:
print "Shut Down"
pygame.quit()
drone.halt()
break
try:
buff = stream.grab()
imageyuv = stream.retrieve(buff)
imagergb = cv2.cvtColor(imageyuv[1],cv2.COLOR_BGR2RGB)
surface = pygame.image.frombuffer(imagergb, (W, H), 'RGB')
bat = drone.navdata.get('battery', 0)
screen.blit(surface, (0, 0))
pygame.display.flip()
clock.tick(50)
pygame.display.set_caption("FPS: %.2f" % clock.get_fps())
f = pygame.font.Font(None, 20)
hud = f.render('Battery: %i%%' % bat, True, hud_color)
print bat
screen.blit(hud,(10,10))
except:
print "Missed Frame"
pass
pygame.quit()