Beispiel #1
0
def scan_thread():
    '''image scanning thread'''
    state = mpstate.camera_state

    while not state.unload.wait(0.02):
        try:
            # keep the queue size below 100, so we don't run out of memory
            if state.scan_queue.qsize() > 100:
                (frame_time,im) = state.scan_queue.get(timeout=0.2)
            (frame_time,im) = state.scan_queue.get(timeout=0.2)
        except Queue.Empty:
            continue

        t1 = time.time()
        im_full = numpy.zeros((960,1280,3),dtype='uint8')
        im_640 = numpy.zeros((480,640,3),dtype='uint8')
        scanner.debayer_full(im, im_full)
        scanner.downsample(im_full, im_640)
        if state.settings.fullres:
            regions = cuav_region.RegionsConvert(scanner.scan(im_full), 1280, 960)
        else:
            regions = cuav_region.RegionsConvert(scanner.scan(im_640))
        t2 = time.time()
        state.scan_fps = 1.0 / (t2-t1)
        state.scan_count += 1

        regions = cuav_region.filter_regions(im_full, regions, min_score=min(state.settings.minscore,state.settings.minscore2),
                                             filter_type=state.settings.filter_type)

        state.region_count += len(regions)
        if state.transmit_queue.qsize() < 100:
            state.transmit_queue.put((frame_time, regions, im_full, im_640))
Beispiel #2
0
def scan_thread():
    '''image scanning thread'''
    state = mpstate.camera_state

    while not state.unload.wait(0.02):
        try:
            # keep the queue size below 100, so we don't run out of memory
            if state.scan_queue.qsize() > 100:
                (frame_time,im) = state.scan_queue.get(timeout=0.2)
            (frame_time,im) = state.scan_queue.get(timeout=0.2)
        except Queue.Empty:
            continue

        t1 = time.time()
        im_full = numpy.zeros((960,1280,3),dtype='uint8')
        im_640 = numpy.zeros((480,640,3),dtype='uint8')
        scanner.debayer_full(im, im_full)
        scanner.downsample(im_full, im_640)
        regions = cuav_region.RegionsConvert(scanner.scan(im_640))
        t2 = time.time()
        state.scan_fps = 1.0 / (t2-t1)
        state.scan_count += 1

        regions = cuav_region.filter_regions(im_full, regions, min_score=min(state.settings.minscore,state.settings.minscore2))

        state.region_count += len(regions)
        if state.transmit_queue.qsize() < 100:
            state.transmit_queue.put((frame_time, regions, im_full, im_640))
Beispiel #3
0
def scan_thread():
  '''thread for scanning for Joe'''
  total_time = 0
  count = 0
  while True:
    frame_time, im = state.scan_queue.get()
    t0=time.time()
    regions = scanner.scan(im)
    t1=time.time()
    total_time += (t1-t0)
    count += 1
    for i in range(opts.scan_skip):
      frame_time, im = state.scan_queue.get()
Beispiel #4
0
def scan_thread():
    """thread for scanning for Joe"""
    total_time = 0
    count = 0
    while True:
        frame_time, im = state.scan_queue.get()
        t0 = time.time()
        regions = scanner.scan(im)
        t1 = time.time()
        total_time += t1 - t0
        count += 1
        for i in range(opts.scan_skip):
            frame_time, im = state.scan_queue.get()
Beispiel #5
0
    def scan_threadfunc(self):
        '''image scanning thread'''
        while not self.unload_event.wait(0.05):
            if self.scan_queue.empty():
                continue
            (frame_time, im) = self.scan_queue.get()
            scan_parms = {}
            for name in self.image_settings.list():
                scan_parms[name] = self.image_settings.get(name)
            scan_parms['BlueEmphasis'] = float(
                self.camera_settings.blue_emphasis)

            if self.terrain_alt is not None:
                altitude = self.terrain_alt
                if altitude < self.camera_settings.minalt:
                    altitude = self.camera_settings.minalt
                scan_parms['MetersPerPixel'] = cuav_util.pixel_width(
                    altitude, self.c_params.xresolution, self.c_params.lens,
                    self.c_params.sensorwidth)

            t1 = time.time()
            try:
                img_scan = cv2.imread(im, -1)
            except Exception:
                continue
            if img_scan is None:
                continue
            (h, w) = img_scan.shape[:2]
            if self.camera_settings.rotate180:
                M = cv2.getRotationMatrix2D(center, angle180, scale)
                img_scan = cv2.warpAffine(img_scan, M, (w, h))
            im_numpy = numpy.ascontiguousarray(img_scan)
            regions = scanner.scan(im_numpy, scan_parms)
            regions = cuav_region.RegionsConvert(
                regions, cuav_util.image_shape(img_scan),
                cuav_util.image_shape(img_scan))
            t2 = time.time()
            self.scan_fps = 1.0 / (t2 - t1)
            self.scan_count += 1

            regions = cuav_region.filter_regions(
                img_scan,
                regions,
                min_score=self.camera_settings.minscore,
                filter_type=self.camera_settings.filter_type)
            self.region_count += len(regions)

            # possibly send a preview image
            self.send_preview(img_scan)

            if self.camera_settings.roll_stabilised:
                roll = 0
            else:
                roll = None
            pos = self.get_plane_position(frame_time, roll=roll)
            if pos is not None:
                self.posmapping[str(frame_time)] = pos

            # this adds the latlon field to the regions (georeferencing)
            for r in regions:
                r.latlon = cuav_util.gps_position_from_image_region(
                    r, pos, w, h, altitude=None, C=self.c_params)

            if self.joelog:
                self.log_joe_position(pos, frame_time, regions)

            # filter out any regions outside the target radius
            if self.camera_settings.target_radius > 0 and pos is not None:
                regions = cuav_region.filter_radius(
                    regions, (self.camera_settings.target_latitude,
                              self.camera_settings.target_longitude),
                    self.camera_settings.target_radius)

            # filter out any regions outside the boundary
            if self.boundary_polygon:
                regions = cuav_region.filter_boundary(regions,
                                                      self.boundary_polygon,
                                                      pos)

            #filter by minscore
            regions = cuav_region.filter_regions(
                img_scan,
                regions,
                min_score=self.camera_settings.minscore,
                filter_type=self.camera_settings.filter_type)
            high_score = 1
            for r in regions:
                if r.score > high_score:
                    high_score = r.score

            if len(regions) > 0 and pos is not None:
                for r in regions:
                    self.lz.checkaddregion(r, pos)
                try:
                    lzresult = self.lz.calclandingzone()
                except Exception as ex:
                    print("calclandingzone failed: ", ex)
                    continue
                if lzresult:
                    self.transmit_queue.put((lzresult, 100000, None))
                    if self.msend:
                        self.transmit_queue.put((lzresult, 100000, 'msend'))

            if len(regions) > 0 and self.camera_settings.transmit:
                # send a region message with thumbnails to the ground station
                thumb_img = cuav_region.CompositeThumbnail(
                    img_scan,
                    regions,
                    thumb_size=self.camera_settings.thumbsize)
                encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
                (result, thumb) = cv2.imencode('.jpg', thumb_img, encode_param)
                pkt = cuav_command.ThumbPacket(frame_time, regions, thumb, pos)

                if self.transmit_queue.qsize() < 100:
                    self.transmit_queue.put((pkt, None, None))
                    if self.msend is not None and high_score >= self.camera_settings.m_minscore:
                        self.transmit_queue.put((pkt, None, 'msend'))
                else:
                    self.send_message("Warning: image Tx queue too long")
                    print("Warning: image Tx queue too long")
Beispiel #6
0
def process(args):
  '''process a set of files'''

  global slipmap, mosaic
  scan_count = 0
  files = []
  if os.path.isdir(args.directory):
    files.extend(file_list(args.directory, ['jpg', 'jpeg', 'png']))
  else:
    if args.directory.find('*') != -1:
      files.extend(glob.glob(args.directory))
    else:
      files.append(args.directory)
  files.sort()
  num_files = len(files)
  print("num_files=%u" % num_files)
  region_count = 0

  slipmap = mp_slipmap.MPSlipMap(service=args.service, elevation=True, title='Map')
  if args.vehicle_type == "Copter":
      icon = slipmap.icon('redcopter.png')
  else:
      icon = slipmap.icon('redplane.png')
  slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0,
                                         follow=True,
                                         trail=mp_slipmap.SlipTrail()))

  for flag in args.flag:
    a = flag.split(',')
    lat = a[0]
    lon = a[1]
    icon = 'flag.png'
    if len(a) > 2:
      icon = a[2] + '.png'
    icon = slipmap.icon(icon)
    slipmap.add_object(mp_slipmap.SlipIcon('icon - %s' % str(flag), (float(lat),float(lon)), icon, layer=3, rotation=0, follow=False))

  if args.mission:
    from pymavlink import mavwp
    wp = mavwp.MAVWPLoader()
    wp.load(args.mission.name)
    plist = wp.polygon_list()
    if len(plist) > 0:
        for i in range(len(plist)):
          slipmap.add_object(mp_slipmap.SlipPolygon('Mission-%s-%u' % (args.mission.name,i), plist[i], layer='Mission',
                                     linewidth=2, colour=(255,255,255)))

  if args.mavlog:
    mpos = mav_position.MavInterpolator()
    mpos.set_logfile(args.mavlog.name)
  else:
    mpos = None

  if args.gammalog is not None:
    gamma = parse_gamma_log(args.gammalog)
  else:
    gamma = None

  if args.kmzlog:
    kmzpos = mav_position.KmlPosition(args.kmzlog.name)
  else:
    kmzpos = None

  if args.triggerlog:
    triggerpos = mav_position.TriggerPosition(args.triggerlog.name)
  else:
    triggerpos = None

  # create a simple lens model using the focal length

  if args.camera_params:
    C_params = cam_params.CameraParams.fromfile(args.camera_params.name)
  else:
    im_orig = cv2.imread(files[0])
    (w,h) = cuav_util.image_shape(im_orig)
    C_params = cam_params.CameraParams(lens=args.lens, sensorwidth=args.sensorwidth, xresolution=w, yresolution=h)

  if args.target:
    target = args.target.split(',')
  else:
    target = [0,0,0]
    
  camera_settings = MPSettings(
    [ MPSetting('roll_stabilised', bool, args.roll_stabilised, 'Roll Stabilised'),
      MPSetting('pitch_stabilised', bool, args.pitch_stabilised, 'Pitch Stabilised'),
      MPSetting('roll_offset', float, args.roll_offset, 'Roll Offset'),
      MPSetting('pitch_offset', float, args.pitch_offset, 'Pitch Offset'),
      MPSetting('altitude', int, args.altitude, 'Altitude', range=(0,10000), increment=1),
      MPSetting('minalt', int, 30, 'MinAltitude', range=(0,10000), increment=1),
      MPSetting('mpp100', float, 0.0977, 'MPPat100m', range=(0,10000), increment=0.001),
      MPSetting('rotate180', bool, False, 'rotate180'),
      MPSetting('filter_type', str, 'simple', 'Filter Type',
                choice=['simple']),
      MPSetting('target_lattitude', float, float(target[0]), 'target latitude', increment=1.0e-7),
      MPSetting('target_longitude', float, float(target[1]), 'target longitude', increment=1.0e-7),
      MPSetting('target_radius', float, float(target[2]), 'target radius', increment=1),
      MPSetting('quality', int, 75, 'Compression Quality', range=(1,100), increment=1),
      MPSetting('thumbsize', int, args.thumbsize, 'Thumbnail Size', range=(10, 200), increment=1),
      MPSetting('RegionHue', int, ags.targethue, 'Target Hue (0 to disable)', range=(0,180), increment=1, tab='Imaging'),
      MPSetting('map_thumbsize', int, args.map_thumbsize, 'Map Thumbnail Size', range=(10, 200), increment=1),
      MPSetting('minscore', int, args.minscore, 'Min Score', range=(0,1000), increment=1, tab='Scoring'),
      MPSetting('brightness', float, 1.0, 'Display Brightness', range=(0.1, 10), increment=0.1,
                digits=2, tab='Display'),      
      ],
    title='Camera Settings'
    )

  image_settings = MPSettings(
    [ MPSetting('MinRegionArea', float, 0.15, range=(0,100), increment=0.05, digits=2, tab='Image Processing'),
      MPSetting('MaxRegionArea', float, 1.0, range=(0,100), increment=0.1, digits=1),
      MPSetting('MinRegionSize', float, 0.2, range=(0,100), increment=0.05, digits=2),
      MPSetting('MaxRegionSize', float, 1.0, range=(0,100), increment=0.1, digits=1),
      MPSetting('MaxRarityPct',  float, 0.02, range=(0,100), increment=0.01, digits=2),
      MPSetting('RegionMergeSize', float, 1.0, range=(0,100), increment=0.1, digits=1),
      MPSetting('SaveIntermediate', bool, args.debug)
      ],
    title='Image Settings')
  
  mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params,
                              camera_settings=camera_settings,
                              image_settings=image_settings,
                              start_menu=True,
                              classify=args.categories,
                              thumb_size=args.mosaic_thumbsize,
                              map_thumb_size=args.map_thumbsize)

  joelog = cuav_joe.JoeLog(None)

  if args.view:
    viewer = mp_image.MPImage(title='Image', can_zoom=True, can_drag=True)

  start_time = time.time()
  for f in files:
      if not mosaic.started():
        print("Waiting for startup")
        if args.start:
          mosaic.has_started = True
        while not mosaic.started():
          mosaic.check_events()
          time.sleep(0.01)

      if mpos:
        # get the position by interpolating telemetry data from the MAVLink log file
        # this assumes that the filename contains the timestamp 
        if gamma is not None:
          frame_time = parse_gamma_time(f, gamma)
        else:
          frame_time = cuav_util.parse_frame_time(f)
        frame_time += args.time_offset
        if camera_settings.roll_stabilised:
          roll = 0
        else:
          roll = None
        if camera_settings.pitch_stabilised:
          pitch = 0
        else:
          pitch = None
        try:
          pos = mpos.position(frame_time, roll=roll, pitch=pitch,
                              roll_offset=camera_settings.roll_offset,
                              pitch_offset=camera_settings.pitch_offset)
        except Exception:
          print("No position available for %s" % frame_time)
          # skip this frame
          continue
      elif kmzpos is not None:
        pos = kmzpos.position(f)
      elif triggerpos is not None:
        pos = triggerpos.position(f)
      else:
        # get the position using EXIF data
        pos = mav_position.exif_position(f)
        pos.time += args.time_offset

      # update the vehicle icon on the map
      if pos is not None:
        slipmap.set_position("plane", (pos.lat, pos.lon), rotation=pos.yaw)
        if camera_settings.altitude > 0:
          pos.altitude = camera_settings.altitude

      # check for any events from the map
      slipmap.check_events()
      mosaic.check_events()

      #im_orig = cuav_util.LoadImage(f, rotate180=camera_settings.rotate180)
      im_orig = cv2.imread(f)

      if im_orig is None:
        continue
      (w,h) = cuav_util.image_shape(im_orig)

      if args.downsample:
        im_full = cv2.resize(im_orig, (0,0), fx=0.5, fy=0.5)
      else:
        im_full = im_orig

      count = 0
      total_time = 0

      t0=time.time()
      img_scan = im_full

      scan_parms = {}
      for name in image_settings.list():
        scan_parms[name] = image_settings.get(name)
      scan_parms['SaveIntermediate'] = float(scan_parms['SaveIntermediate'])

      if pos is not None:
        (sw,sh) = cuav_util.image_shape(img_scan)
        altitude = pos.altitude
        if altitude < camera_settings.minalt:
          altitude = camera_settings.minalt
        scan_parms['MetersPerPixel'] = camera_settings.mpp100 * altitude / 100.0

        regions = scanner.scan(img_scan, scan_parms)
      else:
        regions = scanner.scan(img_scan)
      regions = cuav_region.RegionsConvert(regions, cuav_util.image_shape(img_scan), cuav_util.image_shape(im_full))
      count += 1
      t1=time.time()

      frame_time = pos.time

      regions = cuav_region.filter_regions(im_full, regions, min_score=camera_settings.minscore,
                                           filter_type=camera_settings.filter_type,
                                           target_hue=camera_settings.RegionHue)

      if pos:
        for r in regions:
          r.latlon = cuav_util.gps_position_from_image_region(r, pos, w, h, altitude=altitude, C=C_params)

        if camera_settings.target_radius > 0 and pos is not None:
          regions = cuav_region.filter_radius(regions, (camera_settings.target_lattitude,
                                                        camera_settings.target_longitude),
                                              camera_settings.target_radius)

      scan_count += 1

      if pos and len(regions) > 0:
        joelog.add_regions(frame_time, regions, pos, f)

      mosaic.add_image(pos.time, f, pos)

      region_count += len(regions)

      if len(regions) > 0:
          composite = cuav_region.CompositeThumbnail(im_full, regions)
          thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
          thumbsRGB = []

          #colour space conversion
          for thumb in thumbs:
              thumbsRGB.append(cv2.cvtColor(thumb, cv2.COLOR_BGR2RGB))
          mosaic.add_regions(regions, thumbsRGB, f, pos)

      if args.view:
        img_view = img_scan
        (wview,hview) = cuav_util.image_shape(img_view)
        for r in regions:
          r.draw_rectangle(img_view, (255,0,0))
        #cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
        img_view = cv2.cvtColor(img_view, cv2.COLOR_BGR2RGB)
        viewer.set_image(img_view)
        viewer.set_title('Image: ' + os.path.basename(f))

      total_time += (t1-t0)
      if t1 != t0:
          print('%s scan %.1f fps  %u regions [%u/%u]' % (
              os.path.basename(f), count/total_time, region_count, scan_count, num_files))
      #raw_input("hit ENTER when ready")

  print("All images processed (%u seconds)" % (time.time() - start_time))
  while True:
      # check for any events from the map
      slipmap.check_events()
      mosaic.check_events()
      time.sleep(0.2)
