Beispiel #1
0
def webcam_run():
    """
		Run people detection on webcam 
		and publish the data
	"""
    video_capture = cv2.VideoCapture(0)
    ros_publisher = rospy.Publisher("/people_detection", String, queue_size=10)
    processed_image = rospy.Publisher("/processed_image",
                                      CompressedImage,
                                      queue_size=10)
    people_detect = PeopleDetection()

    while (True):
        # Get image from webcam
        ret, frame = video_capture.read()
        details = people_detect.detect(frame)

        if len(details) == 0:
            continue

        rospy.loginfo(details)
        ros_publisher.publish(json.dumps(details))

        # frame  = cv2.resize(frame, (0,0), fx=0.3, fy=0.3)
        msg = CompressedImage()
        msg.header.stamp = rospy.Time.now()
        msg.format = "jpeg"
        msg.data = np.array(cv2.imencode('.jpg', frame)[1]).tostring()
        processed_image.publish(msg)
Beispiel #2
0
    def __init__(self):
        """
			Initialize the kinect module. It starts all the publishers and subscribers
		"""
        self.people_pub = rospy.Publisher("/people_detection",
                                          String,
                                          queue_size=10)
        self.depth_pub = rospy.Publisher("/depth_detection",
                                         String,
                                         queue_size=10)
        self.processed_image = rospy.Publisher("/processed_image",
                                               CompressedImage,
                                               queue_size=10)

        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("/kinect2/qhd/image_color", Image,
                                          self.callback)
        self.image_depth_sub = rospy.Subscriber(
            "/kinect2/qhd/image_depth_rect", Image, self.depth_callback)

        self.person_x = 0
        self.person_y = 0
        self.person_id = -1

        self.people_detect = PeopleDetection()
 def __init__(self, camera):
     if not isinstance(camera, Scam):
         raise Exception("Error, camera must be an Scam instance.")
     self.camera = camera
     self.mask = self.camera._get_background_mask_path()
     self.percentage_list = []
     if not self.camera._get_background_img_path():
         imwrite(self.camera._get_background_img_path(),
                 self.camera.get_cam_image())
     self.detector = PeopleDetection(
         self.camera._get_background_img_path(),
         self.camera._get_background_mask_path())
    def get_occupation_percentage(self,
                                  min_area=1500,
                                  debug=False,
                                  reset_gap=350):
        """Return the occupation percentage of monitored camera. And take care to update the background image."""
        img = PeopleDetection.pil_to_cv2_img(self.camera.get_cam_image())

        if debug:
            self.detector.debug_process(img, "debug/%s" % int(time()),
                                        min_area)
        percent = self.detector.compute_percent_occupation(img, min_area)

        # Check if the image remain approximately the same for enough time to be save as new background.
        if self.percentage_list:
            if self.approx_equal(self.percentage_list[0], percent, 5):
                self.percentage_list.append(percent)
            else:
                self.percentage_list = []
        else:
            self.percentage_list.append(percent)
        if len(self.percentage_list) >= reset_gap:
            self.detector.set_background(img, cam._get_background_img_path())
            self.percentage_list = []

        return percent
class CameraMonitor(object):
    def __init__(self, camera):
        if not isinstance(camera, Scam):
            raise Exception("Error, camera must be an Scam instance.")
        self.camera = camera
        self.mask = self.camera._get_background_mask_path()
        self.percentage_list = []
        if not self.camera._get_background_img_path():
            imwrite(self.camera._get_background_img_path(),
                    self.camera.get_cam_image())
        self.detector = PeopleDetection(
            self.camera._get_background_img_path(),
            self.camera._get_background_mask_path())

    @staticmethod
    def approx_equal(left, right, tolerance=2):
        """Check is (left) is equal to (right) with tolerance."""
        if (abs(left - right) > tolerance):
            return False
        return True

    def get_occupation_percentage(self,
                                  min_area=1500,
                                  debug=False,
                                  reset_gap=350):
        """Return the occupation percentage of monitored camera. And take care to update the background image."""
        img = PeopleDetection.pil_to_cv2_img(self.camera.get_cam_image())

        if debug:
            self.detector.debug_process(img, "debug/%s" % int(time()),
                                        min_area)
        percent = self.detector.compute_percent_occupation(img, min_area)

        # Check if the image remain approximately the same for enough time to be save as new background.
        if self.percentage_list:
            if self.approx_equal(self.percentage_list[0], percent, 5):
                self.percentage_list.append(percent)
            else:
                self.percentage_list = []
        else:
            self.percentage_list.append(percent)
        if len(self.percentage_list) >= reset_gap:
            self.detector.set_background(img, cam._get_background_img_path())
            self.percentage_list = []

        return percent
