예제 #1
0
 def __init__(self, params, calib_data, hourly_data, daily_data,
              monthly_data):
     self.logger = logging.getLogger('pywws.Tasks.RegularTasks')
     self.params = params
     self.calib_data = calib_data
     self.hourly_data = hourly_data
     self.daily_data = daily_data
     self.monthly_data = monthly_data
     # get directories
     self.work_dir = self.params.get('paths', 'work', '/tmp/weather')
     self.template_dir = self.params.get(
         'paths', 'templates', os.path.expanduser('~/weather/templates/'))
     self.graph_template_dir = self.params.get(
         'paths', 'graph_templates',
         os.path.expanduser('~/weather/graph_templates/'))
     # create calibration object
     self.calibrator = Calib(self.params)
     # create templater object
     self.templater = Template.Template(self.params, self.calib_data,
                                        self.hourly_data, self.daily_data,
                                        self.monthly_data)
     # create plotter objects
     self.plotter = Plot.GraphPlotter(self.params, self.calib_data,
                                      self.hourly_data, self.daily_data,
                                      self.monthly_data, self.work_dir)
     self.roseplotter = WindRose.RosePlotter(self.params, self.calib_data,
                                             self.hourly_data,
                                             self.daily_data,
                                             self.monthly_data,
                                             self.work_dir)
     # directory of service uploaders
     self.services = dict()
     # create a YoWindow object
     self.yowindow = YoWindow.YoWindow(self.calib_data)
     # get local time's offset from UTC, without DST
     now = self.calib_data.before(datetime.max)
     if not now:
         now = datetime.now()  # was kenmc utcnow
     time_offset = Local.utcoffset(now) - Local.dst(now)
     print "Time Offset = %s" % time_offset
     # get daytime end hour, in UTC
     self.day_end_hour = eval(params.get('config', 'day end hour', '21'))
     self.day_end_hour = (self.day_end_hour -
                          (time_offset.seconds / 3600)) % 24
     # convert config from underground/metoffice to new services
     for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
         services = eval(self.params.get(section, 'services', '[]'))
         for svc in ('underground', 'metoffice'):
             if self.params.get(section, svc) == 'True':
                 if svc not in services:
                     services.append(svc)
             self.params._config.remove_option(section, svc)
         self.params.set(section, 'services', str(services))
     # create service uploader objects
     for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
         for service in eval(self.params.get(section, 'services', '[]')):
             if service not in self.services:
                 self.services[service] = ToService(self.params,
                                                    self.calib_data,
                                                    service_name=service)
예제 #2
0
    def __init__(self, params, calib_data, hourly_data, daily_data, monthly_data):
        self.logger = logging.getLogger('pywws.Tasks.RegularTasks')
        self.params = params
        self.calib_data = calib_data
        self.hourly_data = hourly_data
        self.daily_data = daily_data
        self.monthly_data = monthly_data
        # get directories
        self.work_dir = self.params.get('paths', 'work', '/tmp/weather')
        self.template_dir = self.params.get(
            'paths', 'templates', os.path.expanduser('~/weather/templates/'))
        self.graph_template_dir = self.params.get(
            'paths', 'graph_templates', os.path.expanduser('~/weather/graph_templates/'))
        # create calibration object
        self.calibrator = Calib(self.params)
        # create templater object
        self.templater = Template.Template(
            self.params, self.calib_data, self.hourly_data, self.daily_data,
            self.monthly_data)
        # create plotter objects
        self.plotter = Plot.GraphPlotter(
            self.params, self.calib_data, self.hourly_data, self.daily_data,
            self.monthly_data, self.work_dir)
        self.roseplotter = WindRose.RosePlotter(
            self.params, self.calib_data, self.hourly_data, self.daily_data,
            self.monthly_data, self.work_dir)
        # directory of service uploaders
        self.services = dict()
        # create a YoWindow object
        self.yowindow = YoWindow.YoWindow(self.calib_data)
        # get local time's offset from UTC, without DST
        now = self.calib_data.before(datetime.max)
        if not now:
            now = datetime.now() # was kenmc utcnow 
        time_offset = Local.utcoffset(now) - Local.dst(now)
	print "Time Offset = %s" % time_offset
        # get daytime end hour, in UTC
        self.day_end_hour = eval(params.get('config', 'day end hour', '21'))
        self.day_end_hour = (self.day_end_hour - (time_offset.seconds / 3600)) % 24
        # convert config from underground/metoffice to new services
        for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
            services = eval(self.params.get(section, 'services', '[]'))
            for svc in ('underground', 'metoffice'):
                if self.params.get(section, svc) == 'True':
                    if svc not in services:
                        services.append(svc)
                self.params._config.remove_option(section, svc)
            self.params.set(section, 'services', str(services))
        # create service uploader objects
        for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
            for service in eval(self.params.get(section, 'services', '[]')):
                if service not in self.services:
                    self.services[service] = ToService(
                        self.params, self.calib_data, service_name=service)
