-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileSocketServer.py
More file actions
78 lines (61 loc) · 2.34 KB
/
Copy pathfileSocketServer.py
File metadata and controls
78 lines (61 loc) · 2.34 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 asyncio
import os
import garbageIdentifier
from websockets.server import serve
import socket
hostname = socket.getfqdn()
print("IP Address:", socket.gethostbyname_ex(hostname)[2][0])
ip = hostname
model = "possiblyTheBestModel.h5"
garbageIdentifier.guess(model, "spoon.jpg")
async def test(websocket):
print("Client Found")
while True:
# on every interaction with the garbage bin, the server will receive a label and data
print("Waiting for label...")
label = await websocket.recv()
print("Received label:", label)
print("Waiting for data...")
data = await websocket.recv()
print("Data received!")
# Auto Mode
if label == "auto":
# Write the image to a file
print("Capturing image...")
with open("espcapture.jpg", "wb") as f:
f.write(data)
print("Image captured!")
# Guess the image and send result to client
print("Identifying image...")
guess = garbageIdentifier.guess(model, "espcapture.jpg")
await websocket.send(guess) # MAYBE REMOVE AWAIT?
print("Identification successful. Sent message:", guess)
# Manual Mode
elif label == "blue" or label == "black" or label == "green":
if data == "timeout":
print("Item undetected. Waiting timed out.")
else:
# Write a new image to the dataset based on the label
# Determine target directory
targetFolder = "garbage_dataset_custom/" + label
# Find unique filename
i = 0
while os.path.exists(f"{targetFolder}{'/'}{label}{i}.jpg"):
i += 1
# Save image to dataset
print("Saving image to dataset...")
filename = "{}/{}{}.jpg".format(targetFolder, label, i)
with open(filename, "wb") as f:
f.write(data)
print("Image saved to", filename)
# Print mode for debugging
elif label == "print":
print(data)
else:
print("Label or data invalid.")
print()
async def main():
async with serve(test, ip, 8080):
print("Server started")
await asyncio.Future() # run forever
asyncio.run(main())