Пример #1
0
        public ViewableTreeNode(Tree node)
        {
            Node = node;
            Children = new BindingList<ViewableTreeNode>();
            foreach (Tree child in node.GetChildren())
            {
                Children.Add(new ViewableTreeNode(child));
            }

            if (node.HasTag("type")){

                Id = "{x=" + node.Left + ", y=" + node.Top + ", width=" + node.Width + ", height=" + node.Height + "}";

                //switch(node["type"].ToString()){

                //    case "ptype":
                //        Id = ((string)node["ptype_id"]);
                //        break;

                //    default:
                //        Id = node["type"].ToString();
                //        break;
                //}
                

            }

            else
                Id = "node";
        }
Пример #2
0
 public void AfterInterpret(Tree tree)
 {
     if (_afterInterpretFunc != null)
     {
         _afterInterpretFunc(tree);
     }
 }
Пример #3
0
		public void FindContent(Bitmap image, Bitmap foregroundImage, Tree currentNode, List<Tree> foundContent)
		{
			Utils.RectData[] found = ConnectedComponents.TwoPass(ccLabelingBitmap, foregroundImage, currentNode, Utils.BACKGROUND);

			foreach (Utils.RectData rect in found)
			{
				if(rect != null){
					int hash = 17;
					for (int row = rect.Top; row <= rect.Bottom; row++)
					{
						for (int col = rect.Left; col <= rect.Right; col++)
						{
							hash = 31 * hash + image[row, col];
						}
					}

					BoundingBox bb = new BoundingBox(rect.Left, rect.Top, (rect.Right - rect.Left) + 1, (rect.Bottom - rect.Top) + 1);

					Dictionary<string,object> tags = new Dictionary<string, object>();
					tags.Add("type", "content");
					tags.Add("pixel_hash", hash);

					Tree node = Tree.FromBoundingBox(bb, tags);

					foundContent.Add(node);
				}
			}
		}
Пример #4
0
		public AnnotatedNode(Tree matchingNode, Tree root, JObject data, IBoundingBox region, string imageId){
			this.MatchingNode = matchingNode;
			this.Root = root;
			this.Data = data;
			this.Region = region;
			this.ImageId = imageId;
		}
Пример #5
0
		public static Tree InterpretChain(IEnumerable<LayerWrapper> layers, Tree start){
            Tree.BatchTransform updater = new Tree.BatchTransform(start);
            Tree tree = start;
            LayerWrapper currLayer = null;
            try
            {
                
                
                foreach (LayerWrapper wrapper in layers)
                {
                    currLayer = wrapper;
                    InterpretArgs args = new InterpretArgs(tree, updater, wrapper.Intent);

                    wrapper.Layer.Interpret(args);
                    tree = updater.GetUpdatedTree();
                    wrapper.Layer.AfterInterpret(tree);
                    
                }


            }
            catch(Exception e)
            {
                Tree.BatchTransform newupdater = new Tree.BatchTransform(tree);
                LayerException exception = new LayerException(currLayer, e);
                newupdater.Tag(tree, "interpretation_exception", exception);
                tree = newupdater.GetUpdatedTree();
            }


            return tree;
		}
Пример #6
0
        private void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            if(currNode.Height > 3 && currNode["type"] != null && currNode["type"].Equals("content"))
            {
                args.Tag(currNode, "is_text", true);
            }

            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
Пример #7
0
		public static Tree FromPixels(Bitmap image, Dictionary<string, object> tags){
			BoundingBox frame = new BoundingBox(0,0, image.Width, image.Height);
			Tree tree = new Tree();
			tree.Occurrence = frame;
			tree._tags = new Dictionary<string, object>(tags);
			tree._children = new List<Tree>();
			tree._tags.Add("capturedpixels", image);
			tree._tags.Add("type", "frame");

			return tree;
		}
