def postprocess_test_prototxt():
  '''
  Prepares final version of quantized prototxt
  replacing real paths and batch size to respective ck-variables.
  '''
  net = utils.read_prototxt(DST_TEST_PROTOTXT_FILE)

  for layer in net.layer:
    if layer.name == 'data':
      layer.data_param.source = '$#val_lmdb#$'
      layer.data_param.batch_size = 0
      layer.annotated_data_param.label_map_file = '$#path_to_labelmap#$'
    elif layer.name == 'detection_out':
      p = layer.detection_output_param.save_output_param
      p.label_map_file = '$#path_to_labelmap#$'
      p.name_size_file = '$#path_to_name_size#$'
    elif layer.name == 'detection_eval':
      layer.detection_evaluate_param.name_size_file = '$#path_to_name_size#$'

  # We cant insert strings into integer field using caffe_pb2 
  # as it's type safe, so do it with plain string replacement
  txt = text_format.MessageToString(net)
  txt = txt.replace('batch_size: 0', 'batch_size: $#val_batch_size#$')
  txt = txt.replace('num_test_image: 0', 'num_test_image: $#num_test_image#$')

  utils.write_text(DST_TEST_PROTOTXT_FILE, txt)
Exemple #2
0
 def update_info(self):
     for widget in self.frame_1_1.winfo_children():
         widget.destroy()
     write_text(
         self.frame_1_1,
         "Dataset variant: {}, \n DeepLabv3+ encoder: {}".format(
             self.current_variant, self.current_encoder))
Exemple #3
0
 def detect_correct_word(self):
     if not self.error and self.text[-1] == ' ':
         root = find_parent(self, TenFingersPlay)
         root.score += 1
         root.original_text = ''.join(root.original_text.split(' ', 1)[1:])
         root.initial_index = 0
         self.text = ''
         write_text(root)
Exemple #4
0
def main():
    images, ids, rotates = load_data()
    ######
    # FLIP
    flip_images, flip_ids = [], []
    for image, _id, rotated in zip(images, ids, rotates):
        h, w, c = image.shape
        skip_flip = 0.8333 < (h / w) < 1.2
        if not skip_flip and rotated:
            flip_images.append(image)
            flip_ids.append(_id)

    flip_retriever = FlipRetriever(flip_ids, flip_images)
    flip_loader = DataLoader(
        flip_retriever,
        batch_size=BS,
        num_workers=NUM_WORKERS,
        sampler=SequentialSampler(flip_retriever),
        pin_memory=False,
    )
    flip_model = get_flip_model()
    flip_predictor = FlipPredictor(model=flip_model, device=DEVICE)
    flip_predictions = flip_predictor.run_inference(flip_loader)

    ocr_images, ocr_ids = [], []
    for image, _id in zip(images, ids):
        flip_prediction = flip_predictions.get(_id)
        if flip_prediction is not None:
            if flip_prediction['pred_vert']:
                image = cv2.flip(image, 0)
            if flip_prediction['pred_hori']:
                image = cv2.flip(image, 1)
        ocr_images.append(image)
        ocr_ids.append(_id)
    #####

    #####
    # OCR
    ocr_retriever = OCRRetriever(ocr_ids, ocr_images)
    ocr_loader = DataLoader(
        ocr_retriever,
        batch_size=BS,
        num_workers=NUM_WORKERS,
        sampler=SequentialSampler(ocr_retriever),
        pin_memory=False,
    )
    # ------------------------
    # RESNET34
    predictor = OCRPredictor(model=get_resnet34_model(), device=DEVICE)
    ocr_predictions = predictor.run_inference(ocr_loader)

    ######
    # WRITE RESULTS
    for ocr_prediction in ocr_predictions:
        file_path = os.path.join(OUT_FOLDER,  f'{ocr_prediction["id"]}.txt')
        write_text(file_path, CTC_LABELING.decode(ocr_prediction['raw_output']))
Exemple #5
0
    def insert_text(self, substring, from_undo=False):
        root = find_parent(self, TenFingersPlay)

        if check_char(get_char(root.original_text, root.initial_index),
                      substring) and not self.error:
            write_text(root, SUCCESS)
            if not self.is_forward:
                self.is_forward = True
            root.initial_index += 1
            self.error = False
        else:
            self.error = True
            write_text(root, ERROR)
        super(TenFingerInputText, self).insert_text(substring,
                                                    from_undo=from_undo)
        self.detect_correct_word()
        self.focus = True
