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: print("Running face detection...") 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.text((5, 5), "Welcome to the family room!") annotator.update() #print('Iteration #%d: num_faces=%d' % (i, len(faces))) 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=10) 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)) def textXYTransform(bounding_box): x, y, width, height = bounding_box return (scale_x * x, scale_y * y) with CameraInference(object_detection.model()) as inference: for result in inference.run(): objs = object_detection.get_objects(result, 0.3); annotator.clear() for obj in objs: # blue for person, green for cat, purple for dog, red for anything else outlineColor = "blue" if obj.kind == 1 else "green" if obj.kind == 2 else "purple" if obj.kind == 3 else "red" print(obj.kind) annotator.bounding_box(transform(obj.bounding_box), fill=0 , outline=outlineColor) annotator.text(textXYTransform(obj.bounding_box), "person" if obj.kind == 1 else "cat" if obj.kind == 2 else "dog" if obj.kind == 3 else "other", color=outlineColor) annotator.update() camera.stop_preview()
def main(): #### Setup stepper motor #### # create a default object, no changes to I2C address or frequency mh = Adafruit_MotorHAT(addr=0x60) # recommended for auto-disabling motors on shutdown! def turnOffMotors(): mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE) mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE) atexit.register(turnOffMotors) mh = Adafruit_MotorHAT(addr=0x60) myStepper = mh.getStepper(200, 1) # 200 steps/rev, motor port #1 myStepper.setSpeed(30) # 30 RPM #### setup camera #### """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 # Tempolary disable camera preview so that I can see the log on Terminal 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) # Print the (x, y) location of face print('X = %d, Y = %d' % (face.bounding_box[0], face.bounding_box[1])) # Move stepper motor if face.bounding_box[0] > 1640 / 2 and abs( face.bounding_box[0] - 1640 / 2) > 200: print("Double coil step - right") myStepper.step(10, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE) elif face.bounding_box[0] < 1640 / 2 and abs( face.bounding_box[0] - 1640 / 2) > 300: print("Double coil step - left") myStepper.step(10, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.DOUBLE) annotator.update() # print('Iteration #%d: num_faces=%d' % (i, len(faces))) camera.stop_preview()
def main(): 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() ser = serial.Serial('/dev/ttyACM0', 115200, write_timeout=0.03) ser.write(b"<0.0,0.0,0.0,0.0>") with PiCamera() as camera: camera.sensor_mode = 4 camera.resolution = (1640, 1232) camera.framerate = 30 camera.start_preview() global shutdown_bool shutdown_bool = False position = 0 NoFaceCount = 0 NoFaceReset = 60 NoFaceShut = 120 Top_Lid_Offset = 25 # Top Lid = L2 Bottom_Lid_Offset = 25 # Bottom Lid = L1 Top_Lid_Limit = 45 Bottom_Lid_Limit = 45 step_size = 2.0 X_Degrees = 0 Y_Degrees = 0 L1_Degrees = 0 L2_Degrees = 0 # Trying to slow down serial writing Write_Delay = 5 # Milliseconds Last_Write_Time = time.time() * 1000 # Milliseconds # Soft shutdown function def soft_shutdown(): # X_Degrees = 0 # Y_Degrees = 0 # L1_Degrees = -60 # L2_Degrees = -60 # ser.write(b"<%d,%d,%d,%d>" % (X_Degrees, Y_Degrees, L1_Degrees, L2_Degrees)) # camera.stop_preview() # quit() # thread.interrupt_main() print("Shutdown Initiated") global shutdown_bool shutdown_bool = True # Add shutdown function to button button = Button(23) button.when_pressed = soft_shutdown #pretty sure this doesn't work annotator = Annotator(camera, dimensions=(320, 240)) scale_x = 320 / 1640 scale_y = 240 / 1232 # Transform is a function to transform the face bounding box. Incoming boxes are of the form (x, y, width, height). Scale and # transform to the form (x1, y1, x2, y2). X and Y are at the top left corner of the face bounding box. 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)) def x_y_to_angles(x, y): Span_X = 1640 # This is hard-coded resolution Span_Y = 1232 MinDeg_X = 23 # These are hard coded servo angles, will need to be adjusted to match camera FOV MaxDeg_X = -23 MinDeg_Y = -18 MaxDeg_Y = 19 SpanDeg_X = MaxDeg_X - MinDeg_X X_Degrees = MinDeg_X + (SpanDeg_X * (x / Span_X)) SpanDeg_Y = MaxDeg_Y - MinDeg_Y Y_Degrees = MinDeg_Y + (SpanDeg_Y * (y / Span_Y)) return X_Degrees, Y_Degrees with CameraInference(face_detection.model()) as inference: for i, result in enumerate(inference.run()): if i == args.num_frames: break if shutdown_bool is True: X_Degrees = 0 Y_Degrees = 0 L1_Degrees = 0 L2_Degrees = 0 ser.write(b"<%d,%d,%d,%d>" % (X_Degrees, Y_Degrees, L1_Degrees, L2_Degrees)) break faces = face_detection.get_faces(result) annotator.clear() for face in faces: annotator.bounding_box( transform(face.bounding_box), fill=0) # adding bounding box to preview annotator.update() print('Iteration #%d: num_faces=%d' % (i, len(faces))) if faces: # Give servo commands if a face is detected face = faces[0] x_corner, y_corner, width, height = face.bounding_box # bounding box is top left corner x = x_corner + width / 2 y = y_corner + height / 2 print(' : Face is at X = %d' % x) print(' : Face is at Y = %d' % y) Current_Time = time.time() * 1000 if Current_Time >= (Last_Write_Time + Write_Delay): X_Degrees, Y_Degrees = x_y_to_angles(x, y) L1_Degrees = min(Y_Degrees + Bottom_Lid_Offset, Bottom_Lid_Limit) L2_Degrees = min(-Y_Degrees + Top_Lid_Offset, Top_Lid_Limit) ser.cancel_write() # Cancels previous write ser.reset_output_buffer( ) # Removes any data that hasn't been sent yet ser.write( b"<%d,%d,%d,%d>" % (X_Degrees, Y_Degrees, L1_Degrees, L2_Degrees)) NoFaceCount = 0 # print(' : X Servo Angle =%d' % X_Degrees) # print(' : Y Servo Angle =%d' % Y_Degrees) print(b"<%d,%d,%d,%d>" % (X_Degrees, Y_Degrees, L1_Degrees, L2_Degrees)) Last_Write_Time = time.time() * 1000 else: print("Waiting for Write Delay") else: print('NoFaceCount = %d' % NoFaceCount) if NoFaceReset <= NoFaceCount < NoFaceShut: X_Degrees = 0 Y_Degrees = 0 L1_Degrees = 12 L2_Degrees = 12 ser.write( b"<%d,%d,%d,%d>" % (X_Degrees, Y_Degrees, L1_Degrees, L2_Degrees)) NoFaceCount = NoFaceCount + 1 if NoFaceCount >= NoFaceShut: L1_Degrees = 0 L2_Degrees = 0 ser.write( b"<%d,%d,%d,%d>" % (X_Degrees, Y_Degrees, L1_Degrees, L2_Degrees)) # NoFaceCount = NoFaceCount + 1 else: NoFaceCount = NoFaceCount + 1 camera.stop_preview()
def main(): """object detection camera inference example.""" parser = argparse.ArgumentParser() countdown = 20 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() servoX = AngularServo(PIN_B) servoY = AngularServo(PIN_A) relay = DigitalOutputDevice(PIN_C, active_high=True, initial_value=True) #relay.blink(n=1) relay.blink(on_time=0.05, n=1) # 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=10) 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)) def textXYTransform(bounding_box): x, y, width, height = bounding_box return (scale_x * x, scale_y * y) with CameraInference(object_detection.model()) as inference: for result in inference.run(): objs = object_detection.get_objects(result, 0.3) annotator.clear() for obj in objs: # blue for person, green for cat, purple for dog, red for anything else outlineColor = "blue" if obj.kind == 1 else "green" if obj.kind == 2 else "purple" if obj.kind == 3 else "red" print(obj.kind) tBoundingBox = transform(obj.bounding_box) annotator.bounding_box(tBoundingBox, fill=0, outline=outlineColor) annotator.text( textXYTransform(obj.bounding_box), "person" if obj.kind == 1 else "cat" if obj.kind == 2 else "dog" if obj.kind == 3 else "other", color=outlineColor) if len(objs) == 1: x1, y1, x2, y2 = transform(obj.bounding_box) midX = ((x2 - x1) / 2) + x1 midY = ((y2 - y1) / 2) + y1 servoPosX = remap(midX, 0, 320, 75, -75) servoPosY = remap(midY, 0, 240, -90, 80) # 90 is low, -90 is high servoPosX = min(90, servoPosX) servoPosX = max(-90, servoPosX) servoPosY = min(90, servoPosY) servoPosY = max(-90, servoPosY) print("x", midX, servoPosX) print("y", midY, servoPosY) servoX.angle = servoPosX servoY.angle = servoPosY countdown -= 1 if countdown == -1: # squirt annotator.text((midX, midY), "Squirt!!", color=outlineColor) relay.blink(on_time=0.5, n=1) countdown = 20 else: annotator.text((midX, midY), str(countdown), color=outlineColor) if len(objs) == 0: countdown = 20 annotator.update() camera.stop_preview()
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=1, 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) def print_to_screen(classes, object_count,annotator): annotator.clear() s = '' for index, (obj, prob) in enumerate(classes): if index > object_count - 1: break #s += '%s=%1.2f\t|\t' % (obj, prob) elif prob >= 0.9: s = "I see a "+ str(obj) annotator.text((5,5), s) elif prob >= 0.5: s = "I think I see a "+ str(obj) annotator.text((5,5), s) annotator.update() return print("Running Image Classification Camera...") 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 = Annotator(camera, dimensions=(320, 240)) 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) print_to_screen(classes, args.num_objects,annotator) 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) def sound_alarm(): logger.info('Button pressed.') player.play(SIREN_SOUND) animator.danger() scale_x = 320 / 1640 scale_y = 240 / 1232 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)) # Blend the preview layer with the alpha value from the flags. #camera.start_preview(alpha=preview_alpha) camera.start_preview() annotator = Annotator(camera, dimensions=(320, 240)) button = Button(23) button.when_pressed = sound_alarm 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()): annotator.clear() faces = face_detection.get_faces(result) joy_score = joy_score_moving_average.next(average_joy_score(faces)) animator.update_joy_score(joy_score) photographer.update_faces(faces) for face in faces: annotator.bounding_box(transform(face.bounding_box), fill=0) if joy_score >= JOY_SCORE_PEAK: annotator.text((5,5), "I detect a happy human") elif joy_score > 0 and joy_score <= JOY_SCORE_MIN: annotator.text((5,5), "I detect a sad human") elif joy_score > 0: annotator.text((5,5), "I detect a human") #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 annotator.update() 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) # print('size (width by height): %d by %d' % (face.bounding_box[2], face.bounding_box[3])) closest_face_size = 0 closest_face = None for face in faces: face_size = face.bounding_box[2] * face.bounding_box[ 3] # width * height if face_size > closest_face_size: closest_face_size = face_size closest_face = face if closest_face != None: annotator.bounding_box(transform( closest_face.bounding_box), fill=0) annotator.update() print('Iteration #%d: num_faces=%d' % (i, len(faces))) if closest_face != None: closest_face_dims_transformed = transform( closest_face.bounding_box) closest_face_center = ( (closest_face_dims_transformed[0] + closest_face_dims_transformed[2]) / 2, (closest_face_dims_transformed[1] + closest_face_dims_transformed[3]) / 2 ) # (x + width) / 2 # this currently will only find the vertical offset after handling the horizontal # offset. TODO: implement diagonal directions # find direction from the center with buffer of 1/4 the dimension of the camera view # print('center is %d x %d' % closest_face_center) # print('compare: %d vs %d' % (closest_face_center[0], annotator._dims[0] / 2 + annotator._dims[0] * 0.05)) if closest_face_center[0] > annotator._dims[ 0] / 2 + annotator._dims[0] * 0.10: print('Move camera right') elif closest_face_center[0] < annotator._dims[ 0] / 2 - annotator._dims[0] * 0.10: print('Move camera left') elif closest_face_center[1] > annotator._dims[ 1] / 2 + annotator._dims[1] * 0.10: print('Move camera down') elif closest_face_center[1] < annotator._dims[ 1] / 2 - annotator._dims[1] * 0.10: print('Move camera right') else: print('Don\'t move camera') closest_face = None # reset camera.stop_preview()
def __init__(self, camera): self._clear_needed = False self._annotator = Annotator(camera, default_color=(0xFF, 0xFF, 0xFF, 0xFF), dimensions=(320, 240))
class OverlayManager(object): """Overlay utility for managing state and drawing of overlay.""" LINE_HEIGHT = 12 ROW_HEIGHT = 50 def __init__(self, camera): self._clear_needed = False self._annotator = Annotator(camera, default_color=(0xFF, 0xFF, 0xFF, 0xFF), dimensions=(320, 240)) def _draw_annotation(self, result, category, index): self._annotator.text( (5, index * self.ROW_HEIGHT + 5 + 0 * self.LINE_HEIGHT), '{:.2%}'.format(result[1])) self._annotator.text( (5, index * self.ROW_HEIGHT + 5 + 1 * self.LINE_HEIGHT), '{:25.25}'.format(result[0])) self._annotator.text( (5, index * self.ROW_HEIGHT + 5 + 2 * self.LINE_HEIGHT), 'category: {:20.20}'.format(category)) def clear(self): if self._clear_needed: self._annotator.stop() self._clear_needed = False def update(self, classes, categories): self._annotator.clear() self._clear_needed = True for i, result in enumerate(classes): self._draw_annotation(result, categories[i], i) self._annotator.update()