Exemplo n.º 1
0
 def __init__(self, client, pp, protocol):
     self.client = client
     self.lastMessage = None
     self.lastSend = time.time()
     # self.replayfile = open("replay.txt","a")
     self.startTime = time.time()
     self.printPacket = pp
     self.protocol = protocol
Exemplo n.º 2
0
def testFastOCR():
    setupData()
    import nestris_ocr.utils.time as time

    t = time.time()

    img = Image.open("nestris_ocr/assets/test/score.png")
    for i in range(10000):
        scoreImage(img, "ADDDDD")

    result = time.time() - t
    print("10000 iterations took:" + str(result) + " seconds")
Exemplo n.º 3
0
    def benchmark_backends(self, source_id, backends):
        times = []

        for backend in backends:
            timer = time.time()
            try:
                cap = cv2.VideoCapture(int(source_id), backend)
                cap.release()
            except:  # noqa  E722
                pass  # if the capture method is invalid, *sometimes* it crashes
            else:
                t = time.time() - timer
                if t > 0.05:  # invalid caputre loads instantly.
                    times.append((time.time() - timer, backend))

        times.sort(key=lambda x: x[0])
        return times[0]
Exemplo n.º 4
0
    def sendResult(self, message, timeStamp):
        isSame = sameMessage(self.lastMessage, message)
        t = time.time()
        if t - self.lastSend > self.RATE or (not isSame):
            # print(self.lastMessage,"\n",message)
            self.lastMessage = message.copy()
            message["time"] = timeStamp

            message = prePackMessage(message, self.protocol)

            if self.printPacket:
                print(message)

            packed, binary = packMessage(message, self.protocol)

            self.client.sendMessage(packed, binary)

            self.lastSend = time.time()
Exemplo n.º 5
0
    def update(self):
        frame_times = deque([], 10)
        start_frame_ts = None
        while self.running:

            cv2_retval, cv2_image = self.cap.read()
            avg_ft = self.calculate_avg_frametime(frame_times, start_frame_ts)
            start_frame_ts = time.time()

            # deinterlace
            img1, img2 = self.deinterlace_np(cv2_image)

            self.inject_image(cv2_retval, img1, time.time())

            if avg_ft is not None and img2 is not None:
                deinterlaced_ft = avg_ft / 2.0
                time.sleep(deinterlaced_ft - 0.002)  # sleep a bit less than we need.
                self.inject_image(cv2_retval, img2, time.time())

        self.cap.release()
Exemplo n.º 6
0
def main(on_cap, check_network_close):
    strategy = Strategy()

    # The loop makes sure that the program retries constantly even when
    # capturing device is having trouble
    while True:
        try:
            read_ts = time.time()
            ts, image = uncached_capture().get_image(rgb=True)

            if not ts and not image:
                break
        except KeyboardInterrupt:
            break
        except Exception:
            time.sleep(RATE)
            continue

        frame_end_ts = (ts or read_ts) + RATE
        pre_strategy_ts = time.time()

        strategy.update(ts, image)
        result = strategy.to_dict()

        if config["debug.print_benchmark"]:
            elapsed_time = time.time() - (ts or read_ts)
            print(f"Elapsed time since capture: {elapsed_time}")
            strategy_time = time.time() - pre_strategy_ts
            print(f"Strategy processing time: {strategy_time}")

        on_cap(result, ts)

        # error = check_network_close()
        # if error is not None:
        #    return error

        time.sleep(max(frame_end_ts - time.time(), 0))
Exemplo n.º 7
0
        "level": "01",
        "field":
        "03300000000133000000110000000011000000001100000000110000330021222233002223233310223333311022333331102233333330221133113022111111301222113330121133111012213311201123331120111331122021123111102212333110",
        "preview": "L",
        "time": 118.7786123752594,
    }
    raw_data = temp["field"]
    data = np.zeros((20, 10), dtype=np.uint8)
    packed = prePackField(data)
    temp["field"] = packed

    # temp = {"playername": "²fluffy", "score": "008055", "lines": "015", "level": "01", "preview": "L", "time": 118.7786123752594}
    stuffed = stuffDictionary(temp)
    print(stuffed)
    import nestris_ocr.utils.time as time

    t = time.time()
    for i in range(10000):
        packField(raw_data)
    print(time.time() - t)

    t = time.time()
    for i in range(10000):
        json.dumps(raw_data)
    print(time.time() - t)

    t = time.time()
    for i in range(10000):
        prePackField(data)
    print(time.time() - t)
Exemplo n.º 8
0
    def get_image(self, rgb: bool = False) -> Tuple[float, Image.Image]:
        hwnd = self.get_window()

        image = self.cap.ImageCapture(self.xywh_box, hwnd)
        # TODO add rgb support
        return time.time(), image  # use better timestamp
Exemplo n.º 9
0
 def update(self):
     if not self.destroying:
         if time.time() - self.lastUpdate > 1.0:
             self.redrawImages()
         super().update()