Beispiel #7
0
def process(args):
    '''process a set of files'''

    global slipmap, mosaic
    scan_count = 0
    files = []
    for a in args:
        if os.path.isdir(a):
            files.extend(file_list(a, ['jpg', 'pgm', 'png']))
        else:
            if a.find('*') != -1:
                files.extend(glob.glob(a))
            else:
                files.append(a)
    files.sort()
    num_files = len(files)
    print("num_files=%u" % num_files)
    region_count = 0

    slipmap = mp_slipmap.MPSlipMap(service=opts.service,
                                   elevation=True,
                                   title='Map')
    icon = slipmap.icon('redplane.png')
    slipmap.add_object(
        mp_slipmap.SlipIcon('plane', (0, 0),
                            icon,
                            layer=3,
                            rotation=0,
                            follow=True,
                            trail=mp_slipmap.SlipTrail()))

    if opts.mission:
        from pymavlink import mavwp
        wp = mavwp.MAVWPLoader()
        wp.load(opts.mission)
        boundary = wp.polygon()
        slipmap.add_object(
            mp_slipmap.SlipPolygon('mission',
                                   boundary,
                                   layer=1,
                                   linewidth=1,
                                   colour=(255, 255, 255)))

    if opts.mavlog:
        mpos = mav_position.MavInterpolator()
        mpos.set_logfile(opts.mavlog)
    else:
        mpos = None

    if opts.kmzlog:
        kmzpos = mav_position.KmlPosition(opts.kmzlog)
    else:
        kmzpos = None

    if opts.triggerlog:
        triggerpos = mav_position.TriggerPosition(opts.triggerlog)
    else:
        triggerpos = None

    # create a simple lens model using the focal length
    C_params = cam_params.CameraParams(lens=opts.lens,
                                       sensorwidth=opts.sensorwidth)

    if opts.camera_params:
        C_params.load(opts.camera_params)

    camera_settings = MPSettings([
        MPSetting('roll_stabilised', bool, True, 'Roll Stabilised'),
        MPSetting(
            'altitude', int, 0, 'Altitude', range=(0, 10000), increment=1),
        MPSetting('filter_type',
                  str,
                  'simple',
                  'Filter Type',
                  choice=['simple', 'compactness']),
        MPSetting('fullres', bool, False, 'Full Resolution'),
        MPSetting('quality',
                  int,
                  75,
                  'Compression Quality',
                  range=(1, 100),
                  increment=1),
        MPSetting('thumbsize',
                  int,
                  60,
                  'Thumbnail Size',
                  range=(10, 200),
                  increment=1),
        MPSetting('minscore',
                  int,
                  75,
                  'Min Score',
                  range=(0, 1000),
                  increment=1,
                  tab='Scoring'),
        MPSetting('brightness',
                  float,
                  1.0,
                  'Display Brightness',
                  range=(0.1, 10),
                  increment=0.1,
                  digits=2,
                  tab='Display')
    ],
                                 title='Camera Settings')

    image_settings = MPSettings([
        MPSetting('MinRegionArea',
                  float,
                  0.15,
                  range=(0, 100),
                  increment=0.05,
                  digits=2,
                  tab='Image Processing'),
        MPSetting('MaxRegionArea',
                  float,
                  2.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('MinRegionSize',
                  float,
                  0.1,
                  range=(0, 100),
                  increment=0.05,
                  digits=2),
        MPSetting(
            'MaxRegionSize', float, 2, range=(0, 100), increment=0.1,
            digits=1),
        MPSetting('MaxRarityPct',
                  float,
                  0.02,
                  range=(0, 100),
                  increment=0.01,
                  digits=2),
        MPSetting('RegionMergeSize',
                  float,
                  3.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('SaveIntermediate', bool, False)
    ],
                                title='Image Settings')

    mosaic = cuav_mosaic.Mosaic(slipmap,
                                C=C_params,
                                camera_settings=camera_settings,
                                image_settings=image_settings,
                                start_menu=True)

    joelog = cuav_joe.JoeLog(None)

    if opts.view:
        viewer = mp_image.MPImage(title='Image', can_zoom=True, can_drag=True)

    if camera_settings.filter_type == 'compactness':
        calculate_compactness = True
        print("Using compactness filter")
    else:
        calculate_compactness = False

    for f in files:
        if not mosaic.started():
            print("Waiting for startup")
            while not mosaic.started():
                mosaic.check_events()
                time.sleep(0.01)

        if mpos:
            # get the position by interpolating telemetry data from the MAVLink log file
            # this assumes that the filename contains the timestamp
            frame_time = cuav_util.parse_frame_time(f) + opts.time_offset
            if camera_settings.roll_stabilised:
                roll = 0
            else:
                roll = None
            try:
                pos = mpos.position(frame_time, roll=roll)
            except Exception:
                print("No position available for %s" % frame_time)
                # skip this frame
                continue
        elif kmzpos is not None:
            pos = kmzpos.position(f)
        elif triggerpos is not None:
            pos = triggerpos.position(f)
        else:
            # get the position using EXIF data
            pos = mav_position.exif_position(f)
            pos.time += opts.time_offset

        # update the plane icon on the map
        if pos is not None:
            slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)
            if camera_settings.altitude > 0:
                pos.altitude = camera_settings.altitude

        # check for any events from the map
        slipmap.check_events()
        mosaic.check_events()

        im_orig = cuav_util.LoadImage(f)
        (w, h) = cuav_util.image_shape(im_orig)

        if not opts.camera_params:
            C_params.set_resolution(w, h)

        im_full = im_orig

        im_640 = cv.CreateImage((640, 480), 8, 3)
        cv.Resize(im_full, im_640, cv.CV_INTER_NN)
        im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))
        im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

        count = 0
        total_time = 0

        t0 = time.time()
        if camera_settings.fullres:
            img_scan = im_full
        else:
            img_scan = im_640

        scan_parms = {}
        for name in image_settings.list():
            scan_parms[name] = image_settings.get(name)
        scan_parms['SaveIntermediate'] = float(scan_parms['SaveIntermediate'])

        if pos is not None:
            (sw, sh) = cuav_util.image_shape(img_scan)
            mpp = cuav_util.meters_per_pixel(pos, C=C_params)
            if mpp is not None:
                scan_parms['MetersPerPixel'] = mpp * (w / float(sw))
            regions = scanner.scan(img_scan, scan_parms)
        else:
            regions = scanner.scan(img_scan)
        regions = cuav_region.RegionsConvert(regions,
                                             cuav_util.image_shape(img_scan),
                                             cuav_util.image_shape(im_full),
                                             calculate_compactness)
        count += 1
        t1 = time.time()

        frame_time = pos.time

        regions = cuav_region.filter_regions(
            im_full,
            regions,
            frame_time=frame_time,
            min_score=camera_settings.minscore,
            filter_type=camera_settings.filter_type)

        scan_count += 1

        mosaic.add_image(pos.time, f, pos)

        if pos and len(regions) > 0:
            altitude = camera_settings.altitude
            if altitude <= 0:
                altitude = None
            joelog.add_regions(frame_time,
                               regions,
                               pos,
                               f,
                               width=w,
                               height=h,
                               altitude=altitude)

        region_count += len(regions)

        if len(regions) > 0:
            composite = cuav_mosaic.CompositeThumbnail(
                cv.GetImage(cv.fromarray(im_full)), regions)
            thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
            mosaic.add_regions(regions, thumbs, f, pos)

        if opts.view:
            img_view = img_scan
            (wview, hview) = cuav_util.image_shape(img_view)
            mat = cv.fromarray(img_view)
            for r in regions:
                r.draw_rectangle(mat, (255, 0, 0))
            cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
            viewer.set_image(mat)
            viewer.set_title('Image: ' + os.path.basename(f))

        total_time += (t1 - t0)
        if t1 != t0:
            print('%s scan %.1f fps  %u regions [%u/%u]' %
                  (os.path.basename(f), count / total_time, region_count,
                   scan_count, num_files))
Beispiel #8
0
def process(args):
    '''process a set of files'''

    global slipmap, mosaic
    scan_count = 0
    files = []
    for a in args:
        if os.path.isdir(a):
            files.extend(file_list(a, ['jpg', 'pgm', 'png']))
        else:
            if a.find('*') != -1:
                files.extend(glob.glob(a))
            else:
                files.append(a)
    files.sort()
    num_files = len(files)
    print("num_files=%u" % num_files)
    region_count = 0

    slipmap = mp_slipmap.MPSlipMap(service=opts.service,
                                   elevation=True,
                                   title='Map')
    icon = slipmap.icon('redplane.png')
    slipmap.add_object(
        mp_slipmap.SlipIcon('plane', (0, 0),
                            icon,
                            layer=3,
                            rotation=0,
                            follow=True,
                            trail=mp_slipmap.SlipTrail()))

    if opts.mission:
        from pymavlink import mavwp
        wp = mavwp.MAVWPLoader()
        wp.load(opts.mission)
        boundary = wp.polygon()
        slipmap.add_object(
            mp_slipmap.SlipPolygon('mission',
                                   boundary,
                                   layer=1,
                                   linewidth=1,
                                   colour=(255, 255, 255)))

    if opts.mavlog:
        mpos = mav_position.MavInterpolator()
        mpos.set_logfile(opts.mavlog)
    else:
        mpos = None

    if opts.kmzlog:
        kmzpos = mav_position.KmlPosition(opts.kmzlog)
    else:
        kmzpos = None

    if opts.triggerlog:
        triggerpos = mav_position.TriggerPosition(opts.triggerlog)
    else:
        triggerpos = None

    # create a simple lens model using the focal length
    C_params = cam_params.CameraParams(lens=opts.lens,
                                       sensorwidth=opts.sensorwidth)

    if opts.camera_params:
        C_params.load(opts.camera_params)

    mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params)

    joelog = cuav_joe.JoeLog(None)

    if opts.view:
        viewer = mp_image.MPImage(title='Image', can_zoom=True, can_drag=True)

    scan_parms = {
        'MinRegionArea': opts.min_region_area,
        'MaxRegionArea': opts.max_region_area,
        'MinRegionSize': opts.min_region_size,
        'MaxRegionSize': opts.max_region_size,
        'MaxRarityPct': opts.max_rarity_pct,
        'RegionMergeSize': opts.region_merge
    }

    for f in files:
        if mpos:
            # get the position by interpolating telemetry data from the MAVLink log file
            # this assumes that the filename contains the timestamp
            frame_time = cuav_util.parse_frame_time(f) + opts.time_offset
            if opts.roll_stabilised:
                roll = 0
            else:
                roll = None
            try:
                pos = mpos.position(frame_time, roll=roll)
            except Exception:
                print("No position available for %s" % frame_time)
                # skip this frame
                continue
        elif kmzpos is not None:
            pos = kmzpos.position(f)
        elif triggerpos is not None:
            pos = triggerpos.position(f)
        else:
            # get the position using EXIF data
            pos = mav_position.exif_position(f)
            pos.time += opts.time_offset

        # update the plane icon on the map
        if pos is not None:
            slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)

        # check for any events from the map
        slipmap.check_events()
        mosaic.check_events()

        im_orig = cuav_util.LoadImage(f)
        (w, h) = cuav_util.image_shape(im_orig)

        if not opts.camera_params:
            C_params.set_resolution(w, h)

        im_full = im_orig

        im_640 = cv.CreateImage((640, 480), 8, 3)
        cv.Resize(im_full, im_640, cv.CV_INTER_NN)
        im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))
        im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

        count = 0
        total_time = 0

        t0 = time.time()
        if opts.fullres:
            img_scan = im_full
        else:
            img_scan = im_640

        if pos is not None:
            (sw, sh) = cuav_util.image_shape(img_scan)
            mpp = cuav_util.meters_per_pixel(pos, C=C_params)
            if mpp is not None:
                scan_parms['MetersPerPixel'] = mpp * (w / float(sw))
            regions = scanner.scan(img_scan, scan_parms)
        else:
            regions = scanner.scan(img_scan)
        regions = cuav_region.RegionsConvert(regions,
                                             cuav_util.image_shape(img_scan),
                                             cuav_util.image_shape(im_full))
        count += 1
        t1 = time.time()

        frame_time = pos.time

        regions = cuav_region.filter_regions(im_full,
                                             regions,
                                             frame_time=frame_time,
                                             min_score=opts.minscore,
                                             filter_type=opts.filter_type)

        scan_count += 1

        mosaic.add_image(pos.time, f, pos)

        if pos and len(regions) > 0:
            joelog.add_regions(frame_time,
                               regions,
                               pos,
                               f,
                               width=w,
                               height=h,
                               altitude=opts.altitude)

        region_count += len(regions)

        if len(regions) > 0:
            composite = cuav_mosaic.CompositeThumbnail(
                cv.GetImage(cv.fromarray(im_full)), regions)
            thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
            mosaic.add_regions(regions, thumbs, f, pos)

        if opts.view:
            img_view = img_scan
            (wview, hview) = cuav_util.image_shape(img_view)
            mat = cv.fromarray(img_view)
            for r in regions:
                r.draw_rectangle(mat, (255, 0, 0))
            cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
            viewer.set_image(mat)
            viewer.set_title('Image: ' + os.path.basename(f))

        total_time += (t1 - t0)
        if t1 != t0:
            print('%s scan %.1f fps  %u regions [%u/%u]' %
                  (os.path.basename(f), count / total_time, region_count,
                   scan_count, num_files))