def make_deploy_prototxt():
  '''
  Makes deploy.prototxt model based on test.prototxt.
  The differences between them are first and last layers.
  '''
  net = utils.read_prototxt(DST_TEST_PROTOTXT_FILE)

  # Remove first and last layers
  layers_to_remove = []
  for layer in net.layer:
    if layer.name == 'data' or layer.name == 'detection_eval':
      layers_to_remove.append(layer)
  for layer in layers_to_remove:
    net.layer.remove(layer)

  # Prepare standart input
  net.input.append('data')
  shape = net.input_shape.add()
  shape.dim.append(0)
  shape.dim.append(3)
  shape.dim.append(TARGET_IMG_H)
  shape.dim.append(TARGET_IMG_W)

  # Not sure why there params should be removed,
  # but they are not presented in deploy.prototxt from packages
  # caffemodel-ssd-coco-* and caffemodel-ssd-voc-*
  for layer in net.layer:
    if layer.name == 'detection_out':
      p = layer.detection_output_param.save_output_param
      p.label_map_file = '$#path_to_labelmap#$'
      p.output_name_prefix = ''
      p.output_format = ''
      p.name_size_file = ''
      p.num_test_image = 0

  # We can't insert strings into integer field using caffe_pb2 
  # as it's type safe, so do it with plain string replacement
  txt = text_format.MessageToString(net)
  txt = txt.replace('dim: 0', 'dim: $#batch_size#$', 1)

  utils.write_text(DST_DEPLOY_PROTOTXT_FILE, txt)
Exemple #7
0
    def segment_image(self):

        inference_dir = "./inference"
        common_path = "../checkpoints_logs/train_logs/"

        # graphs = {"MobileNetv2": {"full": "mobileNet/full_final_01/mobileNet_full.pb",
        #                           "size_invariant": "mobileNet/size_invariant_01/mobileNet_size.pb",
        #                           "similar_shapes": "mobileNet/similar_shapes_01/mobileNet_shape.pb",
        #                           "binary": "mobileNet/binary_final_02/mobileNet_binary.pb"},
        #           "Xception": {"full": "xception/full_final_01/xception_full.pb",
        #                        "size_invariant": "xception/size_invariant_final_01/xception_size.pb",
        #                        "similar_shapes": "xception/similar_shapes_final_01/xception_shape.pb",
        #                        "binary": "xception/binary_final_01/xception_binary.pb"}}

        graphs = {
            "MobileNetv2": {
                "full": "mobileNet/alldata/full_01/mobileNet_full.pb",
                "size_invariant":
                "mobileNet/alldata/size_01/mobileNet_size.pb",
                "similar_shapes":
                "mobileNet/alldata/shape_01/mobileNet_shape.pb",
                "binary": "mobileNet/alldata/binary_01/mobileNet_binary.pb"
            },
            "Xception": {
                "full": "xception/alldata/full_01/xception_full.pb",
                "size_invariant": "xception/alldata/size_01/xception_size.pb",
                "similar_shapes":
                "xception/alldata/shape_01/xception_shape.pb",
                "binary": "xception/alldata/binary_01/xception_binary.pb"
            }
        }
        inference_graph.FLAGS.image_path = self.image_path
        inference_graph.FLAGS.graph_path = os.path.join(
            common_path, graphs[self.current_encoder][self.current_variant])
        inference_graph.FLAGS.inference_dir = inference_dir
        inference_time = inference_graph.main(None)
        write_text(self.window,
                   "Inference time: {0:.2f} s".format(inference_time),
                   row=4,
                   column=1)
        self.display_results(self.image_path, inference_dir)
Exemple #8
0
    def do_backspace(self, from_undo=False, mode='bkspc'):
        try:
            super(TenFingerInputText, self).do_backspace(from_undo=from_undo,
                                                         mode='bkspc')
        except IndexError:
            return
        root = find_parent(self, TenFingersPlay)
        if self.is_forward:
            self.is_forward = False
        if not self.error:
            root.initial_index = max(0, root.initial_index - 1)

        if not root.original_text[:root.initial_index]:
            color = BLACK
            self.error = False
        elif check_wordish(root.original_text[:root.initial_index + 1],
                           self.text):
            color = SUCCESS
            self.error = False
        else:
            color = ERROR
            self.error = True
        write_text(root, color)
        self.focus = True
