-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimato.py
153 lines (130 loc) · 4.15 KB
/
estimato.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
from google.cloud import vision
from fuzzywuzzy import process
from fuzzywuzzy import fuzz
from picamera import PiCamera
from hx711 import HX711
import RPi.GPIO as GPIO
import time, json, sys, math, io
import paho.mqtt.client as paho
import google.auth
list_of_vegetables = ["onion", "tomato", "bell pepper", "cucumber", "lemon", "herb"]
FUZZY_THRESHOLD = 60
def findMostProbableVegetable(labels):
for label in labels:
#print label.description," ",label.score
extractedVegetable = process.extractOne(label.description, list_of_vegetables, scorer=fuzz.token_sort_ratio)
if (extractedVegetable[1] > FUZZY_THRESHOLD):
return extractedVegetable[0]
return "None"
deviceID = "CART__0_40"
appID = "CART_0"
#DOUT, SCK
hx = HX711(23, 24)
broker = sys.argv[1]
topic = "estimato/" + deviceID + "/item"
GPIO.setup(4, GPIO.OUT)
camera = PiCamera()
camera.start_preview()
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
def on_publish(client, userdata, mid):
print("published : "+str(mid))
def cleanAndExit():
print "Cleaning..."
GPIO.cleanup()
client.loop_stop()
client.disconnect()
print "Bye!"
sys.exit()
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_message(client, userdata, msg):
jsonMsg = json.loads(str(msg.payload))
#print(msg.payload)
class Payload:
deviceID = ""
appID = ""
weight = 0
label = ""
def __init__(self, deviceID, appID):
self.deviceID = deviceID
self.appID = appID
def setWeight(self,weight):
self.weight = weight
def setLabel(self,label):
self.label = label
def allEqual(weightBuffer):
average = sum(weightBuffer)/len(weightBuffer)
for weight in weightBuffer:
if (math.fabs(weight - average) > 0.05 * average and math.fabs(weight - average) > 5):
return 0
return 1
client = paho.Client(client_id="pi_device_1")
client.on_publish = on_publish
client.on_connect = on_connect
client.on_subscribe = on_subscribe
client.on_message = on_message
packet = Payload(deviceID,appID)
client.connect(broker, 1883)
client.loop_start()
hx.set_reading_format("LSB", "MSB")
hx.set_reference_unit(92)
hx.reset()
hx.tare()
credentials, project = google.auth.default()
"""Detects labels in the file."""
vision_client = vision.Client(credentials=credentials)
weightBuffer = [0] * 10
oldWeight = 0
newWeight = 0
weightChangeThreshold = 10
def getLabel(image_path):
with io.open(image_path, 'rb') as image_file:
content = image_file.read()
image = vision_client.image(content=content)
labels = image.detect_labels()
return findMostProbableVegetable(labels)
while True:
try:
val = -1 * hx.get_weight(5)
print "Sensor reading : " + str(val)
hx.power_down()
hx.power_up()
weightBuffer.append(val)
weightBuffer.pop(0)
print weightBuffer
if (allEqual(weightBuffer) == 0):
print "Weight unstable"
GPIO.output(4, GPIO.LOW)
client.publish(topic, "wait", qos=0)
elif (allEqual(weightBuffer) != 0):
print "Weight stable. Current Weight : " + str(oldWeight)
weightChange = math.fabs(val - oldWeight)
print "weight change " + str(weightChange)
client.publish(topic, "ready", qos=0)
if (weightChange > weightChangeThreshold):
print "Weight change more than threshold"
if (val > oldWeight):
print "Weight increased"
name = str(time.time()).split('.')[0]
image_path = '/home/pi/Desktop/estimatoDevice/images/image_' + name + '.jpg'
camera.capture(image_path)
label = getLabel(image_path)
print label
oldWeight = val
packet.setWeight(val)
packet.setLabel(label)
client.publish(topic, json.dumps(packet.__dict__), qos=0)
else:
print "Weight decreased"
hx.tare()
oldWeight = 0
GPIO.output(4, GPIO.HIGH)
else:
GPIO.output(4, GPIO.LOW)
time.sleep(0.1)
except (KeyboardInterrupt, SystemExit):
cleanAndExit()