def analyse_folder(path, min_area=500):
    cam = CoffeeMachineCam()
    detector = PeopleDetection(cam._get_background_img_path(),
                               "%s/mask.jpg" % cam.dir_path)
    i = 0

    os.makedirs("debug/analyse/", exist_ok=True)
    for filename in os.listdir(path):
        img = imread("%s/%s" % (path, filename))
        print("{i}  :: occupation: {percent}%".format(
            i=i,
            percent=int(detector.compute_percent_occupation(img, min_area))))
        detector.debug_process(img, "debug/analyse/%s" % i, min_area)
        detector.set_background(img)
        i += 1
Beispiel #7
0
 def __init__(self, camera):
     """Return the Scam object for the camera."""
     self.region = self._get_camera_region(camera)
     self.camera = camera
     self.dir_path = "img/%s/%s" % (self.region, self.camera)
     self.pd = PeopleDetection(self._get_background_img_path())
Beispiel #8
0
class KinectModule:
    """
		Python module which connects all the to the kinect using ros. It uses object detection to detect people.
		Then using face recognition it finds the person and return 
	"""
    def __init__(self):
        """
			Initialize the kinect module. It starts all the publishers and subscribers
		"""
        self.people_pub = rospy.Publisher("/people_detection",
                                          String,
                                          queue_size=10)
        self.depth_pub = rospy.Publisher("/depth_detection",
                                         String,
                                         queue_size=10)
        self.processed_image = rospy.Publisher("/processed_image",
                                               CompressedImage,
                                               queue_size=10)

        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("/kinect2/qhd/image_color", Image,
                                          self.callback)
        self.image_depth_sub = rospy.Subscriber(
            "/kinect2/qhd/image_depth_rect", Image, self.depth_callback)

        self.person_x = 0
        self.person_y = 0
        self.person_id = -1

        self.people_detect = PeopleDetection()

    def callback(self, data):
        """
			Callback function for the kinect
			It takes the input image from the image and uses people detection module to get the details about the user
			Then passes it on for path planning
			:param data -- brg8 data send from kinect
		"""
        try:
            # Load the image
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")

            # Get locations from people detection module
            details = self.people_detect.detect(cv_image)

            if len(details) > 0:

                # If no one was present take the person with max area
                if self.person_x == None and self.person_y == None:
                    self.person_id = np.argmax([d['area'] for d in details])
                # Else take the person closest to the last known location
                else:
                    self.person_id = np.argmin([
                        np.fabs(d['x'] - self.person_x) +
                        np.fabs(d['y'] - self.person_y) for d in details
                    ])

                # Store person details
                self.person_x = details[self.person_id]['x']
                self.person_y = details[self.person_id]['y']
                print(details)
                self.people_pub.publish(json.dumps(details))

            cv_image = cv2.resize(cv_image, (0, 0), fx=0.3, fy=0.3)
            msg = CompressedImage()
            msg.header.stamp = rospy.Time.now()
            msg.format = "jpeg"
            msg.data = np.array(cv2.imencode('.jpg', cv_image)[1]).tostring()
            self.processed_image.publish(msg)

        except CvBridgeError as e:
            print("CvBridgeError:", e)

        except Exception as e:
            print("Error", e)

    def depth_callback(self, data):
        """
			Use the depth image from kinect for better path planning
		"""

        depth_image = self.bridge.imgmsg_to_cv2(data, "16UC1")
        depth_image[depth_image == 0] = 1600

        # Average pooling with 60,60
        M, N = 540, 960
        K = 30
        L = 30

        MK = M // K
        NL = N // L

        # Get the depth values of 60x60 blocks
        avg = depth_image[:MK * K, :NL * L].reshape(MK, K, NL,
                                                    L).mean(axis=(1, 3))

        # Get the closest object for each og 60 size row
        self.depth_pub.publish(json.dumps(avg.tolist()))