示例#1
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')
示例#2
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')