예제 #3
0
def calibrate_data(logger, params, raw_data, calib_data):
    """'Calibrate' raw data, using a user-supplied function."""
    start = calib_data.before(datetime.max)
    if start is None:
        start = datetime.min
    start = raw_data.after(start + SECOND)
    if start is None:
        return start
    del calib_data[start:]
    calib_data.flush()
    calibrator = Calib(params)
    day = None
    for data in raw_data[start:]:
        idx = data['idx']
        if idx.day != day:
            logger.info("calib: %s", idx.isoformat(' '))
            day = idx.day
        calib_data[idx] = calibrator.calib(data)
    calib_data.flush()
    return start
예제 #4
0
파일: Process.py 프로젝트: edk0/pywws
def calibrate_data(logger, params, raw_data, calib_data):
    """'Calibrate' raw data, using a user-supplied function."""
    start = calib_data.before(datetime.max)
    if start is None:
        start = datetime.min
    start = raw_data.after(start + SECOND)
    if start is None:
        return start
    del calib_data[start:]
    calib_data.flush()
    calibrator = Calib(params)
    day = None
    for data in raw_data[start:]:
        idx = data['idx']
        if idx.day != day:
            logger.info("calib: %s", idx.isoformat(' '))
            day = idx.day
        calib_data[idx] = calibrator.calib(data)
    calib_data.flush()
    return start
예제 #5
0
    def _rgb_hand_seg(self, hand, box, asd, depth):
        box = Calib().box_depth2rgb(box, depth)

        roi = self._crop_box(hand, box)
        roi = cv2.cvtColor(roi, cv2.COLOR_BGR2YCR_CB)
        roi = cv2.threshold(roi[:, :, 2], 0, 255, cv2.THRESH_OTSU)[1]

        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
        asd = cv2.dilate(asd, kernel, iterations=3)
        asd = cv2.resize(asd, (roi.shape[1], roi.shape[0]),
                         interpolation=cv2.cv.CV_INTER_LINEAR)

        roi[asd == 0] = 0
        roi = cv2.medianBlur(roi, 5)
        return roi
