Package: fadoudou2
Paddleocr Package1 Get started quickly1.1 install packageinstall by pypipipinstall"paddleocr>=2.0.1"# Recommend to use version 2.0.1+build own whl package and installpython3setup.pybdist_wheel
pip3installdist/paddleocr-x.x.x-py3-none-any.whl# x.x.x is the version of paddleocr2 Use2.1 Use by codeThe paddleocr whl package will automatically download the ppocr lightweight model as the default model, which can be customized and replaced according to the section 3Custom Model.detection angle classification and recognitionfrompaddleocrimportPaddleOCR,draw_ocr# Paddleocr supports Chinese, English, French, German, Korean and Japanese.# You can set the parameter `lang` as `ch`, `en`, `french`, `german`, `korean`, `japan`# to switch the language model in order.ocr=PaddleOCR(use_angle_cls=True,lang='en')# need to run only once to download and load model into memoryimg_path='PaddleOCR/doc/imgs_en/img_12.jpg'result=ocr.ocr(img_path,cls=True)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)# draw resultfromPILimportImageresult=result[0]image=Image.open(img_path).convert('RGB')boxes=[line[0]forlineinresult]txts=[line[1][0]forlineinresult]scores=[line[1][1]forlineinresult]im_show=draw_ocr(image,boxes,txts,scores,font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show=Image.fromarray(im_show)im_show.save('result.jpg')Output will be a list, each item contains bounding box, text and recognition confidence[[[442.0,173.0],[1169.0,173.0],[1169.0,225.0],[442.0,225.0]],['ACKNOWLEDGEMENTS',0.99283075]][[[393.0,340.0],[1207.0,342.0],[1207.0,389.0],[393.0,387.0]],['We would like to thank all the designers and',0.9357758]][[[399.0,398.0],[1204.0,398.0],[1204.0,433.0],[399.0,433.0]],['contributors whohave been involved in the',0.9592447]]......Visualization of resultsdetection and recognitionfrompaddleocrimportPaddleOCR,draw_ocrocr=PaddleOCR(lang='en')# need to run only once to download and load model into memoryimg_path='PaddleOCR/doc/imgs_en/img_12.jpg'result=ocr.ocr(img_path,cls=False)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)# draw resultfromPILimportImageresult=result[0]image=Image.open(img_path).convert('RGB')boxes=[line[0]forlineinresult]txts=[line[1][0]forlineinresult]scores=[line[1][1]forlineinresult]im_show=draw_ocr(image,boxes,txts,scores,font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show=Image.fromarray(im_show)im_show.save('result.jpg')Output will be a list, each item contains bounding box, text and recognition confidence[[[442.0,173.0],[1169.0,173.0],[1169.0,225.0],[442.0,225.0]],['ACKNOWLEDGEMENTS',0.99283075]][[[393.0,340.0],[1207.0,342.0],[1207.0,389.0],[393.0,387.0]],['We would like to thank all the designers and',0.9357758]][[[399.0,398.0],[1204.0,398.0],[1204.0,433.0],[399.0,433.0]],['contributors whohave been involved in the',0.9592447]]......Visualization of resultsclassification and recognitionfrompaddleocrimportPaddleOCRocr=PaddleOCR(use_angle_cls=True,lang='en')# need to run only once to load model into memoryimg_path='PaddleOCR/doc/imgs_words_en/word_10.png'result=ocr.ocr(img_path,det=False,cls=True)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)Output will be a list, each item contains recognition text and confidence['PAIN',0.990372]only detectionfrompaddleocrimportPaddleOCR,draw_ocrocr=PaddleOCR()# need to run only once to download and load model into memoryimg_path='PaddleOCR/doc/imgs_en/img_12.jpg'result=ocr.ocr(img_path,rec=False)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)# draw resultfromPILimportImageresult=result[0]image=Image.open(img_path).convert('RGB')im_show=draw_ocr(image,result,txts=None,scores=None,font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show=Image.fromarray(im_show)im_show.save('result.jpg')Output will be a list, each item only contains bounding box[[756.0,812.0],[805.0,812.0],[805.0,830.0],[756.0,830.0]][[820.0,803.0],[1085.0,801.0],[1085.0,836.0],[820.0,838.0]][[393.0,801.0],[715.0,805.0],[715.0,839.0],[393.0,836.0]]......Visualization of resultsonly recognitionfrompaddleocrimportPaddleOCRocr=PaddleOCR(lang='en')# need to run only once to load model into memoryimg_path='PaddleOCR/doc/imgs_words_en/word_10.png'result=ocr.ocr(img_path,det=False,cls=False)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)Output will be a list, each item contains recognition text and confidence['PAIN',0.990372]only classificationfrompaddleocrimportPaddleOCRocr=PaddleOCR(use_angle_cls=True)# need to run only once to load model into memoryimg_path='PaddleOCR/doc/imgs_words_en/word_10.png'result=ocr.ocr(img_path,det=False,rec=False,cls=True)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)Output will be a list, each item contains classification result and confidence['0',0.99999964]2.2 Use by command lineshow help informationpaddleocr-hdetection classification and recognitionpaddleocr--image_dirPaddleOCR/doc/imgs_en/img_12.jpg--use_angle_clstrue--langenOutput will be a list, each item contains bounding box, text and recognition confidence[[[441.0,174.0],[1166.0,176.0],[1165.0,222.0],[441.0,221.0]],('ACKNOWLEDGEMENTS',0.9971134662628174)][[[403.0,346.0],[1204.0,348.0],[1204.0,384.0],[402.0,383.0]],('We would like to thank all the designers and',0.9761400818824768)][[[403.0,396.0],[1204.0,398.0],[1204.0,434.0],[402.0,433.0]],('contributors who have been involved in the',0.9791957139968872)]......pdf file is also supported, you can infer the first few pages by using thepage_numparameter, the default is 0, which means infer all pagespaddleocr--image_dir./xxx.pdf--use_angle_clstrue--use_gpufalse--page_num2detection and recognitionpaddleocr--image_dirPaddleOCR/doc/imgs_en/img_12.jpg--langenOutput will be a list, each item contains bounding box, text and recognition confidence[[[441.0,174.0],[1166.0,176.0],[1165.0,222.0],[441.0,221.0]],('ACKNOWLEDGEMENTS',0.9971134662628174)][[[403.0,346.0],[1204.0,348.0],[1204.0,384.0],[402.0,383.0]],('We would like to thank all the designers and',0.9761400818824768)][[[403.0,396.0],[1204.0,398.0],[1204.0,434.0],[402.0,433.0]],('contributors who have been involved in the',0.9791957139968872)]......classification and recognitionpaddleocr--image_dirPaddleOCR/doc/imgs_words_en/word_10.png--use_angle_clstrue--detfalse--langenOutput will be a list, each item contains text and recognition confidence['PAIN',0.9934559464454651]only detectionpaddleocr--image_dirPaddleOCR/doc/imgs_en/img_12.jpg--recfalseOutput will be a list, each item only contains bounding box[[397.0,802.0],[1092.0,802.0],[1092.0,841.0],[397.0,841.0]][[397.0,750.0],[1211.0,750.0],[1211.0,789.0],[397.0,789.0]][[397.0,702.0],[1209.0,698.0],[1209.0,734.0],[397.0,738.0]]......only recognitionpaddleocr--image_dirPaddleOCR/doc/imgs_words_en/word_10.png--detfalse--langenOutput will be a list, each item contains text and recognition confidence['PAIN',0.9934559464454651]only classificationpaddleocr--image_dirPaddleOCR/doc/imgs_words_en/word_10.png--use_angle_clstrue--detfalse--recfalseOutput will be a list, each item contains classification result and confidence['0',0.99999964]3 Use custom modelWhen the built-in model cannot meet the needs, you need to use your own trained model.
First, refer toexportdoc to convert your det and rec model to inference model, and then use it as follows3.1 Use by codefrompaddleocrimportPaddleOCR,draw_ocr# The path of detection and recognition model must contain model and params filesocr=PaddleOCR(det_model_dir='{your_det_model_dir}',rec_model_dir='{your_rec_model_dir}',rec_char_dict_path='{your_rec_char_dict_path}',cls_model_dir='{your_cls_model_dir}',use_angle_cls=True)img_path='PaddleOCR/doc/imgs_en/img_12.jpg'result=ocr.ocr(img_path,cls=True)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)# draw resultfromPILimportImageresult=result[0]image=Image.open(img_path).convert('RGB')boxes=[line[0]forlineinresult]txts=[line[1][0]forlineinresult]scores=[line[1][1]forlineinresult]im_show=draw_ocr(image,boxes,txts,scores,font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show=Image.fromarray(im_show)im_show.save('result.jpg')3.2 Use by command linepaddleocr--image_dirPaddleOCR/doc/imgs/11.jpg--det_model_dir{your_det_model_dir}--rec_model_dir{your_rec_model_dir}--rec_char_dict_path{your_rec_char_dict_path}--cls_model_dir{your_cls_model_dir}--use_angle_clstrue4 Use web images or numpy array as input4.1 Web imageUse by codefrompaddleocrimportPaddleOCR,draw_ocrocr=PaddleOCR(use_angle_cls=True,lang="ch")# need to run only once to download and load model into memoryimg_path='http://n.sinaimg.cn/ent/transform/w630h933/20171222/o111-fypvuqf1838418.jpg'result=ocr.ocr(img_path,cls=True)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)# show resultfromPILimportImageresult=result[0]image=Image.open(img_path).convert('RGB')boxes=[line[0]forlineinresult]txts=[line[1][0]forlineinresult]scores=[line[1][1]forlineinresult]im_show=draw_ocr(image,boxes,txts,scores,font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show=Image.fromarray(im_show)im_show.save('result.jpg')Use by command linepaddleocr--image_dirhttp://n.sinaimg.cn/ent/transform/w630h933/20171222/o111-fypvuqf1838418.jpg--use_angle_cls=true4.2 Numpy arraySupport numpy array as input only when used by codeimportcv2frompaddleocrimportPaddleOCR,draw_ocr,download_with_progressbarocr=PaddleOCR(use_angle_cls=True,lang="ch")# need to run only once to download and load model into memoryimg_path='PaddleOCR/doc/imgs/11.jpg'img=cv2.imread(img_path)# img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY), If your own training model supports grayscale images, you can uncomment this lineresult=ocr.ocr(img,cls=True)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)# show resultfromPILimportImageresult=result[0]download_with_progressbar(img_path,'tmp.jpg')image=Image.open('tmp.jpg').convert('RGB')boxes=[line[0]forlineinresult]txts=[line[1][0]forlineinresult]scores=[line[1][1]forlineinresult]im_show=draw_ocr(image,boxes,txts,scores,font_path='/path/to/PaddleOCR/doc/fonts/simfang.ttf')im_show=Image.fromarray(im_show)im_show.save('result.jpg')5 PDF fileUse by command lineyou can infer the first few pages by using thepage_numparameter, the default is 0, which means infer all pagespaddleocr--image_dir./xxx.pdf--use_angle_clstrue--use_gpufalse--page_num2Use by codefrompaddleocrimportPaddleOCR,draw_ocr# Paddleocr supports Chinese, English, French, German, Korean and Japanese.# You can set the parameter `lang` as `ch`, `en`, `fr`, `german`, `korean`, `japan`# to switch the language model in order.ocr=PaddleOCR(use_angle_cls=True,lang="ch"，page_num=2)# need to run only once to download and load model into memoryimg_path='./xxx.pdf'result=ocr.ocr(img_path,cls=True)foridxinrange(len(result)):res=result[idx]forlineinres:print(line)# draw resultimportfitzfromPILimportImageimportcv2importnumpyasnpimgs=[]withfitz.open(img_path)aspdf:forpginrange(0,pdf.pageCount):page=pdf[pg]mat=fitz.Matrix(2,2)pm=page.getPixmap(matrix=mat,alpha=False)# if width or height > 2000 pixels, don't enlarge the imageifpm.width>2000orpm.height>2000:pm=page.getPixmap(matrix=fitz.Matrix(1,1),alpha=False)img=Image.frombytes("RGB",[pm.width,pm.height],pm.samples)img=cv2.cvtColor(np.array(img),cv2.COLOR_RGB2BGR)imgs.append(img)foridxinrange(len(result)):res=result[idx]image=imgs[idx]boxes=[line[0]forlineinres]txts=[line[1][0]forlineinres]scores=[line[1][1]forlineinres]im_show=draw_ocr(image,boxes,txts,scores,font_path='doc/fonts/simfang.ttf')im_show=Image.fromarray(im_show)im_show.save('result_page_{}.jpg'.format(idx))6 Parameter DescriptionParameterDescriptionDefault valueuse_gpuuse GPU or notTRUEgpu_memGPU memory size used for initialization8000Mimage_dirThe images path or folder path for predicting when used by the command linepage_numValid when the input type is pdf file, specify to predict the previous page_num pages, all pages are predicted by default0det_algorithmType of detection algorithm selectedDBdet_model_dirthe text detection inference model folder. There are two ways to transfer parameters, 1. None: Automatically download the built-in model to~/.paddleocr/det; 2. The path of the inference model converted by yourself, the model and params files must be included in the model pathNonedet_max_side_lenThe maximum size of the long side of the image. When the long side exceeds this value, the long side will be resized to this size, and the short side will be scaled proportionally960det_db_threshBinarization threshold value of DB output map0.3det_db_box_threshThe threshold value of the DB output box. Boxes score lower than this value will be discarded0.5det_db_unclip_ratioThe expanded ratio of DB output box2det_db_score_modeThe parameter that control how the score of the detection frame is calculated. There are 'fast' and 'slow' options. If the text to be detected is curved, it is recommended to use 'slow''fast'det_east_score_threshBinarization threshold value of EAST output map0.8det_east_cover_threshThe threshold value of the EAST output box. Boxes score lower than this value will be discarded0.1det_east_nms_threshThe NMS threshold value of EAST model output box0.2rec_algorithmType of recognition algorithm selectedCRNNrec_model_dirthe text recognition inference model folder. There are two ways to transfer parameters, 1. None: Automatically download the built-in model to~/.paddleocr/rec; 2. The path of the inference model converted by yourself, the model and params files must be included in the model pathNonerec_image_shapeimage shape of recognition algorithm"3,32,320"rec_batch_numWhen performing recognition, the batchsize of forward images30max_text_lengthThe maximum text length that the recognition algorithm can recognize25rec_char_dict_paththe alphabet path which needs to be modified to your own path whenrec_model_Nameuse mode 2./ppocr/utils/ppocr_keys_v1.txtuse_space_charWhether to recognize spacesTRUEdrop_scoreFilter the output by score (from the recognition model), and those below this score will not be returned0.5use_angle_clsWhether to load classification modelFALSEcls_model_dirthe classification inference model folder. There are two ways to transfer parameters, 1. None: Automatically download the built-in model to~/.paddleocr/cls; 2. The path of the inference model converted by yourself, the model and params files must be included in the model pathNonecls_image_shapeimage shape of classification algorithm"3,48,192"label_listlabel list of classification algorithm['0','180']cls_batch_numWhen performing classification, the batchsize of forward images30enable_mkldnnWhether to enable mkldnnFALSEuse_zero_copy_runWhether to forward by zero_copy_runFALSElangThe support language, now only Chinese(ch)、English(en)、French(french)、German(german)、Korean(korean)、Japanese(japan) are supportedchdetEnable detction whenppocr.ocrfunc execTRUErecEnable recognition whenppocr.ocrfunc execTRUEclsEnable classification whenppocr.ocrfunc exec((Use use_angle_cls in command line mode to control whether to start classification in the forward direction)FALSEshow_logWhether to print logFALSEtypePerform ocr or table structuring, the value is selected in ['ocr','structure']ocrocr_versionOCR Model version number, the current model support list is as follows: PP-OCRv3 supports Chinese and English detection, recognition, multilingual recognition, direction classifier models, PP-OCRv2 support Chinese detection and recognition model, PP-OCR support Chinese detection, recognition and direction classifier, multilingual recognition modelPP-OCRv3