Ejemplo n.º 1
0
def main(video=0, dowsample=True):
    print('QUAD Vision, Python:', version, '  OpenCV:', cv2.__version__)
    vid = Video(video,
                'ORB Detection testing',
                delay=1,
                fast=False,
                skip=20,
                downsample=downsample)
    detector = Detector()

    if ros_mode:
        import rospy
        from sensor_msgs.msg import Image
        from cv_bridge import CvBridge, CvBridgeError
        from quad.msg import Target_coordinates

        pub_detection = rospy.Publisher('detection',
                                        Target_coordinates,
                                        queue_size=5)
        pub_frame = rospy.Publisher('annotated_frame',
                                    msg.image,
                                    queue_size=15)
        rospy.init_node('detection', anonymous=True)
        rospy.init_node('annotated_frame', anonymous=True)

    # Detection profiles
    detector.load_profile(profiles.idea_orange)
    # detector.load_profile(profiles.quadbox_black)
    # detector.load_profile(profiles.quadbox_white)
    # detector.load_profile(profiles.idea_red)
    # detector.load_profile(profiles.sim_drone)

    vid.skip(20)
    t = Timer('Detection')

    while vid.next():
        frame = vid.img

        t.start()  # start timer
        img, confidence, x, y = detector.detect(frame)
        print('Detection:', confidence, x, y)
        t.end()
        vid.display(img)

        if ros_mode:
            detection = Target_coordinates()
            detection.confidence = confidence
            detection.x = x
            detection.y = y
            try:
                pub_detection.publish(detection)
            except CvBridgeError as e:
                print(e)
            # ros_img = CvBridge.cv2_to_imgmsg(img, encoding="passthrough")
            # pub_frame.publish(ros_img)

    vid.close()
    t.output()
Ejemplo n.º 2
0
class TLD_IVMIT:
    def __init__(self, frame, window, init_frames_count = 20):
        self.buffer = [cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)]
        self.position = Position(self.buffer, *window)
        self.learning_component = LearningComponent(self.position.calculate_patch())
        self.detector = Detector(self.position, self.learning_component)
        self.tracker = Tracker(self.position)
        self.is_visible = True
        self.integrator = Integrator(self.learning_component)
        self.init_frames_count = init_frames_count
        self.detected_windows = None
        self.tracked_window = None

    def start(self, frame):
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        if self.init_frames_count == 0:
            start = time()
            self.tracked_window = self.tracker.track(frame, self.position)
            self.buffer[0] = frame
            print "Tracking:", time()- start

            start = time()
            self.detected_windows = self.detector.detect(self.position, self.tracked_window is not None)
            print "Detected windows count:", len(self.detected_windows)
            print "Detection:", time()- start

            start = time()
            # filtered_detected_windows = [(window, patch, proba) for window, patch, proba in self.detected_windows if proba > 0.7]
            single_window, self.is_visible = self.integrator.get_single_window(self.position, self.detected_windows, self.tracked_window)
            print "Integration:", time()- start

            if self.is_visible:
                self.position.update(*single_window)
            # start = time()
            # self.learning_component.n_expert()
            # self.learning_component.p_expert()
            # print "Update training set:", time()- start
        else:
            self.tracked_window = self.tracker.track(frame, self.position)
            self.buffer[0] = frame
            if self.tracked_window is not None:
                i = 0
                while i < 5:
                    self.position.update(x=np.random.randint(0,self.buffer[0].shape[1]-self.position.width))
                    if self.position.is_correct() and windows_intersection(self.position.get_window(), self.tracked_window) == 0:
                        self.learning_component.update_negatives(self.position.calculate_patch())
                        i += 1

                self.position.update(*self.tracked_window)
                self.learning_component.update_positives(self.position.calculate_patch())

                self.init_frames_count -= 1
            else:
                self.init_frames_count = 0
                self.is_visible = False

        return self.position
Ejemplo n.º 3
0
class Editor(BoxLayout):
    def __init__(self, **kwargs):

        super(Editor, self).__init__(**kwargs)
        self.cam_show = True
        self.detector = Detector()
        self.img1 = Image(size_hint=(1.0, 0.8))
        self.showData = ShowData()

    def start_cam(self, src=0):
        self._cap = cv2.VideoCapture(src)
        while not self._cap.isOpened():
            pass
        self.cam_show = True
        print('cam started!')
        self.count = 0
        self.fps = 0
        self.start_time = time.time()

    def close_cam(self):
        self.cam_show = False
        self._cap.release()

    def update(self, dt):
        if self._cap.isOpened() is False:
            return

        _, img = self._cap.read()
        # get pose
        pose_img, send_pose = self.detector.detect(img)
        self.showData.set_text(send_pose)
        show_img = cv2.flip(pose_img, 0)
        # set texture
        texture1 = Texture.create(size=(show_img.shape[1], show_img.shape[0]),
                                  colorfmt='bgr')
        texture1.blit_buffer(show_img.tostring(),
                             colorfmt='bgr',
                             bufferfmt='ubyte')
        # show the results
        if self.cam_show:
            self.img1.texture = texture1
        else:
            self.img1.texture = None

        self.count += 1
        if self.count == 10:
            self.calc_fps()

    def calc_fps(self):
        self.fps = 10 / (time.time() - self.start_time)
        self.count = 0
        self.start_time = time.time()

    # event handle
    def connect_zmq(self, ip_id, port_id):
        self.detector.disconnect_zmq()
        ip = ip_id.text
        port = int(port_id.text)
        print('connect zmq')
        self.detector.connect_zmq(ip, port)

    def cam_load(self, src):
        print('cam load', src.text)
        self.close_cam()
        if src.text.find('mp4') != -1 or src.text.find('m4a') != -1:
            self.start_cam(src.text)
        else:
            self.start_cam(int(src.text))

    def toggle_cam_show(self, cb):
        print(cb.active)
        self.cam_show = cb.active

    def change_scale(self, k):
        rk = round(k.value, 2)
        self.detector.set_scale_factor(rk)
        print('scale changed to', rk)

    def change_pose_num(self, k):
        ik = int(k.value)
        self.detector.set_pose_num(ik)
        print('pose num changed to', ik)

    def change_min_pose_score(self, k):
        rk = round(k.value, 2)
        print('min pose score changed to', rk)

    def change_min_part_score(self, k):
        rk = round(k.value, 2)
        print('min part score changed to', rk)
