Ejemplo n.º 1
0
Archivo: server.py Proyecto: zxxia/dds
    def get_regions_to_query(self, rpn_regions, detections):
        req_regions = Results()
        for region in rpn_regions.regions:
            # Continue if the size of region is too large
            if region.w * region.h > self.config.size_obj:
                continue

            # If there are positive detections and they match a region
            # skip that region
            if len(detections) > 0:
                matches = 0
                for detection in detections.regions:
                    if (calc_iou(detection, region) >
                            self.config.objfilter_iou and
                            detection.fid == region.fid and
                            region.label == 'object'):
                        matches += 1
                if matches > 0:
                    continue

            # Enlarge and add to regions to be queried
            region.enlarge(self.config.rpn_enlarge_ratio)
            req_regions.add_single_result(
                region, self.config.intersection_threshold)
        return req_regions
Ejemplo n.º 2
0
    def emulate_high_query(self,
                           vid_name,
                           low_images_direc,
                           req_regions,
                           iter=None):
        images_direc = f"{vid_name}-cropped"  #-{iter}"
        # self.logger.info(f"images_direc: {images_direc}")
        # Extract images from encoded video
        extract_images_from_video(images_direc, req_regions)

        if not os.path.isdir(images_direc):
            self.logger.error("Images directory was not found but the "
                              "second iteration was called anyway")
            return Results()

        fnames = sorted([f for f in os.listdir(images_direc) if "png" in f])

        # Make seperate directory and copy all images to that directory
        merged_images_direc = os.path.join(images_direc, "merged")
        os.makedirs(merged_images_direc, exist_ok=True)
        for img in fnames:
            shutil.copy(os.path.join(images_direc, img), merged_images_direc)

        # self.logger.info(f"{merged_images_direc}, {low_images_direc}")

        merged_images = merge_images(merged_images_direc, low_images_direc,
                                     req_regions)
        results, _ = self.perform_detection(merged_images_direc,
                                            self.config.high_resolution,
                                            fnames, merged_images)

        results_with_detections_only = Results()
        for r in results.regions:
            if r.label == "no obj":
                continue
            results_with_detections_only.add_single_result(
                r, self.config.intersection_threshold)

        high_only_results = Results()
        area_dict = {}
        for r in results_with_detections_only.regions:
            frame_regions = req_regions.regions_dict[r.fid]
            regions_area = 0
            if r.fid in area_dict:
                regions_area = area_dict[r.fid]
            else:
                regions_area = compute_area_of_frame(frame_regions)
                area_dict[r.fid] = regions_area
            regions_with_result = frame_regions + [r]
            total_area = compute_area_of_frame(regions_with_result)
            extra_area = total_area - regions_area
            if extra_area < 0.05 * calc_area(r):
                r.origin = "high-res"
                high_only_results.append(r)

        # shutil.rmtree(merged_images_direc)

        return results_with_detections_only
Ejemplo n.º 3
0
    def simulate_low_query(self,
                           start_fid,
                           end_fid,
                           images_direc,
                           results_dict,
                           simulation=True,
                           rpn_enlarge_ratio=0.0,
                           extract_regions=True):
        if extract_regions:
            # If called from actual implementation
            # This will not run
            base_req_regions = Results()
            for fid in range(start_fid, end_fid):
                base_req_regions.append(
                    Region(fid, 0, 0, 1, 1, 1.0, 2,
                           self.config.high_resolution))
            extract_images_from_video(images_direc, base_req_regions)

        batch_results = Results()

        self.logger.info(f"Getting results with threshold "
                         f"{self.config.low_threshold} and "
                         f"{self.config.high_threshold}")
        # Extract relevant results
        for fid in range(start_fid, end_fid):
            fid_results = results_dict[fid]
            for single_result in fid_results:
                single_result.origin = "low-res"
                batch_results.add_single_result(
                    single_result, self.config.intersection_threshold)

        detections = Results()
        # rpn_regions = Results()
        # Divide RPN results into detections and RPN regions
        for single_result in batch_results.regions:
            if (single_result.conf > self.config.prune_score
                    and single_result.label == "vehicle"):
                detections.add_single_result(
                    single_result, self.config.intersection_threshold)
            # else:
            # rpn_regions.add_single_result(
            # single_result, self.config.intersection_threshold)

        #regions_to_query = self.get_regions_to_query(rpn_regions, detections)
        regions_to_query = self.get_regions_to_query_new(
            start_fid, end_fid, self.low_configuration_results,
            self.high_configuration_results)

        return detections, regions_to_query
