import numpy as np
import os
import argparse
import sys
from ksnn.api import KSNN
from ksnn.types import *
import cv2 as cv
import time
classes = [
"person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck",
"boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
"bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra",
"giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
"skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
"fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange",
"broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa",
"pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse",
"remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink",
"refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier",
"toothbrush"
]
def draw_boxes(frame, class_id, confidence, x, y, w, h, color):
cv.rectangle(frame, (x,y), (x+w,y+h), color, 2)
label = str(classes[class_id])
print(label)
cv.putText(frame, label, (x,y), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
def softmax(x):
return np.exp(x - np.max(x)) / np.sum(np.exp(x - np.max(x)))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--library", help="Path to C static library file")
parser.add_argument("--model", help="Path to nbg file")
parser.add_argument("--level", help="Information printer level: 0/1/2")
args = parser.parse_args()
if args.model :
if os.path.exists(args.model) == False:
sys.exit('Model \'{}\' not exist'.format(args.model))
model = args.model
else :
sys.exit("NBG file not found !!! Please use format: --model")
if args.library :
if os.path.exists(args.library) == False:
sys.exit('C static library \'{}\' not exist'.format(args.library))
library = args.library
else :
sys.exit("C static library not found !!! Please use format: --library")
if args.level == '1' or args.level == '2' :
level = int(args.level)
else :
level = 0
resnet50 = KSNN('VIM3')
print(' |--- KSNN Version: {} +---| '.format(resnet50.get_nn_version()))
print('Start init neural network ...')
resnet50.nn_init(library=library, model=model, level=level)
print('Done.')
cap = cv.VideoCapture(1)
assert cap.isOpened()
color = (0, 255, 0)
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv.resize(frame, (640, 480))
outputs = resnet50.nn_inference(frame[:, :, ::-1], platform='ONNX')
output = outputs[0]
#print(output)
output = np.reshape(output, (84, 8400))
class_probabilities = softmax(output[:80])
bounding_boxes = output[80:]
#print(bounding_boxes.shape)
for i in range(8400):
class_id = np.argmax(class_probabilities[:, i])
confidence = class_probabilities[class_id, i]
if confidence > 0.5:
x,y, w, h = bounding_boxes[:4, :i]
draw_boxes(frame, class_id, confidence, x, y, w, h, color)
print(x,y,w,h)
cv.imshow('Camera Stream', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
this is my code
I am unable to make sense of the outputs…