Ejemplo n.º 4
0
        # image, character_bboxes, new_words, confidence_mask, confidence
        image, character_bboxes, new_words, confidence_mask, confidences = data

        if len(confidences) == 0:
            confidences = 1.0
        else:
            confidences = np.array(confidences).mean()

        region_scores = np.zeros((image.shape[0], image.shape[1]), dtype=np.float32)
        affinity_scores = np.zeros((image.shape[0], image.shape[1]), dtype=np.float32)
        affinity_bboxes = []

        if len(character_bboxes) > 0:
            region_scores = gaussianTransformer.generate_region(region_scores.shape, character_bboxes)
            affinity_scores, affinity_bboxes = gaussianTransformer.generate_affinity(region_scores.shape,
                                                                                     character_bboxes,
                                                                                     new_words)

        saveImage(f'test{i}.png', image.copy(), character_bboxes,
                  affinity_bboxes, region_scores,
                  affinity_scores, confidence_mask)
        if i == 100:
            break


res = detector.detect([img])
    for r in res:
        cavas = tools.drawBoxes(img, r)
    cv2.imwrite('tt.png', cavas)
Ejemplo n.º 5
0
class PeopleObjectDetectionNode(object):
    """docstring for PeopleObjectDetectionNode."""
    def __init__(self):
        super(PeopleObjectDetectionNode, self).__init__()

        # init the node
        rospy.init_node('people_object_detection', anonymous=False)

        # Get the parameters
        (model_name, num_of_classes, label_file, camera_namespace,
         video_name) = self.get_parameters()

        # Create Detector
        self._detector = Detector(model_name, num_of_classes, label_file)

        self._bridge = CvBridge()

        # Advertise the result of Object Detector
        self.pub_detections = rospy.Publisher(
            'people_object_detection/detections', DetectionArray, queue_size=1)

        # Advertise the result of Object Detector
        self.pub_detections_image = rospy.Publisher(
            'people_object_detection/detections_image', Image, queue_size=1)

        self.read_from_video()
        # spin
        rospy.spin()

    def read_from_video(self):

        video_name = -1
        cap = cv2.VideoCapture(video_name)

        while (cap.isOpened()):
            ret, frame = cap.read()

            if frame is not None:
                image_message = self._bridge.cv2_to_imgmsg(frame, "bgr8")
                #self._sub = rospy.Subscriber('image', Image, self.rgb_callback, queue_size=1)
                self.rgb_callback(image_message)

            else:
                break

        cap.release()
        cv2.destroyAllWindows()

        print("Video has been processed!")

        self.shutdown()

    def get_parameters(self):
        """
        Gets the necessary parameters from parameter server

        Args:

        Returns:
        (tuple) (model name, num_of_classes, label_file)

        """
        rospy.set_param("~model_name", 'ssd_mobilenet_v1_coco_11_06_2017')
        rospy.set_param("~num_of_classes", '90')
        rospy.set_param("~label_file", 'mscoco_label_map.pbtxt')
        rospy.set_param("~camera_namespace", 'no')
        rospy.set_param("~video_name", '-1')
        #rospy.set_param("~num_workers",'-1')
        model_name = rospy.get_param("~model_name")
        num_of_classes = rospy.get_param("~num_of_classes")
        label_file = rospy.get_param("~label_file")
        camera_namespace = rospy.get_param("~camera_namespace")
        video_name = rospy.get_param("~video_name")
        #num_workers = rospy.get_param("~num_workers")

        return (model_name, num_of_classes, label_file, \
                camera_namespace,video_name)

    def shutdown(self):
        """
        Shuts down the node
        """
        rospy.signal_shutdown("See ya!")

    def rgb_callback(self, data):
        """
        Callback for RGB images
        """
        try:
            # Convert image to numpy array
            cv_image = self._bridge.imgmsg_to_cv2(data, "bgr8")

            # Detect
            (output_dict, category_index) = self._detector.detect(cv_image)

            # Create the message
            msg = utils.create_detection_msg(data, output_dict, category_index,
                                             self._bridge)

            # Draw bounding boxes
            image_processed = self._detector.visualize(cv_image, output_dict)

            # Convert numpy image into sensor img
            msg_im = self._bridge.cv2_to_imgmsg(image_processed,
                                                encoding="passthrough")

            cv2.imshow("Image window", image_processed)
            cv2.waitKey(3)
            # Publish the messages
            self.pub_detections.publish(msg)
            self.pub_detections_image.publish(msg_im)

        except CvBridgeError as e:
            print(e)