-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketServer.py
134 lines (109 loc) · 3.74 KB
/
socketServer.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
from picamera import PiCamera
import time
import sys
sys.path.append('/home/pi/opnecv-3.0.0/include')
import SocketServer
import cv2
import picamera.array
import numpy as np
import io
import main
#camera = PiCamera()
#camera.start_preview()
time.sleep(2)
vision = main.VisionTargeting(0.1)
def getTarget():
return vision.Loop()
def shutdownPI():
command = "/usr/bin/sudo /sbin/shutdown -h now"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
return 'we did it';
def captureImgInMemory():
# with picamera.array.PiRGBArray(camera) as stream:
#camera.resolution = (640, 480)
# At this point the image is available as stream.array
#image = stream.array
#availableimage2 = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# cv2.imshow('image2',image2)
my_stream = io.BytesIO()
# Camera warm-up time
camera.capture(my_stream, 'jpeg')
print my_stream.__sizeof__()
data = np.fromstring(my_stream.getvalue(), dtype=np.uint8)
dataIMG = cv2.imdecode(data, 1)
cv2.imwrite('gggg2.jpg', dataIMG)
#img = cv2.imread(my_stream,'jpg')
#img = imread_from_blob(my_stream, 'jpg')
#cv2.imshow('Image', img)
return my_stream
def makeGray():
img = cv2.imread('my_image.jpg')
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray_image.jpg', gray_image)
return "Image is gray"
def launchCameraFile():
my_file = open('my_image.jpg', 'wb')
#camera.start_preview()
#time.sleep(1)
camera.capture(my_file)
my_file.close()
def t():
print "ttt test"
def cameraStuff():
launchCameraFile()
return "camera"
def ultraSonic():
return "ultraSound"
def switch_case(argument):
switcher = {
0: cameraStuff,
1: ultraSonic,
2: lambda: "two",
3: makeGray,
4: captureImgInMemory,
5: shutdownPI,
8: getTarget
}
# get function from swither
func = switcher.get(argument, lambda: "nothing")
return func()
class MyUDPHandler(SocketServer.BaseRequestHandler):
"""
This class works similar to the TCP handler class, except that
self.request consists of a pair of data and client socket, and since
there is no connection the client address must be given explicitly
when sending data back via sendto().
"""
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print "{} wrote:".format(self.client_address[0])
x = data[0:3]
print x
if int(x) == 1:
print "We have the number 1"
if int(x) == 0:
print "We have the number 0"
print switch_case(0)
print switch_case(3)
if int(x) == 4:
capImage = switch_case(4)
print "Image captured in memory"
if int(x) == 5:
print switch_case(5)
#targeting
if int(x) == 8:
print("get target data")
socket.sendto(switch_case(8), self.client_address)
# print data[0]
# print switch_case(1)
# if data[0] == 'k':
# print "We did it \n"
# socket.sendto(data.upper(), self.client_address)
if __name__ == "__main__":
#captureImgInMemory()
HOST, PORT = "10.0.20.9", 9999
server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
server.serve_forever()