def __is_initial_screen__(self, *args, **kwargs): original = cv2.imread(os.path.join(self.assets, "start_screen.png")) against = self.get_img_from_screen_shot() # convert the images to grayscale original = mask_image([127], [255], cv2.cvtColor(original, cv2.COLOR_BGR2GRAY), True) against = mask_image([127], [255], cv2.cvtColor(against, cv2.COLOR_BGR2GRAY), True) (score, diff) = compare_ssim(original, against, full=True) if score > .9: return True return False
def test_initial_pass_through_compare(self): original = cv2.imread( os.path.join(self.provider.assets, "start_screen.png")) against = self.provider.get_img_from_screen_shot() wrong = cv2.imread(os.path.join(self.provider.assets, "battle.png")) # convert the images to grayscale original = mask_image([127], [255], cv2.cvtColor(original, cv2.COLOR_BGR2GRAY), True) against = mask_image([127], [255], cv2.cvtColor(against, cv2.COLOR_BGR2GRAY), True) wrong = mask_image([127], [255], cv2.cvtColor(wrong, cv2.COLOR_BGR2GRAY), True) # initialize the figure (score, diff) = compare_ssim(original, against, full=True) diff = (diff * 255).astype("uint8") self.assertTrue( score > .90, 'If this is less then .90 the initial compare of the app will fail' ) (score, nothing) = compare_ssim(original, wrong, full=True) self.assertTrue(score < .90) if self.__debug_pictures__: # threshold the difference image, followed by finding contours to # obtain the regions of the two input images that differ thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] # loop over the contours for c in cnts: # compute the bounding box of the contour and then draw the # bounding box on both input images to represent where the two # images differ (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.rectangle(against, (x, y), (x + w, y + h), (0, 0, 255), 2) # show the output images diffs = ("Original", original), ("Modified", against), ("Diff", diff), ("Thresh", thresh) images = ("Original", original), ("Against", against), ("Wrong", wrong) self.setup_compare_images(diffs) self.setup_compare_images(images)
def get_current_page(self, img): img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) area = crop_image(img, **self.predefined.page_area) area = mask_image([254], [255], area) height, width = area.shape current_page = 0 for x in range(4): box = crop_image(area, (x * width / 4), 0, ((x + 1) * width / 4), height) if cv2.countNonZero(box) > 0: current_page = x break return current_page + 1
def img_to_string(img, char_set=None, mask_area=None): if mask_area is not None: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) lower, upper = mask_area[0], mask_area[1] img = mask_image(lower, upper, img) cv2.imwrite("tmp\\ocr.png", img) command = "bin\\tess\\tesseract.exe --tessdata-dir bin\\tess\\tessdata tmp\\ocr.png tmp\\ocr " if char_set is not None: command += "-c tessedit_char_whitelist=" + char_set + " " command += "-psm 7 " command += "> nul 2>&1" CREATE_NO_WINDOW = 0x08000000 subprocess.call(command, shell=True, creationflags=CREATE_NO_WINDOW) # Get the largest line in txt with open("tmp\\ocr.txt") as f: content = f.read().splitlines() output_line = "" for line in content: line = line.strip() if len(line) > len(output_line): output_line = line return output_line
def prep_for_white_circles(self): lower, upper = ([215, 215, 215], [255, 255, 255]) self.white_query = mask_image(lower, upper, self.query, apply_mask=True)