Beispiel #9
0
def process(args):
  '''process a set of files'''

  global slipmap, mosaic
  scan_count = 0
  files = []
  for a in args:
    if os.path.isdir(a):
      files.extend(glob.glob(os.path.join(a, '*.pgm')))
    else:
      files.append(a)
  files.sort()
  num_files = len(files)
  print("num_files=%u" % num_files)
  region_count = 0
  joes = []

  if opts.mavlog:
    mpos = mav_position.MavInterpolator(gps_lag=opts.gps_lag)
    mpos.set_logfile(opts.mavlog)
  else:
    mpos = None

  if opts.boundary:
    boundary = cuav_util.polygon_load(opts.boundary)
  else:
    boundary = None

  if opts.mosaic:
    slipmap = mp_slipmap.MPSlipMap(service='GoogleSat', elevation=True, title='Map')
    icon = slipmap.icon('planetracker.png')
    slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0,
                                           follow=True,
                                           trail=mp_slipmap.SlipTrail()))
    C_params = cam_params.CameraParams(lens=opts.lens)
    path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..',
                        'cuav', 'data', 'chameleon1_arecont0.json')
    C_params.load(path)
    mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params)
    if boundary is not None:
      mosaic.set_boundary(boundary)

  if opts.joe:
    joes = cuav_util.polygon_load(opts.joe)
    if boundary:
      for i in range(len(joes)):
        joe = joes[i]
        if cuav_util.polygon_outside(joe, boundary):
          print("Error: joe outside boundary", joe)
          return
        icon = slipmap.icon('flag.png')
        slipmap.add_object(mp_slipmap.SlipIcon('joe%u' % i, (joe[0],joe[1]), icon, layer=4))

  joelog = cuav_joe.JoeLog('joe.log')      

  if opts.view:
    viewer = mp_image.MPImage(title='Image')

  frame_time = 0

  for f in files:
    if mpos:
      frame_time = cuav_util.parse_frame_time(f)
      try:
        if opts.roll_stabilised:
          roll = 0
        else:
          roll = None
        pos = mpos.position(frame_time, opts.max_deltat,roll=roll)
        slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)
      except mav_position.MavInterpolatorException as e:
        print e
        pos = None
    else:
      pos = None

    # check for any events from the map
    if opts.mosaic:
      slipmap.check_events()
      mosaic.check_events()

    if f.endswith('.pgm'):
      pgm = cuav_util.PGM(f)
      im = pgm.array
      if pgm.eightbit:
        im_8bit = im
      else:
        im_8bit = numpy.zeros((960,1280,1),dtype='uint8')
        if opts.gamma != 0:
          scanner.gamma_correct(im, im_8bit, opts.gamma)
        else:
          scanner.reduce_depth(im, im_8bit)
      im_full = numpy.zeros((960,1280,3),dtype='uint8')
      scanner.debayer(im_8bit, im_full)
      im_640 = numpy.zeros((480,640,3),dtype='uint8')
      scanner.downsample(im_full, im_640)
    else:
      im_orig = cv.LoadImage(f)
      (w,h) = cuav_util.image_shape(im_orig)
      im_full = im_orig
      im_640 = cv.CreateImage((640, 480), 8, 3)
      cv.Resize(im_full, im_640, cv.CV_INTER_NN)
      im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))
      im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

    count = 0
    total_time = 0
    img_scan = im_640

    t0=time.time()
    for i in range(opts.repeat):
      if opts.fullres:
        regions = scanner.scan(im_full)
        regions = cuav_region.RegionsConvert(regions, 1280, 960)
      else:
        regions = scanner.scan(img_scan)
        regions = cuav_region.RegionsConvert(regions)
      count += 1
    t1=time.time()

    if opts.filter:
      regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time, min_score=opts.minscore,
                                           filter_type=opts.filter_type)

    scan_count += 1

    # optionally link all the images with joe into a separate directory
    # for faster re-running of the test with just joe images
    if pos and opts.linkjoe and len(regions) > 0:
      cuav_util.mkdir_p(opts.linkjoe)
      if not cuav_util.polygon_outside((pos.lat, pos.lon), boundary):
        joepath = os.path.join(opts.linkjoe, os.path.basename(f))
        if os.path.exists(joepath):
          os.unlink(joepath)
        os.symlink(f, joepath)

    if pos and len(regions) > 0:
      joelog.add_regions(frame_time, regions, pos, f, width=1280, height=960, altitude=opts.altitude)

      if boundary:
        regions = cuav_region.filter_boundary(regions, boundary, pos)

    region_count += len(regions)

    if opts.mosaic and len(regions) > 0:
      composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions)
      thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
      mosaic.add_regions(regions, thumbs, f, pos)

    if opts.compress:
      jpeg = scanner.jpeg_compress(im_full, opts.quality)
      jpeg_filename = f[:-4] + '.jpg'
      if os.path.exists(jpeg_filename):
        print('jpeg %s already exists' % jpeg_filename)
        continue
      chameleon.save_file(jpeg_filename, jpeg)

    if opts.view:
      if opts.fullres:
        img_view = im_full
      else:
        img_view = img_scan
      mat = cv.fromarray(img_view)
      for r in regions:
        (x1,y1,x2,y2) = r.tuple()
        (w,h) = cuav_util.image_shape(img_view)
        x1 = x1*w//1280
        x2 = x2*w//1280
        y1 = y1*h//960
        y2 = y2*h//960
        cv.Rectangle(mat, (max(x1-2,0),max(y1-2,0)), (x2+2,y2+2), (255,0,0), 2)
      cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
      viewer.set_image(mat)

    total_time += (t1-t0)
    if t1 != t0:
      print('%s scan %.1f fps  %u regions [%u/%u]' % (
        f, count/total_time, region_count, scan_count, num_files))
Beispiel #10
0
def process(filename):
    '''process one file'''
    pgm = cuav_util.PGM(filename)
    img_full_grey = pgm.array

    im_full = numpy.zeros((960, 1280, 3), dtype='uint8')
    im_640 = numpy.zeros((480, 640, 3), dtype='uint8')

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.debayer_half(img_full_grey, im_640)
    t1 = time.time()
    print('debayer: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.debayer(img_full_grey, im_full)
    t1 = time.time()
    print('debayer_full: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    im_full2 = cv.CreateImage((1280, 960), 8, 3)
    img_full_grey2 = cv.GetImage(cv.fromarray(img_full_grey))
    for i in range(opts.repeat):
        cv.CvtColor(img_full_grey2, im_full2, cv.CV_BayerBG2BGR)
    t1 = time.time()
    print('debayer_cv_full: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        img = cv.GetImage(cv.fromarray(im_full))
        cv.CvtColor(img, img, cv.CV_RGB2HSV)
    t1 = time.time()
    print('RGB2HSV_full: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        img = cv.GetImage(cv.fromarray(im_640))
        cv.CvtColor(img, img, cv.CV_RGB2HSV)
    t1 = time.time()
    print('RGB2HSV_640: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        thumb = numpy.empty((100, 100, 3), dtype='uint8')
        scanner.rect_extract(im_full, thumb, 120, 125)
    t1 = time.time()
    print('rect_extract: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        thumb = cuav_util.SubImage(cv.GetImage(cv.fromarray(im_full)),
                                   (120, 125, 100, 100))
    t1 = time.time()
    print('SubImage: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.downsample(im_full, im_640)
    t1 = time.time()
    print('downsample: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.scan(im_640)
    t1 = time.time()
    print('scan: %.1f fps' % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.scan(im_full)
    t1 = time.time()
    print('scan_full: %.1f fps' % (opts.repeat / (t1 - t0)))

    if not hasattr(scanner, 'jpeg_compress'):
        return

    for quality in [30, 40, 50, 60, 70, 80, 90, 95]:
        t0 = time.time()
        for i in range(opts.repeat):
            jpeg = cPickle.dumps(ImagePacket(
                time.time(), scanner.jpeg_compress(im_full, quality)),
                                 protocol=cPickle.HIGHEST_PROTOCOL)
        t1 = time.time()
        print('jpeg full quality %u: %.1f fps  %u bytes' %
              (quality, opts.repeat / (t1 - t0), len(bytes(jpeg))))

    for quality in [30, 40, 50, 60, 70, 80, 90, 95]:
        t0 = time.time()
        for i in range(opts.repeat):
            img2 = cv.fromarray(im_full)
            jpeg = cPickle.dumps(ImagePacket(
                time.time(),
                cv.EncodeImage(
                    '.jpeg', img2,
                    [cv.CV_IMWRITE_JPEG_QUALITY, quality]).tostring()),
                                 protocol=cPickle.HIGHEST_PROTOCOL)
        t1 = time.time()
        print('EncodeImage full quality %u: %.1f fps  %u bytes' %
              (quality, opts.repeat / (t1 - t0), len(bytes(jpeg))))

    for quality in [30, 40, 50, 60, 70, 80, 90, 95]:
        t0 = time.time()
        for i in range(opts.repeat):
            jpeg = cPickle.dumps(ImagePacket(
                time.time(), scanner.jpeg_compress(im_640, quality)),
                                 protocol=cPickle.HIGHEST_PROTOCOL)
        t1 = time.time()
        print('jpeg 640 quality %u: %.1f fps  %u bytes' %
              (quality, opts.repeat / (t1 - t0), len(bytes(jpeg))))

    for thumb_size in [10, 20, 40, 60, 80, 100]:
        thumb = numpy.zeros((thumb_size, thumb_size, 3), dtype='uint8')
        t0 = time.time()
        for i in range(opts.repeat):
            scanner.rect_extract(im_full, thumb, 0, 0)
            jpeg = cPickle.dumps(ImagePacket(time.time(),
                                             scanner.jpeg_compress(thumb, 85)),
                                 protocol=cPickle.HIGHEST_PROTOCOL)
        t1 = time.time()
        print('thumb %u quality 85: %.1f fps  %u bytes' %
              (thumb_size, opts.repeat / (t1 - t0), len(bytes(jpeg))))
Beispiel #11
0
def process(args):
  '''process a set of files'''

  global slipmap, mosaic
  scan_count = 0
  files = []
  for a in args:
    if os.path.isdir(a):
      files.extend(file_list(a, ['jpg', 'pgm', 'png']))
    else:
      if a.find('*') != -1:
        files.extend(glob.glob(a))
      else:
        files.append(a)
  files.sort()
  num_files = len(files)
  print("num_files=%u" % num_files)
  region_count = 0

  slipmap = mp_slipmap.MPSlipMap(service=opts.service, elevation=True, title='Map')
  icon = slipmap.icon('redplane.png')
  slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0,
                                         follow=True,
                                         trail=mp_slipmap.SlipTrail()))

  if opts.mission:
    from pymavlink import mavwp
    wp = mavwp.MAVWPLoader()
    wp.load(opts.mission)
    boundary = wp.polygon()
    slipmap.add_object(mp_slipmap.SlipPolygon('mission', boundary, layer=1,
                                              linewidth=1, colour=(255,255,255)))


  if opts.mavlog:
    mpos = mav_position.MavInterpolator()
    mpos.set_logfile(opts.mavlog)
  else:
    mpos = None

  if opts.kmzlog:
    kmzpos = mav_position.KmlPosition(opts.kmzlog)
  else:
    kmzpos = None

  if opts.triggerlog:
    triggerpos = mav_position.TriggerPosition(opts.triggerlog)
  else:
    triggerpos = None

  # create a simple lens model using the focal length
  C_params = cam_params.CameraParams(lens=opts.lens, sensorwidth=opts.sensorwidth)

  if opts.camera_params:
    C_params.load(opts.camera_params)

  mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params)

  joelog = cuav_joe.JoeLog(None)

  if opts.view:
    viewer = mp_image.MPImage(title='Image', can_zoom=True, can_drag=True)

  scan_parms = {
    'MinRegionArea' : opts.min_region_area,
    'MaxRegionArea' : opts.max_region_area,
    'MinRegionSize' : opts.min_region_size,
    'MaxRegionSize' : opts.max_region_size,
    'MaxRarityPct'  : opts.max_rarity_pct,
    'RegionMergeSize' : opts.region_merge,
    'SaveIntermediate' : float(opts.debug)
    }

  if opts.filter_type == 'compactness':
    calculate_compactness = True
    print("Using compactness filter")
  else:
    calculate_compactness = False

  for f in files:
      if mpos:
        # get the position by interpolating telemetry data from the MAVLink log file
        # this assumes that the filename contains the timestamp 
        frame_time = cuav_util.parse_frame_time(f) + opts.time_offset
        if opts.roll_stabilised:
          roll = 0
        else:
          roll = None
        try:
          pos = mpos.position(frame_time, roll=roll)
        except Exception:
          print("No position available for %s" % frame_time)
          # skip this frame
          continue
      elif kmzpos is not None:
        pos = kmzpos.position(f)
      elif triggerpos is not None:
        pos = triggerpos.position(f)
      else:
        # get the position using EXIF data
        pos = mav_position.exif_position(f)
        pos.time += opts.time_offset

      # update the plane icon on the map
      if pos is not None:
        slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)
        if opts.altitude is not None:
          pos.altitude = opts.altitude

      # check for any events from the map
      slipmap.check_events()
      mosaic.check_events()

      im_orig = cuav_util.LoadImage(f)
      (w,h) = cuav_util.image_shape(im_orig)

      if not opts.camera_params:
        C_params.set_resolution(w, h)
      
      im_full = im_orig
        
      im_640 = cv.CreateImage((640, 480), 8, 3)
      cv.Resize(im_full, im_640, cv.CV_INTER_NN)
      im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))
      im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

      count = 0
      total_time = 0

      t0=time.time()
      if opts.fullres:
        img_scan = im_full
      else:
        img_scan = im_640

      if pos is not None:
        (sw,sh) = cuav_util.image_shape(img_scan)
        mpp = cuav_util.meters_per_pixel(pos, C=C_params)
        if mpp is not None:
          scan_parms['MetersPerPixel'] = mpp * (w/float(sw))
        regions = scanner.scan(img_scan, scan_parms)
      else:
        regions = scanner.scan(img_scan)
      regions = cuav_region.RegionsConvert(regions, cuav_util.image_shape(img_scan), cuav_util.image_shape(im_full), calculate_compactness)
      count += 1
      t1=time.time()

      frame_time = pos.time

      regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time,
                                           min_score=opts.minscore, filter_type=opts.filter_type)

      scan_count += 1

      mosaic.add_image(pos.time, f, pos)

      if pos and len(regions) > 0:
        joelog.add_regions(frame_time, regions, pos, f, width=w, height=h, altitude=opts.altitude)

      region_count += len(regions)

      if len(regions) > 0:
          composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions)
          thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
          mosaic.add_regions(regions, thumbs, f, pos)

      if opts.view:
        img_view = img_scan
        (wview,hview) = cuav_util.image_shape(img_view)
        mat = cv.fromarray(img_view)
        for r in regions:
          r.draw_rectangle(mat, (255,0,0))
        cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
        viewer.set_image(mat)
        viewer.set_title('Image: ' + os.path.basename(f))

      total_time += (t1-t0)
      if t1 != t0:
          print('%s scan %.1f fps  %u regions [%u/%u]' % (
              os.path.basename(f), count/total_time, region_count, scan_count, num_files))