예제 #6
0
class RegularTasks(object):
    def __init__(self, params, calib_data, hourly_data, daily_data,
                 monthly_data):
        self.logger = logging.getLogger('pywws.Tasks.RegularTasks')
        self.params = params
        self.calib_data = calib_data
        self.hourly_data = hourly_data
        self.daily_data = daily_data
        self.monthly_data = monthly_data
        # get directories
        self.work_dir = self.params.get('paths', 'work', '/tmp/weather')
        self.template_dir = self.params.get(
            'paths', 'templates', os.path.expanduser('~/weather/templates/'))
        self.graph_template_dir = self.params.get(
            'paths', 'graph_templates',
            os.path.expanduser('~/weather/graph_templates/'))
        # create calibration object
        self.calibrator = Calib(self.params)
        # create templater object
        self.templater = Template.Template(self.params, self.calib_data,
                                           self.hourly_data, self.daily_data,
                                           self.monthly_data)
        # create plotter objects
        self.plotter = Plot.GraphPlotter(self.params, self.calib_data,
                                         self.hourly_data, self.daily_data,
                                         self.monthly_data, self.work_dir)
        self.roseplotter = WindRose.RosePlotter(self.params, self.calib_data,
                                                self.hourly_data,
                                                self.daily_data,
                                                self.monthly_data,
                                                self.work_dir)
        # directory of service uploaders
        self.services = dict()
        # create a YoWindow object
        self.yowindow = YoWindow.YoWindow(self.calib_data)
        # get local time's offset from UTC, without DST
        now = self.calib_data.before(datetime.max)
        if not now:
            now = datetime.now()  # was kenmc utcnow
        time_offset = Local.utcoffset(now) - Local.dst(now)
        print "Time Offset = %s" % time_offset
        # get daytime end hour, in UTC
        self.day_end_hour = eval(params.get('config', 'day end hour', '21'))
        self.day_end_hour = (self.day_end_hour -
                             (time_offset.seconds / 3600)) % 24
        # convert config from underground/metoffice to new services
        for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
            services = eval(self.params.get(section, 'services', '[]'))
            for svc in ('underground', 'metoffice'):
                if self.params.get(section, svc) == 'True':
                    if svc not in services:
                        services.append(svc)
                self.params._config.remove_option(section, svc)
            self.params.set(section, 'services', str(services))
        # create service uploader objects
        for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
            for service in eval(self.params.get(section, 'services', '[]')):
                if service not in self.services:
                    self.services[service] = ToService(self.params,
                                                       self.calib_data,
                                                       service_name=service)

    def has_live_tasks(self):
        yowindow_file = self.params.get('live', 'yowindow', '')
        if yowindow_file:
            return True
        for template in eval(self.params.get('live', 'twitter', '[]')):
            return True
        for service in eval(self.params.get('live', 'services', '[]')):
            return True
        for template in eval(self.params.get('live', 'plot', '[]')):
            return True
        for template in eval(self.params.get('live', 'text', '[]')):
            return True
        return False

    def do_live(self, data):
        data = self.calibrator.calib(data)
        OK = True
        yowindow_file = self.params.get('live', 'yowindow', '')
        if yowindow_file:
            self.yowindow.write_file(yowindow_file, data)
        for template in eval(self.params.get('live', 'twitter', '[]')):
            if not self.do_twitter(template, data):
                OK = False
        for service in eval(self.params.get('live', 'services', '[]')):
            self.services[service].RapidFire(data, True)
        uploads = []
        for template in eval(self.params.get('live', 'plot', '[]')):
            upload = self.do_plot(template)
            if upload and upload not in uploads:
                uploads.append(upload)
        for template in eval(self.params.get('live', 'text', '[]')):
            upload = self.do_template(template, data)
            if upload not in uploads:
                uploads.append(upload)
        if uploads:
            if not Upload.Upload(self.params, uploads):
                OK = False
            for file in uploads:
                os.unlink(file)
        return OK

    def do_tasks(self):
        sections = ['logged']
        now = self.calib_data.before(datetime.max)
        if not now:
            now = datetime.now()
        threshold = now.replace(minute=(now.minute / 15) * 15,
                                second=0,
                                microsecond=0)
        last_update = self.params.get_datetime('hourly', 'last update')
        if (not last_update) or (last_update < threshold):
            # time to do hourly tasks
            sections.append('hourly')
            # set 12 hourly threshold
            threshold -= timedelta(hours=(threshold.hour - self.day_end_hour) %
                                   12)
            last_update = self.params.get_datetime('12 hourly', 'last update')
            if (not last_update) or (last_update < threshold):
                # time to do 12 hourly tasks
                sections.append('12 hourly')
            # set daily threshold
            threshold -= timedelta(hours=(threshold.hour - self.day_end_hour) %
                                   24)
            last_update = self.params.get_datetime('daily', 'last update')
            if (not last_update) or (last_update < threshold):
                # time to do daily tasks
                sections.append('daily')
        OK = True
        for section in sections:
            for template in eval(self.params.get(section, 'twitter', '[]')):
                if not self.do_twitter(template):
                    OK = False
        for section in sections:
            yowindow_file = self.params.get(section, 'yowindow', '')
            if yowindow_file:
                self.yowindow.write_file(yowindow_file)
                break
        all_services = list()
        for section in sections:
            for service in eval(self.params.get(section, 'services', '[]')):
                if service not in all_services:
                    all_services.append(service)
        for service in all_services:
            self.services[service].Upload(True)
        uploads = []
        for section in sections:
            for template in eval(self.params.get(section, 'plot', '[]')):
                upload = self.do_plot(template)
                if upload and upload not in uploads:
                    uploads.append(upload)
            for template in eval(self.params.get(section, 'text', '[]')):
                upload = self.do_template(template)
                if upload not in uploads:
                    uploads.append(upload)
        if uploads:
            if not Upload.Upload(self.params, uploads):
                OK = False
            for file in uploads:
                os.unlink(file)
        if OK:
            for section in sections:
                self.params.set(section, 'last update', now.isoformat(' '))
        return OK

    def do_twitter(self, template, data=None):
        import ToTwitter
        twitter = ToTwitter.ToTwitter(self.params)
        self.logger.info("Templating %s", template)
        input_file = os.path.join(self.template_dir, template)
        tweet = self.templater.make_text(input_file, live_data=data)
        self.logger.info("Tweeting")
        return twitter.Upload(tweet[:140])

    def do_plot(self, template):
        self.logger.info("Graphing %s", template)
        input_file = os.path.join(self.graph_template_dir, template)
        output_file = os.path.join(self.work_dir,
                                   os.path.splitext(template)[0])
        if self.plotter.DoPlot(input_file, output_file) == 0:
            return output_file
        elif self.roseplotter.DoPlot(input_file, output_file) == 0:
            return output_file
        return None

    def do_template(self, template, data=None):
        self.logger.info("Templating %s", template)
        input_file = os.path.join(self.template_dir, template)
        output_file = os.path.join(self.work_dir, template)
        self.templater.make_file(input_file, output_file, live_data=data)
        return output_file
