Beispiel #1
0
 def detection_task(num_inferences):
     tid = threading.get_ident()
     print('Thread: %d, %d inferences for detection task' %
           (tid, num_inferences))
     model_name = 'mobilenet_ssd_v1_coco_quant_postprocess_edgetpu.tflite'
     engine = DetectionEngine(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.detect_with_image(img, top_k=1)
             self.assertEqual(len(ret), 1)
             self.assertEqual(ret[0].label_id, 16)  # cat
             self.assertGreater(ret[0].score, 0.7)
             self.assertGreater(
                 test_utils.iou(np.array([[0.1, 0.1], [0.7, 1.0]]),
                                ret[0].bounding_box), 0.88)
     print('Thread: %d, done detection task' % tid)
                        default="edgetpu/test_data/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite", \
                        help=".tflite model to be executed")
    parser.add_argument("-l", "--label_file", default="edgetpu/edgetpu_models/coco_labels.txt", \
                        help="name of file containing labels")
    parser.add_argument("-k", "--top_k", default=5, help="top_k")
    parser.add_argument("-t", "--threshold", default=0.0, help="threshold")
    parser.add_argument("-c", "--loop_counts", default=1, help="loop counts")
    parser.add_argument("-s", "--show_image", default=False, help="show image")
    parser.add_argument("-d", "--device_path", help="device_path")
    args = parser.parse_args()

    if args.device_path:
        engine = DetectionEngine(args.model_file, device_path=args.device_path)
    else:
        engine = DetectionEngine(args.model_file)
    print("device path:", engine.device_path())

    output_sizes = engine.get_all_output_tensors_sizes()
    # print("output sizes:", output_sizes)

    count = 0
    indices = []
    for i in output_sizes:
        count = count + i
        indices.append(count)
    # print("indices:", indices)

    input_tensor_shape = engine.get_input_tensor_shape()
    if (input_tensor_shape.size != 4 or input_tensor_shape[3] != 3
            or input_tensor_shape[0] != 1):
        raise RuntimeError(
Beispiel #3
0
def inference_thread(running, state, result_buffer, frame_buffer, args, identity_dict, current_identity):
    global IDLE, TRACK, RESET, FACE_RECOG_THRESHOLD, FACE_RECOG_THRESHOLD_A
    global od_engine, face_detector, facenet_engine, svm_clf
    # Initialize object detection engine.
    od_engine = DetectionEngine(args.od_model)
    print("device_path: ", od_engine.device_path())
    _, od_width, od_height, _ = od_engine.get_input_tensor_shape()
    print("od input dim: ", od_width, od_height)
    # initial face detector using the opencv haarcascade model
    face_detector = FaceDetector(args.hc_model)
    # Initialize facenet engine.
    facenet_engine = BasicEngine(args.fn_model)
    # load the sklearn support vector machine model from disk
    svm_clf = pickle.load(open(args.svm_model, 'rb'))

    while running.value:
        # check if the frame buffer has a frame, else busy waiting
        if frame_buffer.empty():
            continue
        frame = frame_buffer.get()
        tinf = time.perf_counter()

        if state.value == IDLE:
            fd_results = None
            # reorder image frame from BGR to RGB
            img = frame[:,:,::-1]
            # face detection
            faces_coord = face_detector.detect(img, True)
            # image preprocessing, downsampling
            print("faces_coord: ",faces_coord)
            if not isinstance(faces_coord, type(None)):
                # normalize face image
                face_image = np.array(normalize_faces(img ,faces_coord))
                # facenet to generate face embedding
                facenet_engine.RunInference(face_image.flatten())
                face_emb = facenet_engine.get_raw_output().reshape(1,-1)
                # use SVM to classfy identity with face embedding
                pred_prob = svm_clf.predict_proba(face_emb)
                best_class_index = np.argmax(pred_prob, axis=1)[0]
                best_class_prob = pred_prob[0, best_class_index]
                print("best_class_index: ",best_class_index)
                print("best_class_prob: ",best_class_prob)
                print("label", svm_clf.classes_[best_class_index])
                # Check threshold and verify identify is in the identifiy dictionary
                if best_class_prob > FACE_RECOG_THRESHOLD:
                    face_label = svm_clf.classes_[best_class_index]
                    if face_label in identity_dict:
                        print("\n=================================")
                        print("Identity found: ", face_label, " ",identity_dict[face_label],
                            " with Prob = ", best_class_prob)
                        print("=================================\n")
                        current_identity.value = identity_dict[face_label][0] # ID
                result_buffer.put(faces_coord)
        elif state.value == TRACK:
            od_results = None
            # convert numpy array representation to PIL image with rgb format
            img = Image.fromarray(frame[:,:,::-1], 'RGB')
            # Run inference.
            od_results = od_engine.DetectWithImage(img, threshold=0.30, keep_aspect_ratio=True, relative_coord=False, top_k=10)
            # push result to buffer queue
            result_buffer.put(od_results)
        print(time.perf_counter() - tinf, "sec")
    print("[Finish] inference_thread")
#    print(str(cap.get(3)) + ', ' + str(cap.get(4)) + ', ' + str(cap.get(5)) + ', ' + str(cap.get(16)))

##########load labels
labels = {}
i = 0
for row in open('labels/lite_labelmap.txt'):
    # unpack the row and update the labels dictionary
    label = row.strip()
    labels[int(i)] = label.strip()
    i += 1

###########load models to engines
from edgetpu.detection.engine import DetectionEngine
from PIL import Image
model_a = DetectionEngine('tflite-models/coral_objdtct_mnet_ssd_v1.tflite')
model_b = DetectionEngine('tflite-models/detect.tflite', model_a.device_path())
print('edgetpu models loaded')

############camera
cap = cv.VideoCapture(-1)

############runtime
model_a_times = []
model_b_times = []
while True:
    
    # Capture frame-by-frame
    frame_s = time.time()
    ret, img = cap.read()
    
    # Read and preprocess an image.