def get_packed_output(interpreter, score_threshold, top_k, image_scale=1.0): boxes = common.output_tensor(interpreter, 0) class_ids = common.output_tensor(interpreter, 1) scores = common.output_tensor(interpreter, 2) count = int(common.output_tensor(interpreter, 3)) top_k_scores_id = np.argpartition(np.array(scores), top_k)[-top_k:] top_k_scores = scores[top_k_scores_id] threshold_pass = top_k_scores_id[top_k_scores >= score_threshold] boxes, scores, class_ids = boxes[threshold_pass], scores[threshold_pass], class_ids[threshold_pass] return [boxes, scores, class_ids, count]
def get_output(interpreter, score_threshold, top_k, image_scale=1.0): """Returns list of detected objects.""" boxes = common.output_tensor(interpreter, 0) category_ids = common.output_tensor(interpreter, 1) scores = common.output_tensor(interpreter, 2) def make(i): ymin, xmin, ymax, xmax = boxes[i] return Object(id=int(category_ids[i]), score=scores[i], bbox=BBox(xmin=np.maximum(0.0, xmin), ymin=np.maximum(0.0, ymin), xmax=np.minimum(1.0, xmax), ymax=np.minimum(1.0, ymax))) return [make(i) for i in range(top_k) if scores[i] >= score_threshold]
def get_output(self, interpreter, score_threshold, top_k, image_scale=1.0): boxes = common.output_tensor(interpreter, 0) class_ids = common.output_tensor(interpreter, 1) scores = common.output_tensor(interpreter, 2) count = int(common.output_tensor(interpreter, 3)) def make(i): ymin, xmin, ymax, xmax = boxes[i] return Object(id=int(class_ids[i]), score=scores[i], bbox=BBox(xmin=np.maximum(0.0, xmin), ymin=np.maximum(0.0, ymin), xmax=np.minimum(1.0, xmax), ymax=np.minimum(1.0, ymax))) return [make(i) for i in range(top_k) if scores[i] >= score_threshold]
def get_detection_output(interpreter): threshold = .6 boxes = common.output_tensor(interpreter, 0) classes = common.output_tensor(interpreter, 1) scores = common.output_tensor(interpreter, 2) count = int(common.output_tensor(interpreter, 3)) hand_detections = [] for i in range(count): if scores[i] > threshold: hand_detections.append(boxes[i]) #print(hand_detections) #sys.stdout.flush() return hand_detections
def get_emotion(interpreter, top_k=1, score_threshold=0.1): scores = common.output_tensor(interpreter, 0) print(scores) categories = [ Category(i, scores[i]) for i in np.argpartition(scores, -top_k)[-top_k:] if scores[i] >= score_threshold ] return sorted(categories, key=operator.itemgetter(1), reverse=True)
def get_output(interpreter, top_k, score_threshold): """Returns no more than top_k categories with score >= score_threshold.""" scores = common.output_tensor(interpreter, 0) categories = [ Category(i, scores[i]) for i in np.argpartition(scores, -top_k)[-top_k:] if scores[i] >= score_threshold ] return sorted(categories, key=operator.itemgetter(1), reverse=True)
def get_output(interpreter, image_scale=1.0): """Returns list of detected objects.""" boxes = common.output_tensor(interpreter, 0) class_ids = common.output_tensor(interpreter, 1) scores = common.output_tensor(interpreter, 2) count = int(common.output_tensor(interpreter, 3)) def make(i): ymin, xmin, ymax, xmax = boxes[i] return Object(id=int(class_ids[i]), score=scores[i], bbox=BBox(xmin=np.maximum(0.0, xmin), ymin=np.maximum(0.0, ymin), xmax=np.minimum(1.0, xmax), ymax=np.minimum(1.0, ymax))) return [make(i) for i in range(len(scores)) if not np.isnan(class_ids[i])] #if scores[i] >= score_threshold]
def get_output_CAM(interpreter, dense_layer_weight, cam_w, cam_h): scaling = False CAM = common.output_tensor(interpreter, 0, dense_layer_weight, scaling=scaling) if scaling == True: pass else: CAM = cv2.resize(CAM, (cam_w, cam_h)) # 이거 한 번 하자고 cv2 부르니..? return CAM
def create_pose(model, points): common.set_input_pose(model, points) model.invoke() scores = common.output_tensor(model, 0) x = points[:, 0::2] y = points[:, 1::2] z_pred = np.array(scores) z_pred = np.reshape(z_pred, (-1, len(scores))) pose = np.stack((x, y, z_pred), axis=-1) pose = np.reshape(pose, (len(points), -1)) return pose
def gen_frames(): # generate frame by frame from camera #Current regression model is unstable, so we take running averages of sin, cos, and angle to stabilize the values moving_window = 10 runsin = np.zeros(moving_window) runcos = np.zeros(moving_window) runtheta = np.zeros(moving_window) while True: ret, frame = cap.read() if not ret: break cv2_im = frame cv2_im_rgb = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB) pil_im = Image.fromarray(cv2_im_rgb) common.set_input(interpreter, pil_im) interpreter.invoke() #Uncomment the following if you need to use bounding boxes or classification schemes and to use it for overlay #objs = get_output(interpreter, score_threshold=args.threshold, top_k=args.top_k) #cv2_im = append_objs_to_img(cv2_im, objs, labels) sincos = common.output_tensor(interpreter, 0) runsin = np.roll(runsin, 1) runsin[0] = sincos[0] runcos = np.roll(runcos, 1) runcos[0] = sincos[1] runtheta = np.roll(runtheta, 1) runtheta[0] = 180 / np.pi * np.arctan2(sincos[0], sincos[1]) cv2_im = cv2.putText(cv2_im, 'angle: {}'.format(np.average(runtheta)), (0, 480 - 90), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2) cv2_im = cv2.putText(cv2_im, 'sin: {}'.format(np.average(runsin)), (0, 480 - 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2) cv2_im = cv2.putText(cv2_im, 'cos: {}'.format(np.average(runcos)), (0, 480 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2) sd.putNumber( "WOF Angle", 180 / np.pi * np.arctan2(np.average(runsin), np.average(runcos))) ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n' ) # concat frame one by one and show result if cv2.waitKey(1) & 0xFF == ord('q'): break
def get_features(interpreter, patches): #patches je lista ndarray objekata, treba pretvoriti u tenzor istih dimenzija ili sliku po sliku ubacivat features = [] for patch in patches: if 0 in patch.shape: features.append(None) continue common.set_input(interpreter, Image.fromarray(patch)) interpreter.invoke() feature = common.output_tensor(interpreter, 0) features.append(feature) return features
def get_classification_output(interpreter): scores = common.output_tensor(interpreter, 0) #print(scores) #sys.stdout.flush() return scores