class BirdInference(): def __init__(self): print("starting inference..") self.inference = ImageInference( inaturalist_classification.model(inaturalist_classification.BIRDS)) def run(self, image): self.result = self.inference.run(image) self.bird_class = inaturalist_classification.get_classes(self.result, top_k=1, threshold=0.8) if len(self.bird_class) == 0: # if nothing is found, return none return None elif self.bird_class[0][ 0] == 'background': # if background is found, return none return None else: # If a bird clas is returned, flip the image, run again and ensure same class is returned self.result_2 = self.inference.run(ImageOps.mirror(image)) self.bird_class_2 = inaturalist_classification.get_classes( self.result_2, top_k=1, threshold=0.8) if len(self.bird_class_2) == 0: return None else: if self.bird_class_2[0][0] == self.bird_class[0][0]: return self.bird_class[0] else: return None
def __init__(self, model, frameWidth=240, frameHeight=240, DEBUG=False): # Init the stuff we are inheriting from super().__init__() self.DEBUG = DEBUG self.inference = ImageInference(model) # Set video frame parameters self.frameWidth = frameWidth self.frameHeight = frameHeight self.prev_time = time.time() self.output = None
def main(): parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--input', '-i', dest='input', required=True, help='Input image file.') parser.add_argument('--output', '-o', dest='output', help='Output image file with bounding boxes.') parser.add_argument('--sparse', '-s', action='store_true', default=False, help='Use sparse tensors.') parser.add_argument('--threshold', '-t', type=float, default=0.3, help='Detection probability threshold.') args = parser.parse_args() with ImageInference(object_detection.model()) as inference: image = Image.open(args.input) image_center, offset = crop_center(image) if args.sparse: result = inference.run(image_center, sparse_configs=object_detection.sparse_configs(args.threshold)) objects = object_detection.get_objects_sparse(result, offset) else: result = inference.run(image_center) objects = object_detection.get_objects(result, args.threshold, offset) for i, obj in enumerate(objects): print('Object #%d: %s' % (i, obj)) if args.output: draw = ImageDraw.Draw(image) for i, obj in enumerate(objects): x, y, width, height = obj.bounding_box draw.rectangle((x, y, x + width, y + height), outline='red') image.save(args.output)
def main(): parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--input', '-i', required=True, help='Input image file.') parser.add_argument('--threshold', '-t', type=float, default=0.1, help='Classification probability threshold.') parser.add_argument('--top_k', '-n', type=int, default=5, help='Max number of returned classes.') parser.add_argument('--sparse', '-s', action='store_true', default=False, help='Use sparse tensors.') parser.add_argument('--model', '-m', choices=('plants', 'insects', 'birds'), required=True, help='Model to run.') args = parser.parse_args() model_type = {'plants': inaturalist_classification.PLANTS, 'insects': inaturalist_classification.INSECTS, 'birds': inaturalist_classification.BIRDS}[args.model] with ImageInference(inaturalist_classification.model(model_type)) as inference: image = Image.open(args.input) if args.sparse: configs = inaturalist_classification.sparse_configs(top_k=args.top_k, threshold=args.threshold, model_type=model_type) result = inference.run(image, sparse_configs=configs) classes = inaturalist_classification.get_classes_sparse(result) else: result = inference.run(image) classes = inaturalist_classification.get_classes(result, top_k=args.top_k, threshold=args.threshold) for i, (label, score) in enumerate(classes): print('Result %d: %s (prob=%f)' % (i, label, score))
def do_inference(self): # Start the video process with ImageInference(object_detection.model()) as inference: with ImgCap(inference, self.lock, self.args, self.DEBUG) as self.img: self.camera.start_recording(self.img, format='rgb', splitter_port=1) try: while not self.shutdown: self.camera.wait_recording( timeout=0 ) # using timeout=0, default, it'll return immediately time.sleep(1 / self.args.fps) except KeyboardInterrupt: pass finally: self.camera.stop_recording(splitter_port=1) print("Camera closed!") if hasattr(self.img, 'last_image'): image = Image.fromarray(self.img.last_image) draw = ImageDraw.Draw(image) for obj in self.img.last_objects: x, y, width, height = obj.bounding_box draw.rectangle((x, y, x + width, y + height), outline='red') image.save("last_image.jpg")
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model_name', default='test_model', help='Model identifier.') parser.add_argument('--model_path', required=True, help='Path to model file.') parser.add_argument('--test_file', default=None, help='Path to test file.') args = parser.parse_args() model = ModelDescriptor(name=args.model_name, input_shape=(1, 192, 192, 3), input_normalizer=(0, 1), compute_graph=utils.load_compute_graph( args.model_path)) if args.test_file: with ImageInference(model) as inference: image = Image.open(args.test_file) result = inference.run(image) print(tensors_info(result.tensors)) return with PiCamera(sensor_mode=4, framerate=30): with CameraInference(model) as inference: for result in inference.run(): print('#%05d (%5.2f fps): %s' % (inference.count, inference.rate, tensors_info(result.tensors)))
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--input', '-i', required=True, help='Input image file.') parser.add_argument('--threshold', '-t', type=float, default=0.1, help='Classification probability threshold.') parser.add_argument('--top_k', '-n', type=int, default=5, help='Max number of returned classes.') parser.add_argument('--sparse', '-s', action='store_true', default=False, help='Use sparse tensors.') parser.add_argument('--model', '-m', choices=('squeezenet', 'mobilenet'), default='mobilenet', help='Model to run.') args = parser.parse_args() # There are two models available for image classification task: # 1) MobileNet based (image_classification.MOBILENET), which has 59.9% top-1 # accuracy on ImageNet; # 2) SqueezeNet based (image_classification.SQUEEZENET), which has 45.3% top-1 # accuracy on ImageNet; model_type = { 'squeezenet': image_classification.SQUEEZENET, 'mobilenet': image_classification.MOBILENET }[args.model] with ImageInference(image_classification.model(model_type)) as inference: image = Image.open(args.input) if args.sparse: configs = image_classification.sparse_configs( top_k=args.top_k, threshold=args.threshold, model_type=model_type) result = inference.run(image, sparse_configs=configs) classes = image_classification.get_classes_sparse(result) else: result = inference.run(image) classes = image_classification.get_classes( result, top_k=args.top_k, threshold=args.threshold) for i, (label, score) in enumerate(classes): print('Result %d: %s (prob=%f)' % (i, label, score))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', dest='input', required=True) args = parser.parse_args() with ImageInference(image_classification.model()) as inference: image = Image.open(args.input) classes = image_classification.get_classes(inference.run(image)) for i, (label, score) in enumerate(classes): print('Result %d: %s (prob=%f)' % (i, label, score))
def testDog(self): with TestImage('dog.jpg') as image: image_center, offset = _crop_center(image) with ImageInference(object_detection.model()) as inference: objects = object_detection.get_objects( inference.run(image_center), 0.3, offset) self.assertEqual(1, len(objects)) self.assertEqual(object_detection.Object.DOG, objects[0].kind) self.assertAlmostEqual(0.914, objects[0].score, delta=0.001) self.assertEqual((52, 116, 570, 485), objects[0].bounding_box)
def __init__(self, model, frameWidth=240, frameHeight=240, DEBUG=False): # Init the stuff we are inheriting from super().__init__() self.inference = ImageInference(model) self.ANCHORS = np.genfromtxt( "/opt/aiy/models/mobilenet_ssd_256res_0.125_person_cat_dog_anchors.txt" ) self.DEBUG = DEBUG # Set video frame parameters self.frameWidth = frameWidth self.frameHeight = frameHeight self.prev_time = time.time() self.output = None
def testFaceDetectionWithParams(self): with TestImage('faces.jpg') as image: with ImageInference(face_detection.model()) as inference: params = {'max_face_size': 500} faces = face_detection.get_faces(inference.run(image, params)) self.assertEqual(1, len(faces)) face0 = faces[0] self.assertAlmostEqual(face0.face_score, 0.884, delta=0.001) self.assertAlmostEqual(face0.joy_score, 0.073, delta=0.001) self.assertEqual((748.0, 1063.0, 496.0, 496.0), face0.bounding_box)
def testHotdog(self): with TestImage('hotdog.jpg') as image: with ImageInference(dish_classification.model()) as inference: classes = dish_classification.get_classes(inference.run(image)) label, score = classes[0] self.assertEqual('Hot dog', label) self.assertAlmostEqual(score, 0.744, delta=0.001) label, score = classes[1] self.assertEqual('Lobster roll', label) self.assertAlmostEqual(score, 0.119, delta=0.001)
def process(self, file): model_type = image_classification.MOBILENET with ImageInference( image_classification.model(model_type)) as inference: image = file classes = image_classification.get_classes( inference.run(image), max_num_objects=5, object_prob_threshold=0.1) for i, (label, score) in enumerate(classes): print('Result %d: %s (prob=%f)' % (i, label, score))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', dest='input', required=True) args = parser.parse_args() with ImageInference(dish_classifier.model()) as inference: image = Image.open(args.input) classes = dish_classifier.get_classes( inference.run(image), max_num_objects=5, object_prob_threshold=0.1) for i, (label, score) in enumerate(classes): print('Result %d: %s (prob=%f)' % (i, label, score))
def testCat(self): with TestImage('cat.jpg') as image: image_center, offset = _crop_center(image) with ImageInference(object_detection.model()) as inference: objects = object_detection.get_objects( inference.run(image_center), 0.3, offset) print(objects[0]) self.assertEqual(1, len(objects)) self.assertEqual(object_detection.Object.CAT, objects[0].kind) self.assertAlmostEqual(0.672, objects[0].score, delta=0.001) self.assertEqual((575, 586, 2187, 1758), objects[0].bounding_box)
def test_detection(self): with TestImage(self.image_file) as image: image_center, offset = crop_center(image) with ImageInference(od.model()) as inference: if self.sparse: sparse_configs = od.sparse_configs(threshold=self.THRESHOLD) result = inference.run(image_center, sparse_configs=sparse_configs) objects = od.get_objects_sparse(result, offset) else: result = inference.run(image_center) objects = od.get_objects(result, self.THRESHOLD, offset) self.check(objects)
class ImgCap(io.IOBase): ''' Capturing Image from a Raspicam (V2.1) ''' def __init__(self, model, frameWidth=240, frameHeight=240, DEBUG=False): # Init the stuff we are inheriting from super().__init__() self.DEBUG = DEBUG self.inference = ImageInference(model) # Set video frame parameters self.frameWidth = frameWidth self.frameHeight = frameHeight self.prev_time = time.time() self.output = None def writable(self): ''' To be a nice file, you must have this method ''' return True def write(self, b): ''' Here is where the image data is received and made available at self.output ''' try: # b is the numpy array of the image, 3 bytes of color depth self.output = np.reshape(np.frombuffer(b, dtype=np.uint8), (self.frameHeight, self.frameWidth, 3)) image_center, offset = crop_center(Image.fromarray(self.output)) result = self.inference.run(image_center) if self.DEBUG: print(f"ImgCap - Inference result: {result}") print(f"ImgCap - Image.shape {self.output.shape}") print( f"ImgCap - Running at {1/(time.time()-self.prev_time):2.2f} Hz" ) self.prev_time = time.time() except Exception as e: print("ImgCap error: {}".format(e)) finally: return len(b)
def classify(FilePath): #parser = argparse.ArgumentParser() #parser.add_argument('--input', '-i', dest='input', required=True) #args = parser.parse_args() with ImageInference(foodtype_model.model()) as inference: image = Image.open(FilePath) classes = foodtype_model.get_classes(inference.run(image), max_num_objects=5, object_prob_threshold=0.1) for i, (label, score) in enumerate(classes): if i == 0: return label
def testDogMobilenet(self): with TestImage('dog.jpg') as image: with ImageInference( image_classification.model( image_classification.MOBILENET)) as inference: classes = image_classification.get_classes( inference.run(image)) label, score = classes[0] self.assertEqual('boxer', label) self.assertAlmostEqual(score, 0.684, delta=0.001) label, score = classes[1] self.assertEqual('bull mastiff', label) self.assertAlmostEqual(score, 0.222, delta=0.001)
def test_classification(self): with TestImage(self.image_file) as image: with ImageInference(ic.model(self.model_type)) as inference: if self.sparse: sparse_configs = ic.sparse_configs(top_k=self.TOP_K, threshold=self.THRESHOLD, model_type=self.model_type) result = inference.run(image, sparse_configs=sparse_configs) classes = ic.get_classes_sparse(result) else: result = inference.run(image) classes = ic.get_classes(result, top_k=self.TOP_K, threshold=self.THRESHOLD) self.check(classes)
def testDogSqueezenet(self): with TestImage('dog.jpg') as image: with ImageInference( image_classification.model( image_classification.SQUEEZENET)) as inference: classes = image_classification.get_classes( inference.run(image)) label, score = classes[0] self.assertEqual('pug/pug-dog', label) self.assertAlmostEqual(score, 0.271, delta=0.001) label, score = classes[1] self.assertEqual('bull mastiff', label) self.assertAlmostEqual(score, 0.141, delta=0.001)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', dest='input', required=True) parser.add_argument('--output', '-o', dest='output') args = parser.parse_args() with ImageInference(face_detection.model()) as inference: image = Image.open(args.input) draw = ImageDraw.Draw(image) for i, face in enumerate(face_detection.get_faces(inference.run(image))): print('Face #%d: %s' % (i, str(face))) x, y, width, height = face.bounding_box draw.rectangle((x, y, x + width, y + height), outline='red') if args.output: image.save(args.output)
def testFaceDetection(self): with TestImage('faces.jpg') as image: with ImageInference(face_detection.model()) as inference: faces = face_detection.get_faces(inference.run(image)) self.assertEqual(2, len(faces)) face0 = faces[0] self.assertAlmostEqual(face0.face_score, 1.0, delta=0.001) self.assertAlmostEqual(face0.joy_score, 0.969, delta=0.001) self.assertEqual((812.0, 44.0, 1000.0, 1000.0), face0.bounding_box) face1 = faces[1] self.assertAlmostEqual(face1.face_score, 0.884, delta=0.001) self.assertAlmostEqual(face1.joy_score, 0.073, delta=0.001) self.assertEqual((748.0, 1063.0, 496.0, 496.0), face1.bounding_box)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', dest='input', required=True) parser.add_argument('--output', '-o', dest='output') args = parser.parse_args() with ImageInference(dish_detection.model()) as inference: image = Image.open(read_stdin() if args.input == '-' else args.input) draw = ImageDraw.Draw(image) dishes = dish_detection.get_dishes(inference.run(image)) for i, dish in enumerate(dishes): print('Dish #%d: %s' % (i, dish)) x, y, width, height = dish.bounding_box draw.rectangle((x, y, x + width, y + height), outline='red') if args.output: image.save(args.output)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', dest='input', required=True) parser.add_argument('--output', '-o', dest='output') args = parser.parse_args() with ImageInference(object_detection.model()) as inference: image = Image.open(args.input) image_center, offset = _crop_center(image) draw = ImageDraw.Draw(image) result = inference.run(image_center) for i, obj in enumerate(object_detection.get_objects(result, 0.3, offset)): print('Object #%d: %s' % (i, str(obj))) x, y, width, height = obj.bounding_box draw.rectangle((x, y, x + width, y + height), outline='red') if args.output: image.save(args.output)
def testHotdog(self): with TestImage('hotdog.jpg') as image: with ImageInference(dish_detection.model()) as inference: dishes = dish_detection.get_dishes(inference.run(image), top_k=3, threshold=0.1) self.assertEqual(1, len(dishes)) dish = dishes[0] self.assertEqual((417.0, 51.0, 2438.0, 2388.0), dish.bounding_box) self.assertEqual(2, len(dish.sorted_scores)) label, score = dish.sorted_scores[0] self.assertEqual('Hot dog', label) self.assertAlmostEqual(0.223, score, delta=0.001) label, score = dish.sorted_scores[1] self.assertEqual('Bento', label) self.assertAlmostEqual(0.152, score, delta=0.001)
def main(): # run forever while (True): # Google object detection code adapted to use image from camera instead of # one entered at command line with ImageInference(object_detection.model()) as inference: # take a photo to check overwrite last image with picamera.PiCamera() as camera: h, w = camera.resolution camera.capture('test.jpg') # google code, open image check for objects image = Image.open('test.jpg') image_center, offset = _crop_center(image) draw = ImageDraw.Draw(image) result = inference.run(image_center) # draw boxes around any cat, dogs or humans count_objects = 0 for i, obj in enumerate( object_detection.get_objects(result, 0.3, offset)): print('Object #%d: %s' % (i, str(obj))) x, y, width, height = obj.bounding_box draw.rectangle((x, y, x + width, y + height), outline='red') # if a cat, dog or human in image save it with a timestamp count_objects += 1 # save images with timestmp if cat,dog,human in image if count_objects > 0: time_string = time.strftime("%m%d-%H%M%S") filename = '/home/pi/security_camera/images/' + time_string + '.jpg' #print(filename) image.save(filename) # take a 15 second break between photos time.sleep(15)
def recognize(inputfile, outputfile, outputfile_detected): threshold = 0.3 if inputfile == None: # camera capture with PiCamera() as camera: camera.resolution = (1640, 922) # Full Frame, 16:9 (Camera v2) camera.start_preview() while True: camera.capture(outputfile) image = Image.open(outputfile) image_center, offset = crop_center(image) draw = ImageDraw.Draw(image) is_pet = False with ImageInference(object_detection.model()) as inference: result = inference.run(image_center) objects = object_detection.get_objects(result, threshold, offset) for i, obj in enumerate(objects): print('Object #%d kind%d: %s' % (i, obj.kind, obj)) if obj.kind>1: is_pet = True x0, y0, width, height = obj.bounding_box x1 = x0+width y1 = y0+height d = 5 draw.rectangle((x0, y0, x0+d, y1), fill='red', outline='red') draw.rectangle((x0, y0, x1, y0+d), fill='red', outline='red') draw.rectangle((x0, y1-d, x1, y1), fill='red', outline='red') draw.rectangle((x1-d, y0, x1, y1), fill='red', outline='red') image.save(outputfile) time.sleep(1) # if pet deteced, update pet image in AWS S3 if is_pet: s3.meta.client.upload_file(outputfile, 'iot6765project', 'webapp/'+outputfile_detected, ExtraArgs={'ACL':'public-read'}) else: print('No Pet Detected') # update result image in AWS S3 s3.meta.client.upload_file(outputfile, 'iot6765project', 'webapp/'+outputfile, ExtraArgs={'ACL':'public-read'}) camera.stop_preview()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--input', '-i', dest='input', required=True) args = parser.parse_args() # There are two models available for image classification task: # 1) MobileNet based (image_classification.MOBILENET), which has 59.9% top-1 # accuracy on ImageNet; # 2) SqueezeNet based (image_classification.SQUEEZENET), which has 45.3% top-1 # accuracy on ImageNet; model_type = image_classification.MOBILENET with ImageInference(image_classification.model(model_type)) as inference: image = Image.open( io.BytesIO(sys.stdin.buffer.read()) if args.input == '-' else args.input) classes = image_classification.get_classes(inference.run(image), max_num_objects=5, object_prob_threshold=0.1) for i, (label, score) in enumerate(classes): print('Result %d: %s (prob=%f)' % (i, label, score))
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--input', '-i', dest='input', required=True, help='Input image file.') parser.add_argument('--threshold', '-t', type=float, default=0.3, help='Detection probability threshold.') args = parser.parse_args() with ImageInference(park_detection_model.model()) as inference: image = Image.open(args.input) image_center = crop_object(image, locations['dog_park']) result = inference.run(image_center) processed_result = process(result) print(processed_result)