Exemplo n.º 10
0
 def redrawImages(self, event=None):
     self.lastUpdate = time.time()
     refresh_window_areas()
     self.updateActiveCalibrationTab()
     self.updateActivePlaybackTab()
Exemplo n.º 11
0
    def __init__(self, config):
        self.config = config
        root = tk.Tk()
        super().__init__(root)
        root.protocol("WM_DELETE_WINDOW", self.on_exit)
        root.focus_force()
        root.wm_title("NESTrisOCR calibrator")
        self.pack()
        self.root = root
        self.destroying = False
        root.config(background="black")
        self.strategy = Strategy()
        self.exit_program = True
        self.exit_calibrator = False
        CaptureMethod(
            self,
            (config["capture.method"], config["capture.source_id"]),
            (
                self.gen_set_reload_capture("capture.method"),
                self.gen_set_reload_capture("capture.source_id"),
                partial(config.__setitem__, "capture.source_id"),
            ),
        ).grid(row=0, sticky="nsew")
        StringChooser(
            self,
            "player name",
            config["player.name"],
            partial(config.__setitem__, "player.name"),
            20,
        ).grid(row=1, sticky="nsew")
        if ENABLE_OTHER_OPTIONS:
            Button(
                self,
                text="Other options",
                command=lambda: create_window(root, self.config, self.
                                              otherOptionsClosed),
            ).grid(row=0, column=1)
        Button(self, text="Switch to SIMPLE Mode",
               command=self.simple_mode).grid(row=1, column=1)
        # window coords
        f = tk.Frame(self)
        r = RectChooser(
            f,
            "capture window coords (pixels)",
            config["calibration.game_coords"],
            False,
            self.update_game_coords,
        )
        r.config(relief=tk.FLAT, bd=5, background="orange")
        r.pack(side=tk.LEFT)
        self.winCoords = r
        # auto calibrate
        border = tk.Frame(f)
        border.config(relief=tk.FLAT, bd=5, background="orange")
        border.pack(side=tk.RIGHT, fill="both")
        autoCalibrate = Button(
            border,
            text="Automatically detect field",
            command=self.autoDetectField,
            bg="red",
        )
        autoCalibrate.pack(fill="both", expand=True)
        f.grid(row=2, column=0)

        # refresh
        Button(self,
               text="Refresh Image",
               command=self.redrawImages,
               bg="blue").grid(
                   row=2,
                   column=1,
                   sticky="nsew",
                   rowspan=2,
               )

        # webcam output
        self.setupPlaybackTabs()
        self.playbackTabs.grid(row=4, column=0, sticky="nsew")
        self.calibrationTabs = ttk.Notebook(self)
        self.calibrationTabs.grid(row=4, column=1, sticky="nsew")
        self.calibrationTabs.bind("<<NotebookTabChanged>>", self.redrawImages)

        self.setupTab1()
        self.setupTab2()
        self.setupTab3()
        self.setupTab4()
        self.setupTab5()
        self.setPreviewTextVisible()

        self.progress_bar = ttk.Progressbar(self,
                                            orient=tk.HORIZONTAL,
                                            length=512,
                                            mode="determinate")
        self.progress_bar["maximum"] = 100
        self.progress_bar.grid(row=5, columnspan=2, sticky="nsew")

        self.redrawImages()
        self.lastUpdate = time.time()
        reinit_capture()
Exemplo n.º 12
0
 def calculate_avg_frametime(self, frame_times, start_frame_ts):
     if start_frame_ts is not None:
         t = time.time() - start_frame_ts
         frame_times.append(t)
         return sum(frame_times) / len(frame_times)
     return None
Exemplo n.º 13
0
 def get_image(self, rgb: bool = False) -> Tuple[float, Image.Image]:
     w, h = self.xywh_box[2], self.xywh_box[3]
     im = Image.new("RGB", (w, h))
     return (time.time(), im)
Exemplo n.º 14
0

# expecting all 4 colors as np.array(dtype=np.uint8)
def parseImage(img, colors):
    img = np.array(img, dtype=np.uint8)

    return parseImage2(img, colors.black, colors.white, colors.color1,
                       colors.color2)


if __name__ == "__main__":
    # run this from root directory as "python -m nestris_ocr.ocr_algo.board"
    from nestris_ocr.colors import Colors
    import nestris_ocr.utils.time as time

    img = Image.open("nestris_ocr/assets/test/board_lvl7.png")

    colors = Colors()
    colors.setLevel(7)

    iterations = 25000

    start = time.time()
    for i in range(iterations):
        parseImage(img, colors)

    elapsed = time.time() - start
    print(elapsed, elapsed / iterations)

    print(parseImage(img, colors))
Exemplo n.º 15
0
 def get_image(self, rgb: bool = False) -> Tuple[float, Image.Image]:
     return time.time(), self.source_img.crop(xywh_to_ltrb(self.xywh_box))