Exemple #1
0
def process_pdf(file, crowdRoot, imBlank):
  '''process a single pdf file'''
  imOrigAll = pdf2image.convert_from_path(join(file), dpi=300, thread_count=6)
  for page, imOrig in enumerate(imOrigAll): # loop over all pages in the pdf

    # register image
    imOrigNp = np.array(imOrig.resize(imBlank.shape[1::-1]))
    imReg, h = register_image(imOrigNp, imBlank, threshdist=300)
    if type(imReg) is not np.ndarray:
      warnings.warn('Image registration failed for '+basename(file)+' page '+str(page))
      assert type(imReg) is np.ndarray
      continue

    # decode QR
    seed = get_seed_from_qr(imReg)
    if seed==None:
      warnings.warn('Decode QR seed failed for '+basename(file)+' page '+str(page))
      assert seed!=None
      continue
    labels = pickle.load(open(join(crowdRoot, 'generated', 'label-'+str(seed) + '.pkl'), 'rb')) # obtain the labels given the page seed

    # extract and save crops (along with their true labels as the filename) from that page
    saveDir = join(crowdRoot, 'extracted', str(seed))
    os.makedirs(saveDir, exist_ok=True)
    extract_and_save_crops(imReg, labels, saveDir)
def main():

    parser = argparse.ArgumentParser()
    #parser.add_argument('command', choices=['sync',  'sync-parallel', 'show-modified'])

    #parser.add_argument('--test-file',    dest='testFile', default='TODO',
    #                                      help='Read in test information from this file')
    parser.add_argument('--test-prefix',  dest='testPrefix', default='result',
                                          help='Name prepended to debug files')
    parser.add_argument('--refetch-refs', dest='reloadRefs', action='store_true', default=False,
                                          help='Force refetching of the reference images')
    parser.add_argument('--use-existing', dest='useExisting', action='store_true', default=False,
                                          help='Just print results for the last computed transforms')
    
    parser.add_argument('--sequence', dest='testSequence', action='store_true', default=False,
                                          help='Run the sequence test.')
    

    options = parser.parse_args()
    
    # A seperate test for processing a sequence of images
    if options.testSequence:
        print '----- Running sequence test -----'
        
        seqSeed = TestInstance('/home/smcmich1/data/geocam_images/sequence/ISS001-400-4.JPG, ' + 
                               '31.0, 30.0, 33.1, 31.1, 350, 2001.02.26' )
        
        otherImagePaths = ['/home/smcmich1/data/geocam_images/sequence/ISS001-400-5.JPG',
                           '/home/smcmich1/data/geocam_images/sequence/ISS001-400-8.JPG',
                           '/home/smcmich1/data/geocam_images/sequence/ISS001-400-10.JPG',
                           '/home/smcmich1/data/geocam_images/sequence/ISS001-400-14.JPG',
                           '/home/smcmich1/data/geocam_images/sequence/ISS001-400-15.JPG']
        
        # Run the initial seed
        print 'Processing initial seed...'
        (tform, confidence) = register_image.register_image(seqSeed.imagePath,
                                                                seqSeed.imageCenterLoc[0],
                                                                seqSeed.imageCenterLoc[1],
                                                                seqSeed.focalLength,
                                                                seqSeed.date)
        if not (confidence == register_image.CONFIDENCE_HIGH):
            raise Exception('Cannot run sequence test if first image fails!')
        
        # Run all the other images
        for path in otherImagePaths:
            print 'Testing image: ' + path
            force = not options.useExisting
            (tform, confidence) = register_image.register_image(path,
                                                                    seqSeed.imageCenterLoc[0],
                                                                    seqSeed.imageCenterLoc[1],
                                                                    seqSeed.focalLength,
                                                                    seqSeed.date,
                                                                    seqSeed.imagePath, tform)
            print 'Got confidence: ' + str(confidence)

        print '----- Finished sequence test -----'
        return 0
    

    print '===================== Started running tests ====================='

    testInfo = readTestInfo()
    confidenceCounts = [0, 0, 0]
    results = []
    geoTransform = None
    for i in testInfo:
        try:
            (score, confidence, geoTransform) = runTest(i, options)
        except Exception, e:
            score      = 0
            confidence = register_image.CONFIDENCE_NONE
            print 'Failed to process image ' + i.imagePath
            print(traceback.format_exc())
            
        results.append(score)
        
        confidenceCounts[confidence] += 1
        print i.imagePath + ' ---> ' + str(score) + ' == ' + register_image.CONFIDENCE_STRINGS[confidence]
        
        print "transform"
        print geoTransform
