def eliminateSize(characters, display=None): indexRemove = set([]) currentCount = 0 for cnt in characters: count = 0 while count < len(characters): if count == currentCount: count += 1 continue compareCnt = characters[count] w1, h1 = cnt.w, cnt.h w2, h2 = compareCnt.w, compareCnt.h changeInWidth = abs(w2-w1)/float(w2) changeInHeight = abs(h2-h1)/float(h2) if changeInWidth > cu.MAX_CHANGE_IN_WIDTH: if changeInHeight > cu.MAX_CHANGE_IN_HEIGHT: if h1 > h2: indexRemove.add(currentCount) else: indexRemove.add(count) count += 1 currentCount += 1 indexRemove = sorted(indexRemove, reverse=True, key=int) for i in indexRemove: if display is not None: ip2.drawRect(display, characters[i].boundingRect, RED) del characters[i] cv2.imshow('after_delete_size_red', display)
def detectCharacter(file): img = cv2.imread(file) if img is not None: showimg = img.copy() result_characters = main.findContours(img, main.OTSU) for character in result_characters: x, y, w, h = character.boundingRect ip2.drawRect(showimg, character.boundingRect, (255, 0, 255), 10) blurredImage = ip2.gaussianBlur( ip2.resizeWithSize( ip2.addBorder(ip2.addPadding(img[y:y + h, x:x + w]), 8), (32, 32)), 3) caffe_image = singleimage.cv2caffe(blurredImage) character.index, character.score = singleimage.getProb( main.net, caffe_image) character.score = character.score[0][character.index] sortresult.sortCarplateResult(result_characters) result = '' for character in result_characters: char = singleimage.convertIndex(character.index) result += char ip2.addText(showimg, char, (character.x - 30, character.y - 30), 20) if character.isLastChar: result += '\n' return result, showimg
def eliminateNonMaybe(ori_img, maybeCharacter, total, min_thresh, number, display=None): if number is 1: thresh_img = ip2.preprocessOtsuThreshold(ori_img) elif number is 2: thresh_img = ip2.preprocessWithThreshold(ori_img, min_thresh) else: thresh_img = ip2.preprocessOtsuThreshold(ip2.preprocessWithThreshold(ori_img)) cu.normalChecking(thresh_img, ori_img, total, 150, display) for t in total.getArray(): if display is not None: ip2.drawRect(display, t.boundingRect, MAGENTA) maybeCharacter.append(t)
def eliminateArea(characters, display=None): indexRemove = set([]) avgArea = cu.findAvgArea(characters) threshArea = avgArea/cu.THRESH_AREA_DENOMINATOR count = 0 for cnt in characters: if cnt.area < threshArea: indexRemove.add(count) count += 1 indexRemove = sorted(indexRemove, reverse=True, key=int) for i in indexRemove: if display is not None: ip2.drawRect(display, characters[i].boundingRect, PURPLE) del characters[i] cv2.imshow('after_delete_area_purple', display)
def eliminateDiagonal(characters, display=None): indexRemove = set([]) avgDiagonal = cu.findAvgDiagonal(characters) threshDiagonal = avgDiagonal/cu.THRESH_DIAGONAL_DENOMINATOR count = 0 for cnt in characters: if cnt.diagonalSize < threshDiagonal: indexRemove.add(count) count += 1 indexRemove = sorted(indexRemove, reverse=True, key=int) for i in indexRemove: if display is not None: ip2.drawRect(display, characters[i].boundingRect, YELLOW) del characters[i] cv2.imshow('after_delete_diagonal_yellow', display)
def eliminateNewArea(characters, display=None): indexRemove = set([]) count = 0 median_area = cu.findMedianArea(characters) for cnt in characters: area = cnt.area changeInArea = abs(median_area-area)/median_area if changeInArea > cu.MAX_CHANGE_IN_NEW_AREA: indexRemove.add(count) count += 1 indexRemove = sorted(indexRemove, reverse=True, key=int) for i in indexRemove: if display is not None: ip2.drawRect(display, characters[i].boundingRect, MAROON) del characters[i] if display is not None: cv2.imshow('new_area_maroon', display)
def normalChecking(thresh_img, ori_img, total, min_thresh, display=None): ''' 1. find contours and create cu.Character array 2. eliminate impossible size character :param thresh_img: :param ori_img: :param total: :param min_thresh: :param display: :return: ''' contours, hierarchy = ip2.findContours(thresh_img) for cnt in contours: char = Character(cnt) if display is not None: ip2.drawRect(display, char.boundingRect, ip2.WHITE) if char.isMaybeSize(): if char.aspectRatio <= MAX_ASPECTRATIO: # char.increaseSize() total.__add__(char)
def eliminateDistance(characters, display=None): indexRemove = set([]) count = 0 median_distance = cu.findMedianDistance(characters) # sort by x already for i in xrange(len(characters)-1): cnt1 = characters[i] cnt2 = characters[i+1] distance = cnt1.distanceFromCenter(cnt2) changeInDistance = abs(distance-median_distance)/median_distance if changeInDistance > cu.MAX_CHANGE_IN_DISTANCE: indexRemove.add(count) count += 1 indexRemove = sorted(indexRemove, reverse=True, key=int) for i in indexRemove: if display is not None: ip2.drawRect(display, characters[i].boundingRect, INDIGO) del characters[i] if display is not None: cv2.imshow('distance_indigo', display)
def eliminateNewSize(characters, display=None): ''' eliminate size which small than max change height :param characters: :param display: :return: ''' indexRemove = set([]) count = 0 median_h = cu.findMedianHeight(characters) for cnt in characters: h = cnt.h changeInHeight = float(abs(median_h-h))/median_h if changeInHeight > cu.MAX_CHANGE_IN_NEW_HEIGHT: indexRemove.add(count) count += 1 indexRemove = sorted(indexRemove, reverse=True, key=int) for i in indexRemove: if display is not None: ip2.drawRect(display, characters[i].boundingRect, GREEN) del characters[i] if display is not None: cv2.imshow('new_size_green', display)
def drawAllChar(self, display, color=ip2.GREEN): for char in self.charArray: ip2.drawRect(display, char.boundingRect, color)
def guiRun(file, number): ''' used by gui :param file: image path :param number: threshold method, 1-'otsu' 2-'threshold' 3-'normal-otsu' :return: ''' img = cv2.imread(file) if img is not None: img = resize(img, 320) cv2.imwrite('gui/resize.png', img) # display = img.copy() characters = findContours(img, number) greyscale = ip2.greyscale(img) white = cc.newInRangeOfWhite(img) threshold = ip2.preprocessWithThreshold(img) threshold_otsu = ip2.preprocessOtsuThreshold(img) otsu_normal = ip2.preprocessOtsuThreshold(threshold) if number == 1: contoursImage = threshold_otsu.copy() currentImage = threshold_otsu.copy() elif number == 2: contoursImage = threshold.copy() currentImage = threshold.copy() else: contoursImage = otsu_normal.copy() currentImage = otsu_normal.copy() contoursImage = ip2.colour(contoursImage) indices = [] scores = [] for cnt in characters: #type: cu.Character x, y, w, h = cnt.boundingRect ip2.drawRect(contoursImage, (x, y, w, h), ip2.RED) cv_image = ip2.resizeWithSize(ip2.addBorder(ip2.addPadding(currentImage[y:y+h, x:x+w]), 8), (32, 32)) caffe_image = singleimage.cv2caffe(cv_image) prob, score = singleimage.getProb(net, caffe_image) cnt.index = int(prob) cnt.score = score[0][prob] indices.append(int(prob)) scores.append(score[0][prob]) # indices, scores = sortResultv2(characters, indices, scores) characters = sortresult.sortCarplateResult(characters) carplate_str = '' for cnt in characters: carplate_str += singleimage.convertIndex(cnt.index) if len(carplate_str) <= 4 and number == 1: return guiRun(file, 3) elif len(carplate_str) <= 4 and number == 3: return guiRun(file, 2) cv2.imwrite('gui/greyscale.png', greyscale) cv2.imwrite('gui/white.png', white) cv2.imwrite('gui/threshold.png', threshold) cv2.imwrite('gui/threshold_otsu.png', threshold_otsu) cv2.imwrite('gui/otsu_normal.png', otsu_normal) cv2.imwrite('gui/contours.png', contoursImage) cv2.imwrite('gui/current.png', currentImage) return carplate_str, characters, scores else: return '', None