def main(_argv): logging.info('load cat modem') dog_1 = time.time() dog_model = cnn.get_inception_v2_dog() dog_2 = time.time() logging.info('dog model load in {:.2f}ms'.format((dog_2 - dog_1))) logging.info('Initialization connection at {}:{}'.format(FLAGS.ip, FLAGS.port)) time_c_1 = time.time() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((FLAGS.ip, FLAGS.port)) time_c_2 = time.time() logging.info('Connected to {}:{} in {:.3f}ms'.format(FLAGS.ip, FLAGS.port, (time_c_2 - time_c_1))) message = "0;RASP;127.0.0.1;DOG;" send_1 = time.time() s.send(message.encode('utf-8')) send_2 = time.time() logging.info('Send identification message {} in {}ms'.format(message, (send_2 - send_1))) while True: try: data = s.recv(3000000) message_parsed = parse_message_received(data.decode('utf-8')) logging.info( 'message {} size : {:.2f}Mb received'.format(message_parsed[0], (sys.getsizeof(data) / 1000000))) if len(message_parsed) == 4: logging.info('\t image {}'.format(message_parsed[0])) img = eval('np.array(' + message_parsed[2] + ')') process = ThreadCAT(message_parsed[0], img, dog_model, s) process.start() if data.decode('utf-8') == "close": break except (ConnectionResetError, ConnectionRefusedError): logging.info("Server close the connexion or not online") break # close the connection s.close()
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') logging.info('load cat model :') model_cat = cnn.get_inception_v2_cat() logging.info('\tcat model loaded') logging.info('load dog model :') model_dog = cnn.get_inception_v2_dog() logging.info('\t dog model 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) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) t3 = time.time() cnn_output = cnn.get_more_data(img, model_cat, model_dog, (boxes, scores, classes, nums), class_names) t4 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('primary detections:') for i in range(nums[0]): logging.info('\t{}, {:.2f}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]))) img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names, cnn_output) cv2.imwrite(FLAGS.output, img) logging.info('secondary detections:') for dec2 in cnn_output: logging.info('\t{} : {}'.format(dec2[1], dec2[2])) logging.info('output saved to: {}'.format(FLAGS.output)) cv2.imshow(FLAGS.output, img) cv2.waitKey(0)
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') logging.info('load cat model') model_cat = cnn.get_inception_v2_cat() logging.info('cat model loaded') logging.info('load dog model') model_dog = cnn.get_inception_v2_dog() logging.info('dog model loaded') times = [] times2 = [] try: vid = cv2.VideoCapture(int(FLAGS.video)) except: vid = cv2.VideoCapture(FLAGS.video) out = None 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) t1 = time.time() boxes, scores, classes, nums = yolo.predict(img_in) t2 = time.time() t3 = time.time() cnn_output = cnn.get_more_data(img, model_cat, model_dog, (boxes, scores, classes, nums), class_names) t4 = time.time() times.append(t2 - t1) times2.append(t4 - t3) times2 = times2[-20:] times = times[-20:] img, a, b = draw_outputs(img, model_cat, model_dog, (boxes, scores, classes, nums), class_names, cnn_output) img = cv2.putText( img, "Time prim: {:.2f}ms".format(sum(times) / len(times) * 1000), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), 2) img = cv2.putText( img, "Time sec : {:.2f}ms".format( (sum(times2) / len(times2)) * 1000), (0, 70), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 0), 2) if FLAGS.output: out.write(img) cv2.imshow('output', img) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows()