コード例 #1
0
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()
コード例 #2
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)
コード例 #3
0
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)
コード例 #4
0
	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
コード例 #5
0
ファイル: pupper_vision.py プロジェクト: campusrover/PupperPy
def main():
    cv_publisher = Publisher(105)
    MODELS_DIR = '/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/models/'
    MODEL_PATH = MODELS_DIR + 'ssd_mobilenet_v2_pupper_quant_edgetpu.tflite'
    LABEL_PATH = MODELS_DIR + 'pupper_labels.txt'
    LOG_FILE = '/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/vision_log.txt'
    labels = dataset_utils.read_label_file(LABEL_PATH)
    engine = DetectionEngine(MODEL_PATH)

    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 30
        _, height, width, _ = engine.get_input_tensor_shape()
        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)
                #image = Image.frombuffer('RGB',(width,height), stream.getvalue())
                image = Image.frombuffer('RGB',(320,304), stream.getvalue()) # to account for automatic upscaling by picamera when format='rgb'
                #draw = ImageDraw.Draw(image)
                start_ms = time.time()
                results = engine.detect_with_image(image,threshold=0.2,keep_aspect_ratio=True,relative_coord=False,top_k=10)
                elapsed_ms = time.time() - start_ms
                
                detectedObjs = []
                for obj in results:
                    if (obj.label_id in range(3)):
                        box = obj.bounding_box.flatten().tolist()
                        #draw.rectangle(box, outline='red')
                        #draw.text((box[0],box[1]), labels[obj.label_id] + " " + str(obj.score))
                        w = box[0] - box[2]
                        h = box[1] - box[3]
                        objInfo = {'bbox_x':float(box[0]),
                                   'bbox_y':float(box[1]),
                                   'bbox_h':float(h),
                                   'bbox_w':float(w),
                                   'bbox_label':labels[obj.label_id],
                                   'bbox_confidence': float(obj.score)
                                   }
                        detectedObjs.append(objInfo)
                try:
                    cv_publisher.send(detectedObjs)
                except BaseException as e:
                    print('Failed to send bounding boxes. CV UDP subscriber likely not initialized')
                    pass
                #print(detectedObjs)

                #with open('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/test_images_120120/' + str(count) + '.png','wb') as f:
                #    image.save(f)
                #count+=1
        except BaseException as e:
            with open(LOG_FILE,'w') as f:
                f.write("Failed to run detection loop:\n {0}\n".format(traceback.format_exc()))
コード例 #6
0
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()
コード例 #7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model',
        required=True,
        help='Detection SSD model path (must have post-processing operator).')
    parser.add_argument('--label', help='Labels file path.')
    parser.add_argument('--input', help='Input image path.', required=True)
    parser.add_argument('--output', help='Output image path.')
    parser.add_argument(
        '--keep_aspect_ratio',
        action='store_true',
        help=
        ('keep the image aspect ratio when down-sampling the image by adding '
         'black pixel padding (zeros) on bottom or right. '
         'By default the image is resized and reshaped without cropping. This '
         'option should be the same as what is applied on input images during '
         'model training. Otherwise the accuracy may be affected and the '
         'bounding box of detection result may be stretched.'))
    args = parser.parse_args()

    # Initialize engine.
    engine = DetectionEngine(args.model)
    labels = dataset_utils.read_label_file(args.label) if args.label else None

    # Open image.
    img = Image.open(args.input).convert('RGB')
    draw = ImageDraw.Draw(img)

    # Run inference.
    objs = engine.detect_with_image(img,
                                    threshold=0.05,
                                    keep_aspect_ratio=args.keep_aspect_ratio,
                                    relative_coord=False,
                                    top_k=10)

    # Print and draw detected objects.
    for obj in objs:
        print('-----------------------------------------')
        if labels:
            print(labels[obj.label_id])
        print('score =', obj.score)
        box = obj.bounding_box.flatten().tolist()
        print('box =', box)
        draw.rectangle(box, outline='red')

    if not objs:
        print('No objects detected.')

    # Save image with bounding boxes.
    if args.output:
        img.save(args.output)
コード例 #8
0
 def __init__(self,
              threshold=0.5,
              num_results=10,
              model=MODEL_V2,
              labels=LABELS):
     self.engine = DetectionEngine(model)
     self.model_labels = read_label_file(labels)
     self.objs = None
     self.boxes = None
     self.scores = None
     self.labels = None
     self.threshold = threshold
     self.num_results = num_results
コード例 #9
0
def main():
    cv_publisher = Publisher(105)
    MODELS_DIR = '/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/models/'
    MODEL_PATH = MODELS_DIR + 'ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite'
    LABEL_PATH = MODELS_DIR + 'coco_labels.txt'
    LOG_FILE = '/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/vision_log.txt'
    labels = dataset_utils.read_label_file(LABEL_PATH)
    engine = DetectionEngine(MODEL_PATH)

    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 30
        _, height, width, _ = engine.get_input_tensor_shape()
        
        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)
            #image = Image.frombuffer('RGB',(width,height), stream.getvalue())
            image = Image.frombuffer('RGB',(320,304), stream.getvalue()) # to account for automatic upscaling by picamera when format='rgb'
            draw = ImageDraw.Draw(image)
            start_ms = time.time()
            results = engine.detect_with_image(image,threshold=0.1,keep_aspect_ratio=True,relative_coord=False,top_k=51)
            elapsed_ms = time.time() - start_ms
                
            detectedObjs = []
            for obj in results:
                if (obj.label_id == 0 or obj.label_id == 36):
                    if (obj.label_id == 36):
                        print('Tennis ball detected')
                    box = obj.bounding_box.flatten().tolist()
                    draw.rectangle(box, outline='red')
                    draw.text((box[0],box[1]), labels[obj.label_id] + " " + str(obj.score))
                    w = box[0] - box[2]
                    h = box[1] - box[3]
                    objInfo = {'bbox_x':float(box[0]),
                               'bbox_y':float(box[1]),
                               'bbox_h':float(h),
                               'bbox_w':float(w),
                               'bbox_label':labels[obj.label_id],
                               'bbox_confidence': float(obj.score)
                               }
                    detectedObjs.append(objInfo)
            cv_publisher.send(detectedObjs)
            #print(detectedObjs)

            with open('/home/cerbaris/pupper_code/PupperPy/pupperpy/Vision/test_images/' + str(count) + '.png','wb') as f:
                image.save(f)
            count+=1
