Ejemplo n.º 1
0
def main():
    # Use the input argument as path to file and ingest the file creating an all_rectangles object with the list of dictionaries
    try:  # Check if there is an input argument, otherwise inform.
        path_to_file = sys.argv[1]
        print(path_to_file)
        try:  # Depending on the error (if any), it will be infromed.

            # Initilize the ingestions
            all_rectangles = Ingest(path_to_file)

            # Loop over the list of all_rectangles and return them in a list of objects
            rects_list_obj = Rectangle.object_creator(all_rectangles._rects)

            # Produce the first set of unions
            unions = Rectangle.get_first_union(rects_list_obj)
            temp_union = unions

            # Loop that can handle as many unions there is
            while temp_union:
                union = Rectangle.get_union(rects_list_obj, temp_union)
                temp_union = union
                unions = unions + union

            # Serve object handles the prints
            serve_object = PrintRectangle(rects_list_obj, unions)
            serve_object.print_input()
            serve_object.print_output()
        except:
            print(all_rectangles.error)

    except:
        print("Please specify the path to the input json file")
 def test_union(self):
     a = Rectangle(1, 3, 3, 7)
     b = Rectangle(2, 1, 3, 6)
     self.assertEqual(a.union(b), Rectangle(1, 1, 4, 9), 'overlapping')
     self.assertEqual(b.union(a), Rectangle(1, 1, 4, 9),
                      'overlapping reverse')
     self.assertEqual(a.union(a), a, 'equal')
 def test_TPP(self):
     a = Rectangle(2, 2, 1, 1)
     b = Rectangle(1, 1, 2, 4)
     r = SpatialRelation(a, b)
     self.assertFalse(r.EQ(), "EQ")
     self.assertFalse(r.DC(), "DC")
     self.assertFalse(r.EC(), "EC")
     self.assertTrue(r.TPP(), "TPP")
     self.assertFalse(r.TPPi(), "TPPi")
     self.assertFalse(r.NTPP(), "NTPP")
     self.assertFalse(r.NTPPi(), "NTPPi")
     self.assertFalse(r.PO(), "PO")
Ejemplo n.º 4
0
    def findLabelsWithinBounds(self,
                               top,
                               bottom,
                               contour_approx_strength=0.05):
        """
        Finds labels that are within the specified bounds
        """
        min_contour_area = self.M * self.N / 100
        max_contour_area = self.M * self.N / 6
        _, contours, hierarchy = cv.findContours(
            self.img_eroded[top:bottom, :], cv.RETR_TREE,
            cv.CHAIN_APPROX_SIMPLE)
        largest_contours = list(
            filter(
                lambda c: cv.contourArea(c) >= min_contour_area and cv.
                contourArea(c) <= max_contour_area, contours))

        approximated_contours = []
        for contour in largest_contours:
            epsilon = contour_approx_strength * cv.arcLength(contour, True)
            approx = cv.approxPolyDP(contour, epsilon, True)
            approximated_contours.append(approx)

        for contour in approximated_contours:
            x, y, w, h = cv.boundingRect(contour)
            y += top
            self.label_rectangles.append(Rectangle(x, y, w, h))