Beispiel #12
0
def process(filename, repeat):
    '''process one file'''
    colour = cv2.imread(filename)
    colour_half = cv2.resize(colour, (0,0), fx=0.5, fy=0.5) 

    t0 = time.time()
    for i in range(repeat):
        cv2.cvtColor(colour, cv2.COLOR_RGB2HSV)
    t1 = time.time()
    if t1 > t0:
        print('RGB2HSV_full: %.1f fps' % (repeat/(t1-t0)))
    else:
        print('RGB2HSV_full: (inf) fps')

    t0 = time.time()
    for i in range(repeat):
        cv2.cvtColor(colour_half, cv2.COLOR_RGB2HSV)
    t1 = time.time()
    if t1 > t0:
        print('RGB2HSV_half: %.1f fps' % (repeat/(t1-t0)))
    else:
        print('RGB2HSV_half: (inf) fps')
        
    t0 = time.time()
    for i in range(repeat):
        thumb = numpy.empty((100,100,3),dtype='uint8')
        scanner.rect_extract(colour, thumb, 120, 125)
    t1 = time.time()
    if t1 > t0:
        print('rect_extract: %.1f fps' % (repeat/(t1-t0)))
    else:
        print('rect_extract: (inf) fps')
        
    t0 = time.time()
    for i in range(repeat):
        thumb = cuav_util.SubImage(colour, (120,125,100,100))
    t1 = time.time()
    if t1 > t0:
        print('SubImage: %.1f fps' % (repeat/(t1-t0)))
    else:
        print('SubImage: (inf) fps')

    #t0 = time.time()
    #for i in range(repeat):
    #    scanner.downsample(im_full, im_640)
    #t1 = time.time()
    #print('downsample: %.1f fps' % (repeat/(t1-t0)))

    t0 = time.time()
    for i in range(repeat):
        scanner.scan(colour_half)
    t1 = time.time()
    if t1 > t0:
        print('scan: %.1f fps' % (repeat/(t1-t0)))
    else:
        print('scan: (inf) fps')
        
    t0 = time.time()
    for i in range(repeat):
        scanner.scan(colour)
    t1 = time.time()
    if t1 > t0:
        print('scan_full: %.1f fps' % (repeat/(t1-t0)))
    else:
        print('scan_full: (inf) fps')
Beispiel #13
0
def process(file):
	#Process one file
	scan_count = 0
	region_count = 0
	thumbs=[]
	regions=[]
	scan_parms = {
	        'MinRegionArea' : opts.min_region_area,
	        'MaxRegionArea' : opts.max_region_area,
	        'MinRegionSize' : opts.min_region_size,
	        'MaxRegionSize' : opts.max_region_size,
	        'MaxRarityPct'  : opts.max_rarity_pct,
	        'RegionMerge'   : opts.region_merge
	}

	f = os.path.join(opts.directory, file)
	print "Processing .... ", f
	
	pos = mav_position.exif_position(f)
	pos.time += opts.time_offset
	if opts.filter_type == 'compactness':
		calculate_compactness = True
	else:
		calculate_compactness = False
	
	im_full = cuav_util.LoadImage(f)
	(w,h) = cuav_util.image_shape(im_full)
	C_params = cam_params.CameraParams(lens=opts.lens, sensorwidth=opts.sensorwidth)
	C_params.set_resolution(w, h)
	im_full = numpy.ascontiguousarray(cv.GetMat(im_full))
	count = 0
	total_time = 0
	t0=time.time()
	img_scan = im_full

	if pos is not None:
		(sw,sh) = cuav_util.image_shape(img_scan)
		mpp = cuav_util.meters_per_pixel(pos, C=C_params)
		if mpp is not None:
			scan_parms['MetersPerPixel'] = mpp * (w/float(sw))
		regions = scanner.scan(img_scan, scan_parms)
	else:
		regions = scanner.scan(img_scan)
	regions = cuav_region.RegionsConvert(regions, cuav_util.image_shape(img_scan), cuav_util.image_shape(im_full), calculate_compactness)
	count += 1
	t1=time.time()
	frame_time = pos.time
	regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time, min_score=opts.minscore, filter_type=opts.filter_type)
	scan_count += 1
	region_count += len(regions)

	total_time += (t1-t0)
	
	if len(regions) > 0:
		composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions, 200)
		thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
#		destinationfile = os.path.join(opts.destination, os.path.basename(f))            
#		cv.SaveImage(destinationfile, composite)
		index = 1
		for t in thumbs:
			destinationthumb = os.path.basename(f).split(".")
			destinationthumb = destinationthumb[0] + "_" + str(index) + "." + destinationthumb[1]
			destinationfile1 = os.path.join(opts.destination, destinationthumb)
			cv.SaveImage(destinationfile1, t)
			index = index + 1
			
		NameFile = os.path.basename(f).split(".")
		log_file = log_path +"/" + NameFile[0] + ".txt"
		fo = open(log_file, "a")
		print('Image %s %s %u regions' % (os.path.basename(f), pos, region_count))
		log_entry = os.path.basename(f) + "," + str(pos.lat) + "," + str(pos.lon) + "," + str(pos.altitude) + "," + str(pos.yaw) + "," + str(region_count)
		fo.write(log_entry + "\r\n")
		img_view = img_scan
		(wview,hview) = cuav_util.image_shape(img_view)
#		mat = cv.fromarray(img_view)
		ThumbPos = " "
		index = 1
		for r in regions:
			midx = (r.x1 + r.x2)//2
			midy = (r.y1 + r.y2)//2
			ThisThumbPos = pixel_coordinates(midx,midy,wview, hview, pos, mpp, opts.delay)
			ThumbDetails = str(index) + "," + str(r.scan_score) + "," + str(ThisThumbPos[0]) + "," + str(ThisThumbPos[1]) + "\r\n"
			fo.write(ThumbDetails)
#			r.draw_rectangle(mat, (255,0,0))
			index = index + 1
		fo.close()