コード例 #10
0
ファイル: stop_sign_detector.py プロジェクト: cfox570/dk
    def __init__(self, min_score, show_bounding_box, debug=False):
        MODEL_FILE_NAME = "ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite"
        LABEL_FILE_NAME = "coco_labels.txt"

        MODEL_URL = "https://github.com/google-coral/edgetpu/raw/master/test_data/ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite"
        LABEL_URL = "https://dl.google.com/coral/canned_models/coco_labels.txt"

        self.download_file(MODEL_URL, MODEL_FILE_NAME)
        self.download_file(LABEL_URL, LABEL_FILE_NAME)

        self.last_5_scores = collections.deque(np.zeros(5), maxlen=5)
        self.engine = DetectionEngine(MODEL_FILE_NAME)
        self.labels = dataset_utils.read_label_file(LABEL_FILE_NAME)

        self.STOP_SIGN_CLASS_ID = 12
        self.min_score = min_score
        self.show_bounding_box = show_bounding_box
        self.debug = debug
コード例 #11
0
    def __init__(self, model_path, label_path, use_coral_flag, use_tpu_flag,
                 res_x, res_y, min_conf_threshold):

        self.res_y = res_y
        self.res_x = res_x
        self.use_coral_flag = use_coral_flag
        if use_coral_flag:
            from edgetpu.detection.engine import DetectionEngine
            from edgetpu.utils import dataset_utils
        self.min_conf_threshold = min_conf_threshold

        # Load the label map
        with open(label_path, 'r') as f:
            self.labels = [line.strip() for line in f.readlines()]

        if self.labels[0] == '???':
            del (self.labels[0])

        if use_tpu_flag:
            self.interpreter = Interpreter(
                model_path=model_path,
                experimental_delegates=[load_delegate('libedgetpu.so.1.0')])
        else:
            self.interpreter = Interpreter(model_path=model_path)

        self.interpreter.allocate_tensors()

        # Get model details
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details()
        self.height = self.input_details[0]['shape'][1]
        self.width = self.input_details[0]['shape'][2]

        self.is_floating_model = (self.input_details[0]['dtype'] == np.float32)

        self.input_mean = 127.5
        self.input_std = 127.5

        #Coral
        if use_coral_flag:
            self.engine = DetectionEngine(model_path)
            self.labels = dataset_utils.read_label_file(label_path)
            _, height, width, _ = self.engine.get_input_tensor_shape()
コード例 #12
0
    def load_classifiers(self, input_string):
        for name in input_string.split(","):

            # Check if classifier has already been loaded
            if name not in self.loaded:
                logger.debug("Loading classifier %s " % (name))

                # Read attributes from library and initialise
                try:
                    attr = self.library[name]
                    output = {}
                    output["labels"] = dataset_utils.read_label_file(
                        attr["labels"])
                    output["model"] = ClassificationEngine(attr["model"])
                    output["thresholds"] = attr["thresholds"]
                    self.loaded[name] = output
                except KeyError:
                    raise KeyError("Classifier name not found in database")
                except FileNotFoundError:
                    raise FileNotFoundError(
                        "Model or labels not found in models folder")

            else:
                logger.debug("Classifier already loaded %s " % (name))
コード例 #13
0
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
        _, 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.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()
コード例 #14
0
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.')
  args = parser.parse_args()

  labels = dataset_utils.read_label_file(args.label) if args.label else None
  engine = DetectionEngine(args.model)

  with picamera.PiCamera() as camera:
    preview_size = (640, 480)
    camera.resolution = preview_size
    camera.framerate = 30
    # camera.hflip = True
    # camera.vflip = 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)

    # Actual detection area on preview.
    detect_size = (preview_size[0] * input_size[0] / capture_size[0],
                   preview_size[1] * input_size[1] / capture_size[1])

    # Make annotator smaller for efficiency.
    annotator_factor = 0.5
    annotator_size = (int(preview_size[0] * annotator_factor),
                      int(preview_size[1] * annotator_factor))

    # Font for drawing detection candidates
    font = ImageFont.truetype(
                '/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf',
                size=12)

    camera.start_preview()
    annotator = Annotator(camera,
                          dimensions=annotator_size,
                          default_color=(255, 255, 255, 64))

    def annotate(candidates):
      annotator.clear()

      # Get actual coordinates to draw
      def translate(relative_coord):
        return (detect_size[0] * relative_coord[0] * annotator_factor,
                detect_size[1] * relative_coord[1] * annotator_factor)

      for c in candidates:
        top_left = translate(c.bounding_box[0])
        bottom_right = translate(c.bounding_box[1])

        annotator.bounding_box(top_left + bottom_right)

        text = '{} {:.2f}'.format(labels[c.label_id], c.score) \
                if labels else '{:.2f}'.format(c.score)

        annotator.text(top_left, text, font=font)

      annotator.update()

    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.detect_with_input_tensor(input_tensor, top_k=3)
        elapsed_ms = time.time() - start_ms

        annotate(results)

        camera.annotate_text = '{:.2f}ms'.format(elapsed_ms * 1000.0)

    finally:
      # Maybe should make this an annotator method
      camera.remove_overlay(annotator._overlay)
      camera.stop_preview()
コード例 #15
0
from watchdog.observers.polling import PollingObserver
from watchdog.events import FileSystemEventHandler
import time
import subprocess
import cv2
import numpy as np
import av
from edgetpu.detection.engine import DetectionEngine
from edgetpu.utils import dataset_utils
from PIL import Image
from PIL import ImageDraw
from tflite_runtime.interpreter import Interpreter
from tflite_runtime.interpreter import load_delegate
import sys

