-
-
Save mhanuel26/30baee6e4e0317ebeabf0f30ae3456b0 to your computer and use it in GitHub Desktop.
Code for sending commands from Kria to the Pet Feeder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Copyright 2020 Xilinx Inc. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
''' | |
from ctypes import * | |
from typing import List | |
import cv2 | |
import numpy as np | |
import vart | |
import os | |
import xir | |
import threading, queue | |
import time | |
import argparse | |
import socket | |
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'rtsp_transport;udp' | |
# define a video capture object | |
vid = cv2.VideoCapture("rtsp://tapoadmin:160816@192.168.1.16/stream1") | |
cv2.namedWindow("output", cv2.WINDOW_NORMAL) | |
width = int(vid.get(3)) # float `width` | |
height = int(vid.get(4)) # float `height` | |
print("width: ", width) | |
print("height: ", height) | |
# # define a video capture object | |
vid.set(cv2.CAP_PROP_BUFFERSIZE, 10) | |
FEEDMSG_open = "n\n" | |
FEEDMSG_close = "p\n" | |
UDP_IP = "192.168.1.13" | |
UDP_PORT = 2390 | |
divider = '------------------------------------' | |
def preprocess_fn(image_path, fix_scale): | |
''' | |
Image pre-processing. | |
Rearranges from BGR to RGB then normalizes to range 0:1 | |
and then scales by input quantization scaling factor | |
input arg: path of image file | |
return: numpy array | |
''' | |
image = cv2.imread(image_path) | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
image = image * (1/255.0) * fix_scale | |
image = image.astype(np.int8) | |
return image | |
def get_child_subgraph_dpu(graph: "Graph") -> List["Subgraph"]: | |
assert graph is not None, "'graph' should not be None." | |
root_subgraph = graph.get_root_subgraph() | |
assert (root_subgraph is not None), "Failed to get root subgraph of input Graph object." | |
if root_subgraph.is_leaf: | |
return [] | |
child_subgraphs = root_subgraph.toposort_child_subgraph() | |
assert child_subgraphs is not None and len(child_subgraphs) > 0 | |
return [ | |
cs | |
for cs in child_subgraphs | |
if cs.has_attr("device") and cs.get_attr("device").upper() == "DPU" | |
] | |
def runDPU(dpu,q): | |
FPSP = 1 / 5 | |
capture = time.time() | |
'''get tensor''' | |
inputTensors = dpu.get_input_tensors() | |
outputTensors = dpu.get_output_tensors() | |
input_fixpos = dpu.get_input_tensors()[0].get_attr("fix_point") | |
input_scale = 2**input_fixpos | |
input_ndim = tuple(inputTensors[0].dims) | |
output_ndim = tuple(outputTensors[0].dims) | |
# we can avoid output scaling if use argmax instead of softmax | |
#output_fixpos = outputTensors[0].get_attr("fix_point") | |
#output_scale = 1 / (2**output_fixpos) | |
outputData = [] | |
outputData.append([np.empty(output_ndim, dtype=np.int8, order="C")]) | |
inputData = [np.empty(input_ndim, dtype=np.int8, order="C")] | |
while True: | |
if time.time() - capture >= FPSP: | |
ret, frame = vid.read() | |
# feeder = frame[int(height / 2) - 100:height - 100, int(width / 2):width - 200] | |
# feeder = frame[int(height / 4) - 100:height - 100, int(width / 4)-200:width - 100] | |
image = cv2.resize(frame, (250, 200), interpolation=cv2.INTER_CUBIC) # Resize image | |
cv2.imshow('output', image) | |
# image = image * (1/255.0) * input_scale | |
image = image.astype(np.int8) | |
imageRun = inputData[0] | |
imageRun[0, ...] = image.reshape(input_ndim[1:]) | |
job_id = dpu.execute_async(inputData,outputData[0]) | |
dpu.wait(job_id) | |
index = 0 | |
pos = np.argmax(outputData[index][0][0]) | |
value = outputData[index][0][0][pos] | |
q.put_nowait((pos,value)) | |
capture = time.time() | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
def app(model): | |
g = xir.Graph.deserialize(model) | |
subgraphs = get_child_subgraph_dpu(g) | |
all_dpu_runners = [] | |
all_dpu_runners.append(vart.Runner.create_runner(subgraphs[0], "run")) | |
'''run a single thread ''' | |
# t1 = threading.Thread(target=runDPU, args=(i,start,all_dpu_runners[0], in_q)) | |
q = queue.Queue() | |
t1 = threading.Thread(target=runDPU, args=(all_dpu_runners[0],q)) | |
t1.start() | |
# t1.join() | |
print("loop to get values") | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP | |
classes = ['dog','cat'] | |
feederpos = False | |
feedTimer = time.time() | |
while True: | |
try: | |
res = q.get(timeout=2) # 3s timeout | |
except queue.Empty: | |
print("Image feed ended") | |
break | |
except KeyboardInterrupt: | |
break | |
q.task_done() | |
prediction = classes[res[0]] | |
threshold = res[1] | |
if threshold <= 15: | |
# print("not a dog or a cat, th=%d" % threshold) | |
continue | |
else: | |
print("classification as %s" % prediction) | |
print("has threshold: %s" % repr(threshold)) | |
if prediction == "dog": | |
continue | |
if feederpos == False: | |
feed_my_pet(sock, True) | |
feederpos = True | |
feedTimer = time.time() | |
else: | |
feedTimer = time.time() # reinitialize the feeder timer | |
if feederpos == True: | |
if time.time()-feedTimer >= 120.0: | |
feederpos = False | |
feed_my_pet(sock, False) | |
t1.join() | |
return | |
def feed_my_pet(sock, state): | |
if state is True: | |
print("Smart feeder is open for pets") | |
sock.sendto(bytes(FEEDMSG_open, "utf-8"), (UDP_IP, UDP_PORT)) | |
else: | |
print("Smart feeder is close for pets") | |
sock.sendto(bytes(FEEDMSG_open, "utf-8"), (UDP_IP, UDP_PORT)) | |
# only used if script is run as 'main' from command line | |
def main(): | |
# construct the argument parser and parse the arguments | |
ap = argparse.ArgumentParser() | |
ap.add_argument('-m', '--model', type=str, default='customcnn.xmodel', help='Path of xmodel. Default is customcnn.xmodel') | |
args = ap.parse_args() | |
print ('Command line options:') | |
print (' --model : ', args.model) | |
app(args.model) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment