-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjetson_camera.py
More file actions
222 lines (160 loc) · 5.69 KB
/
jetson_camera.py
File metadata and controls
222 lines (160 loc) · 5.69 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
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
# MIT License
# Copyright (c) 2019 JetsonHacks
# See license
# Using a CSI camera (such as the Raspberry Pi Version 2) connectedto a
# NVIDIA Jetson Nano Developer Kit using OpenCV
# Drivers for the camera and OpenCV are included in the base image
from datetime import date
from model import predict_one_image, setup
import cv2
import os
import uuid
import imgcompare
import numpy as np
import base64
import time
# import serial
# from serial import Serial
from PIL import Image
import requests
# gstreamer_pipeline returns a GStreamer pipeline for capturingfrom the CSI camera
# Defaults to 1280x720 @ 60fps
# Flip the image by setting the flip_method (most common values: 0and 2)
# display_width and display_height determine the size of the windowon the screen
_DELAY_ = 30
_CLASS_ = 'class_3/'
_IMAGE_DIR_PATH_ = 'dataset/'+_CLASS_
tensorflow_model_path = 'assets/Tensorflow_model'
# arduino = serial.Serial(port='/dev/ttyACM0', baudrate=115200, timeout=.1)
def gstreamer_pipeline(
capture_width=1280,
capture_height=720,
display_width=640,
display_height=640,
framerate=50,
flip_method=1,
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d,format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
def do_somthing():
return
def filter(current_image, past_image):
""" 1st TRY"""
# # --- take the absolute difference of the images ---
# res = cv2.absdiff(current_image, past_image)
# # --- convert the result to integer type ---
# res = res.astype(np.uint8)
# # --- find percentage difference based on number of pixels that are not zero ---
# percentage = (np.count_nonzero(res) * 100) / res.size
""" 2nd TRY """
current_image = Image.fromarray(current_image, 'RGB')
past_image = Image.fromarray(past_image, 'RGB')
# --- take the absolute difference of the images ---
percentage = imgcompare.image_diff_percent(current_image, past_image)
print(percentage)
return percentage < 16
def save_image(image):
filename = str(uuid.uuid4())
dir = os.path.abspath(os.getcwd())
path = os.path.join(_IMAGE_DIR_PATH_ + filename + ".png")
cv2.imwrite(path, image)
def send_frame(img):
print('send frame next')
encoded_image = base64.b64encode(img)
try:
requests.post(
"http://127.0.0.1:5000/send_frame", data=encoded_image, timeout=5)
except Exception as e:
print(e)
def show_camera():
# select Stream to collect from
# cap = cv2.VideoCapture(gstreamer_pipeline(
# flip_method=0), cv2.CAP_GSTREAMER)
cap = cv2.VideoCapture(0)
if cap.isOpened():
# Init past image
ret_val, past_image = cap.read()
prediction_results = []
# Start video & exit on window close
while True:
# check if model enabled
res = {'alive': True, 'terminate': False}
try:
res = requests.get('http://127.0.0.1:5000/is_alive').json()
except Exception as e:
print(e)
if res['terminate']:
return
if not res['alive']:
cv2.waitKey(_DELAY_)
continue
# get image
ret_val, img = cap.read()
print('send frame next')
# send frame to server
send_frame(img)
detected_crop = filter(img, past_image)
# first scenario : crop detected
if(detected_crop):
# Pass image through model
frame_prediction_result = predict_one_image(img)
prediction_results.append(frame_prediction_result[0])
print('detected')
elif(not detected_crop and len(prediction_results) > 0):
midian_prediction = []
for i in range(len(prediction_results[0])):
midian = 0
for p in range(len(prediction_results)):
midian = midian + prediction_results[p][i]
midian = midian / len(prediction_results[0])
midian_prediction.append(midian)
print('-------------------------------------')
print('-------------------------------------')
print('Final result')
print(midian_prediction)
print('-------------------------------------')
print('-------------------------------------')
try:
requests.post('http://127.0.0.1:5000/send_anomaly',
json={"prediction": midian_prediction, "time": time.time()}, timeout=5)
except Exception as e:
print(e)
break
""" INJECT YOUR CODE HERE """
# move jetson motors
do_somthing()
""" END INJECTION """
prediction_results.clear()
cv2.waitKey(_DELAY_)
# Clear objects
cap.release()
else:
print("Unable to open camera")
def main():
# Create folders
try:
# os.mkdir('dataset')
os.mkdir(_IMAGE_DIR_PATH_)
except:
print("Already exist")
setup(tensorflow_model_path)
show_camera()
return True
# if __name__ == "__main__":
# main()