labels = dataset_utils.read_label_file("coco_labels.txt")
engine = DetectionEngine(
    "mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite")

interpreter = Interpreter(
    model_path="plate_model.tflite",
    experimental_delegates=[load_delegate('libedgetpu.so.1.0')])
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
_, height, width, _ = input_details[0]['shape']
print(input_details)
print(output_details)

コード例 #16
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model',
        help=
        'Path of the detection model, it must be a SSD model with postprocessing operator.',
        required=True)
    parser.add_argument('--label', help='Path of the labels file.')
    parser.add_argument('--input',
                        help='File path of the input image.',
                        required=True)
    parser.add_argument('--output', help='File path of the output image.')
    parser.add_argument(
        '--keep_aspect_ratio',
        dest='keep_aspect_ratio',
        action='store_true',
        help=
        ('keep the image aspect ratio when down-sampling the image by adding '
         'black pixel padding (zeros) on bottom or right. '
         'By default the image is resized and reshaped without cropping. This '
         'option should be the same as what is applied on input images during '
         'model training. Otherwise the accuracy may be affected and the '
         'bounding box of detection result may be stretched.'))
    parser.set_defaults(keep_aspect_ratio=False)
    args = parser.parse_args()

    if not args.output:
        output_name = 'object_detection_result.jpg'
    else:
        output_name = args.output

    # Initialize engine.
    engine = DetectionEngine(args.model)
    labels = dataset_utils.read_label_file(args.label) if args.label else None

    # Open image.
    img = Image.open(args.input)
    draw = ImageDraw.Draw(img)

    # Run inference.
    ans = engine.detect_with_image(img,
                                   threshold=0.05,
                                   keep_aspect_ratio=args.keep_aspect_ratio,
                                   relative_coord=False,
                                   top_k=10)

    # Save result.
    if ans:
        for obj in ans:
            print('-----------------------------------------')
            if labels:
                print(labels[obj.label_id])
            print('score = ', obj.score)
            box = obj.bounding_box.flatten().tolist()
            print('box = ', box)
            # Draw a rectangle.
            draw.rectangle(box, outline='red')
        img.save(output_name)
        print('Please check ', output_name)
    else:
        print('No object detected!')
def main():
    frame_count = 0
    framerate = 0
    # Start Edge TPU
    default_model_dir = './models'
    default_model1 = 'mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite'
    # default_model1 = 'mobilenet_ssd_v2_face_quant_postprocess_edgetpu.tflite'
    default_labels1 = 'coco_labels.txt'
    default_threshold = 0.6
    enable_grid = True
    enable_multi_inference = True
    enable_inference = False
    divider = 1  # sets the number of image sections for better results with smaller objects
    # e.g. divider = 6 --> image is split into a grid of 6*6 images

    fontStyle = cv2.FONT_HERSHEY_SIMPLEX
    fontScale = 0.7
    fontThickness = 2

    engine = DetectionEngine(os.path.join(default_model_dir, default_model1))
    labels = dataset_utils.read_label_file(
        os.path.join(default_model_dir,
                     default_labels1)) if default_labels1 else None

    def spin_up_engine(input):
        detected_labels = []
        if len(
                input
        ) == 6:  # added a rectangle paarmeter for multi-segment-inferencing
            engine, labels, pil_im, default_threshold, exclude_list, rectangle = input
            rectangle = np.array([rectangle[0], rectangle[0]])
            # where rectangle is a part of the image [pt1,pt2]
        else:  #for usage without multi-segment-inferencing
            engine, labels, pil_im, default_threshold, exclude_list = input
            rectangle = np.array([[0, 0], [0, 0]])
        objs = engine.detect_with_image(pil_im,
                                        threshold=default_threshold,
                                        keep_aspect_ratio='store_true',
                                        relative_coord=False,
                                        top_k=99)
        if objs:
            for obj in objs:
                obj_label = labels[obj.label_id]
                # print(obj.bounding_box)
                obj.bounding_box += rectangle  # convert bbox position from image segment into real image position
                # print("coverted bbox: ",obj.bounding_box)
                x0, y0, x1, y1 = obj.bounding_box.flatten().tolist()
                x0, y0, x1, y1 = int(x0), int(y0), int(x1), int(y1)
                if x1 - x0 > w / 2 or obj_label in exclude_list:
                    objs.remove(obj)
                    continue  #the bounding box is too big, must be false-positive
                else:
                    detected_labels.append(obj_label)
        return objs, labels, detected_labels

    que = Queue()

    #cap = WebcamVideoStream(0).start()
    cap = cv2.VideoCapture(0)
    # Check if camera opened successfully
    if (cap.isOpened() == False):
        print("Unable to read camera feed")
    cap.set(3, 1280)
    cap.set(4, 720)

    time.sleep(0.5)
    # cv2_im = cv2.imread("./models/crowd.jfif")
    w = int(cap.get(3))
    h = int(cap.get(4))
    print(h, "/", w)
    # out = cv2.VideoWriter('./detection_%s.avi'%(datetime.today().strftime('%Y-%m-%d-%H:%M:%S')),cv2.VideoWriter_fourcc('M','J','P','G'), 10, (w,h))
    # out = cv2.VideoWriter('./detection_.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (w,h))

    t0 = time.time()
    while True:
        img_segments = [[[0, 0], [w, h]]]
        ret, cv2_im = cap.read()
        #cv2_im = cv2.rotate(cv2_im, cv2.cv2.ROTATE_90_COUNTERCLOCKWISE)

        # cv2_im = cv2.imread("./models/kites.png")
        # cv2_im = cv2.imread("./models/crowd.jfif")
        (h, w) = cv2_im.shape[:2]
        x_step = int(w / divider)
        y_step = int(h / divider)
        # outer
        for x in range(divider):
            for y in range(divider):
                pt1 = x * x_step, y * y_step
                pt2 = (x + 1) * x_step, (y + 1) * y_step
                if enable_grid == True:
                    cv2.rectangle(cv2_im, pt1, pt2, (0, 0, 255), 4)
                if enable_multi_inference == True:
                    img_segments.append([pt1, pt2])
        img_segments.append([pt1, pt2])
        # middle 1
        pt1 = int(w / 2 - 0.5 * x_step), int(h / 2 - 0.5 * y_step)
        pt2 = int(w / 2 + 0.5 * x_step), int(h / 2 + 0.5 * y_step)
        if enable_grid == True: cv2.rectangle(cv2_im, pt1, pt2, (255, 0, 0), 2)
        if enable_multi_inference == True: img_segments.append([pt1, pt2])
        # middle 2
        pt1 = int(w / 2 - x_step), int(h / 2 - y_step)
        pt2 = int(w / 2 + x_step), int(h / 2 + y_step)
        if enable_grid == True:
            cv2.rectangle(cv2_im, pt1, pt2, (255, 255, 0), 2)
        if enable_multi_inference == True: img_segments.append([pt1, pt2])
        #convert to PIL
        pil_im = Image.fromarray(cv2_im.copy())
        #Start the threads for each image segment
        thread_list = []
        if enable_inference == True:
            for rectangle in img_segments:
                left = rectangle[0][0]  #x1
                top = rectangle[0][1]  #y1
                right = rectangle[1][0]  #x2
                bottom = rectangle[1][1]  #y2
                # print("crop: ",(left, top, right, bottom))

                pil_im_segment = pil_im.crop((left, top, right, bottom))

                t1 = Thread(target=lambda q, arg1: q.put(spin_up_engine(arg1)),
                            args=(que, [
                                engine, labels, pil_im_segment,
                                default_threshold, [], rectangle
                            ]))
                # thread_list.append(t1)
                t1.start()
                t1.join()
                # objs = engine.detect_with_image(pil_im_segment,
                #                                 threshold=default_threshold,
                #                                 keep_aspect_ratio='store_true',
                #                                 relative_coord=False,
                #                                 top_k=10)
                # cv2_im = detect.append_objs_to_img(cv2_im, objs, labels)
            # for thread in thread_list:
            #     thread.join()
            # Check thread's return value
            detected_labels_all = []
            # # sys.exit()
            #
            while not que.empty():
                #Retreive from threding queue
                objs, labels, detected_labels = que.get()
                #button/Contour detector
                # cv2_im, __ = button_cv_final.find_buttons_in_bbox(cv2_im, objs)
                # draw objects
                cv2_im = detect.append_objs_to_img(cv2_im,
                                                   objs,
                                                   labels,
                                                   bbox_only=False)
                # write_to_temporary_dict(objs) # write to temporary dict
                detected_labels_all += detected_labels
        t1 = time.time()
        frame_count += 1
        if t1 - t0 >= 1.0:
            framerate = frame_count
            frame_count = 0
            t0 = time.time()
        cv2.putText(
            cv2_im, "Framerate: " + str(framerate) + " Framecount: " +
            str(frame_count), (10, 15), fontStyle, 0.5, (255, 255, 255), 2)

        cv2.imshow("Camera Feed", cv2_im)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # cap.stop()
    cap.release()
    # out.release()
    cv2.destroyWindow(str(0))
