def extractInfo(self):
     # times = []
     try:
         while not self.exit:
             try:
                 frame = self.frame_queue.get(block=True, timeout=1)
             except queue.Empty:
                 print("Frame queue empty")
                 continue
             # 1 ms per loop
             # TODO: check that this conversion is not needed
             # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
             if self.debug:
                 self.out_queue.put(item=frame, block=False)
             else:
                 if self.frame_num % SAVE_EVERY == 0:
                     cv2.imwrite(
                         "debug/{}_{}.jpg".format(experiment_time,
                                                  self.frame_num), frame)
                 try:
                     # 10 ms per loop
                     # start_time = time.time()
                     turn_percent, centroids = processImage(frame)
                     # times.append(time.time() - start_time)
                     self.out_queue.put(item=(turn_percent, centroids),
                                        block=False)
                 except Exception as e:
                     print("Exception in RBGAnalyser processing image: {}".
                           format(e))
             self.frame_num += 1
     except Exception as e:
         print("Exception in RBGAnalyser after loop: {}".format(e))
Example #2
0
 def extractInfo(self):
     try:
         while not self.exit:
             try:
                 frame = self.frame_queue.get(block=True, timeout=1)
             except queue.Empty:
                 print("Queue empty")
                 continue
             frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
             if self.debug:
                 self.out_queue.put(item=frame, block=False)
             else:
                 if self.frame_num % SAVE_EVERY == 0:
                     cv2.imwrite("debug/{}_{}.jpg".format(experiment_time, self.frame_num), frame)
                     pass
                 try:
                     turn_percent, centroids = processImage(frame)
                     self.out_queue.put(item=(turn_percent, centroids), block=False)
                 except Exception as e:
                     print("Exception in RBGAnalyser processing image: {}".format(e))
             self.frame_num += 1
     except Exception as e:
         print("Exception in RBGAnalyser after loop: {}".format(e))
j = 0
should_exit = False
for idx, name in enumerate(images):
    if should_exit:
        break
    img = cv2.imread('{}/{}'.format(input_folder, name))
    print(name)

    original_img = img.copy()
    for i, r in enumerate(REGIONS):
        # img will be modified by processImage()
        img = original_img.copy()
        margin_left, margin_top, _, _ = r
        im_cropped = original_img[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])]
        centroids, errors, exit_loop = processImage(img, debug=True, regions=[r], interactive=True)
        if not all(errors):
            # Save the labeled image (and store the label in the name)
            x, y = centroids.flatten()

            cx, cy = x - margin_left, y - margin_top
            output_name = '{}'.format(j)
            idx = '{}_r{}'.format(name, i)
            infos_dict['images'][idx]['input_image'] = name
            infos_dict['images'][idx]['label'] = [cx, cy]
            infos_dict['images'][idx]['region'] = list(r)
            infos_dict['images'][idx]['output_name'] = output_name
            cv2.imwrite('{}/{}.jpg'.format(output_folder, output_name), im_cropped)
            # Update infos
            with open('{}/infos.pkl'.format(output_folder), 'wb') as f:
                # protocol=2 for python 2 compatibility
    default=1,
    type=int)
args = parser.parse_args()

if args.input_image != "" or args.folder != "":
    imgs = [args.input_image]
    if args.folder != "":
        imgs = [
            args.folder + '/' + im for im in os.listdir(args.folder)
            if im.split('.')[-1] in images
        ]
    current_idx = 0
    while True:
        img = cv2.imread(imgs[current_idx])
        # r = [margin_left, margin_top, width, height]
        regions = None
        if args.regions == 0:
            regions = [[0, 0, img.shape[1], img.shape[0]]]

        processImage(img, debug=True, regions=regions)

        # Retrieve pressed key
        key = cv2.waitKey(0) & 0xff

        if key in EXIT_KEYS:
            cv2.destroyAllWindows()
            exit()
        elif key in [LEFT_KEY, RIGHT_KEY]:
            current_idx += 1 if key == RIGHT_KEY else -1
            current_idx = np.clip(current_idx, 0, len(imgs) - 1)
Example #5
0
from opencv.image_processing import processImage

N_ITER = 5000

parser = argparse.ArgumentParser(
    description='Benchmark line detection algorithm')
parser.add_argument('-i',
                    '--input_image',
                    help='Input Image',
                    default="",
                    type=str,
                    required=True)
args = parser.parse_args()

image = cv2.imread(args.input_image)
time_deltas = []
for i in range(N_ITER):
    start_time = time.time()
    turn_percent, centroids = processImage(image,
                                           debug=False,
                                           regions=None,
                                           interactive=False)
    time_deltas.append(time.time() - start_time)
    # print(centroids)

time_deltas = np.array(time_deltas)
print("Total time: {:.6f}s".format(time_deltas.sum()))
print("Mean time: {:.6f}s".format(time_deltas.mean()))
print("Std time: {:.6f}s".format(time_deltas.std()))
print("Median time: {:.6f}s".format(np.median(time_deltas)))