예제 #7
0
class RegularTasks(object):
    def __init__(self, params, calib_data, hourly_data, daily_data, monthly_data):
        self.logger = logging.getLogger('pywws.Tasks.RegularTasks')
        self.params = params
        self.calib_data = calib_data
        self.hourly_data = hourly_data
        self.daily_data = daily_data
        self.monthly_data = monthly_data
        # get directories
        self.work_dir = self.params.get('paths', 'work', '/tmp/weather')
        self.template_dir = self.params.get(
            'paths', 'templates', os.path.expanduser('~/weather/templates/'))
        self.graph_template_dir = self.params.get(
            'paths', 'graph_templates', os.path.expanduser('~/weather/graph_templates/'))
        # create calibration object
        self.calibrator = Calib(self.params)
        # create templater object
        self.templater = Template.Template(
            self.params, self.calib_data, self.hourly_data, self.daily_data,
            self.monthly_data)
        # create plotter objects
        self.plotter = Plot.GraphPlotter(
            self.params, self.calib_data, self.hourly_data, self.daily_data,
            self.monthly_data, self.work_dir)
        self.roseplotter = WindRose.RosePlotter(
            self.params, self.calib_data, self.hourly_data, self.daily_data,
            self.monthly_data, self.work_dir)
        # directory of service uploaders
        self.services = dict()
        # create a YoWindow object
        self.yowindow = YoWindow.YoWindow(self.calib_data)
        # get local time's offset from UTC, without DST
        now = self.calib_data.before(datetime.max)
        if not now:
            now = datetime.now() # was kenmc utcnow 
        time_offset = Local.utcoffset(now) - Local.dst(now)
	print "Time Offset = %s" % time_offset
        # get daytime end hour, in UTC
        self.day_end_hour = eval(params.get('config', 'day end hour', '21'))
        self.day_end_hour = (self.day_end_hour - (time_offset.seconds / 3600)) % 24
        # convert config from underground/metoffice to new services
        for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
            services = eval(self.params.get(section, 'services', '[]'))
            for svc in ('underground', 'metoffice'):
                if self.params.get(section, svc) == 'True':
                    if svc not in services:
                        services.append(svc)
                self.params._config.remove_option(section, svc)
            self.params.set(section, 'services', str(services))
        # create service uploader objects
        for section in ('live', 'logged', 'hourly', '12 hourly', 'daily'):
            for service in eval(self.params.get(section, 'services', '[]')):
                if service not in self.services:
                    self.services[service] = ToService(
                        self.params, self.calib_data, service_name=service)

    def has_live_tasks(self):
        yowindow_file = self.params.get('live', 'yowindow', '')
        if yowindow_file:
            return True
        for template in eval(self.params.get('live', 'twitter', '[]')):
            return True
        for service in eval(self.params.get('live', 'services', '[]')):
            return True
        for template in eval(self.params.get('live', 'plot', '[]')):
            return True
        for template in eval(self.params.get('live', 'text', '[]')):
            return True
        return False

    def do_live(self, data):
        data = self.calibrator.calib(data)
        OK = True
        yowindow_file = self.params.get('live', 'yowindow', '')
        if yowindow_file:
            self.yowindow.write_file(yowindow_file, data)
        for template in eval(self.params.get('live', 'twitter', '[]')):
            if not self.do_twitter(template, data):
                OK = False
        for service in eval(self.params.get('live', 'services', '[]')):
            self.services[service].RapidFire(data, True)
        uploads = []
        for template in eval(self.params.get('live', 'plot', '[]')):
            upload = self.do_plot(template)
            if upload and upload not in uploads:
                uploads.append(upload)
        for template in eval(self.params.get('live', 'text', '[]')):
            upload = self.do_template(template, data)
            if upload not in uploads:
                uploads.append(upload)
        if uploads:
            if not Upload.Upload(self.params, uploads):
                OK = False
            for file in uploads:
                os.unlink(file)
        return OK

    def do_tasks(self):
        sections = ['logged']
        now = self.calib_data.before(datetime.max)
        if not now:
            now = datetime.now()
        threshold = now.replace(minute=(now.minute/15)*15, second=0, microsecond=0)
        last_update = self.params.get_datetime('hourly', 'last update')
        if (not last_update) or (last_update < threshold):
            # time to do hourly tasks
            sections.append('hourly')
            # set 12 hourly threshold
            threshold -= timedelta(hours=(threshold.hour - self.day_end_hour) % 12)
            last_update = self.params.get_datetime('12 hourly', 'last update')
            if (not last_update) or (last_update < threshold):
                # time to do 12 hourly tasks
                sections.append('12 hourly')
            # set daily threshold
            threshold -= timedelta(hours=(threshold.hour - self.day_end_hour) % 24)
            last_update = self.params.get_datetime('daily', 'last update')
            if (not last_update) or (last_update < threshold):
                # time to do daily tasks
                sections.append('daily')
        OK = True
        for section in sections:
            for template in eval(self.params.get(section, 'twitter', '[]')):
                if not self.do_twitter(template):
                    OK = False
        for section in sections:
            yowindow_file = self.params.get(section, 'yowindow', '')
            if yowindow_file:
                self.yowindow.write_file(yowindow_file)
                break
        all_services = list()
        for section in sections:
            for service in eval(self.params.get(section, 'services', '[]')):
                if service not in all_services:
                    all_services.append(service)
        for service in all_services:
            self.services[service].Upload(True)
        uploads = []
        for section in sections:
            for template in eval(self.params.get(section, 'plot', '[]')):
                upload = self.do_plot(template)
                if upload and upload not in uploads:
                    uploads.append(upload)
            for template in eval(self.params.get(section, 'text', '[]')):
                upload = self.do_template(template)
                if upload not in uploads:
                    uploads.append(upload)
        if uploads:
            if not Upload.Upload(self.params, uploads):
                OK = False
            for file in uploads:
                os.unlink(file)
        if OK:
            for section in sections:
                self.params.set(section, 'last update', now.isoformat(' '))
        return OK

    def do_twitter(self, template, data=None):
        import ToTwitter
        twitter = ToTwitter.ToTwitter(self.params)
        self.logger.info("Templating %s", template)
        input_file = os.path.join(self.template_dir, template)
        tweet = self.templater.make_text(input_file, live_data=data)
        self.logger.info("Tweeting")
        return twitter.Upload(tweet[:140])

    def do_plot(self, template):
        self.logger.info("Graphing %s", template)
        input_file = os.path.join(self.graph_template_dir, template)
        output_file = os.path.join(self.work_dir, os.path.splitext(template)[0])
        if self.plotter.DoPlot(input_file, output_file) == 0:
            return output_file
        elif self.roseplotter.DoPlot(input_file, output_file) == 0:
            return output_file
        return None

    def do_template(self, template, data=None):
        self.logger.info("Templating %s", template)
        input_file = os.path.join(self.template_dir, template)
        output_file = os.path.join(self.work_dir, template)
        self.templater.make_file(input_file, output_file, live_data=data)
        return output_file
