def detectCarplate(cv_image=None, pil_image=None, image_path=None, threshold_method='otsu'): ''' wrapped method to get the result of a car plate pseudo: convert into cv_image then find contours process the image into threshold image (only binary color) foreach contour image: put into the network save in an array sort the array (optional) convert to string and return the string :param cv_image: image in cv2 form (numpy) :param pil_image: image in pil form (Image) :param image_path: image path (str) :param threshold_method: (best threshold so far) :return: a string of car plate number ''' if cv_image is None and pil_image is None and image_path is None: raise Exception('one of them must not be None') number = 1 if threshold_method == 'normal': number = 2 if threshold_method == 'normal-otsu': number = 3 if cv_image is not None: result = '' img = resize(cv_image, 320) result_character = findContours(img, number) if number == 1: currentImg = ip2.preprocessOtsuThreshold(img) elif number == 2: currentImg = ip2.preprocessWithThreshold(img) else: currentImg = ip2.preprocessWithThreshold(ip2.preprocessWithThreshold(img)) for character in result_character: #type: cu.Character x, y, w, h = character.boundingRect blurredImage = ip2.gaussianBlur( ip2.resizeWithSize(ip2.addBorder(ip2.addPadding(currentImg[y:y+h, x:x+w]), 8), (32, 32)), 3) caffe_image = singleimage.cv2caffe(blurredImage) character.index, character.score = singleimage.getProb(net, caffe_image) character.score = character.score[0][character.index] result_character = sortresult.sortCarplateResult(result_character) for character in result_character: result += singleimage.convertIndex(character.index) cv2.imwrite('test/'+str(character.index)+'-'+str(character.identity)+'.png', character.getImage(img)) if len(result) <= 4 and number == 1: return detectCarplate(cv_image, threshold_method='normal-otsu') elif len(result) <= 4 and number == 3: return detectCarplate(cv_image, threshold_method='normal') elif len(result) <= 4 and number == 2: return result return result elif pil_image is not None: return detectCarplate(cv_image=ip2.PIL2CVImage(pil_image), threshold_method=threshold_method) else: return detectCarplate(cv_image=cv2.imread(image_path), threshold_method=threshold_method)
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 compute(self, thresh=150, selection=1): ori_path = self.ori_path if ori_path == '': return save_path = 'temp.png' compare1_img = None compare2_img = None compare3_img = None if selection == 1: compare1_img = ip2.preprocessWithThreshold( resize(cv2.imread(ori_path), 320), thresh) compare3_img = ip2.preprocessWithSpecialThresh( resize(cv2.imread(ori_path), 320), thresh) if selection == 1 or selection == 2: compare2_img = ip2.erodePreprocessThreshold( resize(cv2.imread(ori_path), 320), thresh, self.sliderValue) print 'enter here' if compare1_img is not None: cv2.imwrite(save_path, compare1_img) self.compare1.setPixmap(QtGui.QPixmap(QtCore.QString(save_path))) cv2.imwrite(save_path, compare2_img) self.compare2.setPixmap(QtGui.QPixmap(QtCore.QString(save_path))) if compare3_img is not None: cv2.imwrite(save_path, compare3_img) self.compare3.setPixmap(QtGui.QPixmap(QtCore.QString(save_path)))
def recursiveChecking(thresh_img, ori_img, min_thresh, total, offset_x, offset_y): if min_thresh <= 180: contours, hierarchy = ip2.findContours(thresh_img) for cnt in contours: char = Character(cnt) if char.isMaybeSize(): if char.aspectRatio >= MAX_ASPECTRATIO: crop_img = ori_img[char.y:char.y+char.h, char.x:char.x+char.w] crop_img = cc.inRangeOfWhite(crop_img) new_thresh_img = ip2.preprocessWithThreshold(crop_img, min_thresh+10) recursiveChecking(new_thresh_img, crop_img, min_thresh+10, total, offset_x+char.x, offset_y+char.y) else: char.setXY(offset_x+char.x, offset_y+char.y) # char.increaseSize() total.__add__(char)
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