def fix_nonrect_polygons(polygon_list, padding=0): 
        #GET PROPER RECTANGLES
        #the manually annotated coordinates do not actually make up rectangles
        #so we find the minbounding rects of the polygons to obtain rectangles

        #draw the detections onto a binary image
        min_polygons = list()
        for polygon in polygon_list:
            bitmap = np.zeros((1200, 2000), np.uint8) 
            utils.draw_detections([polygon], bitmap, fill=True, color=1)

            #get contours
            contours, hierarchy = cv2.findContours(bitmap, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

            #get the min bounding rect for the rects
            min_area_rect = cv2.minAreaRect(contours[0]) # rect = ((center_x,center_y),(width,height),angle)
            min_area_rect_list = [list(x) if type(x) is tuple else x for x in min_area_rect] 
            min_area_rect_list[1][0] += padding 
            min_area_rect_list[1][1] += padding 

            cnt = tuple(tuple(x) if type(x) is list else x for x in min_area_rect_list)#, dtype=np.int32)
            min_poly = np.int0(cv2.cv.BoxPoints(cnt))
            min_polygons.append(min_poly)

        return np.array(min_polygons)
Exemple #2
0
    def print_detections(self, detections, img_name, title):
        if detections is not None:
            for roof_type, detects in detections.iteritems():
                img = cv2.imread(self.in_path + img_name)

                if img_name in self.evaluation_after_neural[0].correct_roofs[
                        roof_type]:
                    #the uninhabited images do not have an entry
                    utils.draw_detections(self.evaluation_after_neural[0].
                                          correct_roofs[roof_type][img_name],
                                          img,
                                          rects=True,
                                          color=(0, 255, 0),
                                          thickness=6)
                if detects.shape[0] > 0:
                    utils.draw_detections(detects,
                                          img,
                                          rects=True,
                                          color=(255, 0, 0),
                                          thickness=3)

                cv2.imwrite(
                    'debug/{}_{}_{}{}.jpg'.format(self.groupThres[roof_type],
                                                  img_name[:-4], roof_type,
                                                  title), img)
def detect_video(obj_det, cam_id, out_file):
    """ """
    print ('CAM = ', cam_id)
    cam = utils.WebCam(cam_id, 'caffe_webcam_demo')
    fourcc  = cv2.VideoWriter_fourcc(*'XVID')
    frame   = cam.get_frame()
    h, w, c = frame.shape
    writer  = cv2.VideoWriter(out_file, fourcc, cam.fps, (w, h))
    start_time = time()
    t_start = datetime.now()
    fps = 0
    frame_idx = 0
    while True:
        if utils.Esc_key_pressed(): break
        frame = cam.get_frame()
        if frame is None: break
        bboxes = obj_det(frame)
        utils.draw_detections(frame, bboxes)
        fps += 1
        frame_idx += 1
        if time() - start_time >= 1:
            print ('fps = ', fps / (time() - start_time))
            fps = 0
            start_time = time()
        cv2.imshow(cam.name, frame)
        writer.write(frame)
    
    print ('Elapsed = ' + str(datetime.now() - t_start))
    print ('Frame Idx = ', frame_idx)
    writer.release()
    cam.close()
Exemple #4
0
    def process_frame(self, frame, preproc):
        self.graph.LoadTensor(preproc, 'frame')

        # Process output of the previous frame
        if self.net_out is not None:
            out_shape = self.net_out.shape
            if self.model['out_size'] is not None:
                out_size = self.model['out_size']
                self.net_out = self.net_out.reshape(out_size)
                self.net_out = np.transpose(self.net_out, [2, 0, 1])
            self.net_out = self.net_out.astype(np.float32)
            self.bboxes = yolo_get_candidate_objects(self.net_out,
                                                     self.prev_frame.shape)
            utils.draw_detections(self.prev_frame, self.bboxes)
            cv2.imshow(self.win_name, self.prev_frame)
            cv2.waitKey(10)
            self.fps.update()

        # Set the next input frame
        self.prev_frame = frame

        # Run network on the current frame
        self.net_out = None
        while self.net_out is None:
            self.net_out, obj = self.graph.GetResult()
    def fix_nonrect_polygons(polygon_list, padding=0):
        #GET PROPER RECTANGLES
        #the manually annotated coordinates do not actually make up rectangles
        #so we find the minbounding rects of the polygons to obtain rectangles

        #draw the detections onto a binary image
        min_polygons = list()
        for polygon in polygon_list:
            bitmap = np.zeros((1200, 2000), np.uint8)
            utils.draw_detections([polygon], bitmap, fill=True, color=1)

            #get contours
            contours, hierarchy = cv2.findContours(bitmap, cv2.RETR_TREE,
                                                   cv2.CHAIN_APPROX_SIMPLE)

            #get the min bounding rect for the rects
            min_area_rect = cv2.minAreaRect(
                contours[0]
            )  # rect = ((center_x,center_y),(width,height),angle)
            min_area_rect_list = [
                list(x) if type(x) is tuple else x for x in min_area_rect
            ]
            min_area_rect_list[1][0] += padding
            min_area_rect_list[1][1] += padding

            cnt = tuple(
                tuple(x) if type(x) is list else x
                for x in min_area_rect_list)  #, dtype=np.int32)
            min_poly = np.int0(cv2.cv.BoxPoints(cnt))
            min_polygons.append(min_poly)

        return np.array(min_polygons)
Exemple #6
0
def test(input_classifier_name, scale=0.6):
    c = load_classifier(input_classifier_name)

    # testImages = utils.getFullImages(
    #     "/home/mataevs/ptz/dumpNew1",
    #     "/home/mataevs/ptz/dumpNew2",
    #     "/home/mataevs/ptz/dumpNew3",
    #     "/home/mataevs/ptz/ns1",
    #     "/home/mataevs/ptz/ns2",
    #     "/home/mataevs/ptz/ns3",
    #     "/home/mataevs/ptz/ns4")

    testImages = utils.getFullImages(
        "/home/mataevs/ptz/INRIAPerson/Test/pos"
    )

    while True:
        imgPath = random.choice(testImages)

        bestWindow = test_img(c, imgPath, scale)

        img = cv2.imread(imgPath)
        img = cv2.resize(img, (0, 0), fx=scale, fy=scale)

        utils.draw_detections(img, [bestWindow])

        cv2.imshow("detection", img)
        key = cv2.waitKey(0)

        if key == 27:
            exit(1)
Exemple #7
0
def test(input_classifier_name, scale=0.6):
    c = load_classifier(input_classifier_name)

    # testImages = utils.getFullImages(
    #     "/home/mataevs/ptz/dumpNew1",
    #     "/home/mataevs/ptz/dumpNew2",
    #     "/home/mataevs/ptz/dumpNew3",
    #     "/home/mataevs/ptz/ns1",
    #     "/home/mataevs/ptz/ns2",
    #     "/home/mataevs/ptz/ns3",
    #     "/home/mataevs/ptz/ns4")

    testImages = utils.getFullImages("/home/mataevs/ptz/INRIAPerson/Test/pos")

    while True:
        imgPath = random.choice(testImages)

        bestWindow = test_img(c, imgPath, scale)

        img = cv2.imread(imgPath)
        img = cv2.resize(img, (0, 0), fx=scale, fy=scale)

        utils.draw_detections(img, [bestWindow])

        cv2.imshow("detection", img)
        key = cv2.waitKey(0)

        if key == 27:
            exit(1)
Exemple #8
0
    def detect_img(self, image_file, count=10):
        net_out = None
        bboxes = None
        in_frame = cv2.imread(image_file)
        preproc = self.preprocess(in_frame)
        fps = FPS().start()
        t_s = datetime.now()
        while True:
            self.graph.LoadTensor(preproc, 'image')
            if net_out is not None:
                out_shape = net_out.shape
                if self.model['out_size'] is not None:
                    out_size = self.model['out_size']
                    net_out = net_out.reshape(out_size)
                    net_out = np.transpose(net_out, [2, 0, 1])
                net_out = net_out.astype(np.float32)
                bboxes = yolo_get_candidate_objects(net_out, in_frame.shape)
                utils.draw_detections(in_frame, bboxes)
                fps.update()
                if fps._numFrames == 10:
                    fps.stop()
                    print("Elapsed = {:.2f}".format(fps.elapsed()))
                    print("FPS     = {:.2f}".format(fps.fps()))
                    break

            in_frame = cv2.imread(image_file)
            preproc = self.preprocess(in_frame)
            net_out = None
            while net_out is None:
                net_out, obj = self.graph.GetResult()
        print('Time = ', datetime.now() - t_s)
 def posprocess_thread(self):
     while True:
         frame = self.frame_q.get()
         net_out = self.out_q.get()
         if frame is None: break
         if net_out is None: break
         self.bboxes = self.detector.get_bboxes(frame, net_out)
         self.frame_q.task_done()
         self.out_q.task_done()
         utils.draw_detections(frame, self.bboxes)
         cv2.imshow(self.name, frame)
         self.fps.update()
    def print_detections(self, detections, img_name, title):
        if detections is not None:
            for roof_type, detects in detections.iteritems():
                img = cv2.imread(self.in_path+img_name)

                if img_name in self.evaluation_after_neural[0].correct_roofs[roof_type]:
                    #the uninhabited images do not have an entry
                    utils.draw_detections(self.evaluation_after_neural[0].correct_roofs[roof_type][img_name], img, rects=True, color=(0,255,0), thickness=6)
                if detects.shape[0] > 0: 
                    utils.draw_detections(detects, img, rects=True, color=(255,0,0), thickness=3)

                cv2.imwrite('debug/{}_{}_{}{}.jpg'.format(self.groupThres[roof_type], img_name[:-4],roof_type, title), img)
Exemple #11
0
def detect_images(detector, obj_det, img_list):
    """ """
    start_time = datetime.now()
    images_dir = os.path.join(os.path.dirname(__file__), 'images')
    out_dir = os.path.join(images_dir, detector)
    if not os.path.isdir(out_dir):
        os.makedirs(out_dir)
    
    for img in img_list:
        image   = cv2.imread(os.path.join(images_dir, img))
        bboxes = obj_det(image)
        utils.draw_detections(image, bboxes)
        cv2.imwrite(os.path.join(out_dir, img), image)
    print ('Elapsed = ', (datetime.now() - start_time))
Exemple #12
0
    def call_noth(self, frame):
        print('Time = ', datetime.now() - self.last_call)
        stop = False
        if utils.Esc_key_pressed():
            stop = True
            self.fps.stop()
            print("Elapsed = {:.2f}".format(self.fps.elapsed()))
            print("FPS     = {:.2f}".format(self.fps.fps()))

        # Preprocess Current frame
        if self.started is True:
            self.graph.LoadTensor(self.preproc, 'image')

        # Process output of the previous frame
        if self.net_out is not None:
            out_shape = self.net_out.shape
            if self.model['out_size'] is not None:
                out_size = self.model['out_size']
                self.net_out = self.net_out.reshape(out_size)
                self.net_out = np.transpose(self.net_out, [2, 0, 1])
            self.net_out = self.net_out.astype(np.float32)
            self.bboxes = yolo_get_candidate_objects(self.net_out,
                                                     self.in_frame.shape)
            utils.draw_detections(self.prev_frame, self.bboxes)
            cv2.imshow(self.win_name, self.prev_frame)
            self.fps.update()

        # Set the nest input frame
        t1 = datetime.now()
        self.prev_frame = self.in_frame
        self.in_frame = frame
        self.preproc = self.preprocess(self.in_frame)
        print('pre_proc = ', datetime.now() - t1)

        # Run network on the current frame
        self.net_out = None
        if self.started is True:
            while self.net_out is None:
                self.net_out, obj = self.graph.GetResult()
        else:
            self.started = True
            self.fps = FPS().start()

        self.last_call = datetime.now()
        return stop
Exemple #13
0
    def group_min_bound(self, polygons, img_shape, erosion=0):
        '''
        Attempt at finding the minbound of all overlapping rects and merging them
        to a single detection. This unfortunately will merge nearby roofs.
        '''
        bitmap = np.zeros(img_shape, dtype='uint8')
        utils.draw_detections(np.array(polygons), bitmap, fill=True, color=1)

        if erosion>0:
            kernel = np.ones((5,5),np.uint8)
            bitmap = cv2.erode(bitmap,kernel,iterations = erosion)

        #get contours
        contours, hierarchy = cv2.findContours(bitmap, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        #get the min bounding rect for the rects
        min_area_conts = [np.int0(cv2.cv.BoxPoints(cv2.minAreaRect(cnt))) for cnt in contours]
        return min_area_conts
    def detect_roofs(self, img_name, in_path=None):
        in_path = self.in_path if in_path is None else in_path 
        try:
            rgb_unrotated = cv2.imread(in_path+img_name, flags=cv2.IMREAD_COLOR)
            gray = cv2.cvtColor(rgb_unrotated, cv2.COLOR_BGR2GRAY)
            gray = cv2.equalizeHist(gray)

            if self.downsized:
                rgb_unrotated = utils.resize_rgb(rgb_unrotated, h=rgb_unrotated.shape[0]/2, w=rgb_unrotated.shape[1]/2)
                gray = utils.resize_grayscale(gray, w=gray.shape[1]/2, h=gray.shape[0]/2)

        except IOError as e:
            print e
            sys.exit(-1)
        else:
            for roof_type, detectors in self.roof_detectors.iteritems():
                for i, detector in enumerate(detectors):
                    for angle in self.angles:
                        #for thatch we only need one angle
                        if self.rotate_detectors[i] == False and angle>0 or (roof_type=='thatch' and angle>0):#roof_type == 'thatch' and angle>0:
                            continue

                        print 'Detecting with detector: '+str(i)
                        print 'ANGLE '+str(angle)

                        with Timer() as t: 
                            rotated_image = utils.rotate_image(gray, angle) if angle>0 else gray
                            delete_image = utils.rotate_image_RGB(rgb_unrotated, angle) if angle>0 else gray
                            detections, _ = self.detect_and_rectify(detector, rotated_image, angle, rgb_unrotated.shape[:2], rgb_rotated=delete_image) 
                            if self.downsized:
                                detections = detections*2
                            self.viola_detections.set_detections(roof_type=roof_type, img_name=img_name, 
                                    angle=angle, detection_list=detections, img=rotated_image)

                        print 'Time detection: {0}'.format(t.secs)
                        self.viola_detections.total_time += t.secs
                        if DEBUG:
                            rgb_to_write = cv2.imread(in_path+img_name, flags=cv2.IMREAD_COLOR)
                            utils.draw_detections(detections, rgb_to_write, color=(255,0,0))
                            cv2.imwrite('{0}{3}{1}_{2}.jpg'.format('', img_name[:-4], angle, roof_type), rgb_to_write)
            return rgb_unrotated
    def save_images(self, img_name, fname='', out_path='debug/'):
        '''Displays the ground truth, along with the true and false positives for a given image
        '''
        try:
            img = cv2.imread(self.in_path + img_name, flags=cv2.IMREAD_COLOR)
        except IOError:
            print 'Cannot open {0}'.format(self.in_path + img_name)
            sys.exit(-1)

        rects = True if self.full_dataset else False
        for roof_type in utils.ROOF_TYPES:
            #false pos
            false_pos = self.detections.easy_false_pos[roof_type][img_name]
            utils.draw_detections(false_pos,
                                  img,
                                  color=(0, 0, 255),
                                  rects=rects)
            #ground truth
            utils.draw_detections(self.correct_roofs[roof_type][img_name],
                                  img,
                                  color=(255, 0, 0),
                                  rects=rects)
            #true pos
            true_pos = self.detections.easy_true_pos[roof_type][img_name]
            utils.draw_detections(true_pos,
                                  img,
                                  color=(0, 255, 0),
                                  rects=rects)
            cv2.imwrite(
                '{0}{1}{2}_{3}.jpg'.format(out_path, img_name[:-4], fname,
                                           roof_type), img)
    def get_score(self, contours=False, rows=1200, cols=2000, roof=None, detection=None):
        #assert len(roof) == 4 and len(detection) == 4

        #First, count the area that was detected
        count_detection_area_mask = np.zeros((rows, cols), dtype='uint8')
        if contours == False:
            utils.draw_detections(np.array([detection]), count_detection_area_mask, fill=True, color=1)
        else:
            cv2.drawContours(count_detection_area_mask, np.array([detection]), 0, 1, -1)
        detection_area = utils.sum_mask(count_detection_area_mask)

        #binary mask of ground truth roof on the original image, non-rotated
        matching_mask = np.zeros((rows, cols), dtype='uint8')
        utils.draw_detections(np.array([roof]), matching_mask, fill=True, color=1)
        roof_area = utils.sum_mask(matching_mask)

        #subtract the detection
        if contours == False:
            utils.draw_detections(np.array([detection]), matching_mask, fill=True, color=0)
        else:
            cv2.drawContours(matching_mask, [detection], 0, 0, -1)

        #calculate the intersection 
        roof_missed = utils.sum_mask(matching_mask)
        intersection_area = roof_area-roof_missed

        #VOC measure
        union_area = (roof_area + (detection_area)) - intersection_area
        voc_score = float(intersection_area)/union_area

        #How much of the detection is roof? If it's high, they this detection is mostly covering a roof
        detection_roof_portion = float(intersection_area)/detection_area 
        return voc_score, detection_roof_portion
    def __call__(self, frame):
        ret = False
        if utils.Esc_key_pressed():
            ret = True
            self.fps.stop()
            print("Elapsed = {:.2f}".format(self.fps.elapsed()))
            print("FPS     = {:.2f}".format(self.fps.fps()))
            self.out_q.put(None)
            self.frame_q.put(None)
            self.in_q.put(None)
        else:
            if self.use_threading is True:
                self.frame_q.put(frame)
                frame = self.detector.preprocess(frame)
                self.in_q.put(frame)
            else:
                bboxes = self.detector(frame)
                utils.draw_detections(frame, bboxes)
                cv2.imshow(self.name, frame)
                # self.writer.write(frame)
                self.fps.update()

        return ret
Exemple #18
0
    def posprocess(self):
        while True:
            if self.stopped is True: break
            frame   = self.frame_q.get()
            net_out = self.out_q.get()
            if frame is None and net_out is None:
                break
            if net_out is not None:
                bboxes = self.detector.get_bboxes(frame, net_out)
                utils.draw_detections(frame, bboxes)

            if self.writer is None:
                fourcc  = cv2.VideoWriter_fourcc(*'XVID')
                h, w, c = frame.shape
                self.writer  = cv2.VideoWriter(self.out_file, fourcc, self.cam.fps, (w, h))
            
            cv2.imshow(self.cam.name, frame)
            self.writer.write(frame)

            self.frame_q.task_done()
            self.out_q.task_done()
        print ('Thread 3 Exited')
        self.stopped = True
Exemple #19
0
def main():
    image_file = 'images/office.jpg'
    for model in ['tiny-yolo-v1', 'tiny-yolo-v2']:
        print("PROFILING Model :  ", model)
        mvnc_det = YoloObjectDetector(model, 'mvncs')

        fps = FPS().start()
        image = cv2.imread(image_file)
        pre_proc = mvnc_det.preprocess(image)
        for i in range(10):
            t_s = datetime.now()
            net_out = mvnc_det.detect(pre_proc)
            print('Time = ', datetime.now() - t_s)
            bboxes = mvnc_det.get_bboxes(image, net_out)
            utils.draw_detections(image, bboxes)
            fps.update()
        fps.stop()

        print("Elapsed = {:.2f}".format(fps.elapsed()))
        print("FPS     = {:.2f}".format(fps.fps()))
        cv2.imwrite(model + '.jpg', image)
        mvnc_det.close()
        sleep(2)
    def get_score(self,
                  contours=False,
                  rows=1200,
                  cols=2000,
                  roof=None,
                  detection=None):
        #assert len(roof) == 4 and len(detection) == 4

        #First, count the area that was detected
        count_detection_area_mask = np.zeros((rows, cols), dtype='uint8')
        if contours == False:
            utils.draw_detections(np.array([detection]),
                                  count_detection_area_mask,
                                  fill=True,
                                  color=1)
        else:
            cv2.drawContours(count_detection_area_mask, np.array([detection]),
                             0, 1, -1)
        detection_area = utils.sum_mask(count_detection_area_mask)

        #binary mask of ground truth roof on the original image, non-rotated

        matching_mask = np.zeros((rows, cols), dtype='uint8')
        utils.draw_detections(np.array([roof]),
                              matching_mask,
                              fill=True,
                              color=1)
        roof_area = utils.sum_mask(matching_mask)

        #subtract the detection
        if contours == False:
            utils.draw_detections(np.array([detection]),
                                  matching_mask,
                                  fill=True,
                                  color=0)
        else:
            cv2.drawContours(matching_mask, [detection], 0, 0, -1)

        #calculate the intersection
        roof_missed = utils.sum_mask(matching_mask)
        intersection_area = roof_area - roof_missed

        #VOC measure
        union_area = (roof_area + (detection_area)) - intersection_area
        voc_score = float(intersection_area) / union_area

        #How much of the detection is roof? If it's high, they this detection is mostly covering a roof
        detection_roof_portion = float(intersection_area) / detection_area
        return voc_score, detection_roof_portion
    def save_images(self, img_name, fname=''):
        '''Displays the ground truth, along with the true and false positives for a given image
        '''
        try:
            img = cv2.imread(self.in_path+img_name, flags=cv2.IMREAD_COLOR)
        except IOError:
            print 'Cannot open {0}'.format(self.in_path+img_name)
            sys.exit(-1)
        for roof_type in utils.ROOF_TYPES:
            utils.draw_detections(self.detections.false_positives[roof_type][img_name], img, color=(0, 0, 255))
            utils.draw_detections(self.correct_roofs[roof_type][img_name], img, color=(0, 0, 0))
            utils.draw_detections(self.detections.true_positives[roof_type][img_name], img, color=(0, 255, 0))


        cv2.imwrite('{0}{1}{2}.jpg'.format(self.out_path, img_name[:-4], fname), img)
    def save_training_FP_and_TP_helper(self, num_patches, img_name, detections, patches_path, 
                                        general_path, img, roof_type, extraction_type, color, rects=False):
        #this is where we write the detections we're extraction. One image per roof type
        #we save: 1. the patches and 2. the image with marks of what the detections are, along with the true roofs (for debugging)
        img_debug = np.copy(img) 
        if roof_type == 'background':
            utils.draw_detections(self.correct_roofs['metal'][img_name], img_debug, color=(0, 0, 0), thickness=2, rects=rects)
            utils.draw_detections(self.correct_roofs['thatch'][img_name], img_debug, color=(0, 0, 0), thickness=2, rects=rects)
        else:
            utils.draw_detections(self.correct_roofs[roof_type][img_name], img_debug, color=(0, 0, 0), thickness=2, rects=rects)

        for i, detection in enumerate(detections):
            batch_path =  'batch{}/'.format(int(num_patches/20000))  
            if num_patches % 20000 == 0:
                utils.mkdir('{}falsepos/batch{}/'.format(general_path, num_patches/20000))
                utils.mkdir('{}truepos/batch{}/'.format(general_path, num_patches/20000))
            num_patches += 1

            current_patch_path = patches_path+batch_path

            #extract the patch, rotate it to a horizontal orientation, save it
            if rects == False:
                bitmap = np.zeros((img.shape[:2]), dtype=np.uint8)
                padded_detection = utils.add_padding_polygon(detection, bitmap)
                warped_patch = utils.four_point_transform(img, padded_detection)
                cv2.imwrite('{0}{1}_{2}_roof{3}.jpg'.format(current_patch_path, roof_type, img_name[:-4], i), warped_patch)
                
                #mark where roofs where taken out from for debugging
                utils.draw_polygon(padded_detection, img_debug, fill=False, color=color, thickness=2, number=i)
            else:
                pad = 10
                xmin = (detection.xmin-pad) if (detection.xmin-pad)>0 else detection.xmin
                ymin = (detection.ymin-pad) if (detection.ymin-pad)>0 else detection.ymin
                xmax = (detection.xmax+pad) if (detection.xmax+pad)<img.shape[1] else detection.xmax
                ymax = (detection.ymax+pad) if (detection.ymax+pad)<img.shape[0] else detection.ymax
                patch = img[ymin:ymax, xmin:xmax, :]
                #print 'saving {0}{1}_{2}_roof{3}.jpg'.format(current_patch_path, roof_type, img_name[:-4], i) 
                cv2.imwrite('{0}{1}_{2}_roof{3}.jpg'.format(current_patch_path, roof_type, img_name[:-4], i), patch)
                self.TOTAL += 1
                if self.TOTAL % 1000 == 0:
                    print 'Saved {} patches'.format(self.TOTAL)
        return num_patches
    def save_images(self, img_name, fname='', out_path='debug/'):
        '''Displays the ground truth, along with the true and false positives for a given image
        '''
        try:
            img = cv2.imread(self.in_path+img_name, flags=cv2.IMREAD_COLOR)
        except IOError:
            print 'Cannot open {0}'.format(self.in_path+img_name)
            sys.exit(-1)

        rects = True if self.full_dataset else False
        for roof_type in utils.ROOF_TYPES:
            #false pos
            false_pos = self.detections.easy_false_pos[roof_type][img_name] 
            utils.draw_detections(false_pos, img, color=(0, 0, 255), rects=rects)
            #ground truth
            utils.draw_detections(self.correct_roofs[roof_type][img_name], img, color=(255, 0, 0), rects=rects)
            #true pos
            true_pos = self.detections.easy_true_pos[roof_type][img_name]
            utils.draw_detections(true_pos, img, color=(0, 255, 0), rects=rects)
            cv2.imwrite('{0}{1}{2}_{3}.jpg'.format(out_path, img_name[:-4], fname, roof_type), img)
def save_training_FP_and_TP_helper(img_name,evaluation, detections, patches_path, general_path, img, roof_type, extraction_type, color):
    #this is where we write the detections we're extraction. One image per roof type
    #we save: 1. the patches and 2. the image with marks of what the detections are, along with the true roofs (for debugging)
    img_debug = np.copy(img) 

    if roof_type == 'background':
        utils.draw_detections(evaluation.correct_roofs['metal'][img_name], img_debug, color=(0, 0, 0), thickness=2)
        utils.draw_detections(evaluation.correct_roofs['thatch'][img_name], img_debug, color=(0, 0, 0), thickness=2)
    else:
        utils.draw_detections(evaluation.correct_roofs[roof_type][img_name], img_debug, color=(0, 0, 0), thickness=2)

    for i, detection in enumerate(detections):
        #extract the patch, rotate it to a horizontal orientation, save it
        bitmap = np.zeros((img.shape[:2]), dtype=np.uint8)
        padded_detection = utils.add_padding_polygon(detection, bitmap)
        warped_patch = utils.four_point_transform(img, padded_detection)
        cv2.imwrite('{0}{1}_{2}_roof{3}.jpg'.format(patches_path, roof_type, img_name[:-4], i), warped_patch)
        
        #mark where roofs where taken out from for debugging
        utils.draw_polygon(padded_detection, img_debug, fill=False, color=color, thickness=2, number=i)

    #write this type of extraction and the roofs to an image
    cv2.imwrite('{0}{1}_{2}_extract_{3}.jpg'.format(general_path, img_name[:-4], roof_type, extraction_type), img_debug)
                            r['class_ids'], r['masks'], r['coords'], depth,
                            intrinsics, synset_names, image_path)
                    #save_dir+'/'+'{}_{}_{}_pred_'.format(data, image_path_parsing[-2], image_path_parsing[-1]))
                    print('New alignment takes {:03f}s.'.format(time.time() -
                                                                start))
                    elapse_times += elapses
                    if len(error_message):
                        f_log.write(error_message)

                    if args.draw:
                        draw_rgb = False
                        utils.draw_detections(
                            image, save_dir, data, image_path_parsing[-2] +
                            '_' + image_path_parsing[-1], intrinsics,
                            synset_names, draw_rgb, gt_bbox, gt_class_ids,
                            gt_mask, gt_coord, result['gt_RTs'], gt_scales,
                            result['gt_handle_visibility'], r['rois'],
                            r['class_ids'], r['masks'], r['coords'],
                            result['pred_RTs'], r['scores'],
                            result['pred_scales'])

                else:
                    if args.draw:
                        draw_rgb = False
                        utils.draw_coco_detections(
                            image, save_dir, data, image_path_parsing[-2] +
                            '_' + image_path_parsing[-1], synset_names,
                            draw_rgb, gt_bbox, gt_class_ids, gt_mask,
                            r['rois'], r['class_ids'], r['masks'], r['coords'],
                            r['scores'])
def test_cascade(
        classifier_file,
        icf_result_dir,
        checker,
        resultsFile):

    cascade_detector = classifier.loadCascadeClassifier(classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    resultsIcf = open(resultsFile + "cascade.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        before = time.time()
        pos_windows = test_img(cascade_detector, imgPath, imgScales, subwindow=boundingRect)
        after = time.time()

        print "Sample", sample, "time elapsed=", after-before

        if pos_windows != None and pos_windows != []:
            utils.draw_detections(img, pos_windows)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(imgPath, pos_windows)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        sample += 1

    resultsIcf.close()
Exemple #27
0
def test_multiscale_checker(hog_classifier_file, icf_classifier_file,
                            hog_result_dir, icf_result_dir, checker,
                            resultsFile):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    # icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    resultsHog = open(resultsFile + "hog.txt", "w")
    # resultsIcf = open(resultsFile + "icf.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        posWindowsHog = tester_hog.test_img(hog_classifier,
                                            imgPath,
                                            imgScales,
                                            subwindow=boundingRect)
        if posWindowsHog != None and posWindowsHog != []:
            utils.draw_detections(img, posWindowsHog)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(
            imgPath, posWindowsHog)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsHog.write(imgPath + " tp=" + str(truePositives) + " fp=" +
                         str(falsePositives) + " fn=" + str(falseNegative) +
                         "\n")

        # beforeIcf = time.time()
        # windowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect)
        # afterIcf = time.time()
        #
        # print "Sample", sample, "time elapsed=", afterIcf-beforeIcf
        #
        # if windowsIcf != None and windowsIcf != []:
        #     scale = windowsIcf[0][4]
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        #     utils.draw_detections(img_icf, windowsIcf)
        # else:
        #     scale = 0.5
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        # cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
        #
        # # Check detections
        # detections, truePositive = checker.checkDetections(imgPath, windowsIcf)
        # c = Counter(detections)
        # truePositives = c[True]
        # falsePositives = c[False]
        # falseNegative = 0 if truePositive else 1
        # resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        sample += 1
Exemple #28
0
def test_multiscale(hog_classifier_file, icf_classifier_file, hog_result_dir,
                    icf_result_dir, no_samples):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*filepaths)

    metadata = utils.parseMetadata(*filepaths)

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    for sample in range(0, no_samples):
        print "### Sample " + str(sample) + " ###"
        imgPath = random.choice(testImages)

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        prev_img_path = utils.get_prev_img(imgPath)
        prev_img = cv2.imread(prev_img_path)

        flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)

        height, width, _ = img.shape

        bestWindowsHog = tester_hog.test_img(hog_classifier,
                                             imgPath,
                                             imgScales,
                                             allPositive=True,
                                             flow_rgb=flow_rgb,
                                             subwindow=boundingRect)
        if bestWindowsHog != None and bestWindowsHog != []:
            scale = bestWindowsHog[0][4]
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_hog.shape[1],
                                                      img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

            utils.draw_detections(img_hog, bestWindowsHog)
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_hog.shape[1],
                                                      img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg",
                    img_hog)

        bestWindowsIcf = tester_icf.test_img(icf_classifier,
                                             imgPath,
                                             imgScales,
                                             allPositive=True,
                                             subwindow=boundingRect)
        if bestWindowsIcf != None and bestWindowsIcf != []:
            scale = bestWindowsIcf[0][4]
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_icf.shape[1],
                                                      img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

            utils.draw_detections(img_icf, bestWindowsIcf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect,
                                                      img_icf.shape[1],
                                                      img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x + w, y + h), (0, 0, 255),
                              thickness=2,
                              lineType=8)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg",
                    img_icf)
 def default_cb(frame, bboxes):
     utils.draw_detections(frame, bboxes)
     cv2.imshow(self.cam.name, frame)
    def save_training_FP_and_TP_helper(self,
                                       num_patches,
                                       img_name,
                                       detections,
                                       patches_path,
                                       general_path,
                                       img,
                                       roof_type,
                                       extraction_type,
                                       color,
                                       rects=False):
        #this is where we write the detections we're extraction. One image per roof type
        #we save: 1. the patches and 2. the image with marks of what the detections are, along with the true roofs (for debugging)
        img_debug = np.copy(img)
        if roof_type == 'background':
            utils.draw_detections(self.correct_roofs['metal'][img_name],
                                  img_debug,
                                  color=(0, 0, 0),
                                  thickness=2,
                                  rects=rects)
            utils.draw_detections(self.correct_roofs['thatch'][img_name],
                                  img_debug,
                                  color=(0, 0, 0),
                                  thickness=2,
                                  rects=rects)
        else:
            utils.draw_detections(self.correct_roofs[roof_type][img_name],
                                  img_debug,
                                  color=(0, 0, 0),
                                  thickness=2,
                                  rects=rects)

        for i, detection in enumerate(detections):
            batch_path = 'batch{}/'.format(int(num_patches / 20000))
            if num_patches % 20000 == 0:
                utils.mkdir('{}falsepos/batch{}/'.format(
                    general_path, num_patches / 20000))
                utils.mkdir('{}truepos/batch{}/'.format(
                    general_path, num_patches / 20000))
            num_patches += 1

            current_patch_path = patches_path + batch_path

            #extract the patch, rotate it to a horizontal orientation, save it
            if rects == False:
                bitmap = np.zeros((img.shape[:2]), dtype=np.uint8)
                padded_detection = utils.add_padding_polygon(detection, bitmap)
                warped_patch = utils.four_point_transform(
                    img, padded_detection)
                cv2.imwrite(
                    '{0}{1}_{2}_roof{3}.jpg'.format(current_patch_path,
                                                    roof_type, img_name[:-4],
                                                    i), warped_patch)

                #mark where roofs where taken out from for debugging
                utils.draw_polygon(padded_detection,
                                   img_debug,
                                   fill=False,
                                   color=color,
                                   thickness=2,
                                   number=i)
            else:
                pad = 10
                xmin = (detection.xmin -
                        pad) if (detection.xmin - pad) > 0 else detection.xmin
                ymin = (detection.ymin -
                        pad) if (detection.ymin - pad) > 0 else detection.ymin
                xmax = (detection.xmax +
                        pad) if (detection.xmax +
                                 pad) < img.shape[1] else detection.xmax
                ymax = (detection.ymax +
                        pad) if (detection.ymax +
                                 pad) < img.shape[0] else detection.ymax
                patch = img[ymin:ymax, xmin:xmax, :]
                #print 'saving {0}{1}_{2}_roof{3}.jpg'.format(current_patch_path, roof_type, img_name[:-4], i)
                cv2.imwrite(
                    '{0}{1}_{2}_roof{3}.jpg'.format(current_patch_path,
                                                    roof_type, img_name[:-4],
                                                    i), patch)
                self.TOTAL += 1
                if self.TOTAL % 1000 == 0:
                    print 'Saved {} patches'.format(self.TOTAL)
        return num_patches
        img_index = -1
        for i, img_path in enumerate(train_imgs):
            cumulative_metal += roof_dict[img_path][0]
            cumulative_thatch += roof_dict[img_path][1]
            if (cumulative_metal>metal_40 and cumulative_thatch>metal_40) and (cumulative_metal<metal_60 and cumulative_thatch<thatch_60):
                img_index = i
                break
        assert img_index != -1
        return train_imgs[:i+1], train_imgs[i+1:], total_metal-cumulative_metal, total_thatch-cumulative_thatch, cumulative_metal, cumulative_thatch 



