def circle(frame, coordinates, radius, color=default.BACKGROUND_COLOR):

        # Convert frame into image and make it ready to draw
        image = Image.fromarray(frame)
        draw = ImageDraw.Draw(image)

        # Draw
        draw.ellipse(
            (coordinates['left'] - radius, coordinates['top'] - radius,
             coordinates['left'] + radius, coordinates['top'] + radius),
            fill=color)
        # Clear
        del draw

        # Convert image to numpy array (frame) and return
        return Action.image_to_array(image)
示例#2
0
    def rectangle(frame,
                  coordinates,
                  text='',
                  solid=False,
                  box_color=default.BOX_COLOR,
                  font_color=default.FONT_COLOR,
                  font=FONT):

        # Convert frame into image and make it ready to draw
        image = Image.fromarray(frame)
        draw = ImageDraw.Draw(image)

        if solid:
            # Draw solid
            draw.rectangle(((coordinates['left'], coordinates['top']),
                            (coordinates['right'], coordinates['bottom'])),
                           fill=box_color,
                           outline=box_color)
        else:
            # Draw hollow
            draw.rectangle(((coordinates['left'], coordinates['top']),
                            (coordinates['right'], coordinates['bottom'])),
                           outline=box_color)

        if text:
            text_width, text_height = draw.textsize(text)
            text_x = ((coordinates['left'] + coordinates['right']) -
                      text_width / 2) / 2 - 12
            text_y = coordinates['bottom'] - text_height - 5

            # Draw solid box
            draw.rectangle(((coordinates['left'],
                             coordinates['bottom'] - text_height - 10),
                            (coordinates['right'], coordinates['bottom'])),
                           fill=box_color,
                           outline=box_color)

            # Draw center aligned text
            draw.text((text_x, text_y), text, font=font, fill=font_color)

        # Clear
        del draw

        # Convert image to numpy array (frame) and return
        return Action.image_to_array(image)
示例#3
0
    def text(frame,
             coordinates,
             text,
             font_color=default.FONT_COLOR,
             font=FONT):

        # Convert frame into image and make it ready to draw
        image = Image.fromarray(frame)
        draw = ImageDraw.Draw(image)

        # Draw
        draw.text((coordinates['left'], coordinates['top']),
                  text,
                  font=font,
                  fill=font_color)

        # Clear
        del draw

        # Convert image to numpy array (frame) and return
        return Action.image_to_array(image)
示例#4
0
    def stream(self):
        Handler.stream(self)
        print('[INFO] Overriding stream method...')

        # Initialise capture
        capture = _capture(src=application.CAPTURING_DEVICE,
                           use_pi_camera=application.USE_PI_CAMERA,
                           resolution=application.RESOLUTION,
                           frame_rate=application.FRAME_RATE)

        if application.USE_PI_CAMERA:
            print('[INFO] Warming up pi camera...')
        else:
            print('[INFO] Warming up camera...')

        time.sleep(2.0)

        print('[INFO] Start capturing...')

        while True:
            # Read a frame from capture
            frame = capture.read()

            # Down size frame to 50% (to increase performance on Raspberry Pi)
            frame = _frame.scale(frame=frame, scale=0.5)

            # Convert frame to gray (to increase performance on Raspberry Pi)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            # Dlib detection (set `up_sample=0` to increase performance on Raspberry Pi)
            detections = _dlib.frontal_face_detector(frame=gray, up_sample=0)

            # Up size frame to 50% (how the frame was before down sizing)
            frame = _frame.scale(frame=frame, scale=2)

            # If Dlib returns any detection
            for det in detections:

                # Up size coordinate to 50% (according to the frame size before down sizing)
                coordinates = {
                    'left': det.left() * 2,
                    'top': det.top() * 2,
                    'right': det.right() * 2,
                    'bottom': det.bottom() * 2
                }

                # Draw box around detection with text on the frame
                frame = _draw.rectangle(frame=frame,
                                        coordinates=coordinates,
                                        text='Detected')

                # Determine the facial landmarks for the face region
                # (using gray image to increase performance on Raspberry Pi)
                landmarks = _dlib.shape_predictor_68_face_landmarks(gray, det)

                # Draw circle around landmarks on the frame
                for (x, y) in landmarks:
                    frame = _draw.circle(frame=frame,
                                         coordinates={
                                             'left': x * 2,
                                             'top': y * 2
                                         },
                                         radius=0.5)

            # Write date time on the frame
            frame = _draw.text(frame=frame,
                               coordinates={
                                   'left': application.WIDTH - 150,
                                   'top': application.HEIGHT - 20
                               },
                               text=time.strftime('%d/%m/%Y %H:%M:%S',
                                                  time.localtime()),
                               font_color=(0, 0, 255))

            # Convert frame into buffer for streaming
            retval, buffer = cv2.imencode('.jpg', frame)

            # Write buffer to HTML Handler
            self.wfile.write(b'--FRAME\r\n')
            self.send_header('Content-Type', 'image/jpeg')
            self.send_header('Content-Length', len(buffer))
            self.end_headers()
            self.wfile.write(buffer)
            self.wfile.write(b'\r\n')
示例#5
0
    def stream(self):
        Handler.stream(self)
        print('[INFO] Overriding stream method...')

        # Initialise capture
        capture = _capture(src=application.CAPTURING_DEVICE,
                           use_pi_camera=application.USE_PI_CAMERA,
                           resolution=application.RESOLUTION,
                           frame_rate=application.FRAME_RATE)

        if application.USE_PI_CAMERA:
            print('[INFO] Warming up pi camera...')
        else:
            print('[INFO] Warming up camera...')

        time.sleep(2.0)

        print('[INFO] Start capturing...')

        while True:
            # Read a frame from capture
            frame = capture.read()

            # Down size frame to 50% (to increase performance on Raspberry Pi)
            frame = _frame.scale(frame=frame, scale=0.5)

            # Convert frame to gray (to increase performance on Raspberry Pi)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            # OpenCV detection
            detections = _opencv.haarcascade_frontalface_default_detector(
                gray, scale_factor=1.0, min_neighbours=5, min_size=(15, 15))

            # Up size frame to 50% (how the frame was before down sizing)
            frame = _frame.scale(frame=frame, scale=2)

            # If returns any detection
            for (x, y, w, h) in detections:

                # Up size coordinate to 50% (according to the frame size before down sizing)
                coordinates = {
                    'left': x * 2,
                    'top': y * 2,
                    'right': x * 2 + w * 2,
                    'bottom': y * 2 + h * 2
                }

                # Draw box around detection with text on the frame
                frame = _draw.rectangle(frame=frame,
                                        coordinates=coordinates,
                                        text='Detected')

            # Write date time on the frame
            frame = _draw.text(frame=frame,
                               coordinates={
                                   'left': application.WIDTH - 150,
                                   'top': application.HEIGHT - 20
                               },
                               text=time.strftime('%d/%m/%Y %H:%M:%S',
                                                  time.localtime()),
                               font_color=(0, 0, 255))

            # Convert frame into buffer for streaming
            retval, buffer = cv2.imencode('.jpg', frame)

            # Write buffer to HTML Handler
            self.wfile.write(b'--FRAME\r\n')
            self.send_header('Content-Type', 'image/jpeg')
            self.send_header('Content-Length', len(buffer))
            self.end_headers()
            self.wfile.write(buffer)
            self.wfile.write(b'\r\n')