コード例 #18
0
ファイル: Detector.py プロジェクト: agui1era/Agrifrut
def main():
  model='model.tflite'
  labels='labels.txt'
  img_input='input.jpg'
  img_output='output.jpg'
  limite=0.1
  cantidad=20
  rosada=0
  negra=0

  cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop) 
  filename =  img_input

  # Initialize engine.
  engine = DetectionEngine(model)
  labels = dataset_utils.read_label_file(labels) 


  while cv2.waitKey(1) & 0xFF != ord('q'):
    # Open the url image, set stream to True, this will return the stream content.
    rosada=0
    negra=0
  
    ret,frame = cap.read() # return a single frame in variable `frame`
    cv2.imwrite(img_input,frame)   
   
    # Open image.
    img = Image.open(img_input).convert('RGB')
    #Make the new image half the width and half the height of the original image
    img = img.resize((round(img.size[0]*0.5), round(img.size[1]*0.5)))
 
    draw = ImageDraw.Draw(img)
   
    # Run inference.
    objs = engine.detect_with_image(img,
                                    threshold=limite,
                                    keep_aspect_ratio='store_true',
                                    relative_coord=False,
                                    top_k=cantidad)

    # Print and draw detected objects.
    for obj in objs:
      #print('-----------------------------------------')
      if labels:
        if(labels[obj.label_id] == "negra"):
            negra=negra+1
        if(labels[obj.label_id] == "rosada"):
            rosada=rosada+1
      #print('score =', obj.score)
      box = obj.bounding_box.flatten().tolist()
      #print('box =', box)
      draw.rectangle(box, outline='red')

    if not objs:
      print('No objects detected.')
    print('__________________________________')
    print('')
    print('TOTAL negra: '+str(negra))
    print('TOTAL rosada:'+str(rosada))
    print('__________________________________')
    print('')

    data = {'CAM1':{'negra':str(negra),'rosada':str(rosada)}}

    with open('/var/www/html/data.json', 'w') as outfile:
        json.dump(data, outfile)

    # Save image with bounding boxes.
    if img_output:
      img.save(img_output)
    image = cv2.imread(img_output) 
    
    #cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
    #cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
    #cv2.imshow("window",image)
    
      #closing all open windows  
  cv2.destroyAllWindows()   
  cap.release()   
コード例 #19
0
from PIL import Image

from edgetpu.utils import dataset_utils
from edgetpu.classification.engine import ClassificationEngine

from .utils import piece2id
from .detect_board import get_board_cases

logger = logging.getLogger('reachy.tictactoe')

dir_path = os.path.dirname(os.path.realpath(__file__))
model_path = os.path.join(dir_path, 'models')

