-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo.py
More file actions
78 lines (66 loc) · 2.14 KB
/
video.py
File metadata and controls
78 lines (66 loc) · 2.14 KB
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
import time
import cv2
from threading import Thread, RLock
import socket
import numpy
class Video():
def __init__(self, telloInstance=None):
self.isStreamOn = False
self.lock = RLock()
self.tello = telloInstance
def videoStreamSetup(self):
# Setup UDP socket for video
self.video_port = 1111
self.video_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.video_socket.bind(('', self.video_port))
# Start thread to receive frame streamed over UDP socket
self.receive_video_thread = Thread(target=self.get_video, daemon=True)
self.receive_video_thread.start()
def setTello(self, telloInstance):
self.tello = telloInstance
def streamOn(self):
if self.tello is None:
print('Not Connected to Drone')
return False
print("Turn On Video Stream")
if not self.isStreamOn:
self.tello.send("streamon", 1)
self.isStreamOn = True
self.videoStreamSetup()
else:
print('already start streaming')
return True
def streamOff(self):
print("Turn Off Video Stream")
if self.isStreamOn:
# self.receive_video_thread.join()
self.tello.send("streamoff", 1)
self.isStreamOn = False
else:
print('already off streaming')
def get_video(self):
s = ""
while self.isStreamOn:
print('Getting updated view')
# with self.lock:
data, ip = self.video_socket.recvfrom(2048)
s += data
if len(s) == (2048*20):
self.frame = numpy.fromstring(s, dtype=numpy.uint8)
self.frame = self.frame.reshape(480, 640, 3)
# cv2.imshow("frame",frame)
s = ""
# *
# Return the current frame
# *
def get_frame(self):
return self.frame
# # *
# # Test Driver Code
# # *
# if __name__ == "__main__":
# tello = Tello()
# video_handler = Video(telloInstance=tello)
# video_handler.streamOn()
# video_handler.get_video()
# video_handler.streamOff()