Method, device and electronic device for real-time recognition of PDF text information of images and storage
By using technologies such as fitz library and OpenCV, the problems of high cost and poor recognition effect in the process of identifying image PDF text information in the existing technology are solved, and low-cost and efficient text information storage processing are achieved.
Patent Information
- Application Number
- CN202411353623.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-26
- Publication Date
- 2025-01-03
- Estimated Expiration
- 2044-09-26
AI Technical Summary
The prior art has high cost and strict requirements for files in the process of identifying picture-type PDF text information, so it is impossible to effectively identify pictures with irregular layout or manual photography.
Use the fitz library to open the PDF file, read the file through PyPDF2 and export the pictures on each page in batches, use OpenCV to perform edge cropping, combine the text interface to identify and parse the content, form a text file, and clean it to enter the library.
Reduces recognition costs, improves processing capabilities for complex files, reduces text statement errors, and achieves fast and ideal output.
Smart Images

Figure CN118887693B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a method, device and electronic equipment for real-time recognition of PDF text information of images into a database. Background Art
[0002] The PDF book text recognition solution is mainly based on OCR (Optical Character Recognition) technology, which aims to convert images in PDF files or text content in scanned documents into editable and searchable text formats.
[0003] The existing technology has the following problems in the process of real-time recognition of image-based PDF text information and storage:
[0004] 1. Currently, the solution for identifying PDF book files mainly uses paid software manufacturers such as WPS, which is relatively costly.
[0005] 2. When manufacturers identify PDF book files, they have relatively high requirements for the files and can only identify pictures with neat and clear text layout. For some manually taken pictures where the same page is divided into two sections, the recognition effect is poor.
[0006] The above problems become technical problems that need to be solved. Summary of the invention
[0007] In view of this, the embodiments of the present invention provide a method, device and electronic device for real-time recognition of image-based PDF text information and storage, which at least partially solve the problems existing in the prior art.
[0008] In a first aspect, an embodiment of the present invention provides a method for real-time recognition of image-based PDF text information and storage, comprising:
[0009] Use the fitz library to open the PDF file. After reading the file through PyPDF2, get the number of pages of the PDF file based on the getNumPages function in PyPDF2. Use the load_page event to read the images of each page in the PDF file, and export the images of each page in batches.
[0010] Read the folder of the target book, traverse all the exported images of the target book, use cv2.Canny to crop the edges of the images, identify the middle line in the image according to the situation of the target book, and cut each page of the image into two parts based on the middle line;
[0011] Use the text interface to recognize all the text, and store the picture page number and the left and right special serial numbers of each page together with the recognized content to form a first text file;
[0012] Reading the recognized first text file, parsing the content in the first text file, and obtaining a second text file containing independent semantic content;
[0013] The second text file is read, a cleaning operation is performed on the second text file, and a third text file obtained after the cleaning is put into storage.
[0014] According to a specific implementation of the embodiment of the present disclosure, the method of obtaining the number of pages of a PDF file based on the function getNumPages in PyPDF2 and using the load_page event to read the image of each page in the PDF file includes:
[0015] Create a PdfReader object to open the PDF file and provide the path of the PDF file as a parameter;
[0016] Get the integer value returned by PdfReader.getNumPages() as the total number of pages in the PDF file.
[0017] According to a specific implementation of the embodiment of the present disclosure, the method of obtaining the number of pages of a PDF file based on the function getNumPages in PyPDF2 and using the load_page event to read the image of each page in the PDF file also includes:
[0018] Traverse each page of the PDF file, use the load_page() method to load each page of images, use the get_images() method to extract all images on each page, and save each extracted image.
[0019] According to a specific implementation of the embodiment of the present disclosure, the folder of the target book is read, all exported pictures of the target book are traversed, the edge of the picture is cropped using cv2.Canny, the middle line in the picture is identified according to the situation of the target book, and each page of the picture is cropped into two parts based on the middle line, including:
[0020] Import the cv2 library, traverse the target book folder containing the exported images, and use cv2.imread() to load each image;
[0021] Use cv2.cvtColor() to convert the color image to a grayscale image and use cv2.Canny() function to detect the edges in the image;
[0022] Use cv2.HoughLines() to detect straight lines in the image and determine the middle line based on the position and length of the straight line;
[0023] The cropping position is determined based on the identified middle line, and the image is cropped using NumPy's slicing function.
[0024] According to a specific implementation of the embodiment of the present disclosure, the text interface is used to identify all texts, and the number of picture pages and the special serial number marks on the left and right of each page are stored together with the identified content to form a first text file, including:
[0025] Load the preprocessed image and use the OCR tool to recognize the text in the image;
[0026] Assign page numbers to each image and add left and right marks to the cropped images;
[0027] Create a text file to store the recognized content, and write the page number, left and right marks, and recognized text into the text file.
[0028] According to a specific implementation of the embodiment of the present disclosure, the reading of the recognized first text file, parsing the content in the first text file, and obtaining a second text file containing independent semantic content includes:
[0029] Determine the n sentences T={S 1 ,S 2 ,...,S n}, calculate the m words contained in all sentences w={w 1 ,w 2 ,…,w m}, the weight H(wi) of the i-th word is expressed as:
[0030]
[0031] Words to express The frequency of occurrence in the text of the first text file, α is the correction parameter;
[0032] For each sentence , its evaluation value for:
[0033]
[0034] Expressing sentences Length;
[0035] A threshold τ is set to filter out the second text file T' containing independent semantic content. T' is a set of sentences containing independent semantic content. T' is expressed as:
[0036] .
[0037] According to a specific implementation of the embodiment of the present disclosure, the reading of the recognized first text file, parsing the content in the first text file to obtain a second text file containing independent semantic content, further includes:
[0038] For each sentence , calculate its structural complexity score and thematic consistency scores :
[0039]
[0040] Expressing sentences The number of dependent clauses in , δ, β, γ are adjustment coefficients;
[0041]
[0042] Kn is the number of topics in the first text file, Expressing sentences The probability of being on the kth topic, represents the probability of the kth topic in the first text file T;
[0043] when and When the product of is greater than the preset value CT0, the sentence Placed in the second text file T'.
[0044] According to a specific implementation of the embodiment of the present disclosure, the reading of the second text file, performing a cleaning operation on the second text file, and storing the third text file obtained after cleaning includes:
[0045] Use the open() function to open the second text file in read mode, and use the read() method to read the contents of the second text file;
[0046] Use the string method .strip() to clean the text, use regular expressions to remove non-text content, and use stop word lists to filter the text;
[0047] Create a table in the database for the PDF file and construct an insert statement to insert the cleaned contents of the third text file into the database.
[0048] In a second aspect, an embodiment of the present invention provides a device for real-time recognition of image-based PDF text information storage, including:
[0049] The export module uses the fitz library to open the PDF file. After reading the file through PyPDF2, it obtains the number of pages of the PDF file based on the getNumPages function in PyPDF2, uses the load_page event to read the images of each page in the PDF file, and exports the images of each page in batches;
[0050] The cropping module reads the target book's folder, traverses all exported images of the target book, uses cv2.Canny to crop the edges of the images, identifies the middle line in the image according to the target book, and crops each page of the image into two parts based on the middle line;
[0051] The recognition module uses the text interface to recognize all the text, and stores the image page number and the special serial number marks on the left and right of each page together with the recognized content to form a first text file;
[0052] A parsing module reads the identified first text file, parses the content in the first text file, and obtains a second text file containing independent semantic content;
[0053] The storage module reads the second text file, performs a cleaning operation on the second text file, and performs storage processing on the third text file obtained after the cleaning.
[0054] In a third aspect, an embodiment of the present invention further provides an electronic device, the electronic device comprising:
[0055] at least one processor; and,
[0056] a memory communicatively connected to the at least one processor; wherein,
[0057] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for real-time recognition of image-type PDF text information and storage in any of the first aspects or any implementation of the first aspect.
[0058] In a fourth aspect, an embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the method for real-time recognition of image-type PDF text information into a database in the aforementioned first aspect or any implementation method of the first aspect.
[0059] In the fifth aspect, an embodiment of the present invention further provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method for real-time recognition of image-type PDF text information into a database in the aforementioned first aspect or any implementation method of the first aspect.
[0060] The solution for real-time recognition and warehousing of PDF text information of image type in the embodiment of the present invention includes: using the fitz library to open the PDF file, after reading the file through PyPDF2, obtaining the number of pages of the PDF file based on the function getNumPages in PyPDF2, using the load_page event to read the image of each page in the PDF file, and exporting the image of each page in batches; reading the folder of the target book, traversing all the exported images of the target book, using cv2.Canny to perform edge cropping on the image, recognizing the middle line in the image according to the situation of the target book, and cropping each page of the image into two parts based on the middle line; using the text interface to recognize all the text, and storing the number of image pages and the left and right special serial number marks of each page together with the recognized content to form a first text file; reading the recognized first text file, parsing the content in the first text file, and obtaining a second text file containing independent semantic content; reading the second text file, performing a cleaning operation on the second text file, and storing the third text file obtained after cleaning.
[0061] The solution of this application has the following beneficial effects:
[0062] The present invention can process some complex PDF files, customize and adjust different parameters according to different file conditions, reduce the probability of text sentence errors, and can quickly verify the results;
[0063] After the picture is processed step by step by the solution of the present invention, it is only necessary to call a low-cost interface to achieve a more ideal output than high-cost software processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0065] Figure 1 A flowchart of a method for real-time recognition of PDF text information of images into a database provided by an embodiment of the present invention;
[0066] Figure 2A flowchart of another method for real-time recognition of PDF text information of images and storage provided by an embodiment of the present invention;
[0067] Figure 3 A schematic diagram of the structure of a device for real-time recognition of PDF text information of images and storage provided by an embodiment of the present invention;
[0068] Figure 4 A schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0069] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0070] The following describes the embodiments of the present disclosure through specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all of the embodiments. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present disclosure. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present disclosure.
[0071] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.
[0072] It should also be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present disclosure. The drawings only show components related to the present disclosure rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0073] Additionally, in the following description, specific details are provided to facilitate a thorough understanding of the examples. However, it will be understood by those skilled in the art that the aspects described may be practiced without these specific details.
[0074] The disclosed embodiment provides a method for real-time recognition of PDF text information of images and storage. The method for real-time recognition of PDF text information of images and storage provided by the embodiment can be performed by a computing device, which can be implemented as software, or as a combination of software and hardware, and the computing device can be integrated in a server, terminal device, etc.
[0075] See also Figure 1 The embodiment of the present disclosure provides a method for real-time recognition of PDF text information of images and storage, including:
[0076] S101, use the fitz library to open the PDF file, read the file through PyPDF2, get the number of pages of the PDF file based on the getNumPages function in PyPDF2, use the load_page event to read the images of each page in the PDF file, and export the images of each page in batches.
[0077] You can create a PdfReader object to open a PDF file and provide the path of the PDF file as a parameter, and obtain the integer value returned by PdfReader.getNumPages() as the total number of pages in the PDF file.
[0078] You can traverse each page of the PDF file, use the load_page() method to load each page of images, use the get_images() method to extract all images on each page, and save each extracted image.
[0079] S102, read the folder of the target book, traverse all the exported images of the target book, use cv2.Canny to cut the edges of the images, identify the middle line in the image according to the situation of the target book, and cut each page of the image into two parts based on the middle line.
[0080] Specifically, the following steps may be included:
[0081] Step S1021: Read the folder of the target book
[0082] Import required libraries: Import os and cv2 (OpenCV) libraries.
[0083] Traverse Folders: Traverse the target book folders containing exported images.
[0084] Load images: Use cv2.imread() to load each image.
[0085] Step S1022: Use cv2.Canny for edge detection
[0086] Convert to Grayscale Image: Use cv2.cvtColor() to convert the color image to grayscale image.
[0087] Apply Canny Edge Detection: Use cv2.Canny() function to detect the edges in the image.
[0088] Step 3 S102: Identify the middle line
[0089] Finding straight lines: Use cv2.HoughLines() or cv2.HoughLinesP() to detect straight lines in the image.
[0090] Filter the middle line: Determine the middle line based on the position and length of the straight line.
[0091] Step 4: S102 cuts the image based on the middle line
[0092] Determine cutting position: Determine the cutting position based on the identified middle line.
[0093] Cropping images: Use NumPy's slicing function to crop images.
[0094] S103, using a text interface to identify all text, and store the picture page number and the special serial number marks on the left and right of each page together with the identified content to form a first text file.
[0095] The step of using a text recognition interface to recognize all text, and storing the image page number, the left and right marks of each page, and the recognized content to form a first text file includes:
[0096] Step S1031: Prepare OCR tool
[0097] Select OCR Tool: Choose a suitable OCR (Optical Character Recognition) tool like Tesseract OCR.
[0098] Install OCR Tool: Make sure the selected OCR tool is installed.
[0099] Step S1032: Recognize text
[0100] Load image: Load the preprocessed image.
[0101] Recognize text: Use OCR tools to recognize text in images.
[0102] Step S1033: Add page numbers and left and right marks
[0103] Determine number of pages: Assign a number of pages to each image.
[0104] Add left and right marks: Add left and right marks to the cropped image.
[0105] Step S1034: Storing the identified content
[0106] Create text file: Create a text file to store the recognized content.
[0107] Write content: write page numbers, left and right marks, and recognized text to a text file
[0108] S104, reading the recognized first text file, parsing the content in the first text file, and obtaining a second text file containing independent semantic content.
[0109] Specifically, it is possible to determine the n sentences T={S 1 ,S 2 ,...,S n}, calculate the m words contained in all sentences w={w 1 ,w 2 ,…,w m}, the weight H(wi) of the i-th word is expressed as:
[0110]
[0111] Words to express The frequency of occurrence in the text of the first text file, α is the correction parameter;
[0112] For each sentence , its evaluation value for:
[0113]
[0114] Expressing sentences Length;
[0115] A threshold τ is set to filter out the second text file T' containing independent semantic content. T' is a set of sentences containing independent semantic content. T' is expressed as:
[0116] .
[0117] For each sentence , calculate its structural complexity score and thematic consistency scores :
[0118]
[0119] Expressing sentences The number of dependent clauses in , δ, β, γ are adjustment coefficients;
[0120]
[0121] Kn is the number of topics in the first text file, Expressing sentences The probability of being on the kth topic, represents the probability of the kth topic in the first text file T;
[0122] when and When the product of is greater than the preset value CT0, the sentence Placed in the second text file T'.
[0123] S105, reading the second text file, performing a cleaning operation on the second text file, and storing the third text file obtained after cleaning.
[0124] You can use Python's file operation functions to open the second text file and read all the contents in the file.
[0125] The following text cleaning can be performed on the second text file:
[0126] Normalize text: Use string methods like .strip(), .replace(), etc. to clean up text.
[0127] Noise removal: Use regular expressions or other methods to remove non-text content.
[0128] Correct errors: Use the spell check library or custom rules to correct errors.
[0129] Word segmentation: Use natural language processing libraries such as nltk or jieba to segment words.
[0130] Remove stop words: Filter text using a stop word list.
[0131] You can use an appropriate library (such as sqlite3, pymysql, psycopg2, etc.) to connect to the database and create a table structure suitable for storing text content, so that the cleaned text content can be inserted into the database.
[0132] According to a specific implementation of the embodiment of the present disclosure, the method of obtaining the number of pages of a PDF file based on the function getNumPages in PyPDF2 and using the load_page event to read the image of each page in the PDF file includes:
[0133] Create a PdfReader object to open the PDF file and provide the path of the PDF file as a parameter;
[0134] Get the integer value returned by PdfReader.getNumPages() as the total number of pages in the PDF file.
[0135] According to a specific implementation of the embodiment of the present disclosure, the method of obtaining the number of pages of a PDF file based on the function getNumPages in PyPDF2 and using the load_page event to read the image of each page in the PDF file also includes:
[0136] Traverse each page of the PDF file, use the load_page() method to load each page of images, use the get_images() method to extract all images on each page, and save each extracted image.
[0137] According to a specific implementation of the embodiment of the present disclosure, the folder of the target book is read, all exported pictures of the target book are traversed, the edge of the picture is cropped using cv2.Canny, the middle line in the picture is identified according to the situation of the target book, and each page of the picture is cropped into two parts based on the middle line, including:
[0138] Import the cv2 library, traverse the target book folder containing the exported images, and use cv2.imread() to load each image;
[0139] Use cv2.cvtColor() to convert the color image to a grayscale image and use cv2.Canny() function to detect the edges in the image;
[0140] Use cv2.HoughLines() to detect straight lines in the image and determine the middle line based on the position and length of the straight line;
[0141] The cropping position is determined based on the identified middle line, and the image is cropped using NumPy's slicing function.
[0142] According to a specific implementation of the embodiment of the present disclosure, the text interface is used to identify all texts, and the number of picture pages and the special serial number marks on the left and right of each page are stored together with the identified content to form a first text file, including:
[0143] Load the preprocessed image and use the OCR tool to recognize the text in the image;
[0144] Assign page numbers to each image and add left and right marks to the cropped images;
[0145] Create a text file to store the recognized content, and write the page number, left and right marks, and recognized text into the text file.
[0146] According to a specific implementation of the embodiment of the present disclosure, the reading of the recognized first text file, parsing the content in the first text file, and obtaining a second text file containing independent semantic content includes:
[0147] Determine the n sentences T={S 1 ,S 2 ,...,S n}, calculate the m words contained in all sentences w={w 1 ,w 2 ,…,w m}, the weight H(wi) of the i-th word is expressed as:
[0148]
[0149] Words to express The frequency of occurrence in the text of the first text file, α is the correction parameter;
[0150] For each sentence , its evaluation value for:
[0151]
[0152] Expressing sentences Length;
[0153] A threshold τ is set to filter out the second text file T' containing independent semantic content. T' is a set of sentences containing independent semantic content. T' is expressed as:
[0154] .
[0155] According to a specific implementation of the embodiment of the present disclosure, the reading of the recognized first text file, parsing the content in the first text file to obtain a second text file containing independent semantic content, further includes:
[0156] For each sentence , calculate its structural complexity score and thematic consistency scores :
[0157]
[0158] Expressing sentences The number of dependent clauses in , δ, β, γ are adjustment coefficients;
[0159]
[0160] Kn is the number of topics in the first text file, Expressing sentences The probability of being on the kth topic, represents the probability of the kth topic in the first text file T;
[0161] when and When the product of is greater than the preset value CT0, the sentence Placed in the second text file T'.
[0162] According to a specific implementation of the embodiment of the present disclosure, the reading of the second text file, performing a cleaning operation on the second text file, and storing the third text file obtained after cleaning includes:
[0163] Use the open() function to open the second text file in read mode, and use the read() method to read the content of the second text file;
[0164] Use the string method .strip() to clean the text, use regular expressions to remove non-text content, and use a stop word list to filter the text;
[0165] Create a table in the database for the PDF file and construct an insert statement to insert the cleaned contents of the third text file into the database.
[0166] Corresponding to the above method embodiment, see Figure 3 The embodiment of the present invention also discloses a device 30 for real-time recognition of image-type PDF text information storage, comprising:
[0167] The export module 301 uses the fitz library to open the PDF file, reads the file through PyPDF2, obtains the number of pages of the PDF file based on the function getNumPages in PyPDF2, uses the load_page event to read the images of each page in the PDF file, and exports the images of each page in batches;
[0168] The cropping module 302 reads the folder of the target book, traverses all the exported images of the target book, uses cv2.Canny to crop the edges of the images, identifies the middle line in the image according to the situation of the target book, and crops each page of the image into two parts based on the middle line;
[0169] The recognition module 303 uses the text interface to recognize all the text, and stores the picture page number and the special serial number marks on the left and right of each page together with the recognized content to form a first text file;
[0170] The parsing module 304 reads the identified first text file, and parses the content in the first text file to obtain a second text file containing independent semantic content;
[0171] The storage module 305 reads the second text file, performs a cleaning operation on the second text file, and performs storage processing on the third text file obtained after the cleaning.
[0172] See also Figure 4 The embodiment of the present invention further provides an electronic device 60, the electronic device comprising:
[0173] at least one processor; and,
[0174] a memory communicatively connected to the at least one processor; wherein,
[0175] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method of real-time recognition of image-type PDF text information into a database in the aforementioned method embodiment.
[0176] An embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the aforementioned method embodiment.
[0177] An embodiment of the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method for real-time recognition of image-type PDF text information into a database in the aforementioned method embodiment.
[0178] Reference below Figure 4, which shows a schematic diagram of the structure of an electronic device 60 suitable for implementing the embodiment of the present disclosure. The electronic device in the embodiment of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 4 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0179] like Figure 4 As shown, the electronic device 60 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 60 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0180] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, a gage, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 60 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 4 The electronic device 60 is shown with various devices, but it should be understood that it is not required to implement or have all the devices shown. More or fewer devices may be implemented or have instead.
[0181] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.
[0182] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.
Claims
1. A method for real-time recognition of PDF text information of images into a database, characterized in that: include: Use the fitz library to open the PDF file. After reading the file through PyPDF2, get the number of pages of the PDF file based on the getNumPages function in PyPDF2. Use the load_page event to read the images of each page in the PDF file, and export the images of each page in batches. Read the folder of the target book, traverse all the exported images of the target book, use cv2.Canny to crop the edges of the images, identify the middle line in the image according to the situation of the target book, and cut each page of the image into two parts based on the middle line; Use the text interface to recognize all the text, and store the picture page number and the left and right special serial number marks of each page together with the recognized content to form a first text file; Reading the recognized first text file, parsing the content in the first text file, and obtaining a second text file containing independent semantic content; Reading the second text file, performing a cleaning operation on the second text file, and storing the third text file obtained after the cleaning; in The method of reading the identified first text file and parsing the content in the first text file to obtain a second text file containing independent semantic content includes: Determine the n sentences T={S1,S2,...,S n }, calculate the m words w={w1,w2,…,w m }, the weight H(wi) of the i-th word is expressed as: Words to express The frequency of occurrence in the text of the first text file, α is the correction parameter; For each sentence , its evaluation value for: Expressing sentences Length; A threshold τ is set to filter out the second text file T' containing independent semantic content. T' is a set of sentences containing independent semantic content. T' is expressed as: 。 2. The method according to claim 1, characterized in that The method of obtaining the number of pages of a PDF file based on the function getNumPages in PyPDF2 and using the load_page event to read the image of each page in the PDF file includes: Create a PdfReader object to open the PDF file and provide the path of the PDF file as a parameter; Get the integer value returned by PdfReader.getNumPages() as the total number of pages in the PDF file.
3. The method according to claim 2, characterized in that The method of obtaining the number of pages of a PDF file based on the function getNumPages in PyPDF2 and using the load_page event to read the image of each page in the PDF file also includes: Traverse each page of the PDF file, use the load_page() method to load each page of images, use the get_images() method to extract all images on each page, and save each extracted image.
4. The method according to claim 3, characterized in that The method of reading the folder of the target book, traversing all the exported images of the target book, using cv2.Canny to cut the edges of the images, identifying the middle line in the image according to the situation of the target book, and cutting each page of the image into two parts based on the middle line includes: Import the cv2 library, traverse the target book folder containing the exported images, and use cv2.imread() to load each image; Use cv2.cvtColor() to convert the color image to a grayscale image and use cv2.Canny() function to detect the edges in the image; Use cv2.HoughLines() to detect straight lines in the image and determine the middle line based on the position and length of the straight line; The cropping position is determined based on the identified middle line, and the image is cropped using NumPy's slicing function.
5. The method according to claim 4, characterized in that The text interface is used to identify all texts, and the number of picture pages and the left and right special serial numbers of each page are marked and stored together with the identified content to form a first text file, including: Load the preprocessed image and use the OCR tool to recognize the text in the image; Assign page numbers to each image and add left and right marks to the cropped images; Create a text file to store the recognized content, and write the page number, left and right marks, and recognized text into the text file.
6. The method according to claim 1, characterized in that The method of reading the identified first text file and parsing the content in the first text file to obtain a second text file containing independent semantic content also includes: For each sentence , calculate its structural complexity score and thematic consistency scores : Expressing sentences The number of dependent clauses in , δ, β, γ are adjustment coefficients; Kn is the number of topics in the first text file, Expressing sentences The probability of being on the kth topic, represents the probability of the kth topic in the first text file T; when and When the product of is greater than the preset value CT0, the sentence Placed in the second text file T'.
7. The method according to claim 6, characterized in that The step of reading the second text file, performing a cleaning operation on the second text file, and storing the third text file obtained after cleaning includes: Use the open() function to open the second text file in read mode, and use the read() method to read the content of the second text file; Use the string method .strip() to clean the text, use regular expressions to remove non-text content, and use a stop word list to filter the text; Create a table in the database for the PDF file and construct an insert statement to insert the cleaned contents of the third text file into the database.
8. A device for real-time recognition of PDF text information of images into a database, characterized in that: include: The export module uses the fitz library to open the PDF file. After reading the file through PyPDF2, it obtains the number of pages of the PDF file based on the getNumPages function in PyPDF2, uses the load_page event to read the images of each page in the PDF file, and exports the images of each page in batches; The cropping module reads the target book's folder, traverses all exported images of the target book, uses cv2.Canny to crop the edges of the images, identifies the middle line in the image according to the target book, and crops each page of the image into two parts based on the middle line; The recognition module uses the text interface to recognize all the text, and stores the image page number and the special serial number marks on the left and right of each page together with the recognized content to form a first text file; A parsing module reads the identified first text file, parses the content in the first text file, and obtains a second text file containing independent semantic content; A storage module reads the second text file, performs a cleaning operation on the second text file, and performs storage processing on the third text file obtained after cleaning; in The method of reading the identified first text file and parsing the content in the first text file to obtain a second text file containing independent semantic content includes: Determine the n sentences T={S1,S2,...,S n }, calculate the m words w={w1,w2,…,w m }, the weight H(wi) of the i-th word is expressed as: Words to express The frequency of occurrence in the text of the first text file, α is the correction parameter; For each sentence , its evaluation value for: Expressing sentences Length; A threshold τ is set to filter out the second text file T' containing independent semantic content. T' is a set of sentences containing independent semantic content. T' is expressed as: 。 9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for real-time recognition of image-type PDF text information and storage as described in any one of claims 1 to 7.