Exemple #9
0
def main(tracking=False):
    net = cv2.dnn.readNetFromCaffe(PROTOTXT, MODEL)

    print("[INFO] starting video stream...")
    cap = cv2.VideoCapture(0)
    # cap.open("{}://{}:{}@{}:{}".format(proto, user, password, ip, port))

    # cnt = 0
    while (True):
        start_time = time.time()

        ret, frame = cap.read()
        # frame = imutils.resize(frame, width=400)

        ################ PROCESS ##########################
        # detect
        list_face_bbox, list_score, list_classes = detect_face_ssd(
            frame, PROTOTXT, MODEL)
        for index in list_face_bbox:
            if index[2] > frame.shape[1] - 10:
                print('index fail', index, frame.shape[1])
                index[2] = frame.shape[1] - 20
        # tracking
        if tracking:

            features = encoder(frame, list_face_bbox)
            detections = [
                Detection(bbox, 1.0, cls, feature) for bbox, _, cls, feature in
                zip(list_face_bbox, list_score, list_classes, features)
            ]
            # run nms
            # Run non-maxima suppression.
            boxes = np.array([d.tlwh for d in detections])
            # boxes = np.array([d for d in detections])
            scores = np.array([d.confidence for d in detections])
            indices = preprocessing.non_max_suppression(
                boxes, cfg.DEEPSORT.NMS_MAX_OVERLAP, scores)
            detections = [detections[i] for i in indices]
            if len(list_face_bbox) != 0:
                tracker.predict()
                tracker.update(detections)
            else:
                tracker.predict()

                # tracker.update(detections)
            id = []
            list_bbox = []
            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                # bbox = track.mean[:4].copy()
                bbox = track.to_tlbr()
                id.append(track.track_id)
                list_bbox.append(bbox)
                # draw track
                # frame = draw_bbox_maxmin(frame, list_face_bbox, True, track.track_id)
            # print(list_bbox,list_face_bbox)
            list_bbox = np.array(list_bbox).astype('int32')
            print(id)
            for i, box in enumerate(list_bbox):
                if id == [] or len(id) != len(list_bbox):
                    break
                frame = draw_bbox_maxmin(frame, box, True, id[i])

        else:
            for face_bbox, face_score in zip(list_face_bbox, list_score):
                text = "{:.2f}%".format(face_score * 100)

                frame = draw_bbox_maxmin(frame, face_bbox)
                frame = write_text(frame, text, face_bbox[0], face_bbox[1] - 3)

        # cal fps
        fps = round(1.0 / (time.time() - start_time), 2)
        frame = write_text(frame, "fps: {}".format(fps), 5, 15)

        cv2.imshow("Frame", frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
Exemple #10
0
    box_lst, cls_lst, score_lst = box_ten[0].tolist(), cls_ten[0].tolist(
    ), score_ten[0].tolist()

    # clamp outside image
    box_lst = [
        list(map(lambda x: max(0, min(x, args.imsize)), box))
        for box in box_lst
    ]

    # draw box, class and score per prediction
    for i, (box, cls, score) in enumerate(zip(box_lst, cls_lst, score_lst)):
        img_pil = draw_box(img_pil, box, color=CLASS2COLOR[cls])
        if args.fontsize > 0:
            text = '%s: %1.2f' % (INDEX2CLASS[cls], score)
            coord = [box[0], box[1] - args.fontsize]
            img_pil = write_text(img_pil, text, coord, fontsize=args.fontsize)

        # resize origin scale of image
        xmin, ymin, xmax, ymax = box
        xmin = xmin * origin_size[0] / args.imsize
        ymin = ymin * origin_size[1] / args.imsize
        xmax = xmax * origin_size[0] / args.imsize
        ymax = ymax * origin_size[0] / args.imsize

        print(
            '%s: Index: %3d, Class: %7s, Score: %1.2f, Box: %4d, %4d, %4d, %4d'
            %
            (time.ctime(), i, INDEX2CLASS[cls], score, xmin, ymin, xmax, ymax))

    # resize to origin size and save the result image
    img_pil.resize(origin_size).save(os.path.join(args.save_path, 'image.png'))
Exemple #11
0
    def __init__(self, window, window_title, video_source=0):

        self.window = window
        self.window.title(window_title)
        self.video_source = video_source
        self.resize_to_fit = (320, 240)

        self.current_variant = "full"
        self.current_encoder = "MobileNetv2"
        self.image_path = None

        frame_0_0 = Tkinter.Frame(window, width=450, height=50, padx=3, pady=3)
        frame_0_0.grid(row=0, sticky="ew")
        frame_0_1 = Tkinter.Frame(window, width=450, height=50, padx=3, pady=3)
        frame_0_1.grid(row=0, column=1, sticky="ew")
        self.frame_1_0 = Tkinter.Frame(window,
                                       width=450,
                                       height=50,
                                       padx=3,
                                       pady=3)
        self.frame_1_0.grid(row=1, column=0, sticky="ew")
        self.frame_1_1 = Tkinter.Frame(window,
                                       width=450,
                                       height=50,
                                       padx=3,
                                       pady=3)
        self.frame_1_1.grid(row=1, column=1, sticky="ew")
        frame_2_0 = Tkinter.Frame(window, width=450, height=50, padx=3, pady=3)
        frame_2_0.grid(row=2, column=0, sticky="ew")
        self.frame_2_1 = Tkinter.Frame(window,
                                       width=450,
                                       height=50,
                                       padx=3,
                                       pady=3)
        self.frame_2_1.grid(row=2, column=1, sticky="ew")
        self.frame_3_0 = Tkinter.Frame(window,
                                       width=450,
                                       height=50,
                                       padx=3,
                                       pady=3)
        self.frame_3_0.grid(row=3, column=0, sticky="ew")
        self.frame_3_1 = Tkinter.Frame(window,
                                       width=450,
                                       height=50,
                                       padx=3,
                                       pady=3)
        self.frame_3_1.grid(row=3, column=1, sticky="ew")

        write_text(frame_0_0, "Dataset variant:")
        write_text(frame_0_1, "DeepLabv3+ Encoder:")
        write_text(self.frame_1_0, "Color Guide:")
        write_text(frame_2_0, "Live Video:")
        write_text(self.frame_2_1, "Current Image:")
        write_text(self.frame_3_0, "Segmented result:")
        write_text(self.frame_3_1, "Segmentation Overlayed:")

        btn_variant = Tkinter.Button(frame_0_0,
                                     text="full",
                                     width=10,
                                     command=lambda: self.set_variant("full"))
        btn_variant.grid(row=1, column=0)

        btn_variant = Tkinter.Button(
            frame_0_0,
            text="size_invariant",
            width=10,
            command=lambda: self.set_variant("size_invariant"))
        btn_variant.grid(row=1, column=1)

        btn_variant = Tkinter.Button(
            frame_0_0,
            text="similar_shapes",
            width=10,
            command=lambda: self.set_variant("similar_shapes"))
        btn_variant.grid(row=2, column=0)

        btn_variant = Tkinter.Button(
            frame_0_0,
            text="binary",
            width=10,
            command=lambda: self.set_variant("binary"))
        btn_variant.grid(row=2, column=1)

        btn_encoder = Tkinter.Button(
            frame_0_1,
            text="MobileNetv2",
            width=10,
            command=lambda: self.set_encoder("MobileNetv2"))
        btn_encoder.grid(row=1, column=0)
        btn_encoder = Tkinter.Button(
            frame_0_1,
            text="Xception",
            width=10,
            command=lambda: self.set_encoder("Xception"))
        btn_encoder.grid(row=1, column=1)

        # open video source (by default this will try to open the computer webcam)
        self.vid = MyVideoCapture(self.video_source)

        # Create a canvas that can fit the above video source size
        self.canvas_vid = Tkinter.Canvas(frame_2_0,
                                         width=self.resize_to_fit[0],
                                         height=self.resize_to_fit[1])
        self.canvas_vid.grid(row=1, column=0)

        # Button that lets the user take a snapshot
        btn_snapshot = Tkinter.Button(frame_2_0,
                                      text="Snapshot",
                                      width=10,
                                      command=self.snapshot)
        btn_snapshot.grid(row=2, column=0)

        # After it is called once, the update method will be automatically called every delay milliseconds
        self.delay = 15
        self.update()

        self.window.mainloop()
Exemple #12
0
parser = argparse.ArgumentParser(description='Championship Manager 01/02 Bot')
parser.add_argument('--screen_height', type=int, help='Your screen\'s height')
parser.add_argument('--screen_width', type=int, help='Your screen\'s width')
parser.add_argument(
    '--load_game',
    type=str,
    help='Enable this if you want to load an existing saved game')
args = parser.parse_args()

if args.screen_height:
    screen_height = args.screen_height
else:
    screen_height = 1080

if args.screen_width:
    screen_width = args.screen_width
else:
    screen_height = 2560

if args.load_game:
    load_game = True
    load_game_name = args.load_game

wait_for_context()
if load_game:
    click('LOAD_GAME')
    write_text(load_game_name)
    click('NEXT_BUTTON')
else:
    click('START_NEW_GAME')
Exemple #13
0
 def charset(self):
     text = utils.read_text(filename=self.filename)
     charset = sorted(list(set(text)), key=ord)
     utils.write_text(filename='material/charset.txt', obj=''.join(charset))
Exemple #14
0
 def save_data(self, target):
     utils.mkdir(dirr=target)
     utils.write_text(filename=target + '/training.txt',
                      obj='\n'.join(self.traingData))
     utils.write_text(filename=target + '/test.txt',
                      obj='\n'.join(self.testData))
Exemple #15
0
def __output(name, text):
    out_path = os.path.join(OUT_DIR, name + '.js')
    print(out_path)
    utils.write_text(out_path, text)