Exemple #3
0
        join(
            raw_images_dir, 'Suitability Form - Nationwide - BLANK-' +
            str(pagenumber) + '.png'))[0]
    print("Reading reference image : ", refFilename)
    imRef = cv2.imread(refFilename, cv2.IMREAD_COLOR)

    # get path of all the image files that we are gonna register
    allFiles = glob(join(raw_images_dir, '*-' + str(pagenumber) + '.png'))
    for imFilename in allFiles:

        # Read image to be aligned
        print("Reading image to align : ", imFilename)
        im = cv2.imread(imFilename, cv2.IMREAD_COLOR)

        # align the image
        imReg, h = register_image(im, imRef)
        imReg = imReg[:im.shape[0], :im.shape[1], :]

        # add space for annotation on top of image ("original" or "aligned")
        blank = 255 * np.ones_like(imReg)[:160, :, :]
        imReg = np.append(blank, imReg, axis=0)
        im = np.append(blank, im, axis=0)

        # add the actual annotation
        textorg = (int(np.floor(imReg.shape[1] / 3)), 120)
        font = cv2.FONT_HERSHEY_TRIPLEX
        cv2.putText(im,
                    'Original',
                    textorg,
                    font,
                    fontScale=3,
def matchLocally(mission, roll, frame, cursor, georefDb, sourceImagePath):
    '''Performs image alignment to an already aligned ISS image'''

    # Load new frame info
    targetFrameData = source_database.FrameInfo()
    targetFrameData.loadFromDb(cursor, mission, roll, frame)
    targetFrameData = computeFrameInfoMetersPerPixel(targetFrameData)

    # Find candidate names to match to
    possibleNearbyMatches = findNearbyResults(targetFrameData, cursor, georefDb)
        
    if not possibleNearbyMatches:
        print 'Did not find any potential local matches!'
    
    for (otherFrame, ourResult) in possibleNearbyMatches:

        print 'Trying local match with frame: ' + str(otherFrame.frame)
        
        # Get path to other frame image
        otherImagePath, exifSourcePath = source_database.getSourceImage(otherFrame)
        source_database.clearExif(exifSourcePath)
        otherTransform = ourResult[0] # This is still in the google projected format
        
        #print 'otherTransform = ' + str(otherTransform.matrix)
        
        print 'New image mpp = ' + str(targetFrameData.metersPerPixel)
        print 'Local match image mpp = ' + str(otherFrame.metersPerPixel)
        # If we could not estimate the MPP value of the new image, guess that it is the same as
        #  the local reference image we are about to try.
        thisMpp = targetFrameData.metersPerPixel
        if not thisMpp:
            thisMpp = otherFrame.metersPerPixel
        
        print 'Attempting to register image...'
        (imageToProjectedTransform, imageToGdcTransform, confidence, imageInliers, gdcInliers, refMetersPerPixel) = \
            register_image.register_image(sourceImagePath,
                                          otherFrame.centerLon, otherFrame.centerLat,
                                          thisMpp, targetFrameData.date,
                                          refImagePath         =otherImagePath,
                                          referenceGeoTransform=otherTransform,
                                          refMetersPerPixelIn  =otherFrame.metersPerPixel,
                                          debug=options.debug, force=True, slowMethod=False)       
        if not options.debug:
            os.remove(otherImagePath) # Clean up the image we matched against

        # Quit once we get a good match
        if confidence == registration_common.CONFIDENCE_HIGH:
            print 'High confidence match!'
            # Convert from the image-to-image GCPs to the reference image GCPs
            #  located in the new image.
            refFrameGdcInliers = ourResult[3] # TODO: Clean this up!
            (width, height)    = IrgGeoFunctions.getImageSize(sourceImagePath)
            
            print '\n\n'
            print refFrameGdcInliers
            print '\n\n'
            
            (imageInliers, gdcInliers) = registration_common.convertGcps(refFrameGdcInliers,
                                                imageToProjectedTransform, width, height)
            
            print imageInliers
            print '\n\n'
            
            # If none of the original GCPs fall in the new image, don't use this alignment result.
            # - We could use this result, but we don't in order to maintain accuracy standards.
            if imageInliers:
                print 'Have inliers'
                print otherFrame
                return (imageToProjectedTransform, imageToGdcTransform, confidence,
                        imageInliers, gdcInliers, refMetersPerPixel, otherFrame)
            else:
                print 'Inliers out of bounds!'

    # Match failure, return junk values
    return (registration_common.getIdentityTransform(), registration_common.getIdentityTransform(),
            registration_common.CONFIDENCE_NONE, [], [], 9999, None)
def processFrame(options, frameDbData, searchNearby=False):
    '''Process a single specified frame.
       Returns True if we attempted to perform image alignment and did not hit an exception.'''
    try:
        georefDb = georefDbWrapper.DatabaseLogger()
        # Increase the error slightly for chained image transforms
        LOCAL_TRANSFORM_ERROR_ADJUST = 1.10
        sourceImagePath, exifSourcePath = source_database.getSourceImage(frameDbData, overwrite=True)
        if not options.debug:
            source_database.clearExif(exifSourcePath)
        try:
            # If requested, get nearby previously matched frames to compare to.
            if searchNearby:
                sourceDb = sqlite3.connect(settings.DB_PATH)
                sourceDbCursor = sourceDb.cursor()
                
                (imageToProjectedTransform, imageToGdcTransform, confidence, imageInliers, gdcInliers, refMetersPerPixel, otherFrame) = \
                    matchLocally(frameDbData.mission, frameDbData.roll, frameDbData.frame, sourceDbCursor, georefDb, sourceImagePath)
                if otherFrame:
                    matchedImageId = otherFrame.getIdString()
                else:
                    matchedImageId = 'None'
                sourceDb.close()
            else: # Try to register the image to Landsat
                print 'Attempting to register image...'
                (imageToProjectedTransform, imageToGdcTransform, confidence, imageInliers, gdcInliers, refMetersPerPixel) = \
                    register_image.register_image(sourceImagePath,
                                                  frameDbData.centerLon, frameDbData.centerLat,
                                                  frameDbData.metersPerPixel, frameDbData.date,
                                                  refImagePath=None,
                                                  debug=False, force=True, slowMethod=True)
                matchedImageId = 'Landsat'
        except Exception as e:
            print 'Computing transform for frame '+frameDbData.getIdString()+', caught exception: ' + str(e)
            print "".join(traceback.format_exception(*sys.exc_info()))
            print 'Logging the result as no-confidence.'
            confidence   = registration_common.CONFIDENCE_NONE
            imageInliers = []
            gdcInliers   = []
            matchedImageId    = 'NA'
            refMetersPerPixel = 999
            imageToProjectedTransform = registration_common.getIdentityTransform()
            imageToGdcTransform       = registration_common.getIdentityTransform()

        # A very rough estimation of localization error at the inlier locations!
        errorMeters = refMetersPerPixel * 1.5
        # Convert into format that our DB is looking for.
        sourceDateTime = frameDbData.getMySqlDateTime()
        if confidence > registration_common.CONFIDENCE_NONE:
            (centerLon, centerLat) = computeCenterGdcCoord(imageToGdcTransform, frameDbData)
        else:
            (centerLon, centerLat) = (-999, -999)
        # Log the results to our database
        centerPointSource = frameDbData.centerPointSource 
        georefDb.addResult(frameDbData.mission, frameDbData.roll, frameDbData.frame,
                           imageToProjectedTransform, imageToGdcTransform,
                           centerLon, centerLat, refMetersPerPixel,
                           confidence, imageInliers, gdcInliers,
                           matchedImageId, sourceDateTime, centerPointSource)
        # This tool just finds the interest points and computes the transform,
        # a different tool will actually write the output images.
        if not options.debug:
            os.remove(sourceImagePath) # Clean up the source image
        print ('Finished processing frame ' + frameDbData.getIdString()
               + ' with confidence ' + registration_common.CONFIDENCE_STRINGS[confidence])
        return confidence
    
    except Exception as e:
        print 'Processing frame '+frameDbData.getIdString()+', caught exception: ' + str(e)
        print "".join(traceback.format_exception(*sys.exc_info()))
        #raise Exception('FAIL')
        return 0