Ejemplo n.º 5
0
    def get_all_bounding_boxes(self, image):
        # grab the frame dimensions and convert it to a blob
        (h, w) = image.shape[:2]

        start=time.time()
        blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
            (300, 300), (104.0, 177.0, 123.0))
        print('>>> cv2.dnn.blobFromImage took %.3f seconds' % (time.time() - start))

        # pass the blob through the network and obtain the detections and predictions
        start=time.time()
        self.net.setInput(blob)
        print('>>> net.setInput(blob) took %.3f seconds' % (time.time() - start))
        start=time.time()
        detections = self.net.forward()
        print('>>> dnn face_detector net.forward took %.3f seconds' % (time.time() - start))

        start=time.time()
        bounding_boxes=[]
        for i in range(0, detections.shape[2]):
            # extract the confidence (i.e., probability) associated with the prediction
            confidence = detections[0, 0, i, 2]

            # filter out weak detections by ensuring the `confidence` is greater than the minimum confidence
            if confidence < self.confidence_threshold:
                continue

            # compute the (x, y)-coordinates of the bounding box for the object
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
            bounding_boxes.append(Rectangle(x1*scale, y1*scale, x2*scale, y2*scale))

        bounding_boxes = sorted(bounding_boxes, key=lambda rect: rect.area())
        print('>>> creating bounding_boxes from dnn face_detector\'s output took %.3f seconds' % (time.time() - start))
        return bounding_boxes
    def test_vertical_direction(self):
        a = Rectangle(1, 3, 4, 2)  # center:(3, 4)
        b = Rectangle(2, 0, 4, 4)  # center:(4, 2)

        r1 = SpatialRelation(a, b)  # v = (1, -2) -> degree ~ 296
        r2 = SpatialRelation(b, a)  # v = (1, -2) -> degree ~ 116

        self.assertFalse(r1.right())
        self.assertFalse(r1.left())
        self.assertFalse(r1.above())
        self.assertTrue(r1.below())

        self.assertFalse(r2.right())
        self.assertFalse(r2.left())
        self.assertTrue(r2.above())
        self.assertFalse(r2.below())
    def test_horizontal_direction(self):
        a = Rectangle(3, 1, 2, 4)  # center:(4, 3)
        b = Rectangle(5, 2, 2, 4)  # center:(6, 4)

        r1 = SpatialRelation(a, b)  # v = (2, 1) -> degree ~ 26
        r2 = SpatialRelation(b, a)  # v = (-2, -1) -> degree ~ 206

        self.assertTrue(r1.right())
        self.assertFalse(r1.left())
        self.assertFalse(r1.above())
        self.assertFalse(r1.below())

        self.assertFalse(r2.right())
        self.assertTrue(r2.left())
        self.assertFalse(r2.above())
        self.assertFalse(r2.below())
Ejemplo n.º 8
0
    def estimate(self, img = None, draw = False, color = (0, 255, 0), verbose = False):
        if self.initialized:
            num_estimated = int(round(self.persistent_weights.sum()))
            
            if num_estimated > 0:
                kmeans = KMeans(n_clusters = num_estimated).fit(self.persistent_states)
                estimated_targets = np.rint(kmeans.cluster_centers_).astype(int)
                
                new_tracks = []
                label = 0
                for et in estimated_targets:
                    while (label in self.labels or label in self.current_labels):
                        label+=1
                    target = Target(Rectangle(et[0], et[1], et[2], et[3]), label,\
                    (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) )
                    new_tracks.append(target)
                    self.current_labels.append(label)
                    label+=1

                # affinity
                '''new_tracks_features = self.detector.get_features(img, new_tracks)
                affinity_matrix = appearance_affinity(self.tracks_features, new_tracks_features) *\
                                  motion_affinity(self.tracks, new_tracks) * \
                                  shape_affinity(self.tracks, new_tracks)
                affinity_matrix = 1./affinity_matrix
                row_ind, col_ind = linear_sum_assignment(affinity_matrix)'''
                
                #######
                cost = cost_matrix(self.tracks, new_tracks)
                row_ind, col_ind = linear_sum_assignment(cost)

                for r,c in zip(row_ind, col_ind):
                    new_tracks[c].color = self.tracks[r].color
                    new_tracks[c].label = self.tracks[r].label

                self.tracks = new_tracks[:]
                #self.tracks_features = new_tracks_features[:]

                del self.current_labels[:]
                for track in self.tracks:
                    self.current_labels.append(track.label)
                self.labels = self.labels.union(self.current_labels)

                if draw:
                    for track in self.tracks:
                        cv2.rectangle(img, (track.bbox.x, track.bbox.y), (track.bbox.x + track.bbox.width, track.bbox.y + track.bbox.height), track.color, 3)
                if verbose:
                    print 'estimated targets: ' + str(num_estimated)
                return self.tracks