Ejemplo n.º 4
0
    def get_regions_to_query_new(self,
                                 start_fid,
                                 end_fid,
                                 low_configuration_results,
                                 high_configuration_results,
                                 shrink_max=25):
        '''
        Args: 
            all_regions [list] a list of regions.
            low_resolution_results [dict] a dict of regions using 'fid' as key.
            high_resolution_results [dict] a dict of regions using 'fid' as key.
        Returns:
            req_regions [list] a list of regions detected in low configuration but are not detected in high configuration.
        '''

        # self.logger.info(f"Running get_regions_to_query_new")
        # for fid in range(start_fid, end_fid):
        #     for high_detection in high_configuration_results[fid]:
        #         self.logger.info(f"{fid} {high_detection.x} {high_detection.y} {high_detection.w} {high_detection.h} {high_detection.label} {high_detection.origin}")
        self.load_mpeg_result()
        req_regions = Results()

        shrink_r = [(1.0 / (shrink_max**0.5)) * i**0.5
                    for i in range(1, shrink_max + 1)]

        for fid in range(start_fid, end_fid):
            # single_regions = Results()
            for high_detection in high_configuration_results[fid]:
                # self.logger.info(f"Checking {fid} {high_detection.x} {high_detection.y} {high_detection.w} {high_detection.h}")
                has_overlap = False
                for low_detection in low_configuration_results[fid]:
                    if calc_iou(high_detection,
                                low_detection) > self.config.objfilter_iou:
                        # self.logger.info(f"Overlap with {low_detection.x} {low_detection.y} {low_detection.w} {low_detection.h}")
                        has_overlap = True
                        break
                if has_overlap == False:
                    # occur only in high quality reference, need to return
                    # self.logger.info(f"{fid},{high_detection.x},{high_detection.y},{high_detection.w},{high_detection.h},{high_detection.conf},{high_detection.label},{high_detection.resolution}")

                    ratio = self.query_iter_enlarge_ratio(
                        fid, high_detection.x, high_detection.y,
                        high_detection)
                    if ratio < 0:
                        # self.logger.info(f"No overlap, min enlarge ratio failed")
                        continue
                    # self.logger.info(f"Enlarge ratio {ratio}")
                    if ratio >= shrink_max:
                        ratio -= (shrink_max - 1)
                        ratio *= 0.005
                    else:
                        ratio = -shrink_r[ratio]

                    # ratio *= 0.01
                    # ratio = 1.0

                    self.logger.info(
                        f"{fid} {high_detection.x} {high_detection.y} {high_detection.w} {high_detection.h} {ratio}"
                    )

                    # xx, yy, ww, hh = high_detection.x, high_detection.y, high_detection.w, high_detection.h
                    high_detection.enlarge_absolute(ratio)
                    # high_detection.enlarge_absolute(self.config.rpn_enlarge_ratio)

                    # self.logger.info(f"{fid} {high_detection.x} {high_detection.y} {high_detection.w} {high_detection.h} {ratio}")

                    req_regions.add_single_result(
                        high_detection, self.config.intersection_threshold)

                    # single_regions.add_single_result(high_detection, self.config.intersection_threshold)
                    # high_detection.x, high_detection.y, high_detection.w, high_detection.h = xx, yy, ww, hh

        return req_regions