boxes_classifier = ClassificationEngine(
    os.path.join(model_path, 'ttt-boxes.tflite'))
boxes_labels = dataset_utils.read_label_file(
    os.path.join(model_path, 'ttt-boxes.txt'))

valid_classifier = ClassificationEngine(
    os.path.join(model_path, 'ttt-valid-board.tflite'))
valid_labels = dataset_utils.read_label_file(
    os.path.join(model_path, 'ttt-valid-board.txt'))

board_cases = np.array(
    (  #Coordinates first board cases (top-left corner) (Xbl, Xbr, Ytr, Ybr)
        (
            (120, 270, 180, 290),
            (270, 420, 180, 290),
            (420, 550, 180, 290),
        ),
        (
            (110, 280, 290, 430),
コード例 #20
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model',
        help=
        'Path of the detection model, it must be a SSD model with postprocessing operator.',
        required=True)
    parser.add_argument('--label', help='Path of the labels file.')
    parser.add_argument(
        '--keep_aspect_ratio',
        dest='keep_aspect_ratio',
        action='store_true',
        help=
        ('keep the image aspect ratio when down-sampling the image by adding '
         'black pixel padding (zeros) on bottom or right. '
         'By default the image is resized and reshaped without cropping. This '
         'option should be the same as what is applied on input images during '
         'model training. Otherwise the accuracy may be affected and the '
         'bounding box of detection result may be stretched.'))
    parser.set_defaults(keep_aspect_ratio=False)
    args = parser.parse_args()

    #Minimum consecutive frames for which eye ratio is below threshold for alarm to be triggered
    EYE_ASPECT_RATIO_CONSEC_FRAMES = 30

    YAWN_THRESH = 20
    YAWN_CONSEC_FRAMES = 20

    #COunts no. of consecutuve frames below threshold value
    COUNTER_EAR = 0
    COUNTER_YAWN = 0

    # Initialize engine.
    print("[INFO] loading Coral model...")
    engine = DetectionEngine(args.model)
    labels = dataset_utils.read_label_file(args.label) if args.label else None

    print("[INFO] loading shape predictor dlib model...")
    predict = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    print("[INFO] loaded shape predictor dlib model...")

    (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
    (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

    blank_img = np.zeros([375, 500, 3], dtype=np.uint8)
    blank_img.fill(255)

    ### Taking still pictures for person-specific thresholds
    camera = PiCamera()

    time.sleep(0.1)

    for i in range(1, 3):
        print('Picture ' + str(i) + ': ' +
              'Please keep your eyes open, as you would do normally')
        msg1 = "Keep your eyes open normally."

        camera.start_preview()
        camera.annotate_text = msg1
        sleep(5)

        camera.capture('pic' + str(i) + '.jpg')

        camera.stop_preview()
        sleep(2)

    eye_img1 = cv2.imread('/home/pi/FaceDetection/pic1.jpg')
    eye_img2 = cv2.imread('/home/pi/FaceDetection/pic2.jpg')

    eye_img12 = cv2.cvtColor(eye_img1, cv2.COLOR_BGR2RGB)
    eye_img22 = cv2.cvtColor(eye_img2, cv2.COLOR_BGR2RGB)

    eye_img13 = Image.fromarray(eye_img12)
    eye_img23 = Image.fromarray(eye_img22)

    results_eye_1 = engine.DetectWithImage(eye_img13,
                                           threshold=0.4,
                                           keep_aspect_ratio=True,
                                           relative_coord=False)
    results_eye_2 = engine.DetectWithImage(eye_img23,
                                           threshold=0.4,
                                           keep_aspect_ratio=True,
                                           relative_coord=False)

    for r in results_eye_1:

        box = r.bounding_box.flatten().astype("int")
        (startX, startY, endX, endY) = box
        cv2.rectangle(eye_img1, (startX, startY), (endX, endY), (0, 255, 0), 2)

        y = startY - 15 if startY - 15 > 15 else startY + 15

        text = "{:.2f}%".format(r.score * 100)

        cv2.putText(eye_img1, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (0, 255, 0), 2)

        left = box[0]
        top = box[1]
        right = box[2]
        bottom = box[3]

        box1 = dlib.rectangle(int(left), int(top), int(right), int(bottom))

        shape = predict(eye_img1, box1)
        shape = face_utils.shape_to_np(shape)

        distance = lip_distance(shape)

        #Get array of coordinates of leftEye and rightEye
        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]

        leftEAR = eye_aspect_ratio(leftEye)
        rightEAR = eye_aspect_ratio(rightEye)
        # average the eye aspect ratio together for both eyes
        ear_1 = (leftEAR + rightEAR) / 2.0

        print('EAR = ' + str(ear_1))

        #Use hull to remove convex contour discrepencies and draw eye shape around eyes
        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)

        lip = shape[48:60]

    for r in results_eye_2:

        box = r.bounding_box.flatten().astype("int")
        (startX, startY, endX, endY) = box
        cv2.rectangle(eye_img2, (startX, startY), (endX, endY), (0, 255, 0), 2)

        y = startY - 15 if startY - 15 > 15 else startY + 15

        text = "{:.2f}%".format(r.score * 100)

        cv2.putText(eye_img2, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (0, 255, 0), 2)

        left = box[0]
        top = box[1]
        right = box[2]
        bottom = box[3]

        box1 = dlib.rectangle(int(left), int(top), int(right), int(bottom))

        shape = predict(eye_img2, box1)
        shape = face_utils.shape_to_np(shape)

        distance = lip_distance(shape)

        #Get array of coordinates of leftEye and rightEye
        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]

        leftEAR = eye_aspect_ratio(leftEye)
        rightEAR = eye_aspect_ratio(rightEye)
        # average the eye aspect ratio together for both eyes
        ear_2 = (leftEAR + rightEAR) / 2.0

        print('EAR = ' + str(ear_2))

        #Use hull to remove convex contour discrepencies and draw eye shape around eyes
        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)

        lip = shape[48:60]

    camera.close()

    avg_ear_nd = (ear_1 + ear_2) * 0.5
    print('Average normal EAR: ' + str(avg_ear_nd))

    EYE_ASPECT_RATIO_THRESHOLD = avg_ear_nd * 0.9

    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    time.sleep(2.0)

    # loop over the frames from the video stream
    while True:
        # grab the frame from the threaded video stream and resize it
        # to have a maximum width of 500 pixels
        frame = vs.read()
        frame = imutils.resize(frame, width=500)
        orig = frame.copy()
        print(orig.shape)

        # prepare the frame for object detection by converting (1) it
        # from BGR to RGB channel ordering and then (2) from a NumPy
        # array to PIL image format
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frame = Image.fromarray(frame)

        # make predictions on the input frame
        #start = time.time()
        #print("Face detection started at: ", start)
        results = engine.DetectWithImage(frame,
                                         threshold=0.4,
                                         keep_aspect_ratio=True,
                                         relative_coord=False)
        ##
        ##        if results == []:
        ##            orig = cv2.addWeighted(orig,0,blank_img,0.6, 0)
        ##            cv2.putText(orig, "Please position your face correctly.", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

        for r in results:

            if (r.score * 100) < 99.20:
                orig = cv2.addWeighted(orig, 0, blank_img, 0.6, 0)
                cv2.putText(
                    orig,
                    "Cannot detect drowsiness. Please position your face towards the camera.",
                    (25, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)

            else:
                # extract the bounding box and box and predicted class label
                box = r.bounding_box.flatten().astype("int")

                (startX, startY, endX, endY) = box

                # draw the bounding box on the image
                cv2.rectangle(orig, (startX, startY), (endX, endY),
                              (0, 255, 0), 2)
                y = startY - 15 if startY - 15 > 15 else startY + 15

                text = "{:.2f}%".format(r.score * 100)

                cv2.putText(orig, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (0, 255, 0), 2)

                left = box[0]
                top = box[1]
                right = box[2]
                bottom = box[3]

                box1 = dlib.rectangle(int(left), int(top), int(right),
                                      int(bottom))

                shape = predict(orig, box1)
                shape = face_utils.shape_to_np(shape)

                distance = lip_distance(shape)

                #Get array of coordinates of leftEye and rightEye
                leftEye = shape[lStart:lEnd]
                rightEye = shape[rStart:rEnd]

                leftEAR = eye_aspect_ratio(leftEye)
                rightEAR = eye_aspect_ratio(rightEye)
                # average the eye aspect ratio together for both eyes
                ear = (leftEAR + rightEAR) / 2.0

                frame = np.array(frame)

                #Use hull to remove convex contour discrepencies and draw eye shape around eyes
                leftEyeHull = cv2.convexHull(leftEye)
                rightEyeHull = cv2.convexHull(rightEye)
                cv2.drawContours(orig, [leftEyeHull], -1, (0, 255, 0), 1)
                cv2.drawContours(orig, [rightEyeHull], -1, (0, 255, 0), 1)
                flag = 0

                lip = shape[48:60]
                cv2.drawContours(orig, [lip], -1, (0, 255, 0), 1)

                #Detect if eye aspect ratio is less than threshold
                if (ear < EYE_ASPECT_RATIO_THRESHOLD):
                    COUNTER_EAR += 1
                    #If no. of frames is greater than threshold frames,
                    if COUNTER_EAR >= EYE_ASPECT_RATIO_CONSEC_FRAMES:
                        flag = 1
                        #cv2.putText(frame, "The driver is drowsy!", (150,200), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,0,255), 2)
                    else:
                        flag = 0
                else:
                    COUNTER_EAR = 0

                if (distance > YAWN_THRESH):
                    #cv2.putText(frame, "The driver is drowsy!", (10, 30),
                    #cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
                    COUNTER_YAWN += 1

                    if COUNTER_YAWN >= YAWN_CONSEC_FRAMES:
                        flag = 1
                    else:
                        flag = 0
                else:
                    COUNTER_YAWN = 0

                if flag == 1:
                    cv2.putText(orig, "The driver is drowsy!", (10, 30),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

            # show the output frame and wait for a key press
        frame = np.array(frame)
        cv2.imshow('Frame', orig)
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

        # do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model',
        help=
        'Path of the detection model, it must be a SSD model with postprocessing operator.',
        required=True)
    parser.add_argument('--label', help='Path of the labels file.')
    parser.add_argument(
        '--keep_aspect_ratio',
        dest='keep_aspect_ratio',
        action='store_true',
        help=
        ('keep the image aspect ratio when down-sampling the image by adding '
         'black pixel padding (zeros) on bottom or right. '
         'By default the image is resized and reshaped without cropping. This '
         'option should be the same as what is applied on input images during '
         'model training. Otherwise the accuracy may be affected and the '
         'bounding box of detection result may be stretched.'))
    parser.set_defaults(keep_aspect_ratio=False)
    args = parser.parse_args()

    EYE_ASPECT_RATIO_THRESHOLD = 0.3
    #Minimum consecutive frames for which eye ratio is below threshold for alarm to be triggered
    EYE_ASPECT_RATIO_CONSEC_FRAMES = 50

    #COunts no. of consecutuve frames below threshold value
    COUNTER = 0

    # Initialize engine.
    print("[INFO] loading Coral model...")
    engine = DetectionEngine(args.model)
    labels = dataset_utils.read_label_file(args.label) if args.label else None

    # initialize the video stream and allow the camera sensor to warmup
    print("[INFO] starting video stream...")
    vs = VideoStream(src=0).start()
    #vs = VideoStream(usePiCamera=True).start()
    time.sleep(2.0)

    print("[INFO] loading shape predictor dlib model...")
    ### Inserting code for detecting face landmarks here
    predict = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    print("[INFO] loaded shape predictor dlib model...")

    (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
    (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

    # loop over the frames from the video stream
    while True:
        # grab the frame from the threaded video stream and resize it
        # to have a maximum width of 500 pixels
        frame = vs.read()
        frame = imutils.resize(frame, width=500)
        orig = frame.copy()

        # prepare the frame for object detection by converting (1) it
        # from BGR to RGB channel ordering and then (2) from a NumPy
        # array to PIL image format
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frame = Image.fromarray(frame)

        # make predictions on the input frame
        start = time.time()
        print("Face detection started at: ", start)
        results = engine.DetectWithImage(frame,
                                         threshold=0.3,
                                         keep_aspect_ratio=True,
                                         relative_coord=False)
        #print(results)
        end = time.time()
        print("Face detection ended at: ", end)

        # loop over the results
        for r in results:
            # extract the bounding box and box and predicted class label
            box = r.bounding_box.flatten().astype("int")

            (startX, startY, endX, endY) = box

            # draw the bounding box on the image
            cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            text = "{:.2f}%".format(r.score * 100)
            cv2.putText(orig, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (0, 255, 0), 2)

            left = box[0]
            top = box[1]
            right = box[2]
            bottom = box[3]

            box1 = dlib.rectangle(int(left), int(top), int(right), int(bottom))

            shape = predict(orig, box1)
            shape = face_utils.shape_to_np(shape)

            #Get array of coordinates of leftEye and rightEye
            leftEye = shape[lStart:lEnd]
            rightEye = shape[rStart:rEnd]

            leftEAR = eye_aspect_ratio(leftEye)
            rightEAR = eye_aspect_ratio(rightEye)
            # average the eye aspect ratio together for both eyes
            ear = (leftEAR + rightEAR) / 2.0

            frame = np.array(frame)

            #Use hull to remove convex contour discrepencies and draw eye shape around eyes
            leftEyeHull = cv2.convexHull(leftEye)
            rightEyeHull = cv2.convexHull(rightEye)
            cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)

            #Detect if eye aspect ratio is less than threshold
            if (ear < EYE_ASPECT_RATIO_THRESHOLD):
                COUNTER += 1
                #If no. of frames is greater than threshold frames,
                if COUNTER >= EYE_ASPECT_RATIO_CONSEC_FRAMES:
                    cv2.putText(frame, "You are Drowsy!", (150, 200),
                                cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2)
            else:
                COUNTER = 0

        # show the output frame and wait for a key press
        frame = np.array(frame)
        cv2.imshow('Frame', frame)
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

    # do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()
コード例 #22
0
from edgetpu.utils import dataset_utils
import threading
import imutils
import time
import cv2
from PIL import Image

box_color = (0, 0, 255)
label_text_color = (255, 255, 255)

outputFrame = None
lock = threading.Lock()
engine = DetectionEngine(
    '/home/pi/Flask-Coral-Edge-TPU/tpu/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite'
)
labels = dataset_utils.read_label_file(
    '/home/pi/Flask-Coral-Edge-TPU/tpu/coco_labels.txt')

app = Flask(__name__)

# vs = VideoStream(usePiCamera=True,  resolution=(320, 240)).start()
# #vs = VideoStream(src=0).start()
cap = cv2.VideoCapture(0)
time.sleep(2.0)


@app.route("/")
def index():
    return render_template("index.html")


def detect_objects():
コード例 #23
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--model',
      help='Path of the detection model, it must be a SSD model with postprocessing operator.',
      required=True)
  parser.add_argument('--label', help='Path of the labels file.')
  
  parser.add_argument(
      '--keep_aspect_ratio',
      dest='keep_aspect_ratio',
      action='store_true',
      help=(
          'keep the image aspect ratio when down-sampling the image by adding '
          'black pixel padding (zeros) on bottom or right. '
          'By default the image is resized and reshaped without cropping. This '
          'option should be the same as what is applied on input images during '
          'model training. Otherwise the accuracy may be affected and the '
          'bounding box of detection result may be stretched.'))
  parser.set_defaults(keep_aspect_ratio=False)
  args = parser.parse_args()


  # Initialize engine.
  engine = DetectionEngine(args.model)
  labels = dataset_utils.read_label_file(args.label) if args.label else None

  cap = cv2.VideoCapture('fast_forward.mp4')
  
  total_time = 0
  load_time = 0
  preproc_time = 0
  infer_time = 0
  postproc_time = 0
  image_count = 0
  for i in range(10):
      while image_count < 10000:
        # Open image.
        start_time = time.time()
        ret, img = cap.read()
        load_time += time.time() - start_time
        temp_time = time.time()
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        im_width, im_height = img.size
        img_resized = img.resize((300,300))
        preproc_time += time.time() - temp_time
        # Run inference.
        temp_time = time.time()
        ans = engine.detect_with_image(
            img_resized,
            threshold=0.5,
            keep_aspect_ratio=args.keep_aspect_ratio,
            relative_coord=False,
            top_k=15)
        infer_time += time.time() - temp_time
        temp_time = time.time()

        draw = ImageDraw.Draw(img)
        # Display result.
        if ans:
            for obj in ans:
                box = obj.bounding_box.flatten().tolist()
                # Draw a rectangle.
                draw.rectangle([box[0]* im_width / 300, box[1]* im_height /300, box[2]* im_width /300, box[3] * im_height / 300], outline='red')
        #img.save('output/' + file_name[file_name.rfind('/'):])
        postproc_time += time.time() - temp_time
        total_time += time.time() - start_time
        image_count += 1
        if image_count % 50 == 0:
            print('num of inferred images: ', image_count)

  print('total images used for benchmark: ' + str(image_count))
  print('total time (s): ' + str(total_time))
  print('total load time (s): ' + str(load_time))
  print('total preprocess time (s): ' + str(preproc_time))
  print('total inference time (s): ' + str(infer_time) )
  print('total postrocess time (s): ' + str(postproc_time))

  print('average total time (s): ' + str(total_time / image_count))
  print('average load time (s): ' + str(load_time / image_count))
  print('average preprocess time (s): ' + str(preproc_time / image_count))
  print('average inference time (s): ' + str(infer_time / image_count))
  print('average postrocess time (s): ' + str(postproc_time / image_count))
コード例 #24
0
 def __init__(self, using_model: str, label_file: str):
     # Prepare labels.
     self.labels = dataset_utils.read_label_file(label_file)
     # Initialize engine.
     self.engine = ClassificationEngine(using_model)
コード例 #25
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--model',
        help='File path of Tflite model.',
        default=os.path.join(
            'all_models',
            'mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite'))
    parser.add_argument('--label',
                        help='File path of label file.',
                        default=os.path.join('all_models',
                                             'inat_bird_labels.txt'))
    parser.add_argument('--image',
                        help='File path of the image to be recognized.',
                        required=False)
    parser.add_argument('--dir',
                        help='File path of the dir to be recognized.',
                        required=False)
    parser.add_argument('--dryrun',
                        help='Whether to actually move files or not.',
                        required=False,
                        default=False)
    args = parser.parse_args()

    # Prepare labels.
    labels = dataset_utils.read_label_file(args.label)
    # Initialize engine.
    engine = ClassificationEngine(args.model)

    # Run inference.
    if args.image:
        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])
    if args.dir:
        f = []
        for (dirpath, dirnames, filenames) in os.walk(args.dir):
            for filename in filenames:
                try:
                    filepath = "{}/{}".format(dirpath, filename)
                    if "boxed" in filename:
                        print("attempting to classify {}".format(filepath))
                        img = Image.open(filepath)
                        for result in engine.classify_with_image(img, top_k=3):
                            label = labels[result[0]]
                            percent = int(100 * result[1])
                            if label != "background":
                                print('dirpath', dirpath)
                                path_sections = dirpath.split("/")
                                new_dir = "/var/www/html/classified/"
                                if len(path_sections) == 4:
                                    date = path_sections[2]
                                    visitation_id = path_sections[3]
                                    new_dir = "/var/www/html/classified/{}/{}".format(
                                        date, visitation_id)
                                newname = filename.replace(
                                    ".png", "_{}_{}.png".format(
                                        label.replace(" ", "-"), percent))
                                newpath = "{}/{}".format(new_dir, newname)
                                print('move {} -> {}'.format(
                                    filepath, newpath))
                                print('dryrun', args.dryrun)
                                if args.dryrun == False:
                                    if not os.path.exists(new_dir):
                                        os.makedirs(new_dir)
                                    shutil.move(os.path.abspath(filepath),
                                                os.path.abspath(newpath))
                    if "full" in filename:
                        new_dir = get_new_dir(dirpath)
                        print('new full image dir {}'.format(new_dir))
                        new_path = "{}/{}".format(new_dir, filename)
                        if os.path.exists(new_dir):
                            print('full image move {} -> {}'.format(
                                os.path.abspath(filepath),
                                os.path.abspath(new_path)))
                            if args.dryrun == False:
                                shutil.move(os.path.abspath(filepath),
                                            os.path.abspath(new_path))
                        else:
                            print('full image new directory doesnt exist')
                except:
                    print("failed to classify ")
