-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassification.py
53 lines (35 loc) · 1.16 KB
/
Classification.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
import cv2
import asyncio
import websockets
import ast
import base64
import ImageNetConnector as imgNet
import json
import time
import jetson.inference
import jetson.utils
#initializing network
connector = imgNet.ImageNetConnector()
net = jetson.inference.imageNet("googlenet")
async def process(websocket,path):
while not websocket.closed:
async for message in websocket:
# splitting mime-type from base64
_, img_encoded = message.split(',')
img_decoded = base64.b64decode(img_encoded)
file_name = 'myImage.jpg'
with open (file_name , 'wb') as f:
f.write(img_decoded)
# loading image to CPU
image = jetson.utils.loadImage('myImage.jpg')
# running inference
class_desc, confidence, class_id = connector.RunInference(image,net)
# sending class name, confidence, and classID back to client
response = {"className":class_desc, "confidence":confidence, "classID": class_id}
response = json.dumps(response)
await websocket.send(response)
async def main ():
async with websockets.serve(process, "0.0.0.0", 4040, ping_interval = None):
await asyncio.Future()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())