예제 #8
0
        total_time_cost = time.time() - time_start_all
        print(pointXYZ_raw.shape, camera_xyz.shape, camera_uv.shape)
        print("transform time cost:", transform_time_cost)
        print("display time cost:", display_time_cost)
        print("total time cost: ", total_time_cost)
        print()


if __name__ == '__main__':
    try:
        rospy.init_node("fusion")
        image_topic_name = rospy.get_param("~image_topic")
        lidar_topic_name = rospy.get_param("~lidar_topic")

        calib = Calib()
        file_path = rospy.get_param("~calibration_file_path")

        the_view_number = rospy.get_param("~the_view_number")
        the_field_of_view = rospy.get_param("~the_field_of_view")

        the_sensor_height = rospy.get_param("~the_sensor_height")
        the_view_higher_limit = rospy.get_param("~the_view_higher_limit")
        the_view_lower_limit = rospy.get_param("~the_view_lower_limit")
        the_min_distance = rospy.get_param("~the_min_distance")
        the_max_distance = rospy.get_param("~the_max_distance")

        jet_color = rospy.get_param("~jet_color")

        calib.loadcalib(file_path)
        LidarImage()
예제 #9
0
def main(data_filename):
    # Root directory of the project

    # data_filename = FLAGS.image_filename
    ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
    print(ROOT_DIR)

    # Directory to save logs and trained model
    MODEL_DIR = os.path.join(ROOT_DIR, "logs")

    # Directory to get binary and image data
    DATA_DIR = os.path.join("/".join(ROOT_DIR.split("/")[:-1]),
                            "classify/data")

    # Local path to trained weights file
    COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

    # Directory of images to run detection on
    #IMAGE_DIR = os.path.join(ROOT_DIR, "images")

    class InferenceConfig(coco.CocoConfig):
        # Set batch size to 1 since we'll be running inference on
        # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1

    config = InferenceConfig()
    # config.display()

    # Create model object in inference mode.
    model = modellib.MaskRCNN(mode="inference",
                              model_dir=MODEL_DIR,
                              config=config)

    # Load weights trained on MS-COCO
    model.load_weights(COCO_MODEL_PATH, by_name=True)

    # COCO Class names
    # Index of the class in the list is its ID. For example, to get ID of
    # the teddy bear class, use: class_names.index('teddy bear')
    class_names = [
        'BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
        'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
        'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
        'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
        'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
        'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
        'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
        'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
        'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
        'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
        'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
        'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    ]

    # Load a random image from the images folder
    #file_names = next(os.walk(IMAGE_DIR))[2]

    # filename = chooseFile.openFile()
    filename = os.path.join(DATA_DIR,
                            os.path.join("image", data_filename + ".png"))
    image = Image.open(os.path.join(filename))
    start = time()
    img = skimage.io.imread(os.path.join(filename))
    #image = skimage.io.imread(os.path.join(IMAGE_DIR,file_names))

    # Run detection
    results = model.detect([img], verbose=1)
    print(type(img))
    # Visualize results
    r = results[0]
    print("identify image time: " + str(time() - start))
    contour = visualize.display_instances(img, r['rois'], r['masks'],
                                          r['class_ids'], class_names,
                                          r['scores'])
    print(r['class_ids'])
    # visualize.display_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])

    calib = Calib(
        '/Users/berniewang/annotator/lidarAnnotator/app/classify/calib')
    im = PIL.Image.open(os.path.join(filename))
    w, h = im.size
    bin_name = os.path.join(DATA_DIR,
                            os.path.join("bin_data", data_filename + ".bin"))
    scan = np.fromfile(os.path.join(bin_name), dtype=np.float32).reshape(
        (-1, 4))
    print("scan: ", scan[0])
    im_coord = calib.velo2img(scan[:, :3], 2).astype(np.int)
    im_coord2 = [
        im_coord[i] for i in range(len(im_coord))
        if im_coord[i][0] > 0 and im_coord[i][0] <= w and im_coord[i][1] > 0
        and im_coord[i][1] <= h and scan[i][0] >= 0
    ]
    #scan2 = [scan[i] for i in range(len(im_coord)) if im_coord[i][0] > 0 and im_coord[i][0] <= w and im_coord[i][1] > 0 and im_coord[i][1] <= h]

    scan2 = np.array([0], dtype=np.float32)
    visible_indices = np.array([0], dtype=np.int)
    for i in range(len(im_coord)):
        if im_coord[i][0] > 0 and im_coord[i][0] <= w and im_coord[i][
                1] > 0 and im_coord[i][1] <= h:
            if scan[i][0] >= 0:
                scan2 = np.append(scan2, scan[i])
                visible_indices = np.append(visible_indices, i)
    scan2 = np.delete(scan2, 0)
    visible_indices = np.delete(visible_indices, 0)
    scan2 = scan2.reshape((int(len(scan2) / 4), 4))
    #scan2.tofile("scan2.bin")
    '''
    def ray_tracing_method(x,y,poly):
        n = len(poly)
        inside = False

        p1x,p1y = poly[0]
        for i in range(n+1):
            p2x,p2y = poly[i % n]
            if y > min(p1y,p2y):
                if y <= max(p1y,p2y):
                    if x <= max(p1x,p2x):
                        if p1y != p2y:
                            xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
                        if p1x == p2x or x <= xints:
                            inside = not inside
            p1x,p1y = p2x,p2y
        return inside
    '''
    start_time = time()
    print(len(contour))
    bounded_indices = np.array([0], dtype=np.int)
    for i in range(len(contour)):
        #print(len(contour))
        for q in contour[i][0]:
            temp = q[0]
            q[0] = q[1]
            q[1] = temp
        #print(contour[i][0])

        #if len(contour[i][0])>=130:
        polygon = [q for q in contour[i][0]]
        path = mpltPath.Path(polygon)
        inside2 = path.contains_points(im_coord2)
        #inside1 = [ray_tracing_method(p[1], p[0], polygon) for p in im_coord2]
        im_coord3 = []
        scan3 = np.array([0], dtype=np.float32)

        for j in range(len(inside2)):
            if inside2[j] == True:
                scan3 = np.append(scan3, scan2[j])
                bounded_indices = np.append(bounded_indices,
                                            visible_indices[j])
        scan3 = np.delete(scan3, 0)
        print("bounded_indices: ", bounded_indices.shape, bounded_indices)
        scan3 = scan3.reshape((int(len(scan3) / 4), 4))
        print(len(scan3))
        print(scan3)
        class_id = r['class_ids'][i]
        label = class_names[class_id]

        if len(scan3) != 0:
            pass
        #print(len(scan3))
        # commented out
        # scan3.tofile("sample%s%s.bin"%(label,i))
        #scan4 = np.append(scan3,scan)
        #scan4.tofile("a.bin")

        class_id = r['class_ids'][i]
        label = class_names[class_id]

    bounded_indices = np.delete(bounded_indices, 0)
    bounded_indices.tofile(os.path.join(ROOT_DIR, "output/indices.bin"))
    print("Matplotlib contains_points Elapsed time: " +
          str(time() - start_time))

    #getContour()
    print('OK')
예제 #10
0
def generate_2d_lidar():
    calib = Calib(
        '/Users/berniewang/annotator/lidarAnnotator/app/classify/calib')
    # load image
    img_name = getCoord.getPictureName()
    # print("image path again: ", os.path.join(image_ab_path+img_name))
    im = Image.open(os.path.join(image_ab_path + img_name))
    w, h = im.size
    #im = np.array(im)

    # load lidar points
    bin_name = getCoord.getFileName()
    # print(bin_name)
    scan = np.fromfile(os.path.join(bin_ab_path + bin_name),
                       dtype=np.float32).reshape((-1, 4))
    #im_coord0 = calib.velo2img(scan[:, :3], 2).astype(np.int)

    x = getCoord.getX()
    y = getCoord.getY()
    width = getCoord.getWidth()
    length = getCoord.getLength()
    angle = getCoord.getAngle()
    image_path = []

    for i in range(len(x)):
        a = x[i]
        b = y[i]
        c = width[i] / 2
        d = length[i] / 2
        e = angle[i]
        out_of_fov = False
        a_array = np.array([0], dtype=np.float32)
        for j in range(len(scan)):
            if scan[j][0] > a - c and scan[j][0] < a + c and scan[j][
                    1] > b - d and scan[j][1] < b + d:

                #if scan[j][0] > a - c * abs(math.cos(e)) and scan[j][0] < a + c * abs(math.cos(e)) and scan[j][1] > b - d * abs(math.cos(e)) and scan[j][1]< b + d * abs(math.cos(e)):
                a_array = np.append(a_array, scan[j])
                #if scan[j][0] > a - c and scan[j][0] < a + c and scan[j][1] > b - d and scan[j][1]< b + d:
        a_array = np.delete(a_array, 0)

        a_array = a_array.reshape((int(len(a_array) / 4), 4))

        a_array.tofile(os.path.join(current_dir_path, "a.bin"))

        scan2 = np.fromfile(os.path.join(current_dir_path, 'a.bin'),
                            dtype=np.float32).reshape((-1, 4))
        im_coord = calib.velo2img(scan2[:, :3], 2).astype(np.int)
        # plt.imshow(im)
        im_coord2 = [
            im_coord[i] for i in range(len(im_coord))
            if im_coord[i][0] > 0 and im_coord[i][0] <= w
            and im_coord[i][1] > 0 and im_coord[i][1] <= h
        ]
        x_arr = []
        y_arr = []
        # print("length of im_coord2: ", len(im_coord2))
        if len(im_coord2) != 0:
            x_max = im_coord2[0][0]
            for i in range(len(im_coord2)):
                if im_coord2[i][0] > x_max:
                    x_max = im_coord2[i][0]

            x_min = im_coord2[0][0]
            for i in range(len(im_coord2)):
                if im_coord2[i][0] < x_min:
                    x_min = im_coord2[i][0]

            y_max = im_coord2[0][1]
            for i in range(len(im_coord2)):
                if im_coord2[i][1] > y_max:
                    y_max = im_coord2[i][1]

            y_min = im_coord2[0][1]
            for i in range(len(im_coord2)):
                if im_coord2[i][1] < y_min:
                    y_min = im_coord2[i][1]
            #print(x_max,y_max,x_min,y_min)
        else:
            x_max = w
            y_max = h
            x_min = 0
            y_min = 0
            print("your frame can not show on the picture")
            out_of_fov = True
            # filename = 'write_data.txt'
            # with open(filename,'a') as f:
            #     f.write("your frame can not show on the picture\n")

        box = (x_min, y_min, x_max, y_max)
        image1 = im.crop(box)  #图像裁剪
        # image1.show()
        image1.save(
            "/Users/berniewang/annotator/lidarAnnotator/app/classify/inception/%d.jpg"
            % (i + 1))
        if not out_of_fov:
            image_path.append(
                "/Users/berniewang/annotator/lidarAnnotator/app/classify/inception/%d.jpg"
                % (i + 1))
        else:
            image_path.append("")
        for i in range(len(im_coord)):
            x_arr.append(im_coord[i][0])
            y_arr.append(im_coord[i][1])

        # plt.scatter(x_arr,y_arr,s = 1)
        # plt.show()
        # plt.imshow(np.asarray(image1))
        # plt.show()
    return image_path
예제 #11
0
def generate_2d_lidar():
    calib = Calib(CALIBRATION_PATH)
    # load image
    img_name = getCoord.getPictureName()
    drivename, fname = img_name.split("/")
    fname = fname.split(".")[0]
    print("img_name: ", img_name)
    im = Image.open(
        os.path.join(DATA_DIR, drivename, IMAGE_PATH, fname) + '.png')
    w, h = im.size
    #im = np.array(im)

    # load lidar points
    bin_name = getCoord.getFileName()
    # print(bin_name)
    scan = np.fromfile(os.path.join(DATA_DIR, drivename, BIN_PATH, fname) +
                       '.bin',
                       dtype=np.float32).astype(float).reshape((-1, 4))
    #im_coord0 = calib.velo2img(scan[:, :3], 2).astype(np.int)

    x = getCoord.getX()
    y = getCoord.getY()
    width = getCoord.getWidth()
    length = getCoord.getLength()
    angle = getCoord.getAngle()
    image_path = []

    for i in range(len(x)):
        a = x[i]
        b = y[i]
        c = width[i] / 2
        d = length[i] / 2
        e = angle[i]

        transformed_pc = (scan[:, :2] - np.array([a, b])).dot(
            rotation_matrix(-e).T)
        out_of_fov = False
        a_array = np.array([0], dtype=np.float32)
        print(scan, len(scan))
        for j in range(len(scan)):
            # if abs(scan[j][0]) <= c and abs(scan[j][1]) <= d:
            if abs(transformed_pc[j][0]) <= c and abs(
                    transformed_pc[j][1]) <= d:

                # if scan[j][0] > a - c * abs(math.cos(e)) and scan[j][0] < a + c * abs(math.cos(e)) and scan[j][1] > b - d * abs(math.cos(e)) and scan[j][1]< b + d * abs(math.cos(e)):
                a_array = np.append(a_array, scan[j])
                #if scan[j][0] > a - c and scan[j][0] < a + c and scan[j][1] > b - d and scan[j][1]< b + d:

        a_array = np.delete(a_array, 0)

        a_array = a_array.reshape((int(len(a_array) / 4), 4))

        print(a_array, len(a_array))
        a_array.astype(np.float32).tofile(os.path.join(CUR_DIR, "a.bin"))

        scan2 = np.fromfile(os.path.join(CUR_DIR, 'a.bin'),
                            dtype=np.float32).astype(float).reshape((-1, 4))

        im_coord = calib.velo2img(scan2[:, :3], 2).astype(np.int)
        # plt.imshow(im)
        im_coord2 = [
            im_coord[i] for i in range(len(im_coord))
            if im_coord[i][0] > 0 and im_coord[i][0] <= w
            and im_coord[i][1] > 0 and im_coord[i][1] <= h
        ]
        x_arr = []
        y_arr = []
        # print("length of im_coord2: ", len(im_coord2))
        if len(im_coord2) != 0:
            x_max = im_coord2[0][0]
            for i in range(len(im_coord2)):
                if im_coord2[i][0] > x_max:
                    x_max = im_coord2[i][0]

            x_min = im_coord2[0][0]
            for i in range(len(im_coord2)):
                if im_coord2[i][0] < x_min:
                    x_min = im_coord2[i][0]

            y_max = im_coord2[0][1]
            for i in range(len(im_coord2)):
                if im_coord2[i][1] > y_max:
                    y_max = im_coord2[i][1]

            y_min = im_coord2[0][1]
            for i in range(len(im_coord2)):
                if im_coord2[i][1] < y_min:
                    y_min = im_coord2[i][1]
            #print(x_max,y_max,x_min,y_min)
        else:
            x_max = w
            y_max = h
            x_min = 0
            y_min = 0
            print("your frame can not show on the picture")
            out_of_fov = True
            # filename = 'write_data.txt'
            # with open(filename,'a') as f:
            #     f.write("your frame can not show on the picture\n")

        box = (x_min, y_min, x_max, y_max)
        image1 = im.crop(box)  #图像裁剪
        # image1.show()
        image1.save("{}/{}.jpg".format(IMAGE_OUTPUT_PATH, i + 1))
        if not out_of_fov:
            image_path.append("{}/{}.jpg".format(IMAGE_OUTPUT_PATH, i + 1))
        else:
            image_path.append("")
        for i in range(len(im_coord)):
            x_arr.append(im_coord[i][0])
            y_arr.append(im_coord[i][1])

        # plt.imshow(im)
        # plt.scatter(x_arr,y_arr,s = 1)
        # plt.show()
        # plt.imshow(np.asarray(image1))
        # plt.show()
        if not out_of_fov:
            DIR_PATH = os.path.dirname(os.path.realpath(__file__))
            print(type(image1))
            io.imsave(
                os.path.join(DIR_PATH, "../static/images/cropped_image.jpg"),
                np.array(image1))

    return image_path
예제 #12
0
def loadJson():
    image_ab_path = chooseFile.openFile()
    while True:
        num_file = getNumFiles(path)
        for i in getCurrentDirs():
            if getNumFiles(path) > num_file and os.path.splitext(i)[1] == ".json":
                jsonFile = i
                f = io.open("/Users/bc/Downloads/"+ jsonFile, encoding='utf-8')
                        #设置以utf-8解码模式读取文件,encoding参数必须设置,否则默认以gbk模式读取文件,当文件中包含中文时,会报错
                setting = json.load(f)
                file = setting['frames'][0].get('filename')
                x = []
                y = []
                width = []
                length = []
                family = setting['frames'][0]
                for j in range(len(family.get('bounding_boxes'))):
                    x.append(family.get('bounding_boxes')[j].get('center').get('x'))
                    y.append(family.get('bounding_boxes')[j].get('center').get('y'))
                    width.append(family.get('bounding_boxes')[j].get('width'))
                    length.append(family.get('bounding_boxes')[j].get('length'))
                
                current_dir_path = os.path.dirname(os.path.realpath(__file__))
                #image_ab_path = chooseFile.openFile()
                bin_ab_path = current_dir_path+'/'
                
                calib = Calib('/Users/bc/3d-2d/calib')
                im = Image.open(os.path.join(image_ab_path))
                w, h = im.size
                
                scan = np.fromfile(
                    os.path.join(bin_ab_path+file),
                    dtype=np.float32).reshape((-1, 4))

    
                a_array = np.array([0],dtype = np.float32)
                for i in range(len(x)):
                    a = x[i]
                    b = y[i]
                    c = width[i]/2
                    d = length[i]/2
                    #e = angle[i]
                    for j in range(len(scan)):
                        if scan[j][0] > a - c and scan[j][0] < a + c and scan[j][1] > b - d and scan[j][1]< b + d:
                            a_array = np.append(a_array,scan[j])
                a_array = np.delete(a_array,0)
                a_array = a_array.reshape((int(len(a_array)/4),4))
                
                im_coord = calib.velo2img(a_array[:, :3], 2).astype(np.int)
                plt.imshow(im)
                
                x_arr = []
                y_arr = []
                
                for i in range(len(im_coord)):
                    x_arr.append(im_coord[i][0])
                    y_arr.append(im_coord[i][1])
            
            
                plt.scatter(x_arr,y_arr,s = 1)
                plt.show()
                
                num_file = getNumFiles(path)