def ocr(img_path: str, config_str: str) -> str: """ Wrapper for Tesseract image_to_string function Parameters ---------- img_path : str Path to the image config_str: str Config string for tesseract Returns ------- str Text recognized on the image Contribute ---------- PyTesseract: https://pypi.org/project/pytesseract/ """ # config=r'--oem 3 --psm 13' # tessdata_dir_config = r'--tessdata-dir "/c/work/Enigmatos/Projects/lpr-poc/results"' # print("tessdata_dir_config: {}",tessdata_dir_config) # return pytesseract.image_to_string(Image.open(img_path), config=tessdata_dir_config) recognized_txt = '' try: recognized_txt = pytesseract.image_to_string(Image.open(img_path), config=config_str) except Exception as e: logger.debug("ocr: error: e '{}'".format(e)) logger.debug("ocr: recognized_txt '{}'".format(recognized_txt)) return recognized_txt
def delete(id): car_picture = PictureWrapper.query.get_or_404(id) try: logger.debug("Delete id:'{}'".format(id)) db.session.delete(car_picture) db.session.commit() return redirect('/') except: logger.error("Update There was a problem deleting data id:'{}'".format(id)) return "There was a problem deleting data."
def update(id): car_picture = PictureWrapper.query.get_or_404(id) if request.method == 'POST': logger.debug("POST Update id:'{}'".format(id)) car_picture.name = request.form['name'] try: db.session.commit() return redirect('/') except: logger.error("Update There was a problem updating data id:'{}'".format(id)) return "There was a problem updating data." else: logger.debug("GET Update id:'{}'".format(id)) title = "Update Data" return render_template('update.html', title=title, car_picture=car_picture)
def invoke_lpr_eng(name, picture_path): logger.debug("invoke_lpr_eng for picture_path:'{}'".format(picture_path)) config = r'--psm 13' recognized_txt, small_pictures = lpr_utils.license_plate_recognition( img_path=picture_path, new_size=None, # blurring_method=alpr.bilateral_filter, # binarization_method=alpr.adaptive_threshold # blurring_method=alpr.gaussian_blur, # binarization_method=alpr.adaptive_threshold blurring_method=lpr_utils.median_blur, binarization_method=lpr_utils.adaptive_threshold, config_str=config) logger.debug("Going to update DB with new picture: '{}', '{}' ".format( name, picture_path)) new_picture = PictureWrapper(name=name, picture_path=picture_path, recognized_txt=recognized_txt, small_pictures=str(small_pictures)) db.session.add(new_picture) db.session.commit() return new_picture
def index(): if request.method == 'POST': try: logger.debug("POST request.url:'{}', request.files['file']: '{}'".format(request.url, request.files['file'])) # New picture upload if 'file' not in request.files: flash('No file part') logger.error("No file part") return redirect(request.url) file = request.files['file'] if file.filename == '': flash('No image selected for uploading') logger.error("No image selected for uploading") return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) picture_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(picture_path) #print('upload_image filename: ' + filename) name = os.path.splitext(filename)[0] # new_picture = PictureWrapper(name = name, picture_path = picture_path) # invoke lpr_engine and save small_pictures list and OCR text logger.debug("Calling invoke_lpr_eng for picture_path:'{}'".format(picture_path)) invoke_lpr_eng(name = name, picture_path = picture_path) # new_picture.invoke_lpr_eng() flash('Image successfully uploaded and displayed') logger.debug("Image successfully uploaded and displayed".format(name, picture_path)) return redirect(request.url) else: flash('Allowed image types are -> png, jpg, jpeg, gif') logger.error("Allowed image types are -> png, jpg, jpeg, gif: '{}'".format(file.filename)) return redirect(request.url) return redirect(request.url) except: flash('There was a problem adding new picture.') logger.error("There was a problem adding new picture.") return redirect(request.url) # return "There was a problem adding new stuff." else: # GET - render existing pictures logger.debug("GET request.url:'{}'".format(request.url)) car_pictures = PictureWrapper.query.order_by(PictureWrapper.created_at.desc()).all() # TODO: When small_pictures_list will be kept in DB, the below code should be cleaned # extract small_pictures from db and convert it to list as following: # a=car_picture.small_pictures.strip('[]') # pictures_list=a.split() for car_picture in car_pictures: # car_picture.large_picture = os.path.join(app.config['UPLOAD_FOLDER'], car_picture.name + ".jpg") # for small_picture in glob.glob('static/pictures_photo/' + name + '*'): logger.debug("car_picture.name: {} car_picture.picture_path from DB: {}".format(car_picture.name, car_picture.picture_path)) pictures_list = glob.glob(os.path.join(app.config['UPLOAD_FOLDER'], car_picture.name + "*")) for picture_path in pictures_list: small_picture_name = os.path.basename(picture_path) small_picture_name = os.path.splitext(small_picture_name)[0] if small_picture_name == car_picture.name: car_picture.picture_path = picture_path pictures_list.remove(picture_path) car_picture.small_pictures = pictures_list logger.debug("car_picture.picture_path: {}".format(car_picture.picture_path)) logger.debug("car_picture.small_pictures: {}".format(car_picture.small_pictures)) return render_template('index.html', car_pictures=car_pictures)
def license_plate_recognition(img_path: str, new_size: tuple, blurring_method: Callable, binarization_method: Callable, config_str: str = "") -> str: """ Automatic license plate recognition algorithm. Found license plate is stored in ./results/ directiory as license_plate.jpg Parameters ---------- img_path : str Path to the image new_size : tuple of integers First argument of the tuple is new width, second is the new height of the image blurring_method : function Function as an object. Suggested functions from this module: gaussian_blur, median_blur, bilateral_filter binarization_method : function Function as an object. Suggested functions from this module: threshold_otsu, adaptive_threshold, canny, auto_canny config_str: str="" Config string for tesseract Returns ------- str Text recognized on the image """ logger.debug("lpr: img_path: '{}' ".format(img_path)) image = cv2.imread(img_path) binary_img = preprocess(image, new_size, blurring_method, binarization_method) recognized_txt = 'None' small_pictures = [] plate_cnts = plate_contours(binary_img) logger.debug( "lpr: plate_contours(binary_img) ok img_path: '{}' ".format(img_path)) if len(plate_cnts) == 0: logger.debug( "lpr: len(plate_cnts) == 0, return img_path: '{}' ".format( img_path)) return (recognized_txt, small_pictures) i = 0 for c in plate_cnts: cropped = crop_image(image, c) logger.debug("lpr: crop_image ok img_path: '{}' ".format(img_path)) cropped = prepare_ocr(cropped) logger.debug( "lpr: prepare_ocr(cropped) ok img_path: '{}' ".format(img_path)) picture_dir_name = os.path.dirname(img_path) small_picture_name = os.path.basename(img_path) small_picture_name_no_ext = os.path.splitext(small_picture_name)[0] picture_file_name = small_picture_name_no_ext + "_" + str(i) img_path = utils.save_image_plt(picture_dir_name, picture_file_name, cropped) logger.debug( "lpr: save_image_plt picture_dir_name: '{}' picture_file_name: '{}'" .format(picture_dir_name, picture_file_name)) small_pictures.append(img_path) i = i + 1 recognized_txt = utils.remove_special_chars(ocr(img_path, config_str)) logger.debug("lpr: ocr(img_path, config_str) ok: '{}' '{}'".format( img_path, config_str)) if len(recognized_txt) > 0: logger.debug( "lpr: ok: (recognized_txt, small_pictures) '{}' '{}'".format( recognized_txt, small_pictures)) return (recognized_txt, small_pictures) return (recognized_txt, small_pictures)