def testRollerBlock(): import matplotlib.pyplot as plt import platform import cv2 import os if "Windows" in platform.system(): filename = r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1\data\img\style1\2.jpg" elif "Linux" in platform.system(): filename = r"/home/allen/work/digitRecognise/com/huitong/gasMeterv1/data/img/style1/000.jpg" style1 = GasmeterStyle1(desImageDepth=1) image = cv2.imread(filename) ImageTool.showImagePIL(image) style1.setImage(image) rollerBlackImage = style1.getRollerBlackArea() # rollerBlackImage = cv2.cvtColor(rollerBlackImage,cv2.COLOR_BGR2RGB) title = str(rollerBlackImage.shape) + os.path.basename(filename) # rollerBlackImage = ImageTool.convertImgBGR2Gray(rollerBlackImage) # ret,rollerBlackImage = ImageTool.getOTSUGrayImage(rollerBlackImage) plt.figure() plt.imshow(rollerBlackImage) plt.title(title) plt.show()
def getPredict(hps, mode, gasmeter_filename, save_file_name): xp = tf.placeholder( tf.float32, [None, captchaBoxHeight * captchaBoxWidth * gen.ImageDepth]) yp = tf.placeholder(tf.float32, [None, captchaCharacterLength * CHAR_SET_LEN]) model = ResNetModel.ResNetModel(hps, xp, yp, mode, captchaBoxHeight, captchaBoxWidth, gen.ImageDepth) model.create_graph(captchaCharacterLength) with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: saver = tf.train.Saver() saver.restore(sess, save_file_name) # images = gen.get_batch_gasmeter_digit_area_from_filename(gasmeter_filename, hps.batch_nums) image = cv2.imread(gasmeter_filename) # gasmeter = GasmeterStyle0(captchaBoxWidth,captchaBoxHeight,desImageDepth=gen.ImageDepth) gasmeter = GasmeterStyle1(captchaBoxWidth, captchaBoxHeight, desImageDepth=gen.ImageDepth) gasmeter.setImage(image) image = gasmeter.getRollerBlackArea() ImageTool.showImagePIL(image) images = ImageTool.repeatImage2Tensor(image, hps.batch_nums) feed_dict = {xp: images, model.is_training_ph: False} outputs = sess.run([model.outputs], feed_dict=feed_dict) text = get_predict_text(outputs) return text
def getLCDAreaData(image): """ 获取 LCD 区域图片数据 :param image: cv2 读进来的图片对象 """ image = ImageTool.preProcessImage(image) blackMask = MaskTool.getBlackMaskBGR() image = blackMask.getInterestImageAreaData(image) shape = image.shape grayImage = ImageTool.convertImgBGR2Gray(image) mid = int(shape[0] / 2) splitImageBox = (0, mid, shape[1], shape[0]) splitImageGray = ImageTool.getCropImageByBox(grayImage, splitImageBox) splitImage = ImageTool.getCropImageByBox(image, splitImageBox) # 显示剪切的 lcd 屏所在的下半屏灰度图 ImageTool.showImagePIL(splitImageGray, "splitImageGray") retval, otsuImage = ImageTool.getOTSUGrayImage(splitImageGray) otsuImage = ImageTool.convertImgGray2BGR(otsuImage) lower = (250, 250, 250) upper = (255, 255, 255) lcdBoxCorner = ImageTool.getInterestBoxCornerPointByColor( otsuImage, lower, upper) lcdBox = LCDLightDetect.getMinBox(lcdBoxCorner) # ImageTool.showBoxInImageByBox(splitImage, lcdBox) lcdImage = ImageTool.getCropImageByBox(splitImage, lcdBox) return lcdImage
def getPredict(hps, mode, gasmeter_filename, save_file_name): xp = tf.placeholder( tf.float32, [None, captchaBoxHeight * captchaBoxWidth * gen.ImageDepth]) yp = tf.placeholder(tf.float32, [None, captchaCharacterLength * CHAR_SET_LEN]) model = ResNetModel.ResNetModel(hps, xp, yp, mode, captchaBoxHeight, captchaBoxWidth, gen.ImageDepth) model.create_graph(captchaCharacterLength) with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: saver = tf.train.Saver() saver.restore(sess, save_file_name) # image = Image.open(gasmeter_filename) # image = image.resize((captchaBoxWidth,captchaBoxHeight), Image.BICUBIC) image = cv2.imread(gasmeter_filename) image = ImageTool.imageResize(image, captchaBoxWidth, captchaBoxHeight) if gen.ImageDepth == 1: image = ImageTool.convertImgBGR2Gray(image) # image = ImageTool.convertImgRGB2Gray(image) ImageTool.showImagePIL(image) images = ImageTool.repeatImage2Tensor(image, hps.batch_nums) feed_dict = {xp: images, model.is_training_ph: False} outputs = sess.run([model.outputs], feed_dict=feed_dict) text = get_predict_text(outputs) return text
def getRollerBlackArea(self): rollerBox = self.getRollerBox() rollerImage = ImageTool.getCropImageByBox(self._image, rollerBox) rollerImage = ImageTool.imageResize(rollerImage, self._desWidth, self._desHeight) if self._desImageDepth == 1: rollerImage = ImageTool.convertImgBGR2Gray(rollerImage) self._rollerImage = rollerImage return self._rollerImage
def fun8(): from com.huitong.gasMeterv1.framework.tool.GenDigitsImage import GenDigitsPicture characterLength = 1 width = 15 height = 30 bkgColor = (20,20,20) fontColor = (200,200,200) fontSizes = (29,) gen = GenDigitsPicture(characterLength, width, height) while True: text,image = gen.get_text_and_image(backgroundColor=bkgColor,fontColor=fontColor,fontSizes=fontSizes) ImageTool.showImagePIL(image,text)
def get_text_and_image(self, backgroundColor=None, fontColor=None, fontSizes=(28, )): """ 生成字符序列和对应的图片数据 图片颜色通道是(R,G,B)-> text,image""" charBoxWidth = 15 charBoxHeight = 30 blackBkgColor = ImageCaptcha.random_bkg_color(0, 90) fontColor = ImageCaptcha.random_font_color(180, 250) grayBkgColor = ImageCaptcha.random_bkg_color(130, 220) image = ImageCaptcha(width=charBoxWidth, height=charBoxHeight, backgroundColor=blackBkgColor, fontColor=fontColor, font_sizes=fontSizes) captcha_text = self._random_text() captcha_text = ''.join(captcha_text) filename = GenImageGasMeterStyle1m2.getRandomFilename() # print(filename) captcha_image = Image.open(filename) captcha_image = ImageTool.imageResizePIL(captcha_image, self._picBoxWidth, self._picBoxHeight) dxs = [7, 32, 58, 83, 107] dy = 17 i = 0 def randDy(): return 4 - random.randint(1, 8) for c in captcha_text: desX = dxs[i] charImage = image.generate(c) charImage = Image.open(charImage) captcha_image.paste(charImage, (desX, dy + randDy())) i += 1 if self._imageDepth == 1: # 将彩色图片转换成灰度图图片 captcha_image = ImageTool.convertImgRGB2Gray(captcha_image) captcha_image = np.array(captcha_image) return captcha_text, captcha_image
def setImage(self, image): """ 设置要处理的图片 :param image: 是cv2读进来的图片对象 """ image = ImageTool.preProcessImage(image) super(GasmeterStyle0, self).setImage(image)
def getRollerBox(self): """ 获得黑底白字滚轮区域box,并将该 box 赋值给 self._rollerBox """ rollerBoxWidthMax = 425 shape = self._image.shape mid = int(shape[0] / 2) quarter = int(shape[0] / 4) splitImageBox = (70, quarter, 170, mid) splitImageGray = ImageTool.getCropImageByBox(self._grayImage, splitImageBox) retval, otsuImage = ImageTool.getOTSUGrayImage(splitImageGray) otsuImage = ImageTool.convertImgGray2BGR(otsuImage) lower = (250, 250, 250) upper = (255, 255, 255) splitBoxCorner = ImageTool.getInterestBoxCornerPointByColor( otsuImage, lower, upper) splitBox = ImageTool.getBoxFromBoxCorner(splitBoxCorner) rollerBoxUp = int(quarter + splitBox[1]) rollerBoxDown = int(quarter + splitBox[3]) trollerBox = (0, rollerBoxUp, shape[1], rollerBoxDown) rollerImageGray = ImageTool.getCropImageByBox(self._grayImage, trollerBox) retval, otsuImage = ImageTool.getOTSUGrayImage(rollerImageGray) otsuImage = ImageTool.convertImgGray2BGR(otsuImage) trollerBoxCorner = ImageTool.getInterestBoxCornerPointByColor( otsuImage, lower, upper) trollerBox = ImageTool.getBoxFromBoxCorner(trollerBoxCorner) rollerBox = (trollerBox[0], rollerBoxUp + 1, trollerBox[2], rollerBoxDown - 1) getWidth = rollerBox[2] - rollerBox[0] if rollerBoxWidthMax < getWidth: rollerBox = (rollerBox[0], rollerBox[1], rollerBox[0] + rollerBoxWidthMax, rollerBox[3]) right = int(rollerBox[2] * 5 / 8) + 20 rollerBox = (rollerBox[0], rollerBox[1], right, rollerBox[3]) # ImageTool.showBoxInImageByBox(self._image,rollerBox) self._rollerBox = rollerBox return rollerBox
def getInterestBoxCornerPoint(self, bkgImage): """ 获得感兴趣的图片区域所在矩形的四个顶角坐标,如果图片中没有在所选区域范围内,返回 None :param bkgImage: 通过cv2 读进来的图像对象 """ boxCornerPoint = ImageTool.getInterestBoxCornerPointByColor( bkgImage, self._colorLower, self._colorUpper) return boxCornerPoint
def setImage(self, image): """ 设置要处理的图片 :param image: 是cv2读进来的图片对象 提取表头黑色背景区域图片保存到 self._image 域中,方便以后提取条形码、液晶屏、黑底白字滚轮区域数字 """ if image is None: raise ValueError("image is None") image = ImageTool.preProcessImage(image) # ImageTool.showImagePIL(image) super(GasmeterStyle1, self).setImage(image) blackMask = MaskTool.getBlackMaskBGR() self._image = blackMask.getInterestImageAreaData(self._image) self._grayImage = ImageTool.convertImgBGR2Gray(self._image)
def fun7(): baseDir = r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1" imgdirname = ["data", "img","gasmeterHead","style1"] dirname = FileNameUtil.getDirname(baseDir,imgdirname) filelist = FileNameUtil.getPathFilenameList(dirname,".*\.jpg") interestLoweer = (0,0,100) interestUpper = (60,45,255) interestLoweer = (40, 35, 80) interestUpper = (105, 105, 230) for each in filelist: image = cv2.imread(each) cornerpoint = ImageTool.getInterestBoxCornerPointByColor(image,interestLoweer,interestUpper) title = os.path.basename(each) ImageTool.showBoxInImageByBoxCornerPoint(image, cornerpoint, title)
def getPredict(hps, mode, save_file_name, gen): xp = tf.placeholder( tf.float32, [None, captchaBoxHeight * captchaBoxWidth * gen.ImageDepth]) yp = tf.placeholder(tf.float32, [None, captchaCharacterLength * CHAR_SET_LEN]) model = ResNetModel.ResNetModel(hps, xp, yp, mode, captchaBoxHeight, captchaBoxWidth, gen.ImageDepth) model.create_graph(captchaCharacterLength) gen1 = GenImageGasMeterStyle1m1(captchaCharacterLength, captchaBoxWidth, captchaBoxHeight, imageDepth=1) gen2 = GenImageGasMeterStyle1m2(captchaCharacterLength, captchaBoxWidth, captchaBoxHeight, imageDepth=1) gen3 = GenImageGasMeterStyle1m3(captchaCharacterLength, captchaBoxWidth, captchaBoxHeight, imageDepth=1) gens = [gen1, gen2, gen3] with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: saver = tf.train.Saver() saver.restore(sess, save_file_name) while True: gen = random.choice(gens) oriText, image = gen.get_text_and_image() images = ImageTool.repeatImage2Tensor(image, hps.batch_nums) feed_dict = {xp: images, model.is_training_ph: False} outputs = sess.run([model.outputs], feed_dict=feed_dict) predictText = get_predict_text(outputs) title = "text:%s, predict:%s" % (oriText, predictText) ImageTool.showImagePIL(image, title) print(title)
def getInterestBox(self, bkgImage): """ 获得感兴趣的区域box,box 是由 (left, upper, right, lower ) pixel coordinate tuple 来表示 如果没有返回 None :param bkgImage: 通过cv2 读进来的图像对象 """ boxCornerPoint = self.getInterestBoxCornerPoint(bkgImage) if boxCornerPoint is None: return None box = ImageTool.getBoxFromBoxCorner(boxCornerPoint) return box
def test(): import cv2 import platform import os style = GasmeterStyle0(desImageDepth=1) if "Linux" in platform.system(): filename = r'/home/allen/work/digitRecognise/com/huitong/gasMeterv1/data/img/style0/10.jpg' elif "Windows" in platform.system(): filename = r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1\data\img\style0\10.jpg" image = cv2.imread(filename) title = os.path.basename(filename) style.setImage(image) image = style.getRollerBlackArea() # ret, image = ImageTool.getOTSUGrayImage(image) # image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB) ImageTool.showImagePIL(image, str(image.shape) + title)
def getInterestImageAreaData(self, bkgImage): """ 获得图片中感兴趣的颜色范围区域数据,如果没有返回 None :param bkgImage: 通过cv2 读进来的图像对象 """ boxCornerPoint = self.getInterestBoxCornerPoint(bkgImage) if boxCornerPoint is None: return None image = ImageTool.getCropImageByBoxCornerPoint(bkgImage, boxCornerPoint) return image
def get_text_and_image(self, backgroundColor=None, fontColor=None, fontSizes=(28, )): filename = GenImageGasMeterStyle1m2.getRandomFilename() # print(filename) captcha_image = Image.open(filename) captcha_image = ImageTool.imageResizePIL(captcha_image, self._picBoxWidth, self._picBoxHeight) t = FileNameUtil.getFilenameFromFullFilepathname(filename) captcha_text = t[:5] if self._imageDepth == 1: # 将彩色图片转换成灰度图图片 captcha_image = ImageTool.convertImgRGB2Gray(captcha_image) captcha_image = np.array(captcha_image) return captcha_text, captcha_image
def getRollerBlackArea(self): if self._image is None: raise ValueError("应先通过setImage()函数设置image,然后获取感兴趣数据") blackMask = MaskTool.getBlackMaskBGR() blackImage = blackMask.getInterestImageAreaData(self._image) redMask = MaskTool.getRedMaskBGR() redBox = redMask.getInterestBox(blackImage) if redBox is not None: rollerBlackArea = ImageTool.removeRightArea(blackImage, redBox[0]) else: rollerBlackArea = blackImage rollerBlackArea = ImageTool.imageResize(rollerBlackArea, self._desWidth, self._desHeight) if self._desImageDepth == 1: rollerBlackArea = ImageTool.convertImgBGR2Gray(rollerBlackArea) return rollerBlackArea
def getLCDBox(self): """ """ shape = self._image.shape mid = int(shape[0] / 2) splitImageBox = (0, mid, shape[1], shape[0]) splitImageGray = ImageTool.getCropImageByBox(self._grayImage, splitImageBox) ImageTool.showImagePIL(splitImageGray) splitImage = ImageTool.getCropImageByBox(self._image, splitImageBox) retval, otsuImage = ImageTool.getOTSUGrayImage(splitImageGray) otsuImage = ImageTool.convertImgGray2BGR(otsuImage) lower = (250, 250, 250) upper = (255, 255, 255) lcdBoxCorner = ImageTool.getInterestBoxCornerPointByColor( otsuImage, lower, upper) lcdBox = ImageTool.getBoxFromBoxCorner(lcdBoxCorner) self._lcdBox = lcdBox ImageTool.showBoxInImageByBoxCornerPoint(splitImage, lcdBoxCorner, "lcd")
def get_text_and_image(self,backgroundColor = None, fontColor = None, fontSizes = None): """ 生成字符序列和对应的图片数据 图片颜色通道是(R,G,B)-> text,image""" image = ImageCaptcha(width=self._picBoxWidth, height=self._picBoxHeight, backgroundColor=backgroundColor, fontColor=fontColor, font_sizes=fontSizes) captcha_text = self._random_text() captcha_text = ''.join(captcha_text) captcha = image.generate(captcha_text) captcha_image = Image.open(captcha) if self._imageDepth == 1: # 将彩色图片转换成灰度图图片 captcha_image = ImageTool.convertImgRGB2Gray(captcha_image) captcha_image = np.array(captcha_image) return captcha_text, captcha_image
def get_text_and_image(self, backgroundColor=None, fontColor=None, fontSizes=None): """ 生成字符序列和对应的图片数据 图片颜色通道是(R,G,B)-> text,image""" charBoxWidth = 20 charBoxHeight = 50 blackBkgColor = ImageCaptcha.random_bkg_color(0, 65) fontColor = ImageCaptcha.random_font_color(180, 250) grayBkgColor = ImageCaptcha.random_bkg_color(130, 220) image = ImageCaptcha(width=charBoxWidth, height=charBoxHeight, backgroundColor=blackBkgColor, fontColor=fontColor) captcha_text = self._random_text() captcha_text = ''.join(captcha_text) captcha_image = Image.new('RGB', (self._picBoxWidth, self._picBoxHeight), grayBkgColor) dx = 4 dy = 7 desX = dx for c in captcha_text: charImage = image.generate(c) charImage = Image.open(charImage) captcha_image.paste(charImage, (desX, dy)) desX += dx + charBoxWidth if self._imageDepth == 1: # 将彩色图片转换成灰度图图片 captcha_image = ImageTool.convertImgRGB2Gray(captcha_image) captcha_image = np.array(captcha_image) return captcha_text, captcha_image
def lcdLighted(lcdImage): """ 检测lcd 区域图片是否点亮,如果亮返回True,如果不亮返回False :param lcdImage: cv2 读进来的图片对象 """ lower = (0, 0, 0) upper = (50, 50, 50) blackBoxCorner = ImageTool.getInterestBoxCornerPointByColor( lcdImage, lower, upper) # 如果获得的 lcd 区域有黑色区域,说明没有点亮。 if blackBoxCorner is not None: return False lcdImageGray = ImageTool.convertImgBGR2Gray(lcdImage) retval, otsuImage = ImageTool.getOTSUGrayImage(lcdImageGray) otsuImage = ImageTool.convertImgGray2BGR(otsuImage) notLcdBoxCorner = ImageTool.getInterestBoxCornerPointByColor( otsuImage, lower, upper) ImageTool.showImagePIL(lcdImage, "lcdimage") return notLcdBoxCorner is None
def fun9(): filename = r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1\data\img\gasmeterRoller\000041.jpg" image = Image.open(filename) image = image.resize((128,64),Image.CUBIC) ImageTool.showImagePIL(image,"des")
def fun5(): filename = r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1\data\img\style0\17.jpg" image = cv2.imread(filename) ImageTool.showImageCv2(image)
def getBlackMaskBGR(): """获得黑色区域的 Mask""" blkLower, blkUpper = ImageTool.getBlackColorRangeBGR() blackMask = BaseMask(blkLower, blkUpper) return blackMask
def getRedMaskBGR(): """获得红色区域的 Mask""" redLower, redUpper = ImageTool.getRedColorRangeBGR() redMask = BaseMask(redLower, redUpper) return redMask