-
Notifications
You must be signed in to change notification settings - Fork 2
/
Data_predict.py
228 lines (147 loc) · 5.37 KB
/
Data_predict.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import csv
import threading
import picamera
import spidev, time
import Adafruit_DHT as Adafruit_DHT # for dht11 sensor
import datetime as dt
import time
from multiprocessing import Process, Value
from mcp3208 import MCP3208
import numpy as np
DIR = '/home/pi/Get_data/'
from keras.utils import *
import tensorflow as tf
import numpy as np
from keras.models import model_from_json
import Data_load_4 as dl
modelname = "Model4_class2_i3d3_ep500"
modelname_json = "Models/" + modelname + ".json"
modelname_weight = "Models/" + modelname + ".h5"
json_file = open(modelname_json,"r")
loaded_model_json = json_file.read()
json_file.close()
model = tf.keras.models.model_from_json(loaded_model_json)
model.load_weights(modelname_weight)
print("Model Loaded...!")
def decision(predict):
print(predict)
if predict[0] > predict[1]:
return "None"
else:
return "Exist"
def initDHT():
isSuccess = False
DHT_HUMID_DATA.value, DHT_TEMP_DATA.value = Adafruit_DHT.read_retry(sensor, DHT_PIN)
if DHT_HUMID_DATA.value is not None and DHT_TEMP_DATA.value is not None:
isSuccess = True
else:
print("Failed to get reading from DHT sensor.")
return isSuccess
def readDHT():
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, DHT_PIN)
if humidity is not None and temperature is not None:
DHT_HUMID_DATA.value = humidity
DHT_TEMP_DATA.value = temperature
print("--------------------------Read !------------------------")
# time.delay(5)
def printDHT():
i = 1
while True:
print('Humidity : ', DHT_HUMID_DATA.value)
print('TEMP : ', DHT_TEMP_DATA.value)
print(i)
i = i + 1
DHT_TEMP_DATA = Value('d', -1.0)
DHT_HUMID_DATA = Value('d', -1.0)
sensor = Adafruit_DHT.DHT11
DHT_PIN = 21
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1000000
slice_time = 0.01
serial_num = 1
pir_channel = 0
light_channel = 5
test_channel = 2
print_text = ""
serial_text = ""
pir_data = list()
light_data = list()
temp_data = list()
humid_data = list()
one_sec = False
adc = MCP3208()
with picamera.PiCamera() as camera:
start_day = str(dt.datetime.now()).split(".")
start = start_day[0].replace(" ", "_")
start = start.replace(":", "-")
camera.resolution = (320, 240)
camera.framerate = 10
camera.annotate_text_size = 10
camera.start_preview()
camera.start_recording(DIR + "Video/Video_" + str(start) + ".h264")
camera.annotate_background = picamera.Color('black')
PIR_fn = "Data_PIR_" + str(start) + ".txt"
Light_fn = "Data_Light_" + str(start) + ".txt"
Temp_fn = "Data_Temp_" + str(start) + ".txt"
Humid_fn = "Data_Humid_" + str(start) + ".txt"
if not initDHT():
print('init Failed')
exit()
th1 = Process(target=readDHT)
th1.start()
start_time = time.time()
while (True):
try:
cur_time = time.time()
if cur_time - start_time >= slice_time:
start_time = cur_time
pir = adc.read(pir_channel)
light = adc.read(light_channel)
DHT_TEMP_DATA.value = round(DHT_TEMP_DATA.value, 2)
DHT_HUMID_DATA.value = round(DHT_HUMID_DATA.value, 2)
serial_num += 1
pir_data.append(pir)
light_data.append(light)
temp_data.append(DHT_TEMP_DATA)
humid_data.append(DHT_HUMID_DATA)
serial_text = "Serial_num :: " + str(serial_num)
one_sec = True
if serial_num % 100 == 0 and one_sec:
print_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S\n')
print_text += ("PIR :: %d %d %d %d %d %d %d \n" % (
pir_data[0], pir_data[1], pir_data[2], pir_data[3], pir_data[4], pir_data[5], pir_data[6]))
print_text += ("LIGHT :: %d %d %d %d %d %d %d \n" % (
light_data[0], light_data[1], light_data[2], light_data[3], light_data[4], light_data[5],
light_data[6]))
print_text += ("DHT :: TEMP = %.2f , Humidity %.2f \n" % (DHT_TEMP_DATA.value, DHT_HUMID_DATA.value))
data_pir = np.asarray(pir_data)
data_light = np.asarray(light_data)
data_temp = np.asarray(temp_data)
data_humid = np.asarray(humid_data)
pir_data = []
light_data = []
temp_data = []
humid_data = []
x_data = np.array((4,100))
x_data[0] = data_pir
x_data[1] = data_light
x_data[2] = data_temp
x_data[3] = data_humid
result = decision(model.predict(x_data))
if serial_num % 10 == 0:
camera.annotate_text = print_text + serial_text
'''
print("DHT :: TEMP = %f , Humidity %f" % (DHT_TEMP_DATA.value, DHT_HUMID_DATA.value))
print("PIR :: ADC = %s(%d) " % (hex(pir), pir))
print("LIGHT :: ADC = %s(%d) " % (hex(light), light))
print("Serial Number :: %d" % (serial_num))
print("")
'''
one_sec = False
except KeyboardInterrupt:
break
th1.terminate()
camera.stop_preview()
camera.close()