Beispiel #14
0
def process(folderfile):
    '''process a file'''
    file_index = 0

    files = []
    if os.path.isdir(folderfile):
        files = file_list(folderfile, ['jpg', 'jpeg', 'png'])
    else:
        files.append(folderfile)

    fname = files[file_index]

    settings = MPSettings([
        MPSetting('MetersPerPixel',
                  float,
                  0.2,
                  range=(0, 10),
                  increment=0.01,
                  digits=2,
                  tab='Image Processing'),
        MPSetting('MinRegionArea',
                  float,
                  0.15,
                  range=(0, 100),
                  increment=0.05,
                  digits=2),
        MPSetting('MaxRegionArea',
                  float,
                  2.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('MinRegionSize',
                  float,
                  0.1,
                  range=(0, 100),
                  increment=0.05,
                  digits=2),
        MPSetting(
            'MaxRegionSize', float, 2, range=(0, 100), increment=0.1,
            digits=1),
        MPSetting('MaxRarityPct',
                  float,
                  0.02,
                  range=(0, 100),
                  increment=0.01,
                  digits=2),
        MPSetting('RegionMergeSize',
                  float,
                  3.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('minscore',
                  int,
                  0,
                  'Min Score',
                  range=(0, 1000),
                  increment=1,
                  tab='Scoring'),
        MPSetting(
            'filter_type', str, 'simple', 'Filter Type', choice=['simple']),
        MPSetting('brightness',
                  float,
                  1.0,
                  'Display Brightness',
                  range=(0.1, 10),
                  increment=0.1,
                  digits=2,
                  tab='Display')
    ],
                          title='Settings')

    menu = MPMenuSubMenu(
        'View',
        items=[
            MPMenuItem('Original Image', 'Original Image', '_1original.pnm'),
            MPMenuItem('Unquantised Image', 'Unquantised Image',
                       '_1unquantised.pnm'),
            MPMenuItem('Thresholded Image', 'Thresholded Image',
                       '_2thresholded.pnm'),
            MPMenuItem('Neighbours Image', 'Neighbours Image',
                       '_3neighbours.pnm'),
            MPMenuItem('Regions Image', 'Regions Image', '_4regions.pnm'),
            MPMenuItem('Prune Large Image', 'Prune Large Image',
                       '_5prunelarge.pnm'),
            MPMenuItem('Merged Image', 'Merged Large', '_6merged.pnm'),
            MPMenuItem('Pruned Image', 'Pruned Image', '_7pruned.pnm'),
            MPMenuItem('Fit Window', 'Fit Window', 'fitWindow'),
            MPMenuItem('Full Zoom', 'Full Zoom', 'fullSize'),
            MPMenuItem('Next Image', 'Next Image', 'nextImage'),
            MPMenuItem('Previous Image', 'Previous Image', 'previousImage')
        ])

    im_orig = cv2.imread(fname, -1)
    im_numpy = numpy.ascontiguousarray(im_orig)

    # create the various views
    view = mp_image.MPImage(title='FullImage', can_zoom=True, can_drag=True)
    view.set_popup_menu(menu)

    dlg = wxsettings.WXSettings(settings)

    selected_image = "_1original.pnm"

    while dlg.is_alive() and view.is_alive():
        last_change = settings.last_change()
        scan_parms = {
            'MinRegionArea': settings.MinRegionArea,
            'MaxRegionArea': settings.MaxRegionArea,
            'MinRegionSize': settings.MinRegionSize,
            'MaxRegionSize': settings.MaxRegionSize,
            'MaxRarityPct': settings.MaxRarityPct,
            'RegionMergeSize': settings.RegionMergeSize,
            'SaveIntermediate': 1.0,
            'MetersPerPixel': settings.MetersPerPixel
        }

        t0 = time.time()
        regions = scanner.scan(im_numpy, scan_parms)
        regions = cuav_region.RegionsConvert(regions,
                                             cuav_util.image_shape(im_orig),
                                             cuav_util.image_shape(im_orig),
                                             False)
        t1 = time.time()
        print("Processing %s took %.2f seconds" % (fname, t1 - t0))
        show_image(view, str(file_index + 1) + selected_image, im_orig, fname)

        while last_change == settings.last_change() and dlg.is_alive():
            new_index = file_index
            for event in view.events():
                if isinstance(event, MPMenuItem):
                    if event.returnkey == 'nextImage':
                        new_index = (file_index + 1) % len(files)
                    elif event.returnkey == 'previousImage':
                        new_index = (file_index - 1) % len(files)
                    elif event.returnkey.endswith("pnm"):
                        selected_image = event.returnkey
                        show_image(view,
                                   str(file_index + 1) + selected_image,
                                   im_orig, fname)
                if new_index != file_index:
                    file_index = new_index
                    fname = files[file_index]
                    im_orig = cv2.imread(fname, -1)
                    im_numpy = numpy.ascontiguousarray(im_orig)
                    break
                time.sleep(0.1)
        #remove all the pnm's
        directory = os.getcwd()
        lst = os.listdir(directory)

        for item in lst:
            if item.endswith(".pnm"):
                os.remove(os.path.join(directory, item))
Beispiel #15
0
def process(args):
    '''process a file'''
    file_index = 0

    if os.path.isdir(args[0]):
        files = file_list(args[0], ['jpg', 'pgm', 'png'])
    else:
        files = args

    fname = files[file_index]

    settings = MPSettings([
        MPSetting('MetersPerPixel',
                  float,
                  0.2,
                  range=(0, 10),
                  increment=0.01,
                  digits=2,
                  tab='Image Processing'),
        MPSetting('MinRegionArea',
                  float,
                  0.15,
                  range=(0, 100),
                  increment=0.05,
                  digits=2),
        MPSetting('MaxRegionArea',
                  float,
                  2.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('MinRegionSize',
                  float,
                  0.1,
                  range=(0, 100),
                  increment=0.05,
                  digits=2),
        MPSetting(
            'MaxRegionSize', float, 2, range=(0, 100), increment=0.1,
            digits=1),
        MPSetting('MaxRarityPct',
                  float,
                  0.02,
                  range=(0, 100),
                  increment=0.01,
                  digits=2),
        MPSetting('RegionMergeSize',
                  float,
                  3.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('minscore',
                  int,
                  0,
                  'Min Score',
                  range=(0, 1000),
                  increment=1,
                  tab='Scoring'),
        MPSetting('filter_type',
                  str,
                  'simple',
                  'Filter Type',
                  choice=['simple', 'compactness']),
        MPSetting('brightness',
                  float,
                  1.0,
                  'Display Brightness',
                  range=(0.1, 10),
                  increment=0.1,
                  digits=2,
                  tab='Display')
    ],
                          title='Settings')

    menu = MPMenuSubMenu(
        'View',
        items=[
            MPMenuItem('Original Image', 'Original Image', 'originalImage'),
            MPMenuItem('Unquantised Image', 'Unquantised Image',
                       'unquantisedImage'),
            MPMenuItem('Thresholded Image', 'Thresholded Image',
                       'thresholdedImage'),
            MPMenuItem('Neighbours Image', 'Neighbours Image',
                       'neighboursImage'),
            MPMenuItem('Regions Image', 'Regions Image', 'regionsImage'),
            MPMenuItem('Prune Large Image', 'Prune Large Image',
                       'prunelargeImage'),
            MPMenuItem('Merged Image', 'Merged Large', 'mergedImage'),
            MPMenuItem('Pruned Image', 'Pruned Image', 'prunedImage'),
            MPMenuItem('Fit Window', 'Fit Window', 'fitWindow'),
            MPMenuItem('Full Zoom', 'Full Zoom', 'fullSize'),
            MPMenuItem('Next Image', 'Next Image', 'nextImage'),
            MPMenuItem('Previous Image', 'Previous Image', 'previousImage')
        ])

    im_orig = cuav_util.LoadImage(fname)
    im_numpy = numpy.ascontiguousarray(cv.GetMat(im_orig))
    im_rgb = cv.fromarray(im_numpy)
    cv.CvtColor(im_rgb, im_rgb, cv.CV_BGR2RGB)

    # create the various views
    view = mp_image.MPImage(title='FullImage', can_zoom=True, can_drag=True)
    view.set_popup_menu(menu)

    dlg = wxsettings.WXSettings(settings)

    selected_image = "original"

    while dlg.is_alive() and view.is_alive():
        last_change = settings.last_change()
        scan_parms = {
            'MinRegionArea': settings.MinRegionArea,
            'MaxRegionArea': settings.MaxRegionArea,
            'MinRegionSize': settings.MinRegionSize,
            'MaxRegionSize': settings.MaxRegionSize,
            'MaxRarityPct': settings.MaxRarityPct,
            'RegionMergeSize': settings.RegionMergeSize,
            'SaveIntermediate': 1.0,
            'MetersPerPixel': settings.MetersPerPixel
        }

        t0 = time.time()
        regions = scanner.scan(im_numpy, scan_parms)
        regions = cuav_region.RegionsConvert(regions,
                                             cuav_util.image_shape(im_orig),
                                             cuav_util.image_shape(im_orig),
                                             False)
        t1 = time.time()
        print("Processing %s took %.2f seconds" % (fname, t1 - t0))
        show_image(view, selected_image, im_rgb, fname)

        while last_change == settings.last_change() and dlg.is_alive():
            new_index = file_index
            for event in view.events():
                if isinstance(event, MPMenuItem):
                    if event.returnkey == 'nextImage':
                        new_index = (file_index + 1) % len(files)
                    elif event.returnkey == 'previousImage':
                        new_index = (file_index - 1) % len(files)
                    elif event.returnkey.endswith("Image"):
                        selected_image = event.returnkey[:-5]
                        show_image(view, selected_image, im_rgb, fname)
            if new_index != file_index:
                file_index = new_index
                fname = files[file_index]
                im_orig = cuav_util.LoadImage(fname)
                im_numpy = numpy.ascontiguousarray(cv.GetMat(im_orig))
                im_rgb = cv.fromarray(im_numpy)
                cv.CvtColor(im_rgb, im_rgb, cv.CV_BGR2RGB)
                break
            time.sleep(0.1)
Beispiel #16
0
def process(args):
  '''process a set of files'''

  global slipmap, mosaic
  scan_count = 0
  files = []
  for a in args:
    if os.path.isdir(a):
      files.extend(glob.glob(os.path.join(a, '*.jpg')))
      files.extend(glob.glob(os.path.join(a, '*.pgm')))
      files.extend(glob.glob(os.path.join(a, '*.png')))
    else:
      if a.find('*') != -1:
        files.extend(glob.glob(a))
      else:
        files.append(a)
  files.sort()
  num_files = len(files)
  print("num_files=%u" % num_files)
  region_count = 0

  slipmap = mp_slipmap.MPSlipMap(service='GoogleSat', elevation=True, title='Map')
  icon = slipmap.icon('planetracker.png')
  slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0,
                                         follow=True,
                                         trail=mp_slipmap.SlipTrail()))

  if opts.grid:
    slipmap.add_object(mp_slipmap.SlipGrid('grid', layer=1, linewidth=1, colour=(255,255,0)))

  if opts.mission:
    from pymavlink import mavwp
    wp = mavwp.MAVWPLoader()
    wp.load(opts.mission)
    boundary = wp.polygon()
    slipmap.add_object(mp_slipmap.SlipPolygon('mission', boundary, layer=1,
                                              linewidth=1, colour=(255,255,255)))


  if opts.mavlog:
    mpos = mav_position.MavInterpolator()
    mpos.set_logfile(opts.mavlog)
  else:
    mpos = None

  # create a simple lens model using the focal length
  C_params = cam_params.CameraParams(lens=opts.lens)

  if opts.camera_params:
    C_params.load(opts.camera_params)

  mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params)

  joelog = cuav_joe.JoeLog(None)

  if opts.view:
    viewer = mp_image.MPImage(title='Image')

  for f in files:
      if mpos:
        # get the position by interpolating telemetry data from the MAVLink log file
        # this assumes that the filename contains the timestamp 
        frame_time = cuav_util.parse_frame_time(f) + opts.time_offset
        if opts.roll_stabilised:
          roll = 0
        else:
          roll = None
        try:
          pos = mpos.position(frame_time, roll=roll)
        except Exception:
          print("No position available for %s" % frame_time)
          # skip this frame
          continue
      else:
        # get the position using EXIF data
        pos = mav_position.exif_position(f)
        pos.time += opts.time_offset

      # update the plane icon on the map
      slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)

      # check for any events from the map
      slipmap.check_events()
      mosaic.check_events()

      im_orig = cuav_util.LoadImage(f)
      (w,h) = cuav_util.image_shape(im_orig)
      if (w,h) != (1280,960):
          im_full = cv.CreateImage((1280, 960), 8, 3)
          cv.Resize(im_orig, im_full)
          cv.ConvertScale(im_full, im_full, scale=0.3)
      else:
          im_full = im_orig
        
      im_640 = cv.CreateImage((640, 480), 8, 3)
      cv.Resize(im_full, im_640, cv.CV_INTER_NN)
      im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))
      im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

      count = 0
      total_time = 0
      img_scan = im_640

      t0=time.time()
      regions = scanner.scan(img_scan)
      count += 1
      regions = cuav_region.RegionsConvert(regions)
      t1=time.time()

      frame_time = pos.time

      regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time,
                                           min_score=opts.minscore, filter_type=opts.filter_type)

      scan_count += 1

      mosaic.add_image(pos.time, f, pos)

      if pos and len(regions) > 0:
        joelog.add_regions(frame_time, regions, pos, f, width=1280, height=960, altitude=opts.altitude)

      region_count += len(regions)

      if len(regions) > 0:
          composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions)
          thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
          mosaic.add_regions(regions, thumbs, f, pos)

      if opts.view:
        img_view = img_scan
        mat = cv.fromarray(img_view)
        for r in regions:
          (x1,y1,x2,y2) = r.tuple()
          (w,h) = cuav_util.image_shape(img_view)
          x1 = x1*w//1280
          x2 = x2*w//1280
          y1 = y1*h//960
          y2 = y2*h//960
          cv.Rectangle(mat, (max(x1-2,0),max(y1-2,0)), (x2+2,y2+2), (255,0,0), 2)
        cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
        viewer.set_image(mat)

      total_time += (t1-t0)
      if t1 != t0:
          print('%s scan %.1f fps  %u regions [%u/%u]' % (
              os.path.basename(f), count/total_time, region_count, scan_count, num_files))
Beispiel #17
0
def process(args):
	'''process a file'''
        file_index = 0

        if os.path.isdir(args[0]):
          files = file_list(args[0], ['jpg', 'pgm', 'png'])
        else:
          files = args

        fname = files[file_index]

        settings = MPSettings(
          [
          MPSetting('MetersPerPixel', float, 0.2, range=(0,10), increment=0.01, digits=2, tab='Image Processing'),
          MPSetting('MinRegionArea', float, 0.15, range=(0,100), increment=0.05, digits=2),
          MPSetting('MaxRegionArea', float, 2.0, range=(0,100), increment=0.1, digits=1),
          MPSetting('MinRegionSize', float, 0.1, range=(0,100), increment=0.05, digits=2),
          MPSetting('MaxRegionSize', float, 2, range=(0,100), increment=0.1, digits=1),
          MPSetting('MaxRarityPct',  float, 0.02, range=(0,100), increment=0.01, digits=2),
          MPSetting('RegionMergeSize', float, 3.0, range=(0,100), increment=0.1, digits=1),
          MPSetting('minscore', int, 0, 'Min Score', range=(0,1000), increment=1, tab='Scoring'),
          MPSetting('filter_type', str, 'simple', 'Filter Type', choice=['simple', 'compactness']),
          MPSetting('brightness', float, 1.0, 'Display Brightness', range=(0.1, 10), increment=0.1,
                    digits=2, tab='Display')
          ],
          title='Settings'
          )

        menu = MPMenuSubMenu('View',
                             items=[
          MPMenuItem('Original Image', 'Original Image', 'originalImage'),
          MPMenuItem('Unquantised Image', 'Unquantised Image', 'unquantisedImage'),
          MPMenuItem('Thresholded Image', 'Thresholded Image', 'thresholdedImage'),
          MPMenuItem('Neighbours Image', 'Neighbours Image', 'neighboursImage'),
          MPMenuItem('Regions Image', 'Regions Image', 'regionsImage'),
          MPMenuItem('Prune Large Image', 'Prune Large Image', 'prunelargeImage'),
          MPMenuItem('Merged Image', 'Merged Large', 'mergedImage'),
          MPMenuItem('Pruned Image', 'Pruned Image', 'prunedImage'),
          MPMenuItem('Fit Window', 'Fit Window', 'fitWindow'),
          MPMenuItem('Full Zoom',  'Full Zoom', 'fullSize'),
          MPMenuItem('Next Image', 'Next Image', 'nextImage'),
          MPMenuItem('Previous Image',  'Previous Image', 'previousImage')])
        
        im_orig = cuav_util.LoadImage(fname)
        im_numpy = numpy.ascontiguousarray(cv.GetMat(im_orig))
        im_rgb = cv.fromarray(im_numpy)
        cv.CvtColor(im_rgb, im_rgb, cv.CV_BGR2RGB)

        # create the various views
        view = mp_image.MPImage(title='FullImage', can_zoom=True, can_drag=True)
        view.set_popup_menu(menu)

        dlg = wxsettings.WXSettings(settings)

        selected_image = "original"
        
        while dlg.is_alive() and view.is_alive():
        	last_change = settings.last_change()
                scan_parms = {
                  'MinRegionArea' : settings.MinRegionArea,
                  'MaxRegionArea' : settings.MaxRegionArea,
                  'MinRegionSize' : settings.MinRegionSize,
                  'MaxRegionSize' : settings.MaxRegionSize,
                  'MaxRarityPct'  : settings.MaxRarityPct,
                  'RegionMergeSize' : settings.RegionMergeSize,
                  'SaveIntermediate' : 1.0,
                  'MetersPerPixel' : settings.MetersPerPixel
                  }

                t0 = time.time()
                regions = scanner.scan(im_numpy, scan_parms)
                regions = cuav_region.RegionsConvert(regions,
                                                     cuav_util.image_shape(im_orig),
                                                     cuav_util.image_shape(im_orig), False)    
                t1=time.time()
                print("Processing %s took %.2f seconds" % (fname, t1-t0))
                show_image(view, selected_image, im_rgb, fname)

                while last_change == settings.last_change() and dlg.is_alive():
                	new_index = file_index
                	for event in view.events():
                        	if isinstance(event, MPMenuItem):
                                        if event.returnkey == 'nextImage':
                                        	new_index = (file_index + 1) % len(files)
                                        elif event.returnkey == 'previousImage':
                                        	new_index = (file_index - 1) % len(files)
                                	elif event.returnkey.endswith("Image"):
                                        	selected_image = event.returnkey[:-5]
                                                show_image(view, selected_image, im_rgb, fname)
                        if new_index != file_index:
                        	file_index = new_index
                                fname = files[file_index]
                                im_orig = cuav_util.LoadImage(fname)
                                im_numpy = numpy.ascontiguousarray(cv.GetMat(im_orig))
                                im_rgb = cv.fromarray(im_numpy)
                                cv.CvtColor(im_rgb, im_rgb, cv.CV_BGR2RGB)
                                break
                        time.sleep(0.1)
