def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', help='File path of Tflite model.', required=True) parser.add_argument('--image', help='File path of file.', required=True) args = parser.parse_args() label_names = [ 'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] img = Image.open(args.image) #np_image = np.array(img) # Load Engine engine = ClassificationEngine(args.model) lap_time = time.time() # Run inference. for result in engine.ClassifyWithImage(img, top_k=3): print('---------------------------') print(label_names[result[0]]) print('Score : ', result[1]) previous_time = lap_time lap_time = time.time() print("Elapsed time for the last inference: ", lap_time - previous_time)
def upload_file(): coralReply = '' coralPercent = '' form = UploadForm() if form.validate_on_submit(): filename = photos.save(form.photo.data) file_url = photos.url(filename) imageSpot = os.path.basename(file_url) # Prepare labels. labels = ReadLabelFile(myLabel) # Initialize engine. engine = ClassificationEngine(myModel) # Run inference. img = Image.open('myPhotos/' + imageSpot) for result in engine.ClassifyWithImage(img, top_k=3): print('---------------------------') print(labels[result[0]]) print('Score : ', result[1]) showResult = labels[result[0]] showPercent = str(int(round(result[1] * 100))) coralReply = showResult coralPercent = showPercent + '% confidence' else: file_url = None coralReply = '' coralPercent = '' return render_template('index.html', form=form, file_url=file_url, coralReply=coralReply, coralPercent=coralPercent)
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', help='TensorFlow Liteモデルのファイルパス', required=True) parser.add_argument( '--label', help='ラベルファイルのファイルパス', required=True) parser.add_argument( '--npz', help='mnist.npzのファイルパス', required=True) parser.add_argument( '--print', help='結果を標準出力へ出力するか否か', type=bool,default=False, required=False) args = parser.parse_args() # ラベルの読み込み labels = ReadLabelFile(args.label) # モデルを読込、エンジンを初期化する engine = ClassificationEngine(args.model) count = 0 # mnist.npzを読込読み込んだ分、画像分類を実行する for input_tensor in LoadData(args.npz): results = engine.ClassifyWithInputTensor( input_tensor=input_tensor.flatten(), threshold=0.1, top_k=3) # 出力オプションが真だった場合は結果を出力する if(args.print): print('-----------{0:6}----------'.format(count)) for result in results: print(labels[result[0]], end="") print(' Score : ', result[1]) count += 1
def test_train_all(self): for model_path in self._MODEL_LIST: with self.subTest(): with test_utils.TemporaryFile( suffix='.tflite') as output_model_path: data_dir = test_utils.test_data_path('imprinting') engine = ImprintingEngine(model_path, keep_classes=False) image_shape = self._get_image_shape(model_path) # train. train_set = [['cat_train_0.bmp'], ['dog_train_0.bmp'], ['hotdog_train_0.bmp', 'hotdog_train_1.bmp']] train_input = [ (test_utils.prepare_images(image_list, data_dir, image_shape)) for image_list in train_set ] engine.train_all(train_input) engine.save_model(output_model_path.name) # Test. engine = ClassificationEngine(output_model_path.name) self.assertEqual(1, engine.get_num_of_output_tensors()) self.assertEqual(3, engine.get_output_tensor_size(0)) label_to_id_map = {'cat': 0, 'dog': 1, 'hot_dog': 2} self._classify_image(engine, data_dir, 'cat_test_0.bmp', label_to_id_map['cat'], 0.99) self._classify_image(engine, data_dir, 'dog_test_0.bmp', label_to_id_map['dog'], 0.99) self._classify_image(engine, data_dir, 'hotdog_test_0.bmp', label_to_id_map['hot_dog'], 0.99)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', help='File path of Tflite model.', required=True) parser.add_argument('--label', help='File path of label file.', required=True) args = parser.parse_args() labels = dataset_utils.ReadLabelFile(args.label) engine = ClassificationEngine(args.model) with picamera.PiCamera() as camera: camera.resolution = (640, 480) camera.framerate = 30 _, height, width, _ = engine.get_input_tensor_shape() camera.start_preview() try: stream = io.BytesIO() for _ in camera.capture_continuous(stream, format='rgb', use_video_port=True, resize=(width, height)): stream.truncate() stream.seek(0) input_tensor = np.frombuffer(stream.getvalue(), dtype=np.uint8) start_ms = time.time() results = engine.ClassifyWithInputTensor(input_tensor, top_k=1) elapsed_ms = time.time() - start_ms if results: camera.annotate_text = '%s %.2f\n%.2fms' % (labels[ results[0][0]], results[0][1], elapsed_ms * 1000.0) finally: camera.stop_preview()
def loadmodel(): global load_time global engine global labels global face_engine global class_arr global emb_arr load_time = time.time() print("load_time", type(load_time), load_time) # Initialize engine. engine = DetectionEngine(Model_weight) print("engine", type(engine), engine) labels = None print("labels", type(labels), labels) # Face recognize engine face_engine = ClassificationEngine(FaceNet_weight) #print(face_engine) print("face_engine", type(face_engine), face_engine) # read embedding class_arr, emb_arr = ImageProcess.read_embedding(Embedding_book) print("class_arr", type(class_arr), class_arr) print("emb_arr", type(emb_arr), emb_arr) l = time.time() - load_time return l
def main(): # Load your model onto your Coral Edgetpu engine = ClassificationEngine(modelPath) labels = loadLabels(labelPath) cap = cv2.VideoCapture(0) while cap.isOpened(): ret, frame = cap.read() if not ret: break # Format the image into a PIL Image so its compatable with Edge TPU cv2_im = frame pil_im = Image.fromarray(cv2_im) # Resize and flip image so its a square and matches training pil_im.resize((224, 224)) pil_im.transpose(Image.FLIP_LEFT_RIGHT) # Classify and display image results = classifyImage(pil_im, engine) cv2.imshow('frame', cv2_im) print(results) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', help='File path of Tflite model.', required=True) parser.add_argument('--label', help='File path of label file.', required=True) parser.add_argument('--image', help='File path of the image to be recognized.', required=True) args = parser.parse_args() try: # Prepare labels. labels = dataset_utils.read_label_file(args.label) except: print("Error loading labels") exit(1) try: # Initialize engine. engine = ClassificationEngine(args.model) except: print("Error loading model") exit(1) # Run inference. img = Image.open(args.image) for result in engine.classify_with_image(img, top_k=3): print('---------------------------') print(labels[result[0]]) print('Score : ', result[1]) exit(0)
def __init__(self, path): '''Initialize ros publisher, ros subscriber''' # topic where we publish self.font_path = "/home/pi/python/cascadia_font/CascadiaCode-Regular-VTT.ttf" self.font = ImageFont.truetype(self.font_path, 15) self.engine = ClassificationEngine(path + '/retrained_model_edgetpu.tflite') self.labels = dataset_utils.read_label_file(path + '/label_map.txt') self.image_pub = rospy.Publisher("/output/image_classified/compressed", CompressedImage, queue_size=1) # self.bridge = CvBridge() self.tpu_objects_pub = rospy.Publisher("/tpu_objects", tpu_objects, queue_size=1) # subscribed Topic self.subscriber = rospy.Subscriber("/output/image_raw/compressed", CompressedImage, self.callback, queue_size=1) self.velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=1) self.vel_msg = Twist() rospy.init_node('image_class', anonymous=True)
def _train_and_test(self, model_path, output_model_path, training_datapoints, test_datapoints, keep_classes=False): engine = ImprintingEngine(model_path, keep_classes) image_shape = self._get_image_shape(model_path) data_dir = test_utils.test_data_path('imprinting') # train. for training_datapoint in training_datapoints: engine.train( test_utils.prepare_images(training_datapoint['image_names'], data_dir, image_shape), training_datapoint['label_id']) engine.save_model(output_model_path) # Test. engine = ClassificationEngine(output_model_path) self.assertEqual(1, engine.get_num_of_output_tensors()) if not keep_classes: self.assertEqual(len(training_datapoints), engine.get_output_tensor_size(0)) for test_datapoint in test_datapoints: self._classify_image(engine, data_dir, test_datapoint['image_name'], test_datapoint['label_id'], test_datapoint['score'])
def main(): parser = argparse.ArgumentParser() parser.add_argument("--model", help="File path of Tflite model.", required=True) parser.add_argument( "--image", help="File path of the image to be recognized.", required=True ) parser.add_argument( "--num", help="Number of inference executions.", default=10, type=int ) args = parser.parse_args() inference_time = [] img = Image.open(args.image) for i in range(args.num): # Initialize engine. engine = ClassificationEngine(args.model) # Run inference. result1 = engine.ClassifyWithImage(img, top_k=3) # Get Inference time. inference_time.append(engine.get_inference_time()) # delete Engine del engine # Print avg print("Model inference time avg: {0:.4f}".format(statistics.mean(inference_time)))
def __init__(self, threshold=0.5, num_results=10, model=MODEL_EFFICIENT_S, labels=LABELS): self.engine = ClassificationEngine(model) self.model_labels = read_label_file(labels) self.objs = None self.scores = None self.labels = None self.threshold = threshold self.num_results = num_results
def main(): args = _ParseArgs() print('--------------- Parsing data set -----------------') print('Dataset path:', args.data) train_set, test_set = _ReadData(args.data, args.test_ratio) print('Image list successfully parsed! Category Num = ', len(train_set)) shape = _GetRequiredShape(args.model_path) print('---------------- Processing training data ----------------') print('This process may take more than 30 seconds.') train_input = [] labels_map = _ReadLabel(args.label) class_id = len(labels_map) for (category, image_list) in (train_set.items()): print('Processing category:', category) train_input.append( _PrepareImages( image_list, os.path.join(args.data, category), shape) ) labels_map[class_id] = category class_id += 1 print('---------------- Start training -----------------') engine = ImprintingEngine(args.model_path, keep_classes=args.keep_classes) engine.TrainAll(train_input) print('---------------- Training finished! -----------------') engine.SaveModel(args.output) print('Model saved as : ', args.output) _SaveLabels(labels_map, args.output) print('------------------ Start evaluating ------------------') engine = ClassificationEngine(args.output) top_k = 12 correct = [0] * top_k wrong = [0] * top_k for category, image_list in test_set.items(): print('Evaluating category [', category, ']') for img_name in image_list: img = Image.open(os.path.join(args.data, category, img_name)) candidates = engine.ClassifyWithImage(img, threshold=0.01, top_k=top_k) recognized = False for i in range(top_k): if i < len(candidates) and labels_map[candidates[i][0]] == category: recognized = True if recognized: correct[i] = correct[i] + 1 else: wrong[i] = wrong[i] + 1 print('---------------- Evaluation result -----------------') for i in range(top_k): print('Top {} : {:.0%}'.format(i+1, correct[i] / (correct[i] + wrong[i])))
def main(): ''' modelPath = searchModelFile("/media") if not modelPath: print("No Model file") return ''' try: modelFile = searchFiles("/media","model_edgetpu.tflite") labelFile = searchFiles("/media","labels.txt") except NoModelFile: print("No Model File Exception") return ledCont=LED() # Load yourf model onto your Coral Edgetpu labels = loadLabels(labelFile) if len(labels) < 1 or len(labels) > 6: print("Out of Bound") ledCont.setLedOn(LED.YELLOW) return engine = ClassificationEngine(modelFile) #initLED() # ledCont.wiggleLEDs(2) ledCont.startLEDs() cap = cv2.VideoCapture(0) while cap.isOpened(): ret, frame = cap.read() if not ret: break # Format the image into a PIL Image so its compatable with Edge TPU cv2_im = frame pil_im = Image.fromarray(cv2_im) # Resize and flip image so its a square and matches training pil_im.resize((224, 224)) pil_im.transpose(Image.FLIP_LEFT_RIGHT) # Classify and display image results = classifyImage(pil_im, engine) #ledCont.setOnlyLED(results) if results >= 0: ledCont.setLedOn(results) cv2.imshow('frame', cv2_im) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) serv_motion = self.add_preload_service('MotionSensor') self.char_detected = serv_motion.configure_char('MotionDetected') self.engine = ClassificationEngine("./models/classify.tflite") self.is_trained = retrain() self.labels = get_labels() self.is_running = True logging.info(self.setup_message())
def classification_job(classification_model, image_name, num_inferences): """Runs classification job.""" engine = ClassificationEngine(classification_model) with open_image(image_name) as image: tensor = get_input_tensor(engine, image) # Using `classify_with_input_tensor` to exclude image down-scale cost. for _ in range(num_inferences): engine.classify_with_input_tensor(tensor, top_k=1)
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', help='File path of Tflite model.', required=True) parser.add_argument('--label', help='File path of label file.', required=True) args = parser.parse_args() labels = dataset_utils.read_label_file(args.label) engine = ClassificationEngine(args.model) detectionEngine = DetectionEngine('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/models/ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite') detectionLabels = dataset_utils.read_label_file('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/models/coco_labels.txt') with picamera.PiCamera() as camera: camera.resolution = (640, 480) camera.framerate = 30 _, height, width, _ = engine.get_input_tensor_shape() camera.start_preview() try: stream = io.BytesIO() count = 0 for _ in camera.capture_continuous( stream, format='rgb', use_video_port=True, resize=(width, height)): stream.truncate() stream.seek(0) input_tensor = np.frombuffer(stream.getvalue(), dtype=np.uint8) print(type(stream.getvalue())) image = Image.frombuffer('RGB',(width,height), stream.getvalue()) draw = ImageDraw.Draw(image) with open('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/test_images/' + str(count) + '.png','wb') as f: image.save(f) start_ms = time.time() results = engine.classify_with_input_tensor(input_tensor, top_k=1) objects = detectionEngine.detect_with_image(image,threshold=0.1,keep_aspect_ratio=True,relative_coord=False,top_k=3) elapsed_ms = time.time() - start_ms print('--------------------------') for obj in objects: if detectionLabels: print(detectionLabels[obj.label_id] + ' score = ' + str(obj.score)) box = obj.bounding_box.flatten().tolist() print('box = ', box) draw.rectangle(box, outline='red') draw.text((box[0],box[1]), detectionLabels[obj.label_id] + " " + str(obj.score)) if not objects: print('No objects detected') else: with open('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/test_images/' + str(count) + '_boxes.png','wb') as f: image.save(f) count+=1 #if results: # camera.annotate_text = '%s %.2f\n%.2fms' % ( # labels[results[0][0]], results[0][1], elapsed_ms * 1000.0) finally: camera.stop_preview()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', help='File path of Tflite model.', required=True) parser.add_argument('--label', help='File path of label file.', required=True) parser.add_argument('--picamera', action='store_true', help="Use PiCamera for image capture", default=False) args = parser.parse_args() # Prepare labels. labels = ReadLabelFile(args.label) if args.label else None # Initialize engine. engine = ClassificationEngine(args.model) # Initialize video stream vs = VideoStream(usePiCamera=args.picamera, resolution=(640, 480)).start() time.sleep(1) fps = FPS().start() while True: try: # Read frame from video screenshot = vs.read() image = Image.fromarray(screenshot) # Perfrom inference and keep time start_time = time.time() results = engine.ClassifyWithImage(image, top_k=1) print(results) result = labels[results[0][0]] if results != [] else 'None' print(result) draw_image(image, result) if (cv2.waitKey(5) & 0xFF == ord('q')): fps.stop() break fps.update() except KeyboardInterrupt: fps.stop() break print("Elapsed time: " + str(fps.elapsed())) print("Approx FPS: :" + str(fps.fps())) cv2.destroyAllWindows() vs.stop() time.sleep(2)
def main(): args = _parse_args() print('--------------- Parsing data set -----------------') print('Dataset path:', args.data) train_set, test_set = _read_data(args.data, args.test_ratio) print('Image list successfully parsed! Category Num = ', len(train_set)) shape = _get_required_shape(args.model_path) print('---------------- Processing training data ----------------') print('This process may take more than 30 seconds.') train_input = [] labels_map = {} for class_id, (category, image_list) in enumerate(train_set.items()): print('Processing category:', category) train_input.append( _prepare_images(image_list, os.path.join(args.data, category), shape)) labels_map[class_id] = category print('---------------- Start training -----------------') engine = ImprintingEngine(args.model_path) engine.train_all(train_input) print('---------------- Training finished! -----------------') engine.save_model(args.output) print('Model saved as : ', args.output) _save_labels(labels_map, args.output) print('------------------ Start evaluating ------------------') engine = ClassificationEngine(args.output) top_k = 5 correct = [0] * top_k wrong = [0] * top_k for category, image_list in test_set.items(): print('Evaluating category [', category, ']') for img_name in image_list: img = Image.open(os.path.join(args.data, category, img_name)) candidates = engine.classify_with_image(img, threshold=0.1, top_k=top_k) recognized = False for i in range(top_k): if i < len(candidates) and labels_map[candidates[i] [0]] == category: recognized = True if recognized: correct[i] = correct[i] + 1 else: wrong[i] = wrong[i] + 1 print('---------------- Evaluation result -----------------') for i in range(top_k): print('Top {} : {:.0%}'.format(i + 1, correct[i] / (correct[i] + wrong[i])))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', help='File path of Tflite model.', required=True) parser.add_argument('--label', help='File path of label file.', required=True) args = parser.parse_args() labels = dataset_utils.read_label_file(args.label) engine = ClassificationEngine(args.model) with picamera.PiCamera() as camera: camera.resolution = (640, 480) camera.framerate = 30 camera.hflip = True camera.rotation = 90 _, input_height, input_width, _ = engine.get_input_tensor_shape() input_size = (input_width, input_height) # Width is rounded up to the nearest multiple of 32, # height to the nearest multiple of 16. capture_size = (math.ceil(input_width / 32) * 32, math.ceil(input_height / 16) * 16) camera.start_preview() try: stream = io.BytesIO() for _ in camera.capture_continuous(stream, format='rgb', use_video_port=True, resize=capture_size): stream.truncate() stream.seek(0) input_tensor = np.frombuffer(stream.getvalue(), dtype=np.uint8) if input_size != capture_size: # Crop to input size. Note dimension order (height, width, channels) input_tensor = input_tensor.reshape( (capture_size[1], capture_size[0], 3))[0:input_height, 0:input_width, :].ravel() start_ms = time.time() results = engine.classify_with_input_tensor(input_tensor, top_k=1) elapsed_ms = time.time() - start_ms if results: camera.annotate_text = '%s %.2f\n%.2fms' % (labels[ results[0][0]], results[0][1], elapsed_ms * 1000.0) finally: camera.stop_preview()
def main(): fashion_mnist = keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() engine = ClassificationEngine("model_edgetpu.tflite") for result in engine.ClassifyWithInputTensor(numpy.asarray( test_images[0]).flatten(), top_k=3): print("------------------") print(test_labels[result[0]]) print("Score: ", result[1])
def main(): parser = argparse.ArgumentParser() parser.add_argument("--model1", help="File path of Tflite model.", required=True) parser.add_argument("--model2", help="File path of Tflite model.", required=True) parser.add_argument("--image", help="File path of the image to be recognized.", required=True) parser.add_argument("--num", help="Number of inference executions.", default=100, type=int) args = parser.parse_args() # Initialize engine. engine1 = ClassificationEngine(args.model1) engine2 = ClassificationEngine(args.model2) # Run inference. inference_time1 = [] inference_time2 = [] for i in range(num + 1): img = Image.open(args.image) result1 = engine1.ClassifyWithImage(img, top_k=3) result2 = engine2.ClassifyWithImage(img, top_k=3) # Get Inference time. if i > 0: inference_time1.append(engine1.get_inference_time()) inference_time2.append(engine2.get_inference_time()) # Avg print("Model1 inference time avg: {0:.4f}".format( statistics.mean(inference_time1))) print("Model2 inference time avg: {0:.4f}".format( statistics.mean(inference_time2)))
def main(): """Creates camera pipeline, and pushes pipeline through ClassificationEngine model. Logs results to user-defined storage. Runs either in training mode to gather images for custom model creation or in deterrent mode that sounds an 'alarm' if a defined label is detected.""" args = user_selections() print("Loading %s with %s labels." % (args.model, args.labels)) engine = ClassificationEngine(args.model) labels = load_labels(args.labels) storage_dir = args.storage #Initialize logging file logging.basicConfig(filename='%s/results.log' % storage_dir, format='%(asctime)s-%(message)s', level=logging.DEBUG) last_time = time.monotonic() last_results = [('label', 0)] def user_callback(image, svg_canvas): nonlocal last_time nonlocal last_results start_time = time.monotonic() results = engine.classify_with_image(image, threshold=args.threshold, top_k=args.top_k) end_time = time.monotonic() results = [(labels[i], score) for i, score in results] if args.print: print_results(start_time, last_time, end_time, results) if args.training: if do_training(results, last_results, args.top_k): save_data(image, results, storage_dir) else: #Custom model mode: #The labels can be modified to detect/deter user-selected items if results[0][0] != 'background': save_data(image, storage_dir, results) if 'fox squirrel, eastern fox squirrel, Sciurus niger' in results: if args.sound is not None: playsound(args.sound) logging.info('Deterrent sounded') last_results = results last_time = end_time # Note: we don't use the framerate paramter because our USB cam doesn't like it result = gstreamer.run_pipeline(user_callback, device='/dev/video1', src_caps='video/x-raw,format=YUY2')
def run_benchmark(model, image): """Returns average inference time in ms on specified model and image.""" print('Benchmark for [%s] on %s' % (model, image)) engine = ClassificationEngine(test_utils.test_data_path(model)) iterations = 200 if 'edgetpu' in model else 10 with test_utils.test_image(image) as img: result = 1000 * timeit.timeit( lambda: engine.classify_with_image(img, threshold=0.4, top_k=10), number=iterations) / iterations print('%.2f ms (iterations = %d)' % (result, iterations)) return result
def read_embedding(path=Embedding_book): try: f = h5py.File(path, 'r') except OSError: face_engine = ClassificationEngine(FaceNet_weight) Create_embeddings(face_engine) f = h5py.File(path, 'r') class_arr = f['class_name'][:] class_arr = [k.decode() for k in class_arr] emb_arr = f['embeddings'][:] return class_arr, emb_arr
def main(): # Prepare labels. labels = ReadLabelFile(labelfile) # Initialize engine. engine = ClassificationEngine(model) # Run inference on all images in images/ folder for image in glob.glob(image_folder): img = Image.open(image) for result in engine.ClassifyWithImage(img, top_k=1): print('---------------------------') print(labels[result[0]]) print('Score : ', result[1]) print('-----END-------------------')
def main(): default_model_dir = "../all_models" default_model = 'mobilenet_v2_1.0_224_quant_edgetpu.tflite' default_labels = 'imagenet_labels.txt' parser = argparse.ArgumentParser() parser.add_argument('--model', help='.tflite model path', default=os.path.join(default_model_dir, default_model)) parser.add_argument('--labels', help='label file path', default=os.path.join(default_model_dir, default_labels)) parser.add_argument('--top_k', type=int, default=3, help='number of classes with highest score to display') parser.add_argument('--threshold', type=float, default=0.1, help='class score threshold') args = parser.parse_args() print("Loading %s with %s labels." % (args.model, args.labels)) engine = ClassificationEngine(args.model) labels = load_labels(args.labels) input_shape = engine.get_input_tensor_shape() inference_size = (input_shape[1], input_shape[2]) # Average fps over last 30 frames. fps_counter = common.avg_fps_counter(30) def user_callback(input_tensor, src_size, inference_box): nonlocal fps_counter start_time = time.monotonic() results = engine.classify_with_input_tensor(input_tensor, threshold=args.threshold, top_k=args.top_k) end_time = time.monotonic() text_lines = [ 'Inference: %.2f ms' % ((end_time - start_time) * 1000), 'FPS: %d fps' % (round(next(fps_counter))), ] for index, score in results: text_lines.append('score=%.2f: %s' % (score, labels[index])) print(' '.join(text_lines)) return generate_svg(src_size, text_lines) result = gstreamer.run_pipeline(user_callback, appsink_size=inference_size)
def load_model(self, label_file=None, model_file=None): """Load a pretrained model""" # Prepared labels if label_file is not None: self.label_file = label_file self.labels = self.read_label_file(self.label_file) # Initialize TPU engine if model_file is not None: self.model_file = model_file from edgetpu.classification.engine import ClassificationEngine self.model = ClassificationEngine(self.model_file)
def main(): # Load your model onto your Coral Edgetpu engine = ClassificationEngine(modelPath) labels = loadLabels(labelPath) cap = cv2.VideoCapture(0) prev_time = 0 while cap.isOpened(): ret, frame = cap.read() if not ret: break cur_time = time.time() sec = cur_time - prev_time prev_time = cur_time fps = str(round(1 / sec, 1)) + 'fps ' # Format the image into a PIL Image so its compatable with Edge TPU cv2_im = frame cv2_im_input = cv2.resize(frame, (224, 224)) pil_im = Image.fromarray(cv2_im_input) # Resize and flip image so its a square and matches training pil_im.transpose(Image.FLIP_LEFT_RIGHT) # Classify and display image results = classifyImage(pil_im, engine) class_value = results[0][0] probability = str(round(results[0][1] * 100, 1)) + '%' if class_value == 0: result = 'Wood ' elif class_value == 1: result = 'Yellow ' elif class_value == 2: result = 'Marble ' else: result = 'Carpet ' result = fps + result + probability cv2.putText(frame, result, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0)) cv2.imshow('frame', cv2_im) # print(results) if cv2.waitKey(1) & 0xFF == 27: break cap.release() cv2.destroyAllWindows()
def classification_task(num_inferences): tid = threading.get_ident() print('Thread: %d, %d inferences for classification task' % (tid, num_inferences)) labels = test_utils.read_label_file( test_utils.test_data_path('imagenet_labels.txt')) model_name = 'mobilenet_v1_1.0_224_quant_edgetpu.tflite' engine = ClassificationEngine( test_utils.test_data_path(model_name)) print('Thread: %d, using device %s' % (tid, engine.device_path())) with test_utils.test_image('cat.bmp') as img: for _ in range(num_inferences): ret = engine.classify_with_image(img, top_k=1) self.assertEqual(len(ret), 1) self.assertEqual(labels[ret[0][0]], 'Egyptian cat') print('Thread: %d, done classification task' % tid)