if __name__ == '__main__':
    img_list = [img_name for img_name in os.listdir('../data_original/small_test/') if img_name.endswith('.jpg')]
    for img_name in img_list:    
        img = cv2.imread('../data_original/small_test/'+img_name)
        xml_name = img_name[:-3]+'xml'
        xml_path = '../data_original/small_test/'

        fixed_list = DataLoader.get_polygons(roof_type='metal', xml_name=xml_name, xml_path=xml_path, fix_polygons=True, padding=30)  
        non_fixed = DataLoader.get_polygons(roof_type='metal', xml_name=xml_name, xml_path=xml_path, fix_polygons=False)
        utils.draw_detections(fixed_list, img, color=(0, 255, 0))
        utils.draw_detections(non_fixed, img, color=(0, 0, 255))

        cv2.imwrite('delete_'+img_name, img)
         




Exemple #32
0
frm_list = frm_list[0:n_frame]
i = -1
for frm in frm_list:
    i += 1
    found_true = true_detection_list[i]
    found_filtered = []
    found, w = hog_obj.detectMultiScale(frm, **hog_par)
    for r in found:
        inside = False
        for q in found:
            if utils.is_inside(r, q):
                inside = True
                break
        if not inside:
            found_filtered.append(r)
    found_hog = utils.rescale(found_filtered)
    hog_detection_list.append(found_hog)
    if do_draw:
        frm = cv2.cvtColor(frm, cv2.COLOR_GRAY2BGR)
        # utils.draw_detections(frm, found, (0, 0, 255), 1)
        # utils.draw_detections(frm, found_filtered, (0, 0, 255), 3)
        utils.draw_detections(frm, found_hog, (0, 0, 255), 3)
        utils.draw_detections(frm, found_true, (0, 255, 0), 1)
        cv2.imshow('People Detector', frm)
        cv2.waitKey()

