class Test: def __init__(self): self.ImageUtils = ImageUtils() def SetupAll(self, BasePath): """ Inputs: BasePath - Path to images Outputs: ImageSize - Size of the Image DataPath - Paths of all images where testing will be run on """ # Image Input Shape ImageSize = [32, 32, 3] DataPath = [] NumImages = len(glob.glob(BasePath + '*.jpg')) SkipFactor = 1 for count in range(1, NumImages + 1, SkipFactor): DataPath.append(BasePath + str(count) + '.jpg') return ImageSize, DataPath def ReadImages(self, ImageSize, DataPath): """ Inputs: ImageSize - Size of the Image DataPath - Paths of all images where testing will be run on Outputs: I1Combined - I1 image after any standardization and/or cropping/resizing to ImageSize I1 - Original I1 image for visualization purposes only """ ImageName = DataPath I1 = cv2.imread(ImageName) if (I1 is None): # OpenCV returns empty list if image is not read! print('ERROR: Image I1 cannot be read') sys.exit() ########################################################################## # Add any standardization or cropping/resizing if used in Training here! ########################################################################## Im = self.ImageUtils.PreProcess(I1, 640, 480) I1S, H4PtTruth1 = self.ImageUtils.CreateTrainingData(Im, 256, 256, 64) I2S, H4PtTruth2 = self.ImageUtils.CreateTrainingData(Im, 256, 256, 64) I1Combined = np.expand_dims(I1S, axis=0) return I1Combined, I1 def TestOperation(self, ImgPH, ImageSize, ModelPath, DataPath, LabelsPathPred): """ Inputs: ImgPH is the Input Image placeholder ImageSize is the size of the image ModelPath - Path to load trained model from DataPath - Paths of all images where testing will be run on LabelsPathPred - Path to save predictions Outputs: Predictions written to ./TxtFiles/PredOut.txt """ Length = ImageSize[0] # Predict output with forward pass, MiniBatchSize for Test is 1 _, prSoftMaxS = HomographyModel(ImgPH, ImageSize, 1) # Setup Saver Saver = tf.train.Saver() with tf.Session() as sess: Saver.restore(sess, ModelPath) print('Number of parameters in this model are %d ' % np.sum([ np.prod(v.get_shape().as_list()) for v in tf.trainable_variables() ])) OutSaveT = open(LabelsPathPred, 'w') for count in tqdm(range(np.size(DataPath))): DataPathNow = DataPath[count] Img, ImgOrg = self.ReadImages(ImageSize, DataPathNow) FeedDict = {ImgPH: Img} PredT = np.argmax(sess.run(prSoftMaxS, FeedDict)) OutSaveT.write(str(PredT) + '\n') OutSaveT.close() def ReadLabels(self, LabelsPathTest, LabelsPathPred): if (not (os.path.isfile(LabelsPathTest))): print('ERROR: Test Labels do not exist in ' + LabelsPathTest) sys.exit() else: LabelTest = open(LabelsPathTest, 'r') LabelTest = LabelTest.read() LabelTest = map(float, LabelTest.split()) if (not (os.path.isfile(LabelsPathPred))): print('ERROR: Pred Labels do not exist in ' + LabelsPathPred) sys.exit() else: LabelPred = open(LabelsPathPred, 'r') LabelPred = LabelPred.read() LabelPred = map(float, LabelPred.split()) return LabelTest, LabelPred
class Stitcher: """ Read a set of images for Panorama stitching """ def __init__(self, BasePath, ModelPath, NumFeatures): self.BasePath = BasePath self.ModelPath = ModelPath InputImageList = [] for filename in sorted(glob.glob(self.BasePath + '/*.jpg')): ImageTemp = cv2.imread(filename) InputImageList.append(ImageTemp) self.NumFeatures = NumFeatures self.Images = np.array(InputImageList) self.NumImages = len(InputImageList) self.HelperFunctions = HelperFunctions() self.Model = DeepNetwork() self.ImageUtils = ImageUtils() self.ImageSize = InputImageList[0].shape self.ImgPH = tf.placeholder('float', shape=(1, 128, 128, 2)) """ Obtain Homography using Deep Learning Model (Supervised and Unsupervised) """ def ExtractHomographyFromH4Pt(self, H4PtPred): pts1 = np.float32([[0, 0], [self.ImageSize[1], 0], [self.ImageSize[1], self.ImageSize[0]], [0, self.ImageSize[0]]]) pts2 = np.float32( [[0 + H4PtPred[0][0], 0 + H4PtPred[0][4]], [self.ImageSize[1] + H4PtPred[0][1], 0 + H4PtPred[0][5]], [ self.ImageSize[1] + H4PtPred[0][2], self.ImageSize[0] + H4PtPred[0][6] ], [0 + H4PtPred[0][3], self.ImageSize[0] + H4PtPred[0][7]]]) HPred = cv2.getPerspectiveTransform(pts1, pts2) return HPred def EstimateHomographySupervised(self, Image1, Image2): # Setup Saver H4PtPred = self.Model.HomographyNet(self.ImgPH, False) Saver = tf.train.Saver() with tf.Session() as sess: Saver.restore(sess, self.ModelPath) print('Number of parameters in this model are %d ' % np.sum([ np.prod(v.get_shape().as_list()) for v in tf.trainable_variables() ])) Image1 = np.float32(self.ImageUtils.PreProcess(Image1, 128, 128)) Image1 = self.ImageUtils.ImageStandardization(Image1) Image2 = np.float32(self.ImageUtils.PreProcess(Image2, 128, 128)) Image2 = self.ImageUtils.ImageStandardization(Image2) Images = np.dstack((Image1, Image2)) I1Batch = [] I1Batch.append(Images) FeedDict = {self.ImgPH: I1Batch} H4Pt = sess.run(H4PtPred, FeedDict) print("H4pt: {}".format(H4Pt)) H = self.ExtractHomographyFromH4Pt(H4Pt) Hinv = np.linalg.inv(H) return H, Hinv """ Image Warping + Blending Save Panorama output as mypano.png """ def RemoveBlackBoundary(self, ImageIn): gray = cv2.cvtColor(ImageIn, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY) _, contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnt = contours[0] x, y, w, h = cv2.boundingRect(cnt) ImageOut = ImageIn[y:y + h, x:x + w] return ImageOut def Warping(self, Img, Homography, NextShape): nH, nW, _ = Img.shape Borders = np.array([[0, nW, nW, 0], [0, 0, nH, nH], [1, 1, 1, 1]]) BordersNew = np.dot(Homography, Borders) Ymin = min(BordersNew[1] / BordersNew[2]) Xmin = min(BordersNew[0] / BordersNew[2]) Ymax = max(BordersNew[1] / BordersNew[2]) Xmax = max(BordersNew[0] / BordersNew[2]) if Ymin < 0: MatChange = np.array([[1, 0, -1 * Xmin], [0, 1, -1 * Ymin], [0, 0, 1]]) Hnew = np.dot(MatChange, Homography) h = int(round(Ymax - Ymin)) + NextShape[0] else: MatChange = np.array([[1, 0, -1 * Xmin], [0, 1, Ymin], [0, 0, 1]]) Hnew = np.dot(MatChange, Homography) h = int(round(Ymax + Ymin)) + NextShape[0] w = int(round(Xmax - Xmin)) + NextShape[1] sz = (w, h) PanoHolder = cv2.warpPerspective(Img, Hnew, dsize=sz) return PanoHolder, int(Xmin), int(Ymin) def Blender(self): Pano = self.Images[0] for NextImage in self.Images[1:2]: H, Hinv = self.EstimateHomographySupervised(Pano, NextImage) PanoHolder, oX, oY = self.Warping(Pano, H, NextImage.shape) self.HelperFunctions.ShowImage(PanoHolder, 'PanoHolder') oX = abs(oX) oY = abs(oY) for IdY in range(oY, NextImage.shape[0] + oY): for IdX in range(oX, NextImage.shape[1] + oX): y = IdY - oY x = IdX - oX PanoHolder[IdY, IdX, :] = NextImage[y, x, :] # Pano = self.RemoveBlackBoundary(PanoHolder) PanoResize = cv2.resize(Pano, (1280, 1024)) self.HelperFunctions.ShowImage(PanoResize, 'PanoResize') PanoResize = cv2.GaussianBlur(PanoResize, (5, 5), 1.2) return PanoResize