Beispiel #18
0
def process(args):
  '''process a set of files'''

  global slipmap, mosaic
  scan_count = 0
  files = []
  for a in args:
    if os.path.isdir(a):
      files.extend(file_list(a, ['jpg', 'pgm', 'png']))
    else:
      if a.find('*') != -1:
        files.extend(glob.glob(a))
      else:
        files.append(a)
  files.sort()
  num_files = len(files)
  print("num_files=%u" % num_files)
  region_count = 0

  slipmap = mp_slipmap.MPSlipMap(service=opts.service, elevation=True, title='Map')
  icon = slipmap.icon('redplane.png')
  slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0,
                                         follow=True,
                                         trail=mp_slipmap.SlipTrail()))

  for flag in opts.flag:
    a = flag.split(',')
    lat = a[0]
    lon = a[1]
    icon = 'flag.png'
    if len(a) > 2:
      icon = a[2] + '.png'
      icon = slipmap.icon(icon)
      slipmap.add_object(mp_slipmap.SlipIcon('icon - %s' % str(flag), (float(lat),float(lon)), icon, layer=3, rotation=0, follow=False))

  if opts.mission:
    from pymavlink import mavwp
    wp = mavwp.MAVWPLoader()
    wp.load(opts.mission)
    boundary = wp.polygon()
    slipmap.add_object(mp_slipmap.SlipPolygon('mission', boundary, layer=1,
                                              linewidth=1, colour=(255,255,255)))


  if opts.mavlog:
    mpos = mav_position.MavInterpolator()
    mpos.set_logfile(opts.mavlog)
  else:
    mpos = None

  if opts.gammalog is not None:
    gamma = parse_gamma_log(opts.gammalog)
  else:
    gamma = None

  if opts.kmzlog:
    kmzpos = mav_position.KmlPosition(opts.kmzlog)
  else:
    kmzpos = None

  if opts.triggerlog:
    triggerpos = mav_position.TriggerPosition(opts.triggerlog)
  else:
    triggerpos = None

  # create a simple lens model using the focal length
  C_params = cam_params.CameraParams(lens=opts.lens, sensorwidth=opts.sensorwidth)

  if opts.camera_params:
    C_params.load(opts.camera_params)

  camera_settings = MPSettings(
    [ MPSetting('roll_stabilised', bool, opts.roll_stabilised, 'Roll Stabilised'),
      MPSetting('altitude', int, opts.altitude, 'Altitude', range=(0,10000), increment=1),
      MPSetting('minalt', int, 30, 'MinAltitude', range=(0,10000), increment=1),
      MPSetting('mpp100', float, 0.0977, 'MPPat100m', range=(0,10000), increment=0.001),
      MPSetting('filter_type', str, 'simple', 'Filter Type',
                choice=['simple', 'compactness']),
      MPSetting('quality', int, 75, 'Compression Quality', range=(1,100), increment=1),
      MPSetting('thumbsize', int, opts.thumbsize, 'Thumbnail Size', range=(10, 200), increment=1),
      MPSetting('minscore', int, opts.minscore, 'Min Score', range=(0,1000), increment=1, tab='Scoring'),
      MPSetting('brightness', float, 1.0, 'Display Brightness', range=(0.1, 10), increment=0.1,
                digits=2, tab='Display')
      ],
    title='Camera Settings'
    )

  image_settings = MPSettings(
    [ MPSetting('MinRegionArea', float, 0.15, range=(0,100), increment=0.05, digits=2, tab='Image Processing'),
      MPSetting('MaxRegionArea', float, 2.0, range=(0,100), increment=0.1, digits=1),
      MPSetting('MinRegionSize', float, 0.1, range=(0,100), increment=0.05, digits=2),
      MPSetting('MaxRegionSize', float, 2, range=(0,100), increment=0.1, digits=1),
      MPSetting('MaxRarityPct',  float, 0.02, range=(0,100), increment=0.01, digits=2),
      MPSetting('RegionMergeSize', float, 3.0, range=(0,100), increment=0.1, digits=1),
      MPSetting('SaveIntermediate', bool, opts.debug)
      ],
    title='Image Settings')
  
  mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params,
                              camera_settings=camera_settings,
                              image_settings=image_settings,
                              start_menu=True,
                              classify=opts.categories,
                              thumb_size=opts.mosaic_thumbsize)

  joelog = cuav_joe.JoeLog(None)

  if opts.view:
    viewer = mp_image.MPImage(title='Image', can_zoom=True, can_drag=True)

  if camera_settings.filter_type == 'compactness':
    calculate_compactness = True
    print("Using compactness filter")
  else:
    calculate_compactness = False

  for f in files:
      if not mosaic.started():
        print("Waiting for startup")
        while not mosaic.started():
          mosaic.check_events()
          time.sleep(0.01)

      if mpos:
        # get the position by interpolating telemetry data from the MAVLink log file
        # this assumes that the filename contains the timestamp 
        if gamma is not None:
          frame_time = parse_gamma_time(f, gamma)
        else:
          frame_time = cuav_util.parse_frame_time(f)
        frame_time += opts.time_offset
        if camera_settings.roll_stabilised:
          roll = 0
        else:
          roll = None
        try:
          pos = mpos.position(frame_time, roll=roll)
        except Exception:
          print("No position available for %s" % frame_time)
          # skip this frame
          continue
      elif kmzpos is not None:
        pos = kmzpos.position(f)
      elif triggerpos is not None:
        pos = triggerpos.position(f)
      else:
        # get the position using EXIF data
        pos = mav_position.exif_position(f)
        pos.time += opts.time_offset

      # update the plane icon on the map
      if pos is not None:
        slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)
        if camera_settings.altitude > 0:
          pos.altitude = camera_settings.altitude

      # check for any events from the map
      slipmap.check_events()
      mosaic.check_events()

      im_orig = cuav_util.LoadImage(f)
      (w,h) = cuav_util.image_shape(im_orig)

      if not opts.camera_params:
        C_params.set_resolution(w, h)
      
      im_full = im_orig
        
      im_640 = cv.CreateImage((640, 480), 8, 3)
      cv.Resize(im_full, im_640, cv.CV_INTER_NN)
      im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))
      im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

      count = 0
      total_time = 0

      t0=time.time()
      img_scan = im_full

      scan_parms = {}
      for name in image_settings.list():
        scan_parms[name] = image_settings.get(name)
      scan_parms['SaveIntermediate'] = float(scan_parms['SaveIntermediate'])

      if pos is not None:
        (sw,sh) = cuav_util.image_shape(img_scan)
        altitude = pos.altitude
        if altitude < camera_settings.minalt:
          altitude = camera_settings.minalt
        scan_parms['MetersPerPixel'] = camera_settings.mpp100 * altitude / 100.0

        regions = scanner.scan(img_scan, scan_parms)
      else:
        regions = scanner.scan(img_scan)
      regions = cuav_region.RegionsConvert(regions, cuav_util.image_shape(img_scan), cuav_util.image_shape(im_full), calculate_compactness)
      count += 1
      t1=time.time()

      frame_time = pos.time

      regions = cuav_region.filter_regions(im_full, regions, frame_time=frame_time,
                                           min_score=camera_settings.minscore,
                                           filter_type=camera_settings.filter_type)

      scan_count += 1

      if pos and len(regions) > 0:
        altitude = camera_settings.altitude
        if altitude <= 0:
          altitude = None
        joelog.add_regions(frame_time, regions, pos, f, width=w, height=h,
                           altitude=altitude)

      mosaic.add_image(pos.time, f, pos)

      region_count += len(regions)

      if len(regions) > 0:
          composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions)
          thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
          mosaic.add_regions(regions, thumbs, f, pos)

      if opts.view:
        img_view = img_scan
        (wview,hview) = cuav_util.image_shape(img_view)
        mat = cv.fromarray(img_view)
        for r in regions:
          r.draw_rectangle(mat, (255,0,0))
        cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
        viewer.set_image(mat)
        viewer.set_title('Image: ' + os.path.basename(f))

      total_time += (t1-t0)
      if t1 != t0:
          print('%s scan %.1f fps  %u regions [%u/%u]' % (
              os.path.basename(f), count/total_time, region_count, scan_count, num_files))
Beispiel #19
0
def process(args):
    '''process a set of files'''

    global slipmap, mosaic
    scan_count = 0
    files = []
    if os.path.isdir(args.directory):
        files.extend(file_list(args.directory, ['jpg', 'pgm', 'png']))
    else:
        if args.directory.find('*') != -1:
            files.extend(glob.glob(args.directory))
        else:
            files.append(args.directory)
    files.sort()
    num_files = len(files)
    print("num_files=%u" % num_files)
    region_count = 0

    slipmap = mp_slipmap.MPSlipMap(service=args.service,
                                   elevation=True,
                                   title='Map')
    icon = slipmap.icon('redplane.png')
    slipmap.add_object(
        mp_slipmap.SlipIcon('plane', (0, 0),
                            icon,
                            layer=3,
                            rotation=0,
                            follow=True,
                            trail=mp_slipmap.SlipTrail()))

    for flag in args.flag:
        a = flag.split(',')
        lat = a[0]
        lon = a[1]
        icon = 'flag.png'
        if len(a) > 2:
            icon = a[2] + '.png'
            icon = slipmap.icon(icon)
            slipmap.add_object(
                mp_slipmap.SlipIcon('icon - %s' % str(flag),
                                    (float(lat), float(lon)),
                                    icon,
                                    layer=3,
                                    rotation=0,
                                    follow=False))

    if args.mission:
        from pymavlink import mavwp
        wp = mavwp.MAVWPLoader()
        wp.load(args.mission.name)
        plist = wp.polygon_list()
        if len(plist) > 0:
            for i in range(len(plist)):
                slipmap.add_object(
                    mp_slipmap.SlipPolygon('Mission-%s-%u' %
                                           (args.mission.name, i),
                                           plist[i],
                                           layer='Mission',
                                           linewidth=2,
                                           colour=(255, 255, 255)))

    if args.mavlog:
        mpos = mav_position.MavInterpolator()
        mpos.set_logfile(args.mavlog.name)
    else:
        mpos = None

    if args.gammalog is not None:
        gamma = parse_gamma_log(args.gammalog)
    else:
        gamma = None

    if args.kmzlog:
        kmzpos = mav_position.KmlPosition(args.kmzlog.name)
    else:
        kmzpos = None

    if args.triggerlog:
        triggerpos = mav_position.TriggerPosition(args.triggerlog.name)
    else:
        triggerpos = None

    # create a simple lens model using the focal length

    if args.camera_params:
        C_params = cam_params.CameraParams.fromfile(args.camera_params.name)
    else:
        C_params = cam_params.CameraParams(lens=args.lens,
                                           sensorwidth=args.sensorwidth,
                                           xresolution=args.xresolution,
                                           yresolution=args.yresolution)

    if args.target:
        target = args.target.split(',')
    else:
        target = [0, 0, 0]

    camera_settings = MPSettings([
        MPSetting('roll_stabilised', bool, args.roll_stabilised,
                  'Roll Stabilised'),
        MPSetting('altitude',
                  int,
                  args.altitude,
                  'Altitude',
                  range=(0, 10000),
                  increment=1),
        MPSetting(
            'minalt', int, 30, 'MinAltitude', range=(0, 10000), increment=1),
        MPSetting('mpp100',
                  float,
                  0.0977,
                  'MPPat100m',
                  range=(0, 10000),
                  increment=0.001),
        MPSetting('rotate180', bool, args.rotate_180, 'rotate180'),
        MPSetting('filter_type',
                  str,
                  'compactness',
                  'Filter Type',
                  choice=['simple', 'compactness']),
        MPSetting('target_lattitude',
                  float,
                  float(target[0]),
                  'target latitude',
                  increment=1.0e-7),
        MPSetting('target_longitude',
                  float,
                  float(target[1]),
                  'target longitude',
                  increment=1.0e-7),
        MPSetting('target_radius',
                  float,
                  float(target[2]),
                  'target radius',
                  increment=1),
        MPSetting('quality',
                  int,
                  75,
                  'Compression Quality',
                  range=(1, 100),
                  increment=1),
        MPSetting('thumbsize',
                  int,
                  args.thumbsize,
                  'Thumbnail Size',
                  range=(10, 200),
                  increment=1),
        MPSetting('minscore',
                  int,
                  args.minscore,
                  'Min Score',
                  range=(0, 1000),
                  increment=1,
                  tab='Scoring'),
        MPSetting('brightness',
                  float,
                  1.0,
                  'Display Brightness',
                  range=(0.1, 10),
                  increment=0.1,
                  digits=2,
                  tab='Display'),
    ],
                                 title='Camera Settings')

    image_settings = MPSettings([
        MPSetting('MinRegionArea',
                  float,
                  0.05,
                  range=(0, 100),
                  increment=0.05,
                  digits=2,
                  tab='Image Processing'),
        MPSetting('MaxRegionArea',
                  float,
                  4.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('MinRegionSize',
                  float,
                  0.02,
                  range=(0, 100),
                  increment=0.05,
                  digits=2),
        MPSetting('MaxRegionSize',
                  float,
                  3.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('MaxRarityPct',
                  float,
                  0.02,
                  range=(0, 100),
                  increment=0.01,
                  digits=2),
        MPSetting('RegionMergeSize',
                  float,
                  1.0,
                  range=(0, 100),
                  increment=0.1,
                  digits=1),
        MPSetting('BlueEmphasis', bool, args.blue_emphasis),
        MPSetting('SaveIntermediate', bool, args.debug)
    ],
                                title='Image Settings')

    mosaic = cuav_mosaic.Mosaic(slipmap,
                                C=C_params,
                                camera_settings=camera_settings,
                                image_settings=image_settings,
                                start_menu=True,
                                classify=args.categories,
                                thumb_size=args.mosaic_thumbsize)

    joelog = cuav_joe.JoeLog(None)

    if args.view:
        viewer = mp_image.MPImage(title='Image', can_zoom=True, can_drag=True)

    start_time = time.time()
    for f in files:
        if not mosaic.started():
            print("Waiting for startup")
            if args.start:
                mosaic.has_started = True
            while not mosaic.started():
                mosaic.check_events()
                time.sleep(0.01)

        if mpos:
            # get the position by interpolating telemetry data from the MAVLink log file
            # this assumes that the filename contains the timestamp
            if gamma is not None:
                frame_time = parse_gamma_time(f, gamma)
            else:
                frame_time = cuav_util.parse_frame_time(f)
            frame_time += args.time_offset
            if camera_settings.roll_stabilised:
                roll = 0
            else:
                roll = None
            try:
                pos = mpos.position(frame_time, roll=roll)
            except Exception:
                print("No position available for %s" % frame_time)
                # skip this frame
                continue
        elif kmzpos is not None:
            pos = kmzpos.position(f)
        elif triggerpos is not None:
            pos = triggerpos.position(f)
        else:
            # get the position using EXIF data
            pos = mav_position.exif_position(f)
            pos.time += args.time_offset

        # update the plane icon on the map
        if pos is not None:
            slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)
            if camera_settings.altitude > 0:
                pos.altitude = camera_settings.altitude

        # check for any events from the map
        slipmap.check_events()
        mosaic.check_events()

        im_orig = cuav_util.LoadImage(f, rotate180=camera_settings.rotate180)
        if im_orig is None:
            continue
        (w, h) = cuav_util.image_shape(im_orig)

        if False:
            im_640 = cv.CreateImage((640, 480), 8, 3)
            cv.Resize(im_full, im_640, cv.CV_INTER_NN)
            im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))

        im_full = im_orig
        im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

        count = 0
        total_time = 0

        t0 = time.time()
        img_scan = im_full

        scan_parms = {}
        for name in image_settings.list():
            scan_parms[name] = image_settings.get(name)
        scan_parms['SaveIntermediate'] = float(scan_parms['SaveIntermediate'])
        scan_parms['BlueEmphasis'] = float(scan_parms['BlueEmphasis'])

        if pos is not None:
            (sw, sh) = cuav_util.image_shape(img_scan)
            altitude = pos.altitude
            if altitude < camera_settings.minalt:
                altitude = camera_settings.minalt
            scan_parms[
                'MetersPerPixel'] = camera_settings.mpp100 * altitude / 100.0

            regions = scanner.scan(img_scan, scan_parms)
        else:
            regions = scanner.scan(img_scan)
        regions = cuav_region.RegionsConvert(regions,
                                             cuav_util.image_shape(img_scan),
                                             cuav_util.image_shape(im_full))
        count += 1
        t1 = time.time()

        frame_time = pos.time

        if pos:
            for r in regions:
                r.latlon = cuav_util.gps_position_from_image_region(
                    r, pos, w, h, altitude=altitude, C=C_params)

            if camera_settings.target_radius > 0 and pos is not None:
                regions = cuav_region.filter_radius(
                    regions, (camera_settings.target_lattitude,
                              camera_settings.target_longitude),
                    camera_settings.target_radius)

        regions = cuav_region.filter_regions(
            im_full,
            regions,
            frame_time=frame_time,
            min_score=camera_settings.minscore,
            filter_type=camera_settings.filter_type)

        scan_count += 1

        if pos and len(regions) > 0:
            altitude = camera_settings.altitude
            if altitude <= 0:
                altitude = None
            joelog.add_regions(frame_time,
                               regions,
                               pos,
                               f,
                               width=w,
                               height=h,
                               altitude=altitude)

        mosaic.add_image(pos.time, f, pos)

        region_count += len(regions)

        if len(regions) > 0:
            composite = cuav_mosaic.CompositeThumbnail(
                cv.GetImage(cv.fromarray(im_full)), regions)
            thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
            mosaic.add_regions(regions, thumbs, f, pos)

        if args.view:
            img_view = img_scan
            (wview, hview) = cuav_util.image_shape(img_view)
            mat = cv.fromarray(img_view)
            for r in regions:
                r.draw_rectangle(mat, (255, 0, 0))
            cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
            viewer.set_image(mat)
            viewer.set_title('Image: ' + os.path.basename(f))
            if args.saveview:
                cv.CvtColor(mat, mat, cv.CV_RGB2BGR)
                cv.SaveImage('view-' + os.path.basename(f), mat)

        total_time += (t1 - t0)
        if t1 != t0:
            print('%s scan %.1f fps  %u regions [%u/%u]' %
                  (os.path.basename(f), count / total_time, region_count,
                   scan_count, num_files))
        #raw_input("hit ENTER when ready")

    print("All images processed (%u seconds)" % (time.time() - start_time))
    while True:
        # check for any events from the map
        slipmap.check_events()
        mosaic.check_events()
        time.sleep(0.2)