コード例 #26
0
import re
from io import BytesIO
import time
import re
import picamera
import io

from edgetpu.detection.engine import DetectionEngine
from edgetpu.utils import dataset_utils
from PIL import Image
from PIL import ImageDraw

THRESHOLD = 0.5
picam = picamera.PiCamera()
engine = DetectionEngine('detect.tflite')
labels = dataset_utils.read_label_file('labelmap.txt')


def draw_objects(draw, objs, labels):
    for obj in objs:

        bbox = obj.bounding_box.flatten().tolist()

        draw.rectangle(bbox, outline='red')
        draw.text((bbox[0] + 10, bbox[1] + 10),
                  '%s\n%.2f' % (labels[obj.label_id], obj.score),
                  fill='red')


class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
    # check for security token
コード例 #27
0
import cv2, os
from edgetpu.detection.engine import DetectionEngine
from edgetpu.utils import dataset_utils
from PIL import Image
import logging

LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
logging.basicConfig(level=LOGLEVEL)

#Constants
LABELS_PATH = "/models/labels.txt"
MODEL_PATH = "/models/model.tflite"

try:
    # Prepare labels.
    labels = dataset_utils.read_label_file(LABELS_PATH)
    logging.debug("Labels loaded")
except:
    logging.error("Error loading labels")
    exit(1)

