-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
191 lines (132 loc) · 5.44 KB
/
Server.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from PIL import Image
from collections import Counter
DirForFrame = "./Frames/"
HighResChars = False
def GetSound(Frame):
SoundFile = DirForFrame + "/sound.dfpwm"
file = open(SoundFile, "rb")
PosToRead = float(Frame) / 20
PosToRead = PosToRead * 6000
PosToRead = int(PosToRead)
file.seek(PosToRead)
print("reading sound at " + str(file.tell()))
Data = file.read(6000)
file.read()
return Data
def GetFrame(Frame):
FileToOpen = ""
#create blank binary string
FileToSend = ""
if HighResChars == True:
FileToSend = "H" + FileToSend
else:
FileToSend = "L" + FileToSend
MoniterXRes = 164
MoniterYRes = 81
if HighResChars == True:
MoniterXRes = MoniterXRes * 2
MoniterYRes = MoniterYRes * 3
print("image proccessing")
#finds frame name
FileToOpen = Frame
#for some reason ffmpeg makes sure its always atlest 4 number long
#so we add a 0 to the front of the number
for i in range(1,4):
if len(FileToOpen) < 4:
FileToOpen = "0" + FileToOpen
FileToOpen = "frame" + FileToOpen + ".png"
FileToOpen = DirForFrame + FileToOpen
im = Image.open(FileToOpen)
if not (im.size[0] < MoniterXRes or im.size[1] < MoniterYRes):
im = im.resize((MoniterXRes, MoniterYRes), Image.ANTIALIAS)
im = im.convert("P", palette=Image.ADAPTIVE, colors=16)
palette = im.getpalette()
print("getting colors")
ListOfColors = []
#convert list to hex codes
for i in range(0, 16):
#convert rgb into decimal
Value = (palette[(i * 3) + 2] + (palette[(i * 3) + 1] * 256) + (palette[(i * 3) + 0] * 65536) + 10000000)
ListOfColors.append(Value)
# r g b
#binary = bin(Value)[2:].zfill(25)
FileToSend = FileToSend + str(Value)
IntToChr = ["0" ,"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" ,"8" ,"9" ,"a" ,"b" ,"c" ,"d" ,"e" ,"f"]
FileToSend = FileToSend + " : "
print("doing loop")
#loop through all the pixels and find the corasponding hex code in the list then convert that code to binary
for y in range(0, MoniterYRes):
for x in range(0, MoniterXRes):
RunLoop = True
if HighResChars == True:
if x % 2 != 0:
#print("skipping because x:" + str(x % 2) + " :" + str(x))
RunLoop = False
if y % 3 != 0:
#print("skiping because y:" + str(y % 3) + ":" + str(y))
RunLoop = False
#print("not skipping")
if RunLoop == True:
PixelCode = 0
if x > im.size[0] - 1 or y > im.size[1] - 1:
PixelCode = 1
else:
PixelCode = im.getpixel((x, y))
if HighResChars:
#scans in a 2x3 row and gets the 2 most used colors then makes test using that
#get list of colors
colors = []
for ys in range(0, 3):
for xs in range(0, 2):
item = im.getpixel((x + xs, y + ys))
colors.append(IntToChr[item])
MostUsedColor = None
SecondMostUsedColor = None
data = Counter(colors)
#find the most used colors in the list
#print(colors)
MostUsedColor = data.most_common(1)[0][0]
if len(data.most_common(2)) > 1:
SecondMostUsedColor = data.most_common(2)[1][0]
else:
SecondMostUsedColor = MostUsedColor
TextCode = "."
FileToSend = FileToSend + TextCode + MostUsedColor + SecondMostUsedColor
else:
FileToSend = FileToSend + IntToChr[PixelCode]
print("sending data")
return FileToSend
#convert string that is 0 and 1s to proper binary
#print(FileToSend)
#start the websocket server using aiohttp
from aiohttp import web
import aiohttp
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
print("closed connection")
else:
if msg.data[0] == "F":
#remove f from the front of the string
Frame = msg.data[1:]
print("sending video data " + str(Frame))
FileToSend = GetFrame(Frame)
await ws.send_str(FileToSend)
elif msg.data[0] == "S":
#remove s from the front of the string
Sound = msg.data[1:]
print("sending sound data " + str(Sound))
FileToSend = GetSound(Sound)
await ws.send_bytes(FileToSend)
elif msg.type == aiohttp.WSMsgType.ERROR:
print('ws connection closed with exception %s' %
ws.exception())
print('websocket connection closed')
return ws
app = web.Application()
app.add_routes([web.get('/', websocket_handler)])
web.run_app(app)