"""Performances indices"""
if do_perf:
    precision, recall, f_score = utils.get_performances(hog_detection_list, true_detection_list, n_frame)
    print precision, recall, f_score
Exemple #33
0
def test_multiscale(
        hog_classifier_file,
        icf_classifier_file,
        hog_result_dir,
        icf_result_dir,
        no_samples):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*filepaths)

    metadata = utils.parseMetadata(*filepaths)

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    for sample in range(0, no_samples):
        print "### Sample " + str(sample) + " ###"
        imgPath = random.choice(testImages)

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        prev_img_path = utils.get_prev_img(imgPath)
        prev_img = cv2.imread(prev_img_path)

        flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)

        height, width, _ = img.shape

        bestWindowsHog = tester_hog.test_img(hog_classifier, imgPath, imgScales, allPositive=True, flow_rgb=flow_rgb, subwindow=boundingRect)
        if bestWindowsHog != None and bestWindowsHog != []:
            scale = bestWindowsHog[0][4]
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

            utils.draw_detections(img_hog, bestWindowsHog)
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale)
                cv2.rectangle(img_hog, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img_hog)

        bestWindowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect)
        if bestWindowsIcf != None and bestWindowsIcf != []:
            scale = bestWindowsIcf[0][4]
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)

            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

            utils.draw_detections(img_icf, bestWindowsIcf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            if boundingRect != None:
                x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale)
                cv2.rectangle(img_icf, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
Exemple #34
0
def test_multiscale_checker(
        hog_classifier_file,
        icf_classifier_file,
        hog_result_dir,
        icf_result_dir,
        checker,
        resultsFile):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    # icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    resultsHog = open(resultsFile + "hog.txt", "w")
    # resultsIcf = open(resultsFile + "icf.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        posWindowsHog = tester_hog.test_img(hog_classifier, imgPath, imgScales, subwindow=boundingRect)
        if posWindowsHog != None and posWindowsHog != []:
            utils.draw_detections(img, posWindowsHog)

        cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(imgPath, posWindowsHog)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsHog.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        # beforeIcf = time.time()
        # windowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect)
        # afterIcf = time.time()
        #
        # print "Sample", sample, "time elapsed=", afterIcf-beforeIcf
        #
        # if windowsIcf != None and windowsIcf != []:
        #     scale = windowsIcf[0][4]
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        #     utils.draw_detections(img_icf, windowsIcf)
        # else:
        #     scale = 0.5
        #     img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        # cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
        #
        # # Check detections
        # detections, truePositive = checker.checkDetections(imgPath, windowsIcf)
        # c = Counter(detections)
        # truePositives = c[True]
        # falsePositives = c[False]
        # falseNegative = 0 if truePositive else 1
        # resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" + str(falsePositives) + " fn=" + str(falseNegative) + "\n")

        sample += 1
Exemple #35
0
    def detect_roofs(self, img_name, in_path=None):
        in_path = self.in_path if in_path is None else in_path
        try:
            rgb_unrotated = cv2.imread(in_path + img_name,
                                       flags=cv2.IMREAD_COLOR)
            gray = cv2.cvtColor(rgb_unrotated, cv2.COLOR_BGR2GRAY)
            gray = cv2.equalizeHist(gray)

            if self.downsized:
                rgb_unrotated = utils.resize_rgb(rgb_unrotated,
                                                 h=rgb_unrotated.shape[0] / 2,
                                                 w=rgb_unrotated.shape[1] / 2)
                gray = utils.resize_grayscale(gray,
                                              w=gray.shape[1] / 2,
                                              h=gray.shape[0] / 2)

        except IOError as e:
            print e
            sys.exit(-1)
        else:
            for roof_type, detectors in self.roof_detectors.iteritems():
                for i, detector in enumerate(detectors):
                    for angle in self.angles:
                        #for thatch we only need one angle
                        if self.rotate_detectors[i] == False and angle > 0 or (
                                roof_type == 'thatch' and angle > 0
                        ):  #roof_type == 'thatch' and angle>0:
                            continue

                        print 'Detecting with detector: ' + str(i)
                        print 'ANGLE ' + str(angle)

                        with Timer() as t:
                            rotated_image = utils.rotate_image(
                                gray, angle) if angle > 0 else gray
                            delete_image = utils.rotate_image_RGB(
                                rgb_unrotated, angle) if angle > 0 else gray
                            detections, _ = self.detect_and_rectify(
                                detector,
                                rotated_image,
                                angle,
                                rgb_unrotated.shape[:2],
                                rgb_rotated=delete_image)
                            if self.downsized:
                                detections = detections * 2
                            self.viola_detections.set_detections(
                                roof_type=roof_type,
                                img_name=img_name,
                                angle=angle,
                                detection_list=detections,
                                img=rotated_image)

                        print 'Time detection: {0}'.format(t.secs)
                        self.viola_detections.total_time += t.secs
                        if DEBUG:
                            rgb_to_write = cv2.imread(in_path + img_name,
                                                      flags=cv2.IMREAD_COLOR)
                            utils.draw_detections(detections,
                                                  rgb_to_write,
                                                  color=(255, 0, 0))
                            cv2.imwrite(
                                '{0}{3}{1}_{2}.jpg'.format(
                                    '', img_name[:-4], angle, roof_type),
                                rgb_to_write)
            return rgb_unrotated
                img_index = i
                break
        assert img_index != -1
        return train_imgs[:i + 1], train_imgs[
            i +
            1:], total_metal - cumulative_metal, total_thatch - cumulative_thatch, cumulative_metal, cumulative_thatch

if __name__ == '__main__':
    img_list = [
        img_name for img_name in os.listdir('../data_original/small_test/')
        if img_name.endswith('.jpg')
    ]
    for img_name in img_list:
        img = cv2.imread('../data_original/small_test/' + img_name)
        xml_name = img_name[:-3] + 'xml'
        xml_path = '../data_original/small_test/'

        fixed_list = DataLoader.get_polygons(roof_type='metal',
                                             xml_name=xml_name,
                                             xml_path=xml_path,
                                             fix_polygons=True,
                                             padding=30)
        non_fixed = DataLoader.get_polygons(roof_type='metal',
                                            xml_name=xml_name,
                                            xml_path=xml_path,
                                            fix_polygons=False)
        utils.draw_detections(fixed_list, img, color=(0, 255, 0))
        utils.draw_detections(non_fixed, img, color=(0, 0, 255))

        cv2.imwrite('delete_' + img_name, img)
frm_list = frm_list[0:n_frame]
i = -1
for frm in frm_list:
    i += 1
    found_true = true_detection_list[i]
    found_filtered = []
    found, w = hog_obj.detectMultiScale(frm, **hog_par)
    for r in found:
        inside = False
        for q in found:
            if utils.is_inside(r, q):
                inside = True
                break
        if not inside:
            found_filtered.append(r)
    found_hog = utils.rescale(found_filtered)
    hog_detection_list.append(found_hog)
    if do_draw:
        frm = cv2.cvtColor(frm, cv2.COLOR_GRAY2BGR)
        # utils.draw_detections(frm, found, (0, 0, 255), 1)
        # utils.draw_detections(frm, found_filtered, (0, 0, 255), 3)
        utils.draw_detections(frm, found_hog, (0, 0, 255), 3)
        utils.draw_detections(frm, found_true, (0, 255, 0), 1)
        cv2.imshow('People Detector', frm)
        cv2.waitKey()
"""Performances indices"""
if do_perf:
    precision, recall, f_score = utils.get_performances(
        hog_detection_list, true_detection_list, n_frame)
    print precision, recall, f_score
    def collect_features(self):
        """Collect features from chosen target for tracking."""
        ret, self.frame = self.get_frame()
        if self.frame is None:
            return True
        vis = self.frame.copy()
        # Detect people
        dets = self.detector.detect(self.frame)

        if not len(dets):
            return True

        utils.draw_detections(vis, dets)

        # Find center detection
        h, w, _ = self.frame.shape
        center_x = w//2
        center_y = h//2
        det = find_detection(
            dets, center_x, center_y)
        # Detection might not be centered
        if det is None:
            return True

        # Reduce area of the detection
        det = reduce_area_of_detection(
            det, self.args.width_multiplier,
            self.args.height_multiplier)

        # Create mask with detection
        mask = create_upper_mask(det, self.frame.shape[:2])
        kps, des = extract_features(self.frame,
                                    self.args.n_features,
                                    self.args.ftype,
                                    mask)
        if not len(kps):
            return True

        # Keep center features only
        kps, des = filter_features(kps, des, det, self.args.distance)

        # Draw features
        for i, kp in enumerate(kps):
            cv2.circle(vis, tuple(kps[i]), 2, (255, 165, 0), -1)

        # check matches to reduce duplicates
        # and to collect features evenly
        if len(self.tdes) and len(des):
            idx1, idx2 = match_features(
                np.array(self.tdes, dtype=np.uint8), des)
            des = np.delete(des, idx2, 0)
            kps = np.delete(kps, idx2, 0)

        # Populate the tracked features
        if len(des):
            self.tkps.extend(kps)
            self.tdes.extend(des)

        # break if threshold is satisfied
        utils.draw_str(vis, (20, 20), 'Features: %d' % len(self.tkps))
        if len(self.tkps) > self.args.n_tracked:
            return False

        if not self.args.no_gui:
            cv2.imshow(self.args.window_name, vis)
            if cv2.waitKey(1) == 27:
                cv2.destroyAllWindows()
                return False

        self.collection_output()

        return True
Exemple #39
0
def test_multiscale(
        hog_classifier_file,
        icf_classifier_file,
        imgpaths,
        kinect_detections,
        hog_result_dir,
        icf_result_dir):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*imgpaths)
    metadata = utils.parseMetadata(*imgpaths)

    kinect_ts = read_kinect_metadata(kinect_detections)

    print metadata
    print kinect_ts

    scales = [
        [0.45, 0.5, 0.55],
        [0.4, 0.45, 0.5],
        [0.3, 0.35],
        [0.3]
    ]
    scaleSteps = [35, 45, 65, 90]

    for kinectts in kinect_ts:
        sel = min(metadata.items(), key=lambda mt: abs(kinectts - mt[1]['time']))
        print str(kinectts) + " " + str(sel[1]['time'])

        imgPath = sel[0]
        imgTs = sel[1]['time']

        print "### Sample " + str(imgPath) + " ###"

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])

        print tilt

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        bestWindowHog = tester_hog.test_img(hog_classifier, imgPath, imgScales)
        if bestWindowHog != None:
            scale = bestWindowHog[4]
            print "best scale hog = " + str(scale)
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_hog, [bestWindowHog[0:4]])
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        cv2.imwrite(hog_result_dir + "/" + str(imgTs) + ".jpg", img_hog)

        bestWindowIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales)
        if bestWindowIcf != None:
            scale = bestWindowIcf[4]
            print "best scale icf = " + str(scale)
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_icf, [bestWindowIcf[0:4]])
            cv2.imshow("icf", img_icf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            cv2.imshow("icf", img_icf)
        cv2.imwrite(icf_result_dir + "/" + str(imgTs) + ".jpg", img_icf)
Exemple #40
0
            self.detector = HOGDetector()
        elif args.detector == 'mobilenet':
            from mobilenetssd import MobileNetSSD
            self.detector = MobileNetSSD(args)

    def detect(self, frame):
        dets = self.detector.detect(frame)
        return dets


if __name__ == '__main__':
    import cv2

    from options import args
    from utils import draw_detections
    import time

    cap = cv2.VideoCapture(args.input)

    detector = Detector(args)

    ret, frame = cap.read()
    time.sleep(0.5)
    while ret:
        dets = detector.detect(frame)
        draw_detections(frame, dets)
        cv2.imshow('cvwindow', frame)
        if cv2.waitKey(1) == 27:
            break
        ret, frame = cap.read()
def test_cascade(classifier_file, icf_result_dir, checker, resultsFile):

    cascade_detector = classifier.loadCascadeClassifier(classifier_file)

    filepaths = [
        "/home/mataevs/captures/metadata/dump_05_05_01_50",
        "/home/mataevs/captures/metadata/dump_05_06_13_10",
        "/home/mataevs/captures/metadata/dump_10_06_11_47",
        "/home/mataevs/captures/metadata/dump_05_05_01_51",
        "/home/mataevs/captures/metadata/dump_05_06_13_15",
        "/home/mataevs/captures/metadata/dump_07_05_11_40",
        "/home/mataevs/captures/metadata/dump_10_06_11_48",
        "/home/mataevs/captures/metadata/dump_05_05_11_54",
        "/home/mataevs/captures/metadata/dump_05_06_13_20",
        "/home/mataevs/captures/metadata/dump_07_05_11_46",
        "/home/mataevs/captures/metadata/dump_10_06_12_16",
        "/home/mataevs/captures/metadata/dump_05_06_12_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_21",
        "/home/mataevs/captures/metadata/dump_05_06_13_24",
        "/home/mataevs/captures/metadata/dump_16_06_14_57",
        "/home/mataevs/captures/metadata/dump_05_06_13_25",
        "/home/mataevs/captures/metadata/dump_07_05_12_03",
        "/home/mataevs/captures/metadata/dump_16_06_15_26",
        "/home/mataevs/captures/metadata/dump_05_06_13_28",
        "/home/mataevs/captures/metadata/dump_07_05_12_05"
    ]

    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    metadata = utils.parseMetadata(*filepaths)

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    resultsIcf = open(resultsFile + "cascade.txt", "w")

    sample = 0
    for imgPath in checker.getFileList():

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])
        if tilt > 90:
            tilt = 90 - (tilt - 90)

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        #prev_img_path = utils.get_prev_img(imgPath)
        # prev_img = cv2.imread(prev_img_path)

        # flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img)
        flow_rgb, boundingRect = None, None

        height, width, _ = img.shape

        before = time.time()
        pos_windows = test_img(cascade_detector,
                               imgPath,
                               imgScales,
                               subwindow=boundingRect)
        after = time.time()

        print "Sample", sample, "time elapsed=", after - before

        if pos_windows != None and pos_windows != []:
            utils.draw_detections(img, pos_windows)

        cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img)

        # Check detections
        detections, truePositive = checker.checkDetections(
            imgPath, pos_windows)
        c = Counter(detections)
        truePositives = c[True]
        falsePositives = c[False]
        falseNegative = 0 if truePositive else 1
        resultsIcf.write(imgPath + " tp=" + str(truePositives) + " fp=" +
                         str(falsePositives) + " fn=" + str(falseNegative) +
                         "\n")

        sample += 1

    resultsIcf.close()
