def testShowInterestAreaBox(): import platform imgdirname = ["data", "img", "style1"] if "Windows" in platform.system(): imgdirname = FileNameUtil.getDirname( r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1", imgdirname) elif "Linux" in platform.system(): imgdirname = FileNameUtil.getDirname( r"/home/allen/work/digitRecognise/com/huitong/gasMeterv1", imgdirname) pattern = r'.*\.jpg$' filelist = FileNameUtil.getPathFilenameList(imgdirname, pattern) interestColorLower = (0, 0, 0) interestColorUpper = (70, 70, 70) for filename in filelist: image = cv2.imread(filename) image = ImageTool.preProcessImage(image) boxCornerPoint = ImageTool.getInterestBoxCornerPointByColor( image, interestColorLower, interestColorUpper) if boxCornerPoint is not None: box = ImageTool.getBoxFromBoxCorner(boxCornerPoint) boxCornerPoint = ImageTool.getBoxCornerPointFromBox(box) title = "box area, %s" % FileNameUtil.getFilenameFromFullFilepathname( filename) ImageTool.showBoxInImageByBoxCornerPoint(image, boxCornerPoint, title) else: print("no color in range" + str(interestColorLower) + str(interestColorUpper))
def fun2(): imgdirname = ["data","img"] imgdirname = FileNameUtil.getDirname(FileNameUtil.getBasedirname(__file__),imgdirname) imgdirname = FileNameUtil.getPathJoin(imgdirname,"pic1.jpg") img = Image.open(imgdirname) plt.figure() plt.imshow(img) plt.title("ori") plt.show() pastImg = np.zeros([20,40],dtype=np.uint8) plt.figure() plt.imshow(pastImg) plt.title("past") plt.show() pastImg = Image.fromarray(pastImg) # left, upper, right, and lower pixel coordinate img.paste(pastImg,(40,0,80,20)) plt.figure() plt.imshow(img) plt.title("after") plt.show()
def testShowGasmeterArea(): imgdirname = ["data", "img", "style0"] imgdirname = FileNameUtil.getDirname(FileNameUtil.getBasedirname(__file__), imgdirname) pattern = r'.*\.jpg$' filelist = FileNameUtil.getPathFilenameList(imgdirname, pattern) for each in filelist: ImageTool.showGasmeterArea(cv2.imread(each), filename=each)
def dataDirInit(): path = FileNameUtil.getBasedirname(__file__) saveVariableDirname = FileNameUtil.getDirname( path, gps.saveVariableDirnameList) logDirname = FileNameUtil.getDirname(path, gps.logDirnameList) if not FileNameUtil.fileExisted(saveVariableDirname): os.makedirs(saveVariableDirname) if not FileNameUtil.fileExisted(logDirname): os.makedirs(logDirname)
def testgetGasmeterAreaData(): imgdirname = ["data", "img", "style0"] imgdirname = FileNameUtil.getDirname(FileNameUtil.getBasedirname(__file__), imgdirname) pattern = r'.*\.jpg$' filelist = FileNameUtil.getPathFilenameList(imgdirname, pattern) for each in filelist: img = ImageTool.getGasmeterAreaData(cv2.imread(each)) data = np.array(img) print(data.shape) plt.figure() plt.title("gasmeter area") plt.imshow(img) plt.show()
def getFilename(filename, baseDirname=None, dirnameList=None): """ 根据基目录、子目录名列表、文件名获取文件全路径名。 如果基目录名为None,则基目录为当前文件所在目录。 :param dirnameList: :param filename: :param baseDirname: """ path = baseDirname if baseDirname is None: path = FileNameUtil.getBasedirname(__file__) if dirnameList is not None: path = FileNameUtil.getDirname(path, dirnameList) return FileNameUtil.getFilename(path, filename)
def getRandomFilename(): if "Windows" in platform.system(): dirname = r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1\data\img\gasmeterRoller" elif "Linux" in platform.system(): dirname = r"/home/allen/work/digitRecognise/com/huitong/gasMeterv1/data/img/gasmeterRoller" filenameList = FileNameUtil.getPathFilenameList(dirname) return random.choice(filenameList)
def testGasmeterComposite(): captchaCharacterLength = 5 captchaBoxWidth = 128 captchaBoxHeight = 64 gen = GenDigitPicture(captchaCharacterLength, captchaBoxWidth, captchaBoxHeight, backgroundColor=(1, 1, 1), fontColor=(200, 200, 200)) imgdirname = ["data", "img", "style0"] imgdirname = FileNameUtil.getDirname(FileNameUtil.getBasedirname(__file__), imgdirname) pattern = r'.*\.jpg$' filenames = FileNameUtil.getPathFilenameList(imgdirname, pattern) for filename in filenames: gasmeterPic = cv2.imread(filename) for i in range(5): text, image = gen.get_compose_gasmeter_text_and_image(gasmeterPic) print('begin ' + time.strftime("%Y-%m-%d %H:%M:%S") + str(type(image))) f = plt.figure() ax = f.add_subplot(111) ax.text(0.1, 0.9, text, ha='center', va='center', transform=ax.transAxes) title = FileNameUtil.getFilenameFromFullFilepathname( filename) + " for composite" plt.title(title) plt.imshow(image) plt.show() print('end ' + time.strftime("%Y-%m-%d %H:%M:%S")) img = ImageTool.getGasmeterAreaData( cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)) plt.figure() title = title + ", found gasmeter area," + text plt.title(title) plt.imshow(img) plt.show()
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 testGet_compose_gasmeter_next_batch(): imgdirname = ["data", "img", "style0"] imgdirname = FileNameUtil.getDirname(FileNameUtil.getBasedirname(__file__), imgdirname) pattern = r'.*\.jpg$' filelist = FileNameUtil.getPathFilenameList(imgdirname, pattern) captchaCharacterLength = 5 captchaBoxWidth = 128 captchaBoxHeight = 64 gen = GenDigitPicture(captchaCharacterLength, captchaBoxWidth, captchaBoxHeight, backgroundColor=(10, 10, 10), fontColor=(200, 200, 200)) for eachfile in filelist: gen.get_compose_gasmeter_next_batch(cv2.imread(eachfile))
def get_batch_gasmeter_digit_area_from_filename(self, filename, batchsize): """ 根据燃气表图片文件名,获得改燃气表数字区域的数据并返回。主要功能用来预测单张图片数据 :param filename: """ if not FileNameUtil.fileExisted(filename): raise ValueError("%s 文件不存在") image = cv2.imread(filename) image = ImageTool.getGasmeterAreaData(image) image = cv2.resize(image, (self._picBoxWidth, self._picBoxHeight)) image = np.array(image, dtype=np.float32).flatten() image = image.reshape((-1, image.shape[0])) batch_x = np.repeat(image, batchsize, 0) return batch_x
def showGasmeterArea(imgobj, filename=None): """ 显示获得的图片区域,自己对原始图片进行了处理,看到的图片大小跟原始图片不一样 :param imgobj:通过cv2 读进来的图像对象 :param filename: 要处理的原始图片 """ imgTemp = imgobj.copy() imgTemp = cv2.cvtColor(imgTemp, cv2.COLOR_BGR2GRAY) boxCornerPoint = ImageTool.getGasmeterRectBoxCornerPoint( grayImg=imgTemp) title = "box area, %s" % FileNameUtil.getFilenameFromFullFilepathname( filename) imgTemp = ImageTool.preProcessImage(imgobj) ImageTool.showBoxInImage(imgTemp, boxCornerPoint, title)
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 testShowInterestAreaDataStyle1(): import platform imgdirname = ["data", "img", "style1"] if "Windows" in platform.system(): imgdirname = FileNameUtil.getDirname( r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1", imgdirname) elif "Linux" in platform.system(): imgdirname = FileNameUtil.getDirname( r"/home/allen/work/digitRecognise/com/huitong/gasMeterv1", imgdirname) pattern = r'.*\.jpg$' filelist = FileNameUtil.getPathFilenameList(imgdirname, pattern) redLower, redUpper = ImageTool.getRedColorRangeBGR() blackLower, blackUpper = ImageTool.getBlackColorRangeBGR() for filename in filelist: print(filename) image = cv2.imread(filename) image = ImageTool.preProcessImage(image) cornerPoint = ImageTool.getInterestBoxCornerPointByColor( image, blackLower, blackUpper) if cornerPoint is None: raise ValueError("none black data") interestImage = ImageTool.getCropImageByBoxCornerPoint( image, cornerPoint) shape = interestImage.shape mid = int(shape[0] / 2) quarter = int(shape[0] / 4) upbox = (0, 0, shape[1], mid) upImage = ImageTool.getCropImageByBox(interestImage, upbox) title = "box area, %s, digit area,%s" % ( FileNameUtil.getFilenameFromFullFilepathname(filename), str(upImage.shape)) upImageGray = cv2.cvtColor(upImage, cv2.COLOR_BGR2GRAY) # equalizeImage = cv2.equalizeHist(upImageGray) # 灰度图像直方图均衡化 # retval,otsuImage = cv2.threshold(equalizeImage,0,255,cv2.THRESH_OTSU) #只支持灰度图像的STSU shape = upImageGray.shape box = (70, quarter, 170, shape[0]) splitLineImage = ImageTool.getCropImageByBox(upImageGray, box) # equalizeImage = cv2.equalizeHist(splitLineImage) retval, otsuImage = cv2.threshold(splitLineImage, 0, 255, cv2.THRESH_OTSU) ImageTool.showImagePIL(upImageGray, title) # ImageTool.showHistGray(upImageGray,title) # ImageTool.showImagePIL(equalizeImage,title) # ImageTool.showHistGray(equalizeImage,title) # ImageTool.showImagePIL(otsuImage,title) # ImageTool.showImagePIL(splitLineImage,title) # ImageTool.showImagePIL(otsuImage, title) # otsuImage = cv2.cvtColor(otsuImage, cv2.COLOR_GRAY2BGR) lower = (250, 250, 250) upper = (255, 255, 255) tcorner = ImageTool.getInterestBoxCornerPointByColor( otsuImage, lower, upper) ImageTool.showBoxInImageByBoxCornerPoint(otsuImage, tcorner, title)
def testShowInterestAreaDataStyle0(): import platform imgdirname = ["data", "img", "style0"] if "Windows" in platform.system(): imgdirname = FileNameUtil.getDirname( r"D:\chengxu\python\project\digitRecognise\com\huitong\gasMeterv1", imgdirname) elif "Linux" in platform.system(): imgdirname = FileNameUtil.getDirname( r"/home/allen/work/digitRecognise/com/huitong/gasMeterv1", imgdirname) pattern = r'.*\.jpg$' filelist = FileNameUtil.getPathFilenameList(imgdirname, pattern) bkgLower, bkgUpper = ImageTool.getBlackColorRangeBGR() redLower, redUpper = ImageTool.getRedColorRangeBGR() for filename in filelist: print(filename) image = cv2.imread(filename) image = ImageTool.preProcessImage(image) blackBackgroundBoxCornerPoint = ImageTool.getInterestBoxCornerPointByColor( image, bkgLower, bkgUpper) if blackBackgroundBoxCornerPoint is None: raise ValueError("no color is black") bkgBox = ImageTool.getBoxFromBoxCorner(blackBackgroundBoxCornerPoint) bkgimage = ImageTool.getCropImageByBoxCornerPoint( image, blackBackgroundBoxCornerPoint) redBoxCornerPoint = ImageTool.getInterestBoxCornerPointByColor( bkgimage, redLower, redUpper) if redBoxCornerPoint is not None: redBox = ImageTool.getBoxFromBoxCorner(redBoxCornerPoint) box = (0, 0, redBox[0], bkgBox[3] - bkgBox[1]) interestImage = ImageTool.getCropImageByBox(bkgimage, box) interestImage = ImageTool.imageResize(interestImage, 128, 64) else: interestImage = bkgimage # title = "box area, %s, background" % FileNameUtil.getFilenameFromFullFilepathname(filename) # bkgimage = cv2.cvtColor(bkgimage,cv2.COLOR_BGR2RGB) # # plt.figure() # plt.title(title) # plt.imshow(bkgimage) # plt.show() title = "box area, %s, digit area,%s" % ( FileNameUtil.getFilenameFromFullFilepathname(filename), str(interestImage.shape)) grayImage = cv2.cvtColor(interestImage, cv2.COLOR_BGR2GRAY) plt.figure() plt.title(title) plt.imshow(grayImage) plt.show()