Пример #8
0
		/// <summary>
		/// Finds the unpredictable content using the backround regions of elements.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="image"></param>
		/// <param name="background"></param>
		/// <param name="isForeground"></param>
		private static List<Tree> FindAndAddContent(InterpretArgs args, Tree node, Bitmap image, Bitmap isForeground){
			Ptype ptype = (Ptype)node["ptype"];
			List<Tree> allFound = new List<Tree>();
			if(ptype != null){
				ptype.Model.Finder.SetForeground(node, image, isForeground);
			}
			//recurse. if no siblings are overlapping (the majority case) then we can run each child in parallel.
//        if (!anyOverlapping(node.children())){
//            List<Future<ICollection<Tree>>> asyncResults = new List<Future<ICollection<Tree>>>();
//            for(final Tree child : node.children()){
//                Callable<ICollection<Tree>> callable = new Callable<ICollection<Tree>>(){
//                   public ICollection<Tree> call(){
//                        return findAndAddContent(args, ptype, child, image, isForeground);
//                  }
//                };
//                Future<ICollection<Tree>> results =  Utils.service.submit(callable);
//                asyncResults.add(results);
//            }
//
//            for(Future<ICollection<Tree>> res : asyncResults){
//
//                try {
//                    allFound.addAll(res.get());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                } catch (ExecutionException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                }
//            }
//        }
//        else //we can't run children in parallel if any nodes overlap.
//        //nodes only overlap when there's a false positive. so we might be able
//        //to trigger something here and automatically correct that.
//        {
			foreach (Tree child in node.GetChildren())
				FindAndAddContent(args, child, image, isForeground);


			if(ptype != null){
				ptype.Model.Finder.FindContent(node, image, isForeground, allFound);
				foreach(Tree found  in allFound)
					args.SetAncestor(found, node);

                allFound.AddRange(node.GetChildren());
                PrototypeDetectionLayer.SetAncestorsByContainment(allFound, args);
			}



			// }

			return allFound;
		}
Пример #9
0
		public static Tree FromBoundingBox(IBoundingBox bb, Dictionary<string,object> tags){
			Tree tree = new Tree();
			tree.Occurrence = bb;

			if(tags != null)
				tree._tags = new Dictionary<string, object>(tags);
			else
				tree._tags = new Dictionary<string, object>();

			tree._children = new List<Tree>();

			return tree;
		}
Пример #10
0
        public static int CompareByAreaAndFeaturesFirst(Tree a, Tree b)
        {
            int val =  CompareByArea(a, b);
            if (val == 0)
            {
                if (a.HasTag("type") && a["type"].Equals("feature") && 
                    (!b.HasTag("type") || !b["type"].Equals("feature")))
                    return -1;
                else if (b.HasTag("type") && b["type"].Equals("feature"))
                    return 1;
            }

            return val;
        }
Пример #11
0
		public static Utils.RectData[] TwoPass(Bitmap labels, Bitmap srcBitmap, Tree root, int background)
		{
			DisjointSets linked = new DisjointSets(root.Width * root.Height);
			int bottom = root.Top + root.Height;
			int right = root.Left + root.Width;

			int numelements = IdentifyComponents(labels, srcBitmap, linked, root, root.GetHashCode(), bottom, right, background);

			//Run the second pass through the image to label each component
			//and create bounding rectangles
			LabelComponentsAndSetBoundingBoxes(labels, linked, root.Top, root.Left, bottom, right, background);


			return GetRectsFromLabeledImage(labels, srcBitmap, numelements, root.Top, root.Left, bottom, right, background);
		}
Пример #12
0
		private Tree(Tree toCopy)
		{
			_tags = new Dictionary<string, object>();
			foreach (var item in toCopy.GetTags())
				_tags.Add (item.Key, item.Value);

			Occurrence = toCopy.Occurrence;

			List<Tree> children = new List<Tree>();
			foreach (Tree child in toCopy.GetChildren())
			{
				children.Add(new Tree(child));
			}

			_children = children;

		}
Пример #13
0
        public void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            IEnumerable<Tree> textChildren = currNode.GetChildren().Where(c => c.ContainsTag("is_text") && (bool)c["is_text"]);

            List<Tree> readingOrder = SortNodesInReadingOrder(textChildren);

            for (int i = 0; i < readingOrder.Count - 1; i++)
            {
                Tree curr = readingOrder[i];
                Tree next = readingOrder[i + 1];

                args.Tag(curr, "group_next", GroupNext(curr, next));
            }

            //recurse
            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
