class ObjectDetectorLite(): def __init__(self, model_path='detect.tflite', num_threads=12): try: self.interpreter = Interpreter( model_path=model_path) #,num_threads=num_threads) #self.interpreter.set_num_threads(num_threads) except: self.interpreter = tf.lite.Interpreter(model_path=model_path) self.interpreter.set_num_threads(num_threads) self.interpreter.allocate_tensors() self.input_details = self.interpreter.get_input_details() self.output_details = self.interpreter.get_output_details() def _boxes_coordinates(self, image, boxes, classes, scores, max_boxes_to_draw=20, min_score_thresh=.5): if not max_boxes_to_draw: max_boxes_to_draw = boxes.shape[0] number_boxes = min(max_boxes_to_draw, boxes.shape[0]) person_boxes = [] for i in range(number_boxes): if scores is None or scores[i] > min_score_thresh: box = tuple(boxes[i].tolist()) ymin, xmin, ymax, xmax = box _, im_height, im_width, _ = image.shape left, right, top, bottom = [ int(z) for z in (xmin * im_width, xmax * im_width, ymin * im_height, ymax * im_height) ] person_boxes.append([(left, top), (right, bottom), scores[i], LABELS[classes[i]]]) return person_boxes def detect(self, image, threshold=0.1): # run model self.interpreter.set_tensor(self.input_details[0]['index'], image) start_time = time.time() self.interpreter.invoke() stop_time = time.time() print("time: ", stop_time - start_time) # get results boxes = self.interpreter.get_tensor(self.output_details[0]['index']) classes = self.interpreter.get_tensor(self.output_details[1]['index']) scores = self.interpreter.get_tensor(self.output_details[2]['index']) num = self.interpreter.get_tensor(self.output_details[3]['index']) # Find detected boxes coordinates return self._boxes_coordinates(image, np.squeeze(boxes[0]), np.squeeze(classes[0] + 1).astype( np.int32), np.squeeze(scores[0]), min_score_thresh=threshold)
class FaceDetector: def __init__(self, model_face_detect, num_threads): # Init Face Detector self.interpreter_face_detect = Interpreter(model_path=model_face_detect) try: self.interpreter_face_detect.set_num_threads(num_threads) except: print("WARNING: The installed PythonAPI of Tensorflow/Tensorflow Lite runtime does not support Multi-Thread processing.") print("WARNING: It works in single thread mode.") print("WARNING: If you want to use Multi-Thread to improve performance on aarch64/armv7l platforms, please refer to one of the below to implement a customized Tensorflow/Tensorflow Lite runtime.") print("https://github.com/PINTO0309/Tensorflow-bin.git") print("https://github.com/PINTO0309/TensorflowLite-bin.git") pass self.interpreter_face_detect.allocate_tensors() self.input_details = self.interpreter_face_detect.get_input_details()[0]['index'] self.box = self.interpreter_face_detect.get_output_details()[0]['index'] self.scores = self.interpreter_face_detect.get_output_details()[2]['index'] self.count = self.interpreter_face_detect.get_output_details()[3]['index']
default=480, help="height.") parser.add_argument("--vidfps", type=int, default=30, help="Frame rate.") parser.add_argument("--num_threads", type=int, default=4, help="Threads.") args = parser.parse_args() model = args.model usbcamno = args.usbcamno image_width = args.camera_width image_height = args.camera_height vidfps = args.vidfps num_threads = args.num_threads interpreter = Interpreter(model_path=model) try: interpreter.set_num_threads(num_threads) except: print( "WARNING: The installed PythonAPI of Tensorflow/Tensorflow Lite runtime does not support Multi-Thread processing." ) print("WARNING: It works in single thread mode.") print( "WARNING: If you want to use Multi-Thread to improve performance on aarch64/armv7l platforms, please refer to one of the below to implement a customized Tensorflow/Tensorflow Lite runtime." ) print("https://github.com/PINTO0309/Tensorflow-bin.git") print("https://github.com/PINTO0309/TensorflowLite-bin.git") pass interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details()
cam.resolution = (width, height) stream = PiRGBArray(cam) elif camera_type == "video_file": cam = cv2.VideoCapture(args.input_video_file) else: print('[Error] --camera_type: wrong device') parser.print_help() sys.exit() cv2.namedWindow('pose estimation pi', cv2.WINDOW_AUTOSIZE) interpreter = Interpreter(model_path=model) interpreter.allocate_tensors() try: interpreter.set_num_threads(int(num_threads)) except: print( "WARNING: The installed PythonAPI of Tensorflow/Tensorflow Lite runtime does not support Multi-Thread processing." ) print("WARNING: It works in single thread mode.") print( "WARNING: If you want to use Multi-Thread to improve performance on aarch64/armv7l platforms, please refer to one of the below to implement a customized Tensorflow/Tensorflow Lite runtime." ) print("https://github.com/PINTO0309/Tensorflow-bin.git") print("https://github.com/PINTO0309/TensorflowLite-bin.git") pass input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() input_shape = input_details[0]['shape']
from tensorflow.lite.python.interpreter import Interpreter LABELS = [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ] if __name__ == '__main__': image = cv2.imread('dog.jpg') interpreter = Interpreter( model_path= '03_integer_quantization/ssdlite_mobilenet_v2_voc_300_integer_quant_with_postprocess.tflite' ) try: interpreter.set_num_threads(4) except: print( "WARNING: The installed PythonAPI of Tensorflow/Tensorflow Lite runtime does not support Multi-Thread processing." ) print("WARNING: It works in single thread mode.") print( "WARNING: If you want to use Multi-Thread to improve performance on aarch64/armv7l platforms, please refer to one of the below to implement a customized Tensorflow/Tensorflow Lite runtime." ) print("https://github.com/PINTO0309/Tensorflow-bin.git") print("https://github.com/PINTO0309/TensorflowLite-bin.git") pass interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details()
# check the type of the input tensor if input_details[0]['dtype'] == np.float32: floating_model = True # NxHxWxC, H:1, W:2 height = input_details[0]['shape'][1] width = input_details[0]['shape'][2] img = Image.open(args.image) img = img.resize((width, height)) # add N dim input_data = np.expand_dims(img, axis=0) if floating_model: input_data = (np.float32(input_data) - args.input_mean) / args.input_std interpreter.set_num_threads( int(args.num_threads )) #<- Specifies the num of threads assigned to inference interpreter.set_tensor(input_details[0]['index'], input_data) start_time = time.time() interpreter.invoke() stop_time = time.time() output_data = interpreter.get_tensor(output_details[0]['index']) results = np.squeeze(output_data) top_k = results.argsort()[-5:][::-1] labels = load_labels(args.label_file) for i in top_k: if floating_model: print('{0:08.6f}'.format(float(results[i])) + ":", labels[i]) else:
def main(): camera_width = 352 camera_height = 288 input_size = 96 hand_thresh = 0.25 OD_thresh = 0.8 fps = "" message1 = "Push [q] to quit." message2 = "Push [s] to change mode." hand = "" elapsedTime = 0 like_OD = False # like object detection result = None cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FPS, 120) cap.set(cv2.CAP_PROP_FRAME_WIDTH, camera_width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, camera_height) interpreter = Interpreter(model_path=model_path + "weights_weight_quant.tflite") try: interpreter.set_num_threads(4) except: pass interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() time.sleep(1) while cap.isOpened(): t1 = time.time() ret, image = cap.read() image = image[:, 32:320] if not ret: break img = cv2.resize(image, (input_size, input_size)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img / 255 img = np.expand_dims(img, axis=0) img = img.astype(np.float32) interpreter.set_tensor(input_details[0]['index'], img) interpreter.invoke() channel_out = interpreter.get_tensor(output_details[0]['index']) test_vector = interpreter.get_tensor(output_details[1]['index']) score = get_score_arc(vector_pa, test_vector) if score < hand_thresh: # hand is gu hand = "gu" color = (255, 0, 0) heatmap = predict_faster_gradcam(channel_out, test_vector, image, kmeans, channel_weight, channel_adress) if like_OD: x_min, y_min, x_max, y_max = get_x_y_limit(heatmap, OD_thresh) result = bounding_box(image, x_min, y_min, x_max, y_max) else: heatmap = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET) image = np.copy(cv2.addWeighted(heatmap, 0.5, image, 0.5, 2.2)) else: # hand is pa hand = "pa" color = (0, 0, 255) # message cv2.putText(image, "{0} {1:.1f} Score".format(hand, score), (camera_width - 290, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 1, cv2.LINE_AA) cv2.putText(image, message1, (camera_width - 285, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA) cv2.putText(image, message2, (camera_width - 285, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA) cv2.putText(image, fps, (camera_width - 175, 110), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1, cv2.LINE_AA) cv2.imshow("Result", image) # FPS elapsedTime = time.time() - t1 fps = "{:.0f} FPS".format(1 / elapsedTime) # quit or change mode key = cv2.waitKey(1) & 0xFF if key == ord("q"): break if key == ord("s"): if like_OD == True: like_OD = False else: like_OD = True cv2.destroyAllWindows()