Ejemplo n.º 9
0
 def read_groundtruth(self, path_to_groundtruth = ''):
     if path_to_groundtruth == '':
         exit()
     
     with open(path_to_groundtruth, 'rb') as f:
         num_frame = 1
         for line in f:
             line = line.rstrip().split(',')
             xs = map(float, line[::2])
             ys = map(float, line[1::2])
             x = int(min(xs))
             y = int(min(ys))
             width = int(max(xs)) - x
             height = int(max(ys)) - y
             gt = Rectangle( x, y, width, height )
             self.groundtruth[num_frame - 1] = gt
             num_frame+=1
Ejemplo n.º 10
0
 def read_groundtruth(self, path_to_groundtruth = ''):
     if path_to_groundtruth == '':
         exit()
     
     with open(path_to_groundtruth, 'r') as f:
         for line in f:
             line = line.split(',')
             line[-1] = line[-1].strip()
             num_frame = int(line[0])
             if num_frame - 1 not in self.groundtruth:
                 self.groundtruth[num_frame - 1] = []
             #print int(float(line[1]))
             x = int(float(line[2]))
             y = int(float(line[3]))
             width = int(float(line[4]))
             height = int(float(line[5]))
             gt = Rectangle(x, y, width, height)
             self.groundtruth[num_frame - 1].append(gt)
 def test_equal(self):
     self.assertEqual(Rectangle(1, 2, 3, 4), Rectangle(1, 2, 3, 4))
     self.assertNotEqual(Rectangle(10, 2, 3, 4), Rectangle(1, 2, 3, 4))
     self.assertNotEqual(Rectangle(1, 20, 3, 4), Rectangle(1, 2, 3, 4))
     self.assertNotEqual(Rectangle(1, 2, 30, 4), Rectangle(1, 2, 3, 4))
     self.assertNotEqual(Rectangle(1, 2, 3, 40), Rectangle(1, 2, 3, 4))
    def test_intersect(self):
        a = Rectangle(1, 3, 3, 7)
        b = Rectangle(2, 1, 3, 6)
        c = Rectangle(5, 3, 3, 6)
        d = Rectangle(5, 0, 3, 2)

        self.assertEqual(a.signed_intersect(b), Rectangle(2, 3, 2, 4),
                         'overlapping')
        self.assertEqual(b.signed_intersect(a), Rectangle(2, 3, 2, 4),
                         'overlapping reverse')
        self.assertEqual(a.signed_intersect(a), a, 'equal')
        self.assertEqual(a.signed_intersect(c), Rectangle(5, 3, -1, 6),
                         'no intersect, x negative')
        self.assertEqual(a.signed_intersect(d), Rectangle(5, 3, -1, -1),
                         'no intersect, x amd y negative')
 def test_xmax_ymax(self):
     a = Rectangle(2, 3, 3, 5)
     self.assertEqual(a.xmax, 5)
     self.assertEqual(a.ymax, 8)
 def test_area(self):
     a = Rectangle(2, 3, 3, 5)
     self.assertEqual(a.area(), 15)