Пример #14
0
		private static int IdentifyComponents(Bitmap labels, Bitmap srcBitmap, DisjointSets linked, Tree toProcess, int treeNodeIndex, int bottom, int right, int background)
		{
			int numElements = 0;

			//Run the first pass through the image to find any foreground content
			int[] neighbors = new int[8];
			for (int row = toProcess.Top; row < bottom; row++)
			{
				for (int col = toProcess.Left; col < right; col++)
				{
					int sourcepixel = srcBitmap[row, col];

					if (sourcepixel == treeNodeIndex)
					{
						int neighborCount = NeighborsConnected8(neighbors, toProcess, row, col, labels, background);
						if (neighborCount == 0)
						{

							labels[row, col] = numElements+1;
							numElements++;

						}
						else
						{
							int min = Min(neighbors, neighborCount);
							labels[row, col] =  min;
							for(int i = 0; i < neighborCount; i++)
							{
								linked.union(min, neighbors[i]);

							}
						}

					}
					else
						labels[row, col] = background;
				}
			}

			return numElements;

		}
Пример #15
0
        public static Tree GetParent(Tree node, Tree root)
        {
            if (root == null || node == null)
                return null;

            if (root == node)
                return null;

            if (root.GetChildren().Contains(node))
                return root;

            foreach (Tree child in root.GetChildren())
            {
                Tree found = GetParent(node, child);
                if (found != null)
                    return found;
            }

            return null;
        }
Пример #16
0
        public Dictionary<string, List<IAnnotation>> GetAnnotationsMatchingNode(Tree node, Tree root, string bitmapid)
        {
            var anns =  AnnotationLibrary.GetAllAnnotationsForImageUsingAllLayers(_layers, bitmapid, node);

               var annotationsByLib = new Dictionary<string, List<IAnnotation>>();

               foreach (var pair in anns)
               {
               List<IAnnotation> wrappers = new List<IAnnotation>();
               foreach (var annotation in pair.Value)
               {
                       wrappers.Add(new ImageAnnotationWrapper(annotation));
               }

               annotationsByLib[pair.Key] = wrappers;

               }

               return annotationsByLib;
        }
Пример #17
0
        private void InterpretHelper(InterpretArgs args, Tree node)
        {
            if (node.ContainsTag("type") && node["type"].Equals("ptype"))
            {
                args.Tag(node, "is_target", "true");
            }
            else if (node.ContainsTag("type") && !node["type"].Equals("ptype"))
            {
                foreach (Tree child in node.GetChildren())
                {
                    if (child.ContainsTag("type") && !child["type"].Equals("feature") && child.GetChildren().Count() == 0)
                    {
                        args.Tag(child, "is_target", "true");
                    }
                }
            }

            foreach (Tree child in node.GetChildren())
                InterpretHelper(args, child);
        }
Пример #18
0
        public void WriteBackgroundAndRender(Tree tree)
        {
            VideoInterpreter.VideoCapture capture = tree["videocapture"] as VideoInterpreter.VideoCapture;
            IBoundingBox invalidatedpixels = tree["invalidated"] as IBoundingBox;

            //Todo: this is not working properly - returning null when there is a change...
            List<Tree> contentRegionBoxes = GetContentRegionBoxes(tree);
            if (invalidatedpixels != null)
            {
                _backgroundBitmap.Width = tree.Width;
                _backgroundBitmap.Height = tree.Height;
                
                WriteBackgroundPixels(_backgroundBitmap,contentRegionBoxes, tree);
                Dispatcher.BeginInvoke(_update, capture, tree, contentRegionBoxes, tree);
            }
            else
            {
                WriteBackgroundPixels(_backgroundBitmap, contentRegionBoxes, tree);
                Dispatcher.BeginInvoke(_update, capture, tree, contentRegionBoxes, null);
            }
        }