Beispiel #20
0
def process(args):
    '''process a set of files'''

    global slipmap, mosaic
    scan_count = 0
    files = scan_image_directory(args.imagedir)
    files.sort()
    num_files = len(files)
    print("num_files=%u" % num_files)
    region_count = 0
    joes = []

    if args.mavlog:
        mpos = mav_position.MavInterpolator(gps_lag=args.gps_lag)
        mpos.set_logfile(args.mavlog)
    else:
        mpos = None

    if args.boundary:
        boundary = cuav_util.polygon_load(args.boundary)
    else:
        boundary = None

    if args.mosaic:
        slipmap = mp_slipmap.MPSlipMap(service='GoogleSat', elevation=True, title='Map')
        icon = slipmap.icon('redplane.png')
        slipmap.add_object(mp_slipmap.SlipIcon('plane', (0,0), icon, layer=3, rotation=0,
                                         follow=True,
                                         trail=mp_slipmap.SlipTrail()))
        if args.camera_params:
            C_params = cam_params.CameraParams.fromfile(args.camera_params.name)
        else:
            im_orig = cv2.imread(files[0])
            (w,h) = cuav_util.image_shape(im_orig)
            C_params = cam_params.CameraParams(lens=args.lens, sensorwidth=args.sensorwidth, xresolution=w, yresolution=h)
        mosaic = cuav_mosaic.Mosaic(slipmap, C=C_params)
        if boundary is not None:
            mosaic.set_boundary(boundary)

    if args.joe:
        joes = cuav_util.polygon_load(args.joe)
        if boundary:
            for i in range(len(joes)):
                joe = joes[i]
                if cuav_util.polygon_outside(joe, boundary):
                    print("Error: joe outside boundary", joe)
                    return
                icon = slipmap.icon('flag.png')
                slipmap.add_object(mp_slipmap.SlipIcon('joe%u' % i, (joe[0],joe[1]), icon, layer=4))

    joelog = cuav_joe.JoeLog('joe.log')      

    if args.view:
        viewer = mp_image.MPImage(title='Image')

    frame_time = 0

    scan_parms = {
        'MinRegionArea' : args.min_region_area,
        'MaxRegionArea' : args.max_region_area,
        'MinRegionSize' : args.min_region_size,
        'MaxRegionSize' : args.max_region_size,
        'MaxRarityPct'  : args.max_rarity_pct,
        'RegionMergeSize' : args.region_merge,
        'SaveIntermediate' : float(0),
        #'SaveIntermediate' : float(args.debug),
        'MetersPerPixel' : args.meters_per_pixel100 * args.altitude / 100.0
    }

    filenum = 0
        
    for f in files:
        filenum += 1
        if mpos:
            frame_time = cuav_util.parse_frame_time(f)
            try:
                if args.roll_stabilised:
                    roll = 0
                else:
                    roll = None
                pos = mpos.position(frame_time, args.max_deltat,roll=roll)
                slipmap.set_position('plane', (pos.lat, pos.lon), rotation=pos.yaw)
            except mav_position.MavInterpolatorException as e:
                print(e)
                pos = None
        else:
              pos = None

        # check for any events from the map
        if args.mosaic:
            slipmap.check_events()
            mosaic.check_events()

        im_orig = cv2.imread(f)
        (w,h) = cuav_util.image_shape(im_orig)
        im_full = im_orig
        im_half = cv2.resize(im_orig, (0,0), fx=0.5, fy=0.5)
        im_half = numpy.ascontiguousarray(im_half)
        im_full = numpy.ascontiguousarray(im_full)

        count = 0
        total_time = 0
        if args.fullres:
            img_scan = im_full
        else:
            img_scan = im_half

        t0=time.time()
        for i in range(args.repeat):
            regions = scanner.scan(img_scan, scan_parms)
            regions = cuav_region.RegionsConvert(regions, cuav_util.image_shape(img_scan), cuav_util.image_shape(im_full))
            count += 1
        t1=time.time()

        if args.filter:
            regions = cuav_region.filter_regions(im_full, regions, min_score=args.minscore,
                                           filter_type=args.filter_type)

        if len(regions) > 0 and args.debug:
            composite = cuav_region.CompositeThumbnail(im_full, regions, thumb_size=args.thumb_size)
            thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
            thumb_num = 0
            for thumb in thumbs:
                print("thumb %u score %f" % (thumb_num, regions[thumb_num].score))
                cv2.imwrite('%u_thumb%u.jpg' % (filenum,thumb_num), thumb)
                thumb_num += 1
            
        scan_count += 1

        # optionally link all the images with joe into a separate directory
        # for faster re-running of the test with just joe images
        if pos and args.linkjoe and len(regions) > 0:
            cuav_util.mkdir_p(args.linkjoe)
            if not cuav_util.polygon_outside((pos.lat, pos.lon), boundary):
                joepath = os.path.join(args.linkjoe, os.path.basename(f))
                if os.path.exists(joepath):
                    os.unlink(joepath)
                os.symlink(f, joepath)

        if pos and len(regions) > 0:
            joelog.add_regions(frame_time, regions, pos, f, width=w, height=h, altitude=args.altitude, C=C_params)

        if boundary:
            regions = cuav_region.filter_boundary(regions, boundary, pos)

        region_count += len(regions)

        if args.mosaic and len(regions) > 0 and pos:
            composite = cuav_region.CompositeThumbnail(im_full, regions)
            thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
            mosaic.add_regions(regions, thumbs, f, pos)

        if args.view:
            if args.fullres:
                img_view = im_full
            else:
                img_view = img_scan
            #mat = cv.fromarray(img_view)
            for r in regions:
                r.draw_rectangle(img_view, colour=(255,0,0), linewidth=min(max(w/600,1),3), offset=max(w/200,1))
            img_view = cv2.cvtColor(img_view, cv2.COLOR_BGR2RGB)
            viewer.set_image(img_view)

        total_time += (t1-t0)
        if t1 != t0:
            print('%s scan %.1f fps  %u regions [%u/%u]' % (
                f, count/total_time, region_count, scan_count, num_files))
Beispiel #21
0
    def scan_threadfunc(self):
        '''image scanning thread'''
        while not self.unload_event.wait(0.05):
            if self.scan_queue.empty():
                continue
            (frame_time,im) = self.scan_queue.get()
            scan_parms = {}
            for name in self.image_settings.list():
                scan_parms[name] = self.image_settings.get(name)
            scan_parms['BlueEmphasis'] = float(self.camera_settings.blue_emphasis)

            if self.terrain_alt is not None:
                altitude = self.terrain_alt
                if altitude < self.camera_settings.minalt:
                    altitude = self.camera_settings.minalt
                scan_parms['MetersPerPixel'] = cuav_util.pixel_width(altitude,
                                                                     self.c_params.xresolution,
                                                                     self.c_params.lens,
                                                                     self.c_params.sensorwidth)

            t1 = time.time()
            try:
                img_scan = cv2.imread(im, -1)
            except Exception:
                continue
            if img_scan is None:
                continue
            (h, w) = img_scan.shape[:2]
            if self.camera_settings.rotate180:
                M = cv2.getRotationMatrix2D(center, angle180, scale)
                img_scan = cv2.warpAffine(img_scan, M, (w, h))
            im_numpy = numpy.ascontiguousarray(img_scan)
            regions = scanner.scan(im_numpy, scan_parms)
            regions = cuav_region.RegionsConvert(regions,
                                                 cuav_util.image_shape(img_scan),
                                                 cuav_util.image_shape(img_scan))
            t2 = time.time()
            self.scan_fps = 1.0 / (t2-t1)
            self.scan_count += 1

            regions = cuav_region.filter_regions(img_scan, regions,
                                                 min_score=self.camera_settings.minscore,
                                                 filter_type=self.camera_settings.filter_type)
            self.region_count += len(regions)

            # possibly send a preview image
            self.send_preview(img_scan)
            
            if self.camera_settings.roll_stabilised:
                roll=0
            else:
                roll=None
            pos = self.get_plane_position(frame_time, roll=roll)
            if pos is not None:
                self.posmapping[str(frame_time)] = pos

            # this adds the latlon field to the regions (georeferencing)
            for r in regions:
                r.latlon = cuav_util.gps_position_from_image_region(r, pos, w, h, altitude=None, C=self.c_params)

            if self.joelog:
                self.log_joe_position(pos, frame_time, regions)

            # filter out any regions outside the target radius
            if self.camera_settings.target_radius > 0 and pos is not None:
                regions = cuav_region.filter_radius(regions,
                                                    (self.camera_settings.target_latitude,
                                                     self.camera_settings.target_longitude),
                                                    self.camera_settings.target_radius)

            # filter out any regions outside the boundary
            if self.boundary_polygon:
                regions = cuav_region.filter_boundary(regions, self.boundary_polygon, pos)
                
            #filter by minscore
            regions = cuav_region.filter_regions(img_scan, regions, min_score=self.camera_settings.minscore,
                                                 filter_type=self.camera_settings.filter_type)
            high_score = 1
            for r in regions:
                if r.score > high_score:
                    high_score = r.score

            if len(regions) > 0 and pos is not None:
                for r in regions:
                    self.lz.checkaddregion(r, pos)
                try:
                    lzresult = self.lz.calclandingzone()
                except Exception as ex:
                    print("calclandingzone failed: ", ex)
                    continue
                if lzresult:
                    self.transmit_queue.put((lzresult, 100000, None))
                    if self.msend:
                        self.transmit_queue.put((lzresult, 100000, 'msend'))
                    
            if len(regions) > 0 and self.camera_settings.transmit:
                # send a region message with thumbnails to the ground station
                thumb_img = cuav_region.CompositeThumbnail(img_scan, regions,
                                                           thumb_size=self.camera_settings.thumbsize)
                encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
                (result, thumb) = cv2.imencode('.jpg', thumb_img, encode_param)
                pkt = cuav_command.ThumbPacket(frame_time, regions, thumb, pos)

                if self.transmit_queue.qsize() < 100:
                    self.transmit_queue.put((pkt, None, None))
                    if self.msend is not None and high_score >= self.camera_settings.m_minscore:  
                        self.transmit_queue.put((pkt, None, 'msend'))
                else:
                    self.send_message("Warning: image Tx queue too long")
                    print("Warning: image Tx queue too long")