try:
    # Initialize engine.
    engine = DetectionEngine(MODEL_PATH)
    logging.debug("Model loaded")
except:
    logging.error("Error loading model")
    exit(1)


def objects(frame):
コード例 #28
0
import imutils
import time
import cv2
from PIL import Image

box_color = (0, 0, 255)
label_text_color = (255, 255, 255)

outputFrame = None

model = "model.tflite"
labels = "labels.txt"

lock = threading.Lock()
engine = DetectionEngine(model)
labels = dataset_utils.read_label_file(labels)

app = Flask(__name__)

vs = VideoStream(src=0).start()
time.sleep(2.0)


@app.route("/")
def index():
    return render_template("index.html")


def detect_objects():

    global cap, outputFrame, lock
コード例 #29
0
import threading
import imutils
import time
import cv2
from PIL import Image

box_color = (0, 0, 255)
label_text_color = (255, 255, 255)

outputFrame = None
lock = threading.Lock()

engine = DetectionEngine(
    '/home/mendel/example/Flask-Coral-Edge-TPU/tpu/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite'
)
labels = dataset_utils.read_label_file(
    '/home/mendel/example/Flask-Coral-Edge-TPU/tpu/coco_labels.txt')

app = Flask(__name__)

# vs = VideoStream(usePiCamera=True,  resolution=(320, 240)).start()
# #vs = VideoStream(src=0).start()
#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture(1)
time.sleep(2.0)
cap.set(5, 30)


@app.route("/")
def index():
    return render_template("index.html")