Exemple #42
0
def test_multiscale(hog_classifier_file, icf_classifier_file, imgpaths,
                    kinect_detections, hog_result_dir, icf_result_dir):
    hog_classifier = tester_hog.load_classifier(hog_classifier_file)
    icf_classifier = tester_icf.load_classifier(icf_classifier_file)

    if not os.path.exists(hog_result_dir):
        os.makedirs(hog_result_dir)
    if not os.path.exists(icf_result_dir):
        os.makedirs(icf_result_dir)

    testImages = utils.getFullImages(*imgpaths)
    metadata = utils.parseMetadata(*imgpaths)

    kinect_ts = read_kinect_metadata(kinect_detections)

    print metadata
    print kinect_ts

    scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]]
    scaleSteps = [35, 45, 65, 90]

    for kinectts in kinect_ts:
        sel = min(metadata.items(),
                  key=lambda mt: abs(kinectts - mt[1]['time']))
        print str(kinectts) + " " + str(sel[1]['time'])

        imgPath = sel[0]
        imgTs = sel[1]['time']

        print "### Sample " + str(imgPath) + " ###"

        img = cv2.imread(imgPath)

        tilt = int(metadata[imgPath]['tilt'])

        print tilt

        imgScales = []
        for i in range(0, len(scaleSteps)):
            if tilt < scaleSteps[i]:
                imgScales = scales[i]
                break

        print imgScales

        bestWindowHog = tester_hog.test_img(hog_classifier, imgPath, imgScales)
        if bestWindowHog != None:
            scale = bestWindowHog[4]
            print "best scale hog = " + str(scale)
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_hog, [bestWindowHog[0:4]])
        else:
            scale = 0.5
            img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale)
        cv2.imwrite(hog_result_dir + "/" + str(imgTs) + ".jpg", img_hog)

        bestWindowIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales)
        if bestWindowIcf != None:
            scale = bestWindowIcf[4]
            print "best scale icf = " + str(scale)
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            utils.draw_detections(img_icf, [bestWindowIcf[0:4]])
            cv2.imshow("icf", img_icf)
        else:
            scale = 0.5
            img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale)
            cv2.imshow("icf", img_icf)
        cv2.imwrite(icf_result_dir + "/" + str(imgTs) + ".jpg", img_icf)