Beispiel #22
0
def process(filename):
    """process one file"""
    pgm = cuav_util.PGM(filename)
    img_full_grey = pgm.array

    im_full = numpy.zeros((960, 1280, 3), dtype="uint8")
    im_640 = numpy.zeros((480, 640, 3), dtype="uint8")

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.debayer(img_full_grey, im_640)
    t1 = time.time()
    print("debayer: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.debayer_full(img_full_grey, im_full)
    t1 = time.time()
    print("debayer_full: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    im_full2 = cv.CreateImage((1280, 960), 8, 3)
    img_full_grey2 = cv.GetImage(cv.fromarray(img_full_grey))
    for i in range(opts.repeat):
        cv.CvtColor(img_full_grey2, im_full2, cv.CV_BayerBG2BGR)
    t1 = time.time()
    print("debayer_cv_full: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        img = cv.GetImage(cv.fromarray(im_full))
        cv.CvtColor(img, img, cv.CV_RGB2HSV)
    t1 = time.time()
    print("RGB2HSV_full: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        img = cv.GetImage(cv.fromarray(im_640))
        cv.CvtColor(img, img, cv.CV_RGB2HSV)
    t1 = time.time()
    print("RGB2HSV_640: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        thumb = numpy.empty((100, 100, 3), dtype="uint8")
        scanner.rect_extract(im_full, thumb, 120, 125)
    t1 = time.time()
    print("rect_extract: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        thumb = cuav_util.SubImage(cv.GetImage(cv.fromarray(im_full)), (120, 125, 100, 100))
    t1 = time.time()
    print("SubImage: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.downsample(im_full, im_640)
    t1 = time.time()
    print("downsample: %.1f fps" % (opts.repeat / (t1 - t0)))

    t0 = time.time()
    for i in range(opts.repeat):
        scanner.scan(im_640)
    t1 = time.time()
    print("scan: %.1f fps" % (opts.repeat / (t1 - t0)))

    if not hasattr(scanner, "jpeg_compress"):
        return

    for quality in [30, 40, 50, 60, 70, 80, 90, 95]:
        t0 = time.time()
        for i in range(opts.repeat):
            jpeg = cPickle.dumps(
                ImagePacket(time.time(), scanner.jpeg_compress(im_full, quality)), protocol=cPickle.HIGHEST_PROTOCOL
            )
        t1 = time.time()
        print("jpeg full quality %u: %.1f fps  %u bytes" % (quality, opts.repeat / (t1 - t0), len(bytes(jpeg))))

    for quality in [30, 40, 50, 60, 70, 80, 90, 95]:
        t0 = time.time()
        for i in range(opts.repeat):
            img2 = cv.fromarray(im_full)
            jpeg = cPickle.dumps(
                ImagePacket(
                    time.time(), cv.EncodeImage(".jpeg", img2, [cv.CV_IMWRITE_JPEG_QUALITY, quality]).tostring()
                ),
                protocol=cPickle.HIGHEST_PROTOCOL,
            )
        t1 = time.time()
        print("EncodeImage full quality %u: %.1f fps  %u bytes" % (quality, opts.repeat / (t1 - t0), len(bytes(jpeg))))

    for quality in [30, 40, 50, 60, 70, 80, 90, 95]:
        t0 = time.time()
        for i in range(opts.repeat):
            jpeg = cPickle.dumps(
                ImagePacket(time.time(), scanner.jpeg_compress(im_640, quality)), protocol=cPickle.HIGHEST_PROTOCOL
            )
        t1 = time.time()
        print("jpeg 640 quality %u: %.1f fps  %u bytes" % (quality, opts.repeat / (t1 - t0), len(bytes(jpeg))))

    for thumb_size in [10, 20, 40, 60, 80, 100]:
        thumb = numpy.zeros((thumb_size, thumb_size, 3), dtype="uint8")
        t0 = time.time()
        for i in range(opts.repeat):
            scanner.rect_extract(im_full, thumb, 0, 0)
            jpeg = cPickle.dumps(
                ImagePacket(time.time(), scanner.jpeg_compress(thumb, 85)), protocol=cPickle.HIGHEST_PROTOCOL
            )
        t1 = time.time()
        print("thumb %u quality 85: %.1f fps  %u bytes" % (thumb_size, opts.repeat / (t1 - t0), len(bytes(jpeg))))
Beispiel #23
0
def process(args):
    """process a set of files"""

    global slipmap, mosaic
    scan_count = 0
    files = []
    for a in args:
        if os.path.isdir(a):
            files.extend(file_list(a, ["jpg", "pgm", "png"]))
        else:
            if a.find("*") != -1:
                files.extend(glob.glob(a))
            else:
                files.append(a)
    files.sort()
    num_files = len(files)
    print("num_files=%u" % num_files)
    region_count = 0

    slipmap = mp_slipmap.MPSlipMap(service=opts.service, elevation=True, title="Map")
    icon = slipmap.icon("redplane.png")
    slipmap.add_object(
        mp_slipmap.SlipIcon("plane", (0, 0), icon, layer=3, rotation=0, follow=True, trail=mp_slipmap.SlipTrail())
    )

    for flag in opts.flag:
        a = flag.split(",")
        lat = a[0]
        lon = a[1]
        icon = "flag.png"
        if len(a) > 2:
            icon = a[2] + ".png"
            icon = slipmap.icon(icon)
            slipmap.add_object(
                mp_slipmap.SlipIcon(
                    "icon - %s" % str(flag), (float(lat), float(lon)), icon, layer=3, rotation=0, follow=False
                )
            )

    if opts.mission:
        from pymavlink import mavwp

        wp = mavwp.MAVWPLoader()
        wp.load(opts.mission)
        plist = wp.polygon_list()
        if len(plist) > 0:
            for i in range(len(plist)):
                slipmap.add_object(
                    mp_slipmap.SlipPolygon(
                        "Mission-%s-%u" % (opts.mission, i),
                        plist[i],
                        layer="Mission",
                        linewidth=2,
                        colour=(255, 255, 255),
                    )
                )

    if opts.mavlog:
        mpos = mav_position.MavInterpolator()
        mpos.set_logfile(opts.mavlog)
    else:
        mpos = None

    if opts.gammalog is not None:
        gamma = parse_gamma_log(opts.gammalog)
    else:
        gamma = None

    if opts.kmzlog:
        kmzpos = mav_position.KmlPosition(opts.kmzlog)
    else:
        kmzpos = None

    if opts.triggerlog:
        triggerpos = mav_position.TriggerPosition(opts.triggerlog)
    else:
        triggerpos = None

    # create a simple lens model using the focal length
    C_params = cam_params.CameraParams(lens=opts.lens, sensorwidth=opts.sensorwidth)

    if opts.camera_params:
        C_params.load(opts.camera_params)

    if opts.target:
        target = opts.target.split(",")
    else:
        target = [0, 0, 0]

    camera_settings = MPSettings(
        [
            MPSetting("roll_stabilised", bool, opts.roll_stabilised, "Roll Stabilised"),
            MPSetting("altitude", int, opts.altitude, "Altitude", range=(0, 10000), increment=1),
            MPSetting("minalt", int, 30, "MinAltitude", range=(0, 10000), increment=1),
            MPSetting("mpp100", float, 0.0977, "MPPat100m", range=(0, 10000), increment=0.001),
            MPSetting("rotate180", bool, opts.rotate_180, "rotate180"),
            MPSetting("filter_type", str, "compactness", "Filter Type", choice=["simple", "compactness"]),
            MPSetting("target_lattitude", float, float(target[0]), "target latitude", increment=1.0e-7),
            MPSetting("target_longitude", float, float(target[1]), "target longitude", increment=1.0e-7),
            MPSetting("target_radius", float, float(target[2]), "target radius", increment=1),
            MPSetting("quality", int, 75, "Compression Quality", range=(1, 100), increment=1),
            MPSetting("thumbsize", int, opts.thumbsize, "Thumbnail Size", range=(10, 200), increment=1),
            MPSetting("minscore", int, opts.minscore, "Min Score", range=(0, 1000), increment=1, tab="Scoring"),
            MPSetting(
                "brightness", float, 1.0, "Display Brightness", range=(0.1, 10), increment=0.1, digits=2, tab="Display"
            ),
        ],
        title="Camera Settings",
    )

    image_settings = MPSettings(
        [
            MPSetting("MinRegionArea", float, 0.05, range=(0, 100), increment=0.05, digits=2, tab="Image Processing"),
            MPSetting("MaxRegionArea", float, 4.0, range=(0, 100), increment=0.1, digits=1),
            MPSetting("MinRegionSize", float, 0.02, range=(0, 100), increment=0.05, digits=2),
            MPSetting("MaxRegionSize", float, 3.0, range=(0, 100), increment=0.1, digits=1),
            MPSetting("MaxRarityPct", float, 0.02, range=(0, 100), increment=0.01, digits=2),
            MPSetting("RegionMergeSize", float, 1.0, range=(0, 100), increment=0.1, digits=1),
            MPSetting("BlueEmphasis", bool, opts.blue_emphasis),
            MPSetting("SaveIntermediate", bool, opts.debug),
        ],
        title="Image Settings",
    )

    mosaic = cuav_mosaic.Mosaic(
        slipmap,
        C=C_params,
        camera_settings=camera_settings,
        image_settings=image_settings,
        start_menu=True,
        classify=opts.categories,
        thumb_size=opts.mosaic_thumbsize,
    )

    joelog = cuav_joe.JoeLog(None)

    if opts.view:
        viewer = mp_image.MPImage(title="Image", can_zoom=True, can_drag=True)

    for f in files:
        if not mosaic.started():
            print("Waiting for startup")
            while not mosaic.started():
                mosaic.check_events()
                time.sleep(0.01)

        if mpos:
            # get the position by interpolating telemetry data from the MAVLink log file
            # this assumes that the filename contains the timestamp
            if gamma is not None:
                frame_time = parse_gamma_time(f, gamma)
            else:
                frame_time = cuav_util.parse_frame_time(f)
            frame_time += opts.time_offset
            if camera_settings.roll_stabilised:
                roll = 0
            else:
                roll = None
            try:
                pos = mpos.position(frame_time, roll=roll)
            except Exception:
                print("No position available for %s" % frame_time)
                # skip this frame
                continue
        elif kmzpos is not None:
            pos = kmzpos.position(f)
        elif triggerpos is not None:
            pos = triggerpos.position(f)
        else:
            # get the position using EXIF data
            pos = mav_position.exif_position(f)
            pos.time += opts.time_offset

        # update the plane icon on the map
        if pos is not None:
            slipmap.set_position("plane", (pos.lat, pos.lon), rotation=pos.yaw)
            if camera_settings.altitude > 0:
                pos.altitude = camera_settings.altitude

        # check for any events from the map
        slipmap.check_events()
        mosaic.check_events()

        im_orig = cuav_util.LoadImage(f, rotate180=camera_settings.rotate180)
        if im_orig is None:
            continue
        (w, h) = cuav_util.image_shape(im_orig)

        if not opts.camera_params:
            C_params.set_resolution(w, h)

        im_full = im_orig

        im_640 = cv.CreateImage((640, 480), 8, 3)
        cv.Resize(im_full, im_640, cv.CV_INTER_NN)
        im_640 = numpy.ascontiguousarray(cv.GetMat(im_640))
        im_full = numpy.ascontiguousarray(cv.GetMat(im_full))

        count = 0
        total_time = 0

        t0 = time.time()
        img_scan = im_full

        scan_parms = {}
        for name in image_settings.list():
            scan_parms[name] = image_settings.get(name)
        scan_parms["SaveIntermediate"] = float(scan_parms["SaveIntermediate"])
        scan_parms["BlueEmphasis"] = float(scan_parms["BlueEmphasis"])

        if pos is not None:
            (sw, sh) = cuav_util.image_shape(img_scan)
            altitude = pos.altitude
            if altitude < camera_settings.minalt:
                altitude = camera_settings.minalt
            scan_parms["MetersPerPixel"] = camera_settings.mpp100 * altitude / 100.0

            regions = scanner.scan(img_scan, scan_parms)
        else:
            regions = scanner.scan(img_scan)
        regions = cuav_region.RegionsConvert(regions, cuav_util.image_shape(img_scan), cuav_util.image_shape(im_full))
        count += 1
        t1 = time.time()

        frame_time = pos.time

        if pos:
            for r in regions:
                r.latlon = cuav_util.gps_position_from_image_region(r, pos, w, h, altitude=altitude)

            if camera_settings.target_radius > 0 and pos is not None:
                regions = cuav_region.filter_radius(
                    regions,
                    (camera_settings.target_lattitude, camera_settings.target_longitude),
                    camera_settings.target_radius,
                )

        regions = cuav_region.filter_regions(
            im_full,
            regions,
            frame_time=frame_time,
            min_score=camera_settings.minscore,
            filter_type=camera_settings.filter_type,
        )

        scan_count += 1

        if pos and len(regions) > 0:
            altitude = camera_settings.altitude
            if altitude <= 0:
                altitude = None
            joelog.add_regions(frame_time, regions, pos, f, width=w, height=h, altitude=altitude)

        mosaic.add_image(pos.time, f, pos)

        region_count += len(regions)

        if len(regions) > 0:
            composite = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions)
            thumbs = cuav_mosaic.ExtractThumbs(composite, len(regions))
            mosaic.add_regions(regions, thumbs, f, pos)

        if opts.view:
            img_view = img_scan
            (wview, hview) = cuav_util.image_shape(img_view)
            mat = cv.fromarray(img_view)
            for r in regions:
                r.draw_rectangle(mat, (255, 0, 0))
            cv.CvtColor(mat, mat, cv.CV_BGR2RGB)
            viewer.set_image(mat)
            viewer.set_title("Image: " + os.path.basename(f))
            if opts.saveview:
                cv.CvtColor(mat, mat, cv.CV_RGB2BGR)
                cv.SaveImage("view-" + os.path.basename(f), mat)

        total_time += t1 - t0
        if t1 != t0:
            print(
                "%s scan %.1f fps  %u regions [%u/%u]"
                % (os.path.basename(f), count / total_time, region_count, scan_count, num_files)
            )
        # raw_input("hit ENTER when ready")

    print("All images processed")
    while True:
        # check for any events from the map
        slipmap.check_events()
        mosaic.check_events()
        time.sleep(0.2)
Beispiel #24
0
def process(filename, repeat):
    '''process one file'''
    colour = cv2.imread(filename)
    colour_half = cv2.resize(colour, (0, 0), fx=0.5, fy=0.5)

    t0 = time.time()
    for i in range(repeat):
        cv2.cvtColor(colour, cv2.COLOR_RGB2HSV)
    t1 = time.time()
    if t1 > t0:
        print('RGB2HSV_full: %.1f fps' % (repeat / (t1 - t0)))
    else:
        print('RGB2HSV_full: (inf) fps')

    t0 = time.time()
    for i in range(repeat):
        cv2.cvtColor(colour_half, cv2.COLOR_RGB2HSV)
    t1 = time.time()
    if t1 > t0:
        print('RGB2HSV_half: %.1f fps' % (repeat / (t1 - t0)))
    else:
        print('RGB2HSV_half: (inf) fps')

    t0 = time.time()
    for i in range(repeat):
        thumb = numpy.empty((100, 100, 3), dtype='uint8')
        scanner.rect_extract(colour, thumb, 120, 125)
    t1 = time.time()
    if t1 > t0:
        print('rect_extract: %.1f fps' % (repeat / (t1 - t0)))
    else:
        print('rect_extract: (inf) fps')

    t0 = time.time()
    for i in range(repeat):
        thumb = cuav_util.SubImage(colour, (120, 125, 100, 100))
    t1 = time.time()
    if t1 > t0:
        print('SubImage: %.1f fps' % (repeat / (t1 - t0)))
    else:
        print('SubImage: (inf) fps')

    #t0 = time.time()
    #for i in range(repeat):
    #    scanner.downsample(im_full, im_640)
    #t1 = time.time()
    #print('downsample: %.1f fps' % (repeat/(t1-t0)))

    t0 = time.time()
    for i in range(repeat):
        scanner.scan(colour_half)
    t1 = time.time()
    if t1 > t0:
        print('scan: %.1f fps' % (repeat / (t1 - t0)))
    else:
        print('scan: (inf) fps')

    t0 = time.time()
    for i in range(repeat):
        scanner.scan(colour)
    t1 = time.time()
    if t1 > t0:
        print('scan_full: %.1f fps' % (repeat / (t1 - t0)))
    else:
        print('scan_full: (inf) fps')