Ejemplo n.º 15
0
    def track(self, label, debug=False):
        """
        Takes a label and tracks it in a video or webcam stram.
        Displays the video with the tracked objects.
        Returns false if the label is lost
        """
        prev_speed = 0
        count_frames_speed_0 = 0
        mv = MoveRobot()
        # loop over frames from the video stream
        while True:
            # grab the current frame, then handle if we are using a
            # VideoStream or VideoCapture object
            """
            frame = self.vs.read()
            frame = frame[1] if not self.webCam else frame
            """
            if count_frames_speed_0 >= 10:
                break
            frame, boundary_lines = self.video_stream.read()
            # check to see if we have reached the end of the stream
            if frame is None:
                break
            # resize the frame (so we can process it faster) and grab the
            # frame dimensions
            frame = imutils.resize(frame, width=500) if not self.webCam else frame
            (H, W) = frame.shape[:2]

            # Start tracking the given label
            if self.initBB is None and label is not None:
                self.initBB = label
                # start OpenCV object tracker using the supplied bounding box
                # coordinates, then start the FPS throughput estimator as well
                self.tracker.init(frame, self.initBB)
                self.fps = FPS().start()
                label = None

            # check to see if we are currently tracking an object
            if self.initBB is not None:
                # grab the new bounding box coordinates of the object
                (success, box) = self.tracker.update(frame)
                # check to see if the tracking was a success
                if success:
                    (x, y, w, h) = [int(v) for v in box]

                    # Get the spine boundary lines
                    label_rectangle = Rectangle(x, y, w, h)
                    left_spine_bound, right_spine_bound = findSpineBoundaries(label_rectangle, boundary_lines)

                    # Plot the lines
                    if debug and left_spine_bound:
                        left_spine_bound.plotOnImage(frame, thickness=2)
                    if debug and right_spine_bound:
                        right_spine_bound.plotOnImage(frame, thickness=2)

                    distance_to_middle = 0

                    # If both spine bounds are in frame and found
                    if (right_spine_bound is not None) and (left_spine_bound is not None):

                        # Adjust the position of the robot
                        # Find a point on the spine boundaries which is in the middle of the frame height
                        left_spine_coordinate = left_spine_bound.calculateXgivenY(H/2)
                        right_spine_coordinate = right_spine_bound.calculateXgivenY(H/2)

                        # Find a point on the middle of the spine
                        spine_midpoint = left_spine_coordinate + (right_spine_coordinate - left_spine_coordinate) / 2

                        # Distance from the point on the middle of the spine to the middle of the frame
                        # Range 100 if spine is on the very left of the frame to -100 on the right
                        distance_to_middle = int(( (W/2 - spine_midpoint) * 100 ) / (W/2))

                    # If only one spine bound is found
                    if (right_spine_bound is None) and (left_spine_bound is not None):

                        # Distance from the point on the middle of the spine to the middle of the frame
                        # Range 100 if spine is on the very left of the frame to -100 on the right
                        left_spine_coordinate = left_spine_bound.calculateXgivenY(H/2)
                        distance_to_middle = int(( (W/2 - left_spine_coordinate) * 100 ) / (W/2))

                    if (right_spine_bound is not None) and (left_spine_bound is None):

                        # Distance from the point on the middle of the spine to the middle of the frame
                        # Range 100 if spine is on the very left of the frame to -100 on the right
                        right_spine_coordinate = right_spine_bound.calculateXgivenY(H/2)
                        distance_to_middle = int(( (W/2 - right_spine_coordinate) * 100 ) / (W/2))

                    if (right_spine_bound is not None) or (left_spine_bound is not None):
                        if abs(distance_to_middle < 20):
                            abs_speed = 0.005
                        else:
                            abs_speed = 0.001
                        if abs(distance_to_middle) < 5:
                            speed = 0
                            count_frames_speed_0 += 1
                        elif distance_to_middle < 0:
                            speed = abs_speed
                            count_frames_speed_0 = 0
                        else:
                            speed = -abs_speed
                            count_frames_speed_0 = 0
                        if speed != prev_speed:
                            print("Moving with speed " + str(speed) + " !")
                            Thread(target=mv.setSpeed, args=(speed,)).start()
                        prev_speed = speed
                    # Draw the rectangle around the label
                    if debug:
                        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                #else:
                    #return success

                # update the FPS counter
                self.fps.update()
                self.fps.stop()
                print("FPS", "{:.2f}".format(self.fps.fps()))
                # initialize the set of information we'll be displaying on
                # the frame
                info = [
                    ("Tracker", self.trackerType),
                    ("Success", "Yes" if success else "No"),
                    ("FPS", "{:.2f}".format(self.fps.fps())),
                ]
                # loop over the info tuples and draw them on our frame
                if debug:
                    for (i, (k, v)) in enumerate(info):
                        text = "{}: {}".format(k, v)
                        cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

            # show the output frame
            if debug:
                cv2.imshow("Frame", frame)
            key = cv2.waitKey(1) & 0xFF
            # if the `q` key was pressed, break from the loop
            if key == ord("q"):
                break
        mv.shutDown()
        # if we are using a webcam, release the pointer
        if self.webCam:
            #self.vs.stop()
            self.video_stream.stop()
        # otherwise, release the file pointer
        else:
            self.vs.release()
        # close all windows
        cv2.destroyAllWindows()
        return True