Пример #19
0
        public Dictionary<string, List<IAnnotation>> GetAnnotationsMatchingNode(Tree node, Tree root, string bitmapid)
        {
            Dictionary<string, List<IAnnotation>> toreturn = new Dictionary<string, List<IAnnotation>>();

            Dictionary<string, JToken> all = Storage.ReadAllData();

            string nodepath = PathDescriptor.GetPath(node, root);
            List<IAnnotation> matches = new List<IAnnotation>();

            foreach (string key in all.Keys)
            {
                if (key.Equals(nodepath))
                {
                    matches.Add(new PathDescriptorAnnotation(key, all[key]));
                }
            }

            toreturn.Add("prefab_single", matches);

            return toreturn;
        }
Пример #20
0
        private static Tree GetClosestHelper(int left, int top, Tree node, out double closestDistance)
        {
            double currBestDist = double.MaxValue;
            Tree closestTarget = null;
            if(node.HasTag("is_target") && node["is_target"].ToString().ToLower().Equals("true") 
                && (!node.HasTag("is_minor") || node["is_minor"].ToString().ToLower().Equals("false") || Keyboard.IsKeyDown(Key.LeftCtrl) ) )
            {
                currBestDist = DistanceBetweenPointAndRectangle(left, top, node);
                closestTarget = node;
            }

            

            double bestChildDist = double.MaxValue;
            Tree closestChild = null;

            foreach (Tree child in node.GetChildren())
            {
                double childDist = double.MaxValue;
                Tree childCandidate = GetClosestHelper(left, top, child, out childDist);
                if (childCandidate != null && childDist < bestChildDist)
                {
                    bestChildDist = childDist;
                    closestChild = childCandidate;
                }
            }

            if (bestChildDist < currBestDist)
            {
                closestTarget = closestChild;
                currBestDist = bestChildDist;
            }

            closestDistance = currBestDist;

            return closestTarget;
        }
Пример #21
0
 public void AfterInterpret(Tree tree)
 {
 }
Пример #22
0
        //private bool IsToTheRightAndInLine(Tree possibleLeft, Tree possibleRight)
        //{
        //    bool isInLine = BoundingBox.IsAlignedHorizontally(possibleLeft, possibleRight);
        //    return isInLine && possibleLeft.Left + possibleLeft.Width < possibleRight.Left;
        //}
        private bool GroupNext(Tree curr, Tree next)
        {
            int leftDist;
            int topDist;

            if (BoundingBox.IsAlignedVertically(curr, next))
                leftDist = 0;
            else if (curr.Left < next.Left)
                leftDist = next.Left - (curr.Left + curr.Width);
            else
                leftDist = curr.Left - (next.Left + next.Width);

            if (BoundingBox.IsAlignedHorizontally(curr, next))
                topDist = 0;
            else if (curr.Top < next.Top)
            {
                topDist = next.Top - (curr.Top + curr.Height);
            }
            else
            {
                topDist = curr.Top - (next.Top + next.Height);
            }

            bool closeXDist = leftDist < MAX_SPACE_DIST;
            bool closeYDist = topDist < MAX_ROW_DIST;

            //int yDist = next.Top - curr.Top;
            //bool horizontallyAligned = BoundingBox.IsAlignedHorizontally(curr, next);
            return closeXDist && closeYDist;
        }
Пример #23
0
 public void Remove(Tree node)
 {
     _updater.Remove(node);
 }
Пример #24
0
		public void SetAncestor(Tree node, Tree ancestor)
        {
			_updater.SetAncestor (node, ancestor);
		}
Пример #25
0
 public string get_path(Tree node)
 {
     return PathDescriptor.GetPath(node, tree);
 }
Пример #26
0
 public void enqueue_set_tag(Tree node, string key, object value)
 {            
     args.Tag(node, key, value);
 }
Пример #27
0
 public void enqueue_set_ancestor(Tree node, Tree ancestor)
 {
     args.SetAncestor(node, ancestor);
 }
Пример #28
0
		public InterpretArgs (Tree tree, Tree.BatchTransform updater, IRuntimeStorage runtimeStorage)
		{
			this._updater = updater;
			Tree = tree;
            RuntimeStorage = runtimeStorage;
		}
Пример #29
0
 public Tree get_parent(Tree node)
 {
     return Tree.GetParent(node, tree);
 }
Пример #30
0
		public void Tag(Tree node, string key, object value)
        {
			_updater.Tag (node, key, value);
		}