-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautonomous_sender.py
153 lines (121 loc) · 4.15 KB
/
autonomous_sender.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
"""
Sends PiCamera video from Rasperry Pi to Remote Server
for Object Detection Processing. On stop command,
stops car.
"""
import cv2
import numpy as np
import socket
import sys
import time
import _thread
import directions as car
from picamera.array import PiRGBArray
from picamera import PiCamera
# to speed things up, use smaller size frames
camera_width = 320
camera_height = 240
# IP address of server to send video to (MacBook Air)
HOST_IP = '10.251.46.150'
# Ports to send over
VIDEO_PORT = 8022
COMMAND_PORT = 8023
def recv_stop_command(connenction, clientsocket):
"""
Waits to receive stop command from server,
signals car to stop.
"""
# wait for message to start car
connenction.recv(1024)
print("Starting car...")
car.start()
# once message recieved again stop car
connenction.recv(1024)
print("Stopping car...")
car.stop()
# cleanup pins
car.cleanup()
# close client socket
clientsocket.close()
def send_video(protocol):
"""
Sends video to server over given protocol,
starts thread to receive stop command once
stop sign is detected.
"""
print("Initializing car...")
car.init()
# create socket for sending video
# get socket type based on protocol
socket_type = socket.SOCK_STREAM
if protocol == 'UDP':
socket_type = socket.SOCK_DGRAM
print("Creating %s socket..." % protocol)
clientsocket = socket.socket(socket.AF_INET, socket_type)
print("%s Socket created..." % protocol)
# if TCP protocol establish connection
if protocol == 'TCP':
print("Connecting to %s:%d..." % (HOST_IP, VIDEO_PORT))
while True:
try:
clientsocket.connect((HOST_IP, VIDEO_PORT))
break
except:
time.sleep(1)
print("Connected to %s:%d...\n\n" % (HOST_IP, VIDEO_PORT))
# initialize the camera and grab a reference to the raw camera capture
print("Initializing Camera...")
camera = PiCamera()
camera.resolution = (camera_width, camera_height)
camera.framerate = 32
camera.rotation = 180
rawCapture = PiRGBArray(camera, size=(camera_width, camera_height))
# allow the camera to warmup
time.sleep(1)
print("Camera ready...\n\n")
# initialize socket for receiving car commands
print("Creating socket to receive car commands...")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket for receiving car commands created...")
# bind to host
print("Binding to port %d..." % COMMAND_PORT)
sock.bind(('', COMMAND_PORT))
# listen and accept connection
sock.listen(1)
print("Socket for receiving car commands is listening...")
conn, (_, addr) = sock.accept()
print("Established connection with %s for receiving car commands\n\n" % addr)
_thread.start_new_thread(recv_stop_command, (conn, clientsocket))
time.sleep(3)
print("Beginning video...\n\n")
# continue to capture frames from camera and
# send to remote server till interrupt
for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
frame = image.array
# convert to string
data = frame.flatten().tostring()
# send data len then data to client
try:
if protocol == 'TCP':
clientsocket.sendall(data)
else:
pckt_sz = 11522
for i in range(20):
marker = str(i).zfill(2).encode('utf-8')
clientsocket.sendto(marker + data[pckt_sz*i:pckt_sz*(i+1)], (HOST_IP, VIDEO_PORT))
time.sleep(.01)
except:
print("Connection closed...")
break
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# close sockets
sock.close()
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] in ['tcp', 'udp', 'rdp']:
protocol = sys.argv[1].upper()
send_video(protocol)
else:
print("Error: Invalid argument")