x = x.reshape(-1) e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axis=0) def postprocess(scores): """This function takes the scores generated by the network and returns the class IDs in decreasing order of probability.""" prob = softmax(scores) prob = np.squeeze(prob) classes = np.argsort(prob)[::-1] return classes # Launch Zetane zcontext = ztn.Context().launch() zonnx = zcontext.model() zimg_input = zcontext.image() # provide the name of the model and width and height of the images for the variables model = directory + r'/emotion-ferplus-8.onnx' width = 64 height = 64 filepath = directory + r'/happy.jpg' preprocess_image = preprocess(filepath) x = np.array(preprocess_image).astype('float32') #np.save('testferemotions.npy', x) zimg_input.position(2, 7, 0).scale(.5, .5, .5).rotation(0, -1.57, 0).update( filepath=filepath) #.rotation(0, 0, -1.57).
img_path = os.path.join(dir_path, 'pic.jpg') color = (255, 255, 255) orig_image = cv2.imread(img_path) boxes, labels, probs = faceDetector(orig_image) box = scale(boxes[0, :]) cropped = cropImage(orig_image, box) age = ageClassifier(cropped) cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), color, 8) cv2.putText(orig_image, f'{age}', (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 6, color, 8, cv2.LINE_AA) output_path = os.path.join(dir_path, 'output.jpg') cv2.imwrite(output_path, orig_image) zcontext = ztn.Context() zcontext.launch() zcontext.clear_universe() zmodel = zcontext.model() zimg_1 = zcontext.image() zimg_2 = zcontext.image() image = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB) image = cv2.resize(image, (224, 224)) image = np.transpose(image, [2, 0, 1]) image = np.expand_dims(image, axis=0) image = image.astype(np.float32) zmodel.onnx(age_classifier_onnx).update() zmodel.inputs(inputs=image).update()
def __init__(self): self.context = ztn.Context() self.iimage = self.context.image().position(-10.0, 2.5, 0.0).scale( 0.25, 0.25, 0.0) self.vmodel = self.context.model() self.context.clear_universe()
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights) logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') times = [] try: vid = cv2.VideoCapture(int(FLAGS.video)) except: vid = cv2.VideoCapture(FLAGS.video) out = None ctxt = zetane.Context() ctxt.clear_universe() input_panel, output_panel = make_io_panels(ctxt) counter = 0 if FLAGS.output: # by default VideoCapture returns float instead of int width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(vid.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*FLAGS.output_format) out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height)) while True: _, img = vid.read() if img is None: logging.warning("Empty Frame") time.sleep(0.1) continue img_in = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_in = tf.expand_dims(img_in, 0) img_in = transform_images(img_in, FLAGS.size) image_np = np.transpose(img_in.numpy(), (1, 2, 3, 0)) if counter == 0: to_fit = 0.15 / image_np.shape[2] zinput = ctxt.image().data(image_np).scale(to_fit, to_fit).send_to(input_panel).update() zmodel = ctxt.model().keras(yolo).inputs(img_in.numpy()).update() else: zinput.data(image_np).update() zmodel.inputs(img_in.numpy()).update() t1 = time.time() #boxes, scores, classes, nums = yolo.predict(img_in) bbox, confidence, class_probs, scores = yolo(img_in) boxes, scores, classes, nums = tf.image.combined_non_max_suppression( boxes=tf.reshape(bbox, (tf.shape(bbox)[0], -1, 1, 4)), scores=tf.reshape( scores, (tf.shape(scores)[0], -1, tf.shape(scores)[-1])), max_output_size_per_class=FLAGS.yolo_max_boxes, max_total_size=FLAGS.yolo_max_boxes, iou_threshold=FLAGS.yolo_iou_threshold, score_threshold=FLAGS.yolo_score_threshold ) t2 = time.time() times.append(t2-t1) times = times[-20:] out_img = draw_outputs(img/255.0, (boxes, scores, classes, nums), class_names) out_img = cv2.putText(out_img, "Time: {:.2f}ms".format(sum(times)/len(times)*1000), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) if counter == 0: to_fit = 0.15 / out_img.shape[2] zoutput = ctxt.image().data(out_img).scale(to_fit, to_fit).send_to(output_panel).update() else: zoutput.data(out_img).debug(); #if FLAGS.output: # out.write(img) #cv2.imshow('output', img) counter += 1 if cv2.waitKey(1) == ord('q'): break ctxt.disconnect() cv2.destroyAllWindows()
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights).expect_partial() logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') if FLAGS.tfrecord: dataset = load_tfrecord_dataset( FLAGS.tfrecord, FLAGS.classes, FLAGS.size) dataset = dataset.shuffle(512) img_raw, _label = next(iter(dataset.take(1))) else: img_raw = tf.image.decode_image( open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) ctxt = zetane.Context() ctxt.clear_universe() input_panel, output_panel = make_io_panels(ctxt) image_np = np.transpose(img.numpy(), (1, 2, 3, 0)) to_fit = 0.15 / image_np.shape[2] zinput = ctxt.image().data(image_np).scale(to_fit, to_fit).send_to(input_panel).update() zmodel = ctxt.model().keras(yolo).inputs(img.numpy()).update() t1 = time.time() #boxes, scores, classes, nums = yolo(img) bbox, confidence, class_probs, scores = yolo(img) boxes, scores, classes, nums = tf.image.combined_non_max_suppression( boxes=tf.reshape(bbox, (tf.shape(bbox)[0], -1, 1, 4)), scores=tf.reshape( scores, (tf.shape(scores)[0], -1, tf.shape(scores)[-1])), max_output_size_per_class=FLAGS.yolo_max_boxes, max_total_size=FLAGS.yolo_max_boxes, iou_threshold=FLAGS.yolo_iou_threshold, score_threshold=FLAGS.yolo_score_threshold ) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('detections:') for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) #out_img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) out_img = draw_outputs(img_raw.numpy()/225.0, (boxes, scores, classes, nums), class_names) to_fit = 0.15 / out_img.shape[2] zoutput = ctxt.image().data(out_img).scale(to_fit, to_fit).send_to(output_panel).update() #cv2.imwrite(FLAGS.output, img) #logging.info('output saved to: {}'.format(FLAGS.output)) ctxt.disconnect()