def main(): parser = argparse.ArgumentParser( 'Image classification camera inference example.') parser.add_argument( '--num_frames', '-n', type=int, dest='num_frames', default=None, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--num_objects', '-c', type=int, dest='num_objects', default=3, help='Sets the number of object interences to print.') args = parser.parse_args() # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes with PiCamera(sensor_mode=4, framerate=30) as camera: camera.start_preview() with CameraInference(image_classification.model()) as inference: for result in inference.run(args.num_frames): classes = image_classification.get_classes(result) print(classes_info(classes, args.num_objects)) camera.stop_preview()
def main(): parser = argparse.ArgumentParser('Image classification camera inference example.') parser.add_argument('--num_frames', '-n', type=int, default=None, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--num_objects', '-c', type=int, default=3, help='Sets the number of object interences to print.') parser.add_argument('--nopreview', dest='preview', action='store_false', default=True, help='Enable camera preview') parser.add_argument('--threshold', '-t', type=float, default=0.1, help='Classification probability threshold.') parser.add_argument('--top_k', '-k', type=int, default=3, 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 PiCamera(sensor_mode=4, framerate=10) as camera, \ CameraPreview(camera, enabled=args.preview), \ CameraInference(inaturalist_classification.model(model_type)) as inference: for result in inference.run(args.num_frames): classes = inaturalist_classification.get_classes(result, top_k=args.top_k, threshold=args.threshold) print(classes_info(classes)) if classes: camera.annotate_text = '%s (%.2f)' % classes[0]
def main(): parser = argparse.ArgumentParser() parser.add_argument('--num_frames', '-n', type=int, dest='num_frames', default=None, help='Sets the number of frames to run for, otherwise runs forever.') args = parser.parse_args() with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera: camera.start_preview() last_time = datetime.now() with CameraInference(object_detection.model()) as inference: for result in inference.run(args.num_frames): objects = object_detection.get_objects(result) #print('#%05d (%5.2f fps): num_objects=%d, objects=%s' % # (inference.count, inference.rate, len(objects), objects)) if len(objects) > 0: print(f"num_objects={len(objects)}, objects={[objectLabel(obj.kind) for obj in objects]}") if hasPerson(objects): diff_time = datetime.now() - last_time if diff_time.seconds > 3: print(diff_time) last_time = datetime.now() camera.capture('/home/pi/Pictures/person_%d%02d%02d-%02d%02d%02d.jpg' % (last_time.year, last_time.month, last_time.day, last_time.hour, last_time.minute, last_time.second)) camera.stop_preview()
def run(self): while self._running: try: self.logger.debug('loading inference model ...') with CameraInference(face_detection.model()) as inference: self.logger.debug('running inference ...') for result in inference.run(): faces = face_detection.get_faces(result) if faces: self.logger.debug('found {} faces'.format( len(faces))) outgoing_signals = [] for face in faces: signal_dict = { 'bounding_box': face.bounding_box, 'face_score': face.face_score, 'joy_score': face.joy_score, } outgoing_signal = Signal(signal_dict) outgoing_signals.append(outgoing_signal) if not self._running: break self.notify_signals(outgoing_signals) except: self.logger.exception('failed to get inference result!') self.reset_camera() self.release_camera()
def run_inference(num_frames, on_loaded): """Yields (faces, (frame_width, frame_height)) tuples.""" with CameraInference(face_detection.model()) as inference: on_loaded() for result in inference.run(num_frames): yield face_detection.get_faces(result), (result.width, result.height)
def main(): parser = argparse.ArgumentParser( description='Example application for displaying a dial indicator for ' 'what object is seen') parser.add_argument( '--output_overlay', default=True, type=bool, help='Should the visual overlay be generated') parser.add_argument( '--button_enabled', default=True, type=bool, help='Should the button be monitored') parser.add_argument( '--button_active', default=False, type=bool, help='Should the button start out active (true) or only be active once ' 'pressed (false)') flags = parser.parse_args() load_model = time.time() category_count = len(category_mapper.get_categories()) button = AutoButton(flags.button_active, flags.button_enabled) for category in category_mapper.get_categories(): print('Category[%d]: %s' % (category_mapper.get_category_index(category), category)) with picamera.PiCamera() as camera: camera.resolution = (1640, 1232) camera.start_preview() overlay = OverlayManager( camera) if flags.output_overlay else DummyOverlayManager() servo = AngularServo(PIN_A, min_pulse_width=.0005, max_pulse_width=.0019) with CameraInference(image_classification.model()) as classifier: print('Load Model %f' % (time.time() - load_model)) for result in classifier.run(): if not button.on(): overlay.clear() servo.angle = -90 continue classes = image_classification.get_classes(result) probs = [0] * (category_count + 1) result_categories = [] for label, score in classes: category = category_mapper.get_category(label) or 'Other' probs[category_mapper.get_category_index(category) + 1] += score result_categories.append(category) overlay.update(classes, result_categories) max_prob = max(probs) best_category = probs.index(max_prob) if best_category == 0 and max_prob > .5: servo.angle = -90 elif best_category != 0: servo.angle = -90 + (180 * best_category) / category_count print('category: %d - %s' % (best_category, category_mapper.get_categories()[best_category - 1]))
def main(): print('Human detection') # Turn on the LED so we know the box is ready leds.pattern = Pattern.breathe(1000) leds.update(Leds.rgb_pattern(RED)) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) with PiCamera() as camera: # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. camera.sensor_mode = 4 # Set camera to match camera.resolution = (1640, 1232) camera.framerate = 30 with CameraInference(object_detection.model()) as inference: for i, result in enumerate(inference.run()): for i, obj in enumerate( object_detection.get_objects(result, 0.3)): if obj.score > 0.7 and obj.kind == 1: # Person print('Human detected #%d: %s' % (i, str(obj))) x, y, width, height = obj.bounding_box squirt((x + (width / 2) - (1640 / 2)) / 1640)
def main(): importJsonData() ser.write(("START").encode()) ser.write(str.encode('\n')) parser = argparse.ArgumentParser( 'Image classification camera inference example.') parser.add_argument( '--num_frames', '-n', type=int, default=None, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--num_objects', '-c', type=int, default=3, help='Sets the number of object interences to print.') parser.add_argument('--nopreview', dest='preview', action='store_false', default=True, help='Enable camera preview') args = parser.parse_args() with PiCamera(sensor_mode=4, framerate=10) as camera, \ CameraPreview(camera, enabled=args.preview), \ CameraInference(image_classification.model()) as inference: camera.vflip = True for result in inference.run(args.num_frames): classes = image_classification.get_classes(result, top_k=args.num_objects) className = classes[0][0].split('/')[ 0] #this is because I only want one category at the time print(className) index = getClassIndex(className) print(index) smorfia_number = getSmorfiaNumber(index) print(smorfia_number) smorfia_label = getSmorfiaLabel(smorfia_number) print(smorfia_label) ser.write((str(smorfia_number)).encode()) ser.write(str.encode(':')) ser.write(smorfia_label.encode()) ser.write(str.encode('\n')) print('\n') time.sleep( 1) # Delays for 5 seconds. You can also use a float value. if classes: camera.annotate_text = '%s (%.2f)' % classes[0]
def _run_detector(self): with PiCamera() as camera, PrivacyLED(): # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. camera.sensor_mode = 4 camera.resolution = (1640, 1232) camera.framerate = 15 # Blend the preview layer with the alpha value from the flags. camera.start_preview(alpha=self._preview_alpha) with CameraInference(face_detection.model()) as inference: self._play_sound(MODEL_LOAD_SOUND) self._animator.start() for i, result in enumerate(inference.run()): faces = face_detection.get_faces(result) # Calculate joy score as an average for all detected faces. joy_score = 0.0 if faces: joy_score = sum([face.joy_score for face in faces]) / len(faces) # Append new joy score to the window and calculate mean value. self._joy_score_window.append(joy_score) self.joy_score = sum(self._joy_score_window) / len( self._joy_score_window) if self._num_frames == i or not self._run_event.is_set(): break
def run(self): while self._running: try: self.logger.debug('loading inference model ...') with CameraInference( image_classification.model()) as inference: self.logger.debug('running inference ...') for result in inference.run(): predictions = image_classification.get_classes( result, top_k=self.top_k_predictions(), threshold=0) outgoing_signals = [] for label, score in predictions: signal_dict = { 'label': label, 'score': score, } outgoing_signal = Signal(signal_dict) outgoing_signals.append(outgoing_signal) if not self._running: break self.notify_signals(outgoing_signals) except: self.logger.exception('failed to get inference result!') self.reset_camera() self.release_camera()
def facedetect(): with PiCamera() as camera, Leds() as leds: # Configure camera camera.resolution = (1640, 922) # Full Frame, 16:9 (Camera v2) camera.start_preview() leds.update(Leds.privacy_on()) # Do inference on VisionBonnet with CameraInference(face_detection.model()) as inference: for result in inference.run(): if len(face_detection.get_faces(result)) >= 1: camera.capture( 'faces_' + str(datetime.datetime.now()) + '.jpg') # print(device.is_active) print(led.is_active) # device.on() # bz.on() led.on() print(led.is_active) # time.sleep(1) # print(device.is_active) led.off() print(led.is_active) break # Stop preview camera.stop_preview() leds.update(Leds.privacy_on())
def detect_face(): with CameraInference(face_detection.model()) as camera_inference: counter = 1 x_history, y_history, w_history, h_history = [], [], [], [] for result in camera_inference.run(): check_termination_trigger() faces = face_detection.get_faces(result) face = select_face(faces) if face: x, y, w, h = face.bounding_box x_err = error_update(x_history, x) y_err = error_update(y_history, y) w_err = error_update(w_history, w) h_err = error_update(h_history, h) if face_detection_is_stable(x_err, y_err, w_err, h_err, cutoff=0.03): face_box = (int(sum(x_history) / len(x_history)), int(sum(y_history) / len(y_history)), int(sum(w_history) / len(w_history)), int(sum(h_history) / len(h_history))) break counter += 1 return face_box
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(): num_frames = 90 with PiCamera() as camera: camera.sensor_mode = 4 camera.resolution = (1640,1232) camera.framerate = 30 camera.start_preview() annotator = Annotator(camera, dimensions=(320, 240)) scale_x = 320 / 1640 scale_y = 240 / 1232 with CameraInference(model_roll()) as inference: start = time.time() for i, result in enumerate(inference.run()): if i == num_frames: break roll, pitch, yaw = postprocessing(result) print("roll degree:{0}, pitch degree:{0}, yaw degree:{0}".format(roll,pitch,yaw)) location = proj(roll,pitch,yaw) x0 = 120 y0 = 160 annotator.clear() annotator.line(transform(location,x0,y0), fill=0) annotator.update() camera.stop_preview() end = time.time() seconds = end- start print("time taken: {0}".format(seconds)) fps = num_frames/seconds print("frames from testing:{0}".format(fps))
def main(): parser = argparse.ArgumentParser( 'Image classification camera inference example.') parser.add_argument( '--num_frames', '-n', type=int, default=None, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--num_objects', '-c', type=int, default=3, help='Sets the number of object interences to print.') parser.add_argument('--nopreview', dest='preview', action='store_false', default=True, help='Enable camera preview') args = parser.parse_args() with PiCamera(sensor_mode=4, framerate=30) as camera, \ CameraPreview(camera, enabled=args.preview), \ CameraInference(image_classification.model()) as inference: for result in inference.run(args.num_frames): classes = image_classification.get_classes(result, top_k=args.num_objects) print(classes_info(classes)) if classes: camera.annotate_text = '%s (%.2f)' % classes[0]
def detect(num_frames): """Face detection camera inference example""" # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera: camera.start_preview() # Annotator renders in software so use a smaller size and scale results # for increased performance. annotator = Annotator(camera, dimensions=(320, 240)) scale_x = 320 / 1640 scale_y = 240 / 1232 with CameraInference(face_detection.model()) as inference: for result in inference.run(num_frames): faces = face_detection.get_faces(result) annotator.clear() for face in faces: x, y, width, height = face.bounding_box annotator.bounding_box( (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height)), fill=0) annotator.update() print('#%05d (%5.2f fps): num_faces=%d, avg_joy_score=%.2f' % (inference.count, inference.rate, len(faces), avg_joy_score(faces))) camera.stop_preview()
def run(self, num_frames, preview_alpha, image_format, image_folder): logger.info('Starting...') leds = Leds() player = Player(gpio=22, bpm=10) photographer = Photographer(image_format, image_folder) animator = Animator(leds, self._done) try: # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. with PiCamera(sensor_mode=4, resolution=(1640, 1232)) as camera, PrivacyLed(leds): def take_photo(): logger.info('Button pressed.') player.play(BEEP_SOUND) photographer.shoot(camera) # Blend the preview layer with the alpha value from the flags. if preview_alpha > 0: logger.info('Starting preview with alpha %d', preview_alpha) camera.start_preview(alpha=preview_alpha) else: logger.info('Not starting preview, alpha 0') button = Button(23) button.when_pressed = take_photo joy_score_moving_average = MovingAverage(10) prev_joy_score = 0.0 with CameraInference(face_detection.model()) as inference: logger.info('Model loaded.') player.play(MODEL_LOAD_SOUND) for i, result in enumerate(inference.run()): faces = face_detection.get_faces(result) photographer.update_faces(faces) joy_score = joy_score_moving_average.next( average_joy_score(faces)) animator.update_joy_score(joy_score) if joy_score > JOY_SCORE_PEAK > prev_joy_score: player.play(JOY_SOUND) elif joy_score < JOY_SCORE_MIN < prev_joy_score: player.play(SAD_SOUND) prev_joy_score = joy_score if self._done.is_set() or i == num_frames: break finally: player.stop() photographer.stop() player.join() photographer.join() animator.join()
def main(): """Face detection camera inference example.""" parser = argparse.ArgumentParser() parser.add_argument( '--num_frames', '-n', type=int, dest='num_frames', default=-1, help='Sets the number of frames to run for, otherwise runs forever.') args = parser.parse_args() with PiCamera() as camera: # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. camera.sensor_mode = 4 # Scaled and cropped resolution. If different from sensor mode implied # resolution, inference results must be adjusted accordingly. This is # true in particular when camera.start_recording is used to record an # encoded h264 video stream as the Pi encoder can't encode all native # sensor resolutions, or a standard one like 1080p may be desired. camera.resolution = (1640, 1232) # Start the camera stream. camera.framerate = 30 camera.start_preview() # Annotator renders in software so use a smaller size and scale results # for increased performace. annotator = Annotator(camera, dimensions=(320, 240)) scale_x = 320 / 1640 scale_y = 240 / 1232 # Incoming boxes are of the form (x, y, width, height). Scale and # transform to the form (x1, y1, x2, y2). def transform(bounding_box): x, y, width, height = bounding_box return (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height)) with CameraInference(face_detection.model()) as inference: for i, result in enumerate(inference.run()): if i == args.num_frames: break faces = face_detection.get_faces(result) annotator.clear() for face in faces: annotator.bounding_box(transform(face.bounding_box), fill=0) annotator.update() print('Iteration #%d: num_faces=%d' % (i, len(faces))) #write to file F = open('../wwww/check_faces.txt', 'w') F.write(len(faces)) F.close camera.stop_preview()
def main(): """Face detection camera inference example.""" parser = argparse.ArgumentParser() parser.add_argument('--num_frames', '-n', type=int, dest='num_frames', default=None, help='Sets the number of frames to run for, otherwise runs forever.') args = parser.parse_args() leds = Leds() leds.reset() leds.update(Leds.privacy_on()) noCustomerDelay = 0; with PiCamera(sensor_mode=4, resolution=(1640, 1232)) as camera: # with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera: # with PiCamera() as camera: camera.start_preview() with CameraInference(face_detection.model()) as inference: for result in inference.run(): if len(face_detection.get_faces(result)) >= 1: noCustomerDelay = 0 leds.update(Leds.rgb_on(GREEN)) # stream = io.BytesIO() # camera.capture(stream, format='jpeg') # stream.seek(0) camera.capture('faces.jpg') faces = GetFaceId('faces.jpg') print(faces) if(len(faces) > 0): result = GetUserId(faces[0]) print(result) highestScore = 0 userId = "" for face in result: for candidate in face['candidates']: if(highestScore < candidate['confidence']): userId = candidate['personId'] InfoVendingMachine("10", userId) print(userId) # break else: if noCustomerDelay >= 30: leds.update(Leds.rgb_on(WHITE)) InfoVendingMachine("10", '') noCustomerDelay = 0; else: noCustomerDelay += 1; camera.stop_preview() leds.reset()
def _initialize_inference() -> CameraInference: ''' One time, the process died without stopping inference, which made it impossible to start again. So if we get InferenceException when trying to initialize CameraInference, we retry after resetting the InferenceEngine. ''' return CameraInference(face_detection.model())
def main(): """Face detection camera inference example.""" parser = argparse.ArgumentParser() parser.add_argument( '--num_frames', '-n', type=int, dest='num_frames', default=None, help='Sets the number of frames to run for, otherwise runs forever.') args = parser.parse_args() # pool = [ImageProcessor() for i in range(4)] # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera: camera.start_preview() # time.sleep(2) # Annotator renders in software so use a smaller size and scale results # for increased performace. # annotator = Annotator(camera, dimensions=(320, 240)) scale_x = 320 / 1640 scale_y = 240 / 1232 # Incoming boxes are of the form (x, y, width, height). Scale and # transform to the form (x1, y1, x2, y2). def transform(bounding_box): x, y, width, height = bounding_box return (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height)) with CameraInference(face_detection.model()) as inference: # camera.capture_sequence(streams(), use_video_port=True) # print("after capture_sequence") print(args.num_frames) for result in inference.run(args.num_frames): faces = face_detection.get_faces(result) # annotator.clear() # for face in faces: # annotator.bounding_box(transform(face.bounding_box), fill=0) # annotator.update() if len(faces) > 0: # start to identify the person print("Has Customer") hasCustomer = True else: print("No Customer") hasCustomer = False print('#%05d (%5.2f fps): num_faces=%d, avg_joy_score=%.2f' % (inference.count, inference.rate, len(faces), avg_joy_score(faces))) camera.stop_preview()
def run(self, num_frames, preview_alpha, image_format, image_folder, enable_streaming): logger.info('Starting...') leds = Leds() with contextlib.ExitStack() as stack: player = stack.enter_context(Player(gpio=BUZZER_GPIO, bpm=10)) photographer = stack.enter_context(Photographer(image_format, image_folder)) animator = stack.enter_context(Animator(leds)) # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. # Use half of that for video streaming (820x616). camera = stack.enter_context(PiCamera(sensor_mode=4, resolution=(820, 616))) stack.enter_context(PrivacyLed(leds)) server = None if enable_streaming: server = stack.enter_context(StreamingServer(camera)) server.run() def take_photo(): logger.info('Button pressed.') player.play(BEEP_SOUND) photographer.shoot(camera) if preview_alpha > 0: camera.start_preview(alpha=preview_alpha) button = Button(BUTTON_GPIO) button.when_pressed = take_photo joy_score_moving_average = MovingAverage(10) prev_joy_score = 0.0 with CameraInference(face_detection.model()) as inference: logger.info('Model loaded.') player.play(MODEL_LOAD_SOUND) for i, result in enumerate(inference.run()): faces = face_detection.get_faces(result) photographer.update_faces(faces) joy_score = joy_score_moving_average.next(average_joy_score(faces)) animator.update_joy_score(joy_score) if server: data = server_inference_data(result.width, result.height, faces, joy_score) server.send_inference_data(data) if joy_score > JOY_SCORE_PEAK > prev_joy_score: player.play(JOY_SOUND) elif joy_score < JOY_SCORE_MIN < prev_joy_score: player.play(SAD_SOUND) prev_joy_score = joy_score if self._done.is_set() or i == num_frames: break
def test_camera_inference(self): with PiCamera(sensor_mode=4): with CameraInference(fd.model()) as inference: state = inference.engine.get_inference_state() self.assertEqual(len(state.loaded_models), 1) self.assertEqual(len(state.processing_models), 1) results = [ fd.get_faces(result) for result in inference.run(10) ] self.assertEqual(len(results), 10)
def main(): """Image classification camera inference example.""" parser = argparse.ArgumentParser() parser.add_argument( '--num_frames', '-n', type=int, dest='num_frames', default=-1, help='Sets the number of frames to run for, otherwise runs forever.') parser.add_argument('--num_objects', '-c', type=int, dest='num_objects', default=3, help='Sets the number of object interences to print.') args = parser.parse_args() def print_classes(classes, object_count): s = '' for index, (obj, prob) in enumerate(classes): if index > object_count - 1: break s += '%s=%1.2f\t|\t' % (obj, prob) print('%s\r' % s) with PiCamera() as camera: # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. camera.sensor_mode = 4 # Scaled and cropped resolution. If different from sensor mode implied # resolution, inference results must be adjusted accordingly. This is # true in particular when camera.start_recording is used to record an # encoded h264 video stream as the Pi encoder can't encode all native # sensor resolutions, or a standard one like 1080p may be desired. camera.resolution = (1640, 1232) # Start the camera stream. camera.framerate = 30 camera.start_preview() with CameraInference(image_classification.model()) as inference: for i, result in enumerate(inference.run()): if i == args.num_frames: break classes = image_classification.get_classes(result) print_classes(classes, args.num_objects) i = i + 1 camera.stop_preview()
def main(): """Face detection camera inference example.""" parser = argparse.ArgumentParser() parser.add_argument( '--num_frames', '-n', type=int, dest='num_frames', default=None, help='Sets the number of frames to run for, otherwise runs forever.') args = parser.parse_args() # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=30) as camera: camera.start_preview() # Annotator renders in software so use a smaller size and scale results # for increased performace. annotator = Annotator(camera, dimensions=(320, 240)) scale_x = 320 / 1640 scale_y = 240 / 1232 # Incoming boxes are of the form (x, y, width, height). Scale and # transform to the form (x1, y1, x2, y2). def transform(bounding_box): x, y, width, height = bounding_box return (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height)) with CameraInference(face_detection.model()) as inference: for result in inference.run(args.num_frames): faces = face_detection.get_faces(result) print(faces) annotator.clear() for index, face in enumerate(faces): sio.emit('movement', { 'index': index, 'score': face.face_score }) annotator.bounding_box(transform(face.bounding_box), fill=0) annotator.update() print('#%05d (%5.2f fps): num_faces=%d, avg_joy_score=%.2f' % (inference.count, inference.rate, len(faces), avg_joy_score(faces))) camera.stop_preview()
def main(): with PiCamera() as camera: camera.resolution = (1640, 922) camera.start_preview() with CameraInference(face_detection.model()) as inference: for result in inference.run(): if len(face_detection.get_faces(result)) >= 1: camera.capture('faces.jpg') break camera.stop_preview()
def main(): global count global lasttime global testing num_frames = None num_objects = 8 # just for debug printing # Forced sensor mode, 1640x1232, full FoV. use mode 4, was using frame rate of 10 # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # kevinh 3280x2464 is mode 3 (but that is too large for twitter) with PiCamera(sensor_mode=4, framerate=3) as camera: camera.awb_mode = 'sunlight' camera.exposure_mode = 'sports' # if out of memory err occurs see https://stackoverflow.com/questions/39251815/python-not-taking-picture-at-highest-resolution-from-raspberry-pi-camera camera.resolution = (1920, 1080) with CameraInference( image_classification.model( image_classification.MOBILENET)) as inference: for result in inference.run(num_frames): classes = image_classification.get_classes(result) newclasses = list(filter(removeold, classes)) if newclasses: print(classes_info(newclasses, num_objects)) name = newclasses[0][0] filename = name.replace("/", "_").replace(" ", ".") hasbird = list( filter(lambda canidate: (canidate in name), interesting)) filename += ".jpg" # print('writing', filename) if hasbird: camera.capture( filename ) # the twitter client only reads from disk (for now FIXME) now = datetime.datetime.now() deltat = now - lasttime if deltat > maxdelta: lasttime = now if not testing: tweeter.tweet( filename, 'I just saw a hummingbird! tweet tweet!') print('tweet a bird') else: print('test a bird') else: print('ignore a bird')
def _run(self): logger.info('Starting...') leds = Leds() with contextlib.ExitStack() as stack: player = stack.enter_context(Player(gpio=BUZZER_GPIO, bpm=10)) photographer = stack.enter_context( Photographer(self.args.image_format, self.args.image_folder)) animator = stack.enter_context(Animator(leds)) stack.enter_context(PrivacyLed(leds)) server = None if self.args.enable_streaming: server = stack.enter_context(StreamingServer(self.camera)) server.run() def take_photo(): logger.info('Button pressed.') player.play(BEEP_SOUND) photographer.shoot(self.camera) button = Button(BUTTON_GPIO) button.when_pressed = take_photo joy_score_moving_average = MovingAverage(10) prev_joy_score = 0.0 with CameraInference(face_detection.model()) as inference: logger.info('Model loaded.') player.play(MODEL_LOAD_SOUND) for i, result in enumerate(inference.run()): faces = face_detection.get_faces(result) photographer.update_faces(faces) avg_joy_score = average_joy_score(faces) joy_score = joy_score_moving_average.next(avg_joy_score) animator.update_joy_score(joy_score) if server: data = server_inference_data(result.width, result.height, faces, joy_score) server.send_inference_data(data) if avg_joy_score > JOY_SCORE_MIN: photographer.shoot(self.camera) # if joy_score > JOY_SCORE_PEAK > prev_joy_score: # player.play(JOY_SOUND) # elif joy_score < JOY_SCORE_MIN < prev_joy_score: # player.play(SAD_SOUND) prev_joy_score = joy_score if self._done.is_set() or i == self.args.num_frames: break
def facedet(): with CameraInference(FaceDetection.model()) as inference: for result in inference.run(args.num_frames): faces = FaceDetection.get_faces(result) annotator.clear() for face in faces: annotator.bounding_box(transform(face.bounding_box), fill=0) annotator.update() print( '#%05d (%5.2f fps): num_faces=%d, avg_joy_score=%.2f' % (inference.count, inference.rate, len(faces), avg_joy_score(faces)))
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model_path', required=True, help='Path to converted model file that can run on VisionKit.') parser.add_argument('--input_height', type=int, required=True, help='Input height.') parser.add_argument('--input_width', type=int, required=True, help='Input width.') parser.add_argument('--input_mean', type=float, default=128.0, help='Input mean.') parser.add_argument('--input_std', type=float, default=128.0, help='Input std.') parser.add_argument('--input_depth', type=int, default=3, help='Input depth.') args = parser.parse_args() model = ModelDescriptor( name='test_run_model', input_shape=(1, args.input_height, args.input_width, args.input_depth), input_normalizer=(args.input_mean, args.input_std), compute_graph=utils.load_compute_graph(args.model_path)) with PiCamera(sensor_mode=4, framerate=30) as camera: with CameraInference(model) as camera_inference: last_time = time.time() for i, result in enumerate(camera_inference.run()): output_tensor_str = [ '%s [%d elements]' % (k, len(v.data)) for k, v in result.tensors.items() ] cur_time = time.time() fps = 1.0 / (cur_time - last_time) last_time = cur_time print('%d-th inference, fps: %.1f FPS, %s' % (i, fps, ','.join(output_tensor_str)))