-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
127 lines (112 loc) · 5.25 KB
/
main.py
File metadata and controls
127 lines (112 loc) · 5.25 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
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
import jetson.inference
import jetson.utils
import traceback
import pika
import time
import json
import os
from io import StringIO, BytesIO
import numpy as np
import base64
from datetime import datetime
import argparse
import sys
"""
File detectnetWork.py have the role to detect the car/truck in the given images
"""
class detectnetWork:
def __init__(self):
"""Initialize and read from json Config file
RabbitmqServer- Ip of the RabbitMqserver
RabbitmqQueueGet- Queue from where to get json
RabbitmqQueuePut- Queue for where to put json
thresh- threshold of the network ssdv2 """
with open('yolodetect.json') as json_file:
self.JsonConfig = json.load(json_file)
self.RabbitmqServer = self.JsonConfig['RabbitmqServer']
self.RabbitmqQueuePut = self.JsonConfig['RabbitmqQueuePut']
self.RabbitmqQueueGet = self.JsonConfig['RabbitmqQueueGet']
self.thresh = self.JsonConfig['thresh']
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=self.RabbitmqServer))
self.channel = self.connection.channel()
args = {"x-max-length": 200}
self.channel.queue_declare(queue=self.RabbitmqQueueGet, durable=True,arguments=args)
print("Load Weight")
self.LoadWeight()
self.LoadListDetection()
print("Weight Loaded")
self.channel.basic_qos(prefetch_count=1)
self.channel.basic_consume(queue=self.RabbitmqQueueGet, on_message_callback=self.callback)
self.channel.start_consuming()
def LoadWeight(self):
"""Load the weight for network."""
# load the object detection network
self.net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=self.thresh)
def LoadListDetection(self):
"""Load List of the possible detection that the network can do."""
with open('ssdv2classes.txt') as f:
self.listDetections=f.read().splitlines()
#self.listDetections = f.readlines()
def send_to_rabbit(self, load_json):
"""Send the detections with the old JSON to RabbitMQ queue."""
#print("send rabbit")
self.connectionSend = pika.BlockingConnection(pika.ConnectionParameters(host=self.RabbitmqServer))
self.channelSend = self.connectionSend.channel()
args = {"x-max-length": 200}
self.channelSend.queue_declare(queue=self.RabbitmqQueuePut, durable=True,arguments=args)
self.channelSend.basic_publish(exchange='', routing_key=self.RabbitmqQueuePut, body=load_json)
self.connectionSend.close()
def callback(self, ch, method, properties, body):
"""Wait for a json from getFromQueue.py to start processing """
try:
jsonload = json.loads(body)
img, width, height = jetson.utils.loadImageRGBA(jsonload["img"])
detections = self.net.Detect(img, width, height)
jsonObject={}
detectedforJson = list()
"""Create json for future training"""
for det in detections:
detectionjson = {}
detectionjson['type'] = self.listDetections[det.ClassID]
detectionjson['confidence'] = det.Confidence
detectionjson['Bottom'] = det.Bottom
detectionjson['Top'] = det.Top
detectionjson['Right'] = det.Right
detectionjson['Left'] = det.Left
detectionjson['Height'] = det.Height
detectionjson['Width'] = det.Width
detectedforJson.append(detectionjson)
jsonObject['detections']=detectedforJson
#jsonObject['rest'] = jsonload['rest']
fileNameJson=jsonload['img'][:-3]
fileNameJson+="json"
with open(fileNameJson, 'w', encoding='utf-8') as f:
json.dump(jsonObject, f, ensure_ascii=False, indent=4)
f.close()
detected=list()
#detectionjson = {}
#detectionjson['type'] = "truck"
#detectionjson['confidence'] = 0.75
#detected.append(detectionjson)
for det in detections:
"""Append new detections to old json"""
if self.listDetections[det.ClassID]=="car" or self.listDetections[det.ClassID]=="truck" \
or self.listDetections[det.ClassID]=="train" or self.listDetections[det.ClassID]=="bus":
detectionjson={}
detectionjson['type']=self.listDetections[det.ClassID]
detectionjson['confidence']=det.Confidence
detected.append(detectionjson)
jsonsend = {}
jsonsend["rest"] = jsonload
jsonsend["detections"] = detected
self.send_to_rabbit(json.dumps(jsonsend))
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception as e:
print(str(e))
with open('Log_DetectnetWork.txt', 'a') as the_file:
now = datetime.now() # current date and time
the_file.write(
now.strftime("%m/%d/%Y, %H:%M:%S") + " " + repr(e) + " " + traceback.format_exc() + "\n")
the_file.close()
ch.basic_ack(delivery_tag=method.delivery_tag)
aa=detectnetWork()