Automatic article illustrating method and device
Through vectorization processing and model calculations, automatic mapping of articles is realized, which solves the problems of low efficiency and insufficient flexibility of traditional mapping technology, and improves the accuracy and efficiency of mapping.
Patent Information
- Application Number
- CN202410637519.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-22
- Publication Date
- 2024-09-06
AI Technical Summary
Traditional article matching technology relies on manual matching, resulting in heavy workload, lack of flexibility, and low efficiency.
Using automated methods, through image and text vectorization processing, CLIP and BLIP models are used to vectorize images and text, perform deduplication and similarity calculations, and realize automatic matching of images and articles.
It improves the efficiency and flexibility of map matching. Through two-way matching technology, it improves the accuracy of map matching. Automation reduces manual intervention and improves matching quality.
Smart Images

Figure CN118606503A
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of Internet and intelligent recognition, and in particular, relates to a method and device for automatically matching pictures to articles. Background Art
[0002] Traditional image matching technology matches images based on keyword tags, tags images and articles separately, and then manually matches images based on the content of the tags. This manual method is labor-intensive and lacks flexibility. Summary of the invention
[0003] In order to solve the problems of heavy workload and lack of flexibility of traditional manual image matching technology.
[0004] The purpose of this embodiment is to provide a method for automatically adding pictures to an article, including:
[0005] The invention provides a method and device for automatically matching pictures to articles, which solves the problems of heavy workload and lack of flexibility in the traditional manual matching technology.
[0006] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0007] Figure 1 : A method step diagram of the method for automatically matching pictures to articles provided by an embodiment of the present invention;
[0008] Figure 2 : A method step diagram of automatically matching pictures with texts in a method for matching pictures with articles provided in an embodiment of the present invention;
[0009] Figure 3 : A device structure diagram of the device for automatically matching pictures to articles provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0010] The technical solution in the embodiment of the present invention will be described below in conjunction with the accompanying drawings in the embodiment of the present invention. The example code in the embodiment uses Python language.
[0011] In order to solve the problem of low efficiency and lack of flexibility in manual article illustration,
[0012] The present invention provides a method for automatically matching pictures to articles, comprising:
[0013] S01: Preprocessing the image library to obtain a normalized image library, including adjusting image pixel modification, executing image cropping instructions, and executing image scaling instructions;
[0014] In this embodiment, a third-party tool is used to modify the pixel size of the image, execute the image cropping instruction, and execute the image scaling instruction on the image library directory to be processed. Step S02 is performed;
[0015] S02: Execute model vector operation according to the normalized image library to obtain a picture vector list. In this embodiment, the pytorch library is used to execute the picture vector list to obtain the following example code;
[0016] # Load CLIP model
[0017] device="cuda"if torch.cuda.is_available()else"cpu"
[0018] model,preprocess=Clip.load("ViT-B / 32",device=device)
[0019] # Load the image and preprocess it
[0020] image_path="path / to / your / image.jpg"
[0021]
[0022] The image-to-text operation is performed according to the normalized image library to obtain a text vector list. The image-to-text operation includes a BLIP model operation and a CLIP model operation. The sample code in this embodiment is as follows:
[0023]
[0024] device="cuda"if torch.cuda.is_available()else"cpu"
[0025] model,preprocess=Clip.load("ViT-B / 32",device=device)
[0026] # Load text and preprocess it
[0027] texts=clip.tokenize(image_caption).to(device)
[0028] #Generate text vector
[0029] with torch.no_grad():
[0030] caption_vec=model.encode_text(texts)
[0031] # Output text vector
[0032] In the code, image_vec is the image vector, image_caption is the image title, caption_vec is the image title vector, and step S03 is performed;
[0033] S03: recursively traverse the normalized image library, compare the MD5 strings of the image files, perform first duplicate image deletion to obtain a first deduplicated image list, and proceed to step S04; the first deduplication operation includes:
[0034] Create a first deduplication MD5 dictionary, where the first deduplication MD5 dictionary is an empty dictionary;
[0035] Read the normalized image library directory to obtain a file list;
[0036] Traverse the file list to obtain picture elements;
[0037] Perform MD5 operation according to the image element to obtain the element MD5 value;
[0038] Perform the first deduplication MD5 dictionary search according to the element MD5 value;
[0039] If the picture MD5 value does not exist in the first deduplication MD5 dictionary, append the picture MD5 value to the first deduplication MD5 dictionary;
[0040] If the file list is traversed completely, output the first deduplicated MD5 dictionary as the first deduplicated MD5 list;
[0041] The pseudo code used in this embodiment is as follows:
[0042] Function to remove pictures with the same MD5 (picture list):
[0043] Define an empty dictionary md5 dictionary
[0044] For each image in the images list:
[0045] Read the binary data of the image according to the image path Path
[0046] Calculate the MD5 value of data
[0047] If md5 is not in the md5 dictionary:
[0048] Add md5 to the key of the md5 dictionary, and the corresponding value is the picture
[0049] Return all values of the md5 dictionary as a list of duplicate-free images;
[0050] S04: Perform a second deduplication operation according to the first deduplication picture list and the picture vector list to obtain a second deduplication picture list, and proceed to step S05; wherein the second deduplication operation includes performing an equality comparison of the picture title string values and performing a picture similarity operation on the pictures corresponding to the equal strings, and if the similarity is greater than 90%, performing the title vector list acquisition; the pseudo code example is as follows:
[0051] Deduplicated image list = []
[0052] For each image a in the image list:
[0053] Similar images = []
[0054] For each image b in the image list:
[0055] If a is not equal to b and a.image_caption is equal to b.image_caption:
[0056] Similarity = cos(a.image_vec,b.image_vec)
[0057] If similarity > 0.9:
[0058]
[0059] The picture similarity calculation includes performing a COS similarity calculation of two pictures according to a cosine similarity formula;
[0060] The cosine similarity formula includes
[0061]
[0062] Among them, a=(x1,x2,x3,…,xi), b=(y1,y2,y3,…,yi), i=768;
[0063] S05: Execute the text vector list and the second deduplication picture library storage instruction to save the picture library metadata list on a disk;
[0064] S06: Execute text-to-picture instructions according to the content of the article, including:
[0065] S0601: Execute the LLM model operation according to the content of the article to obtain a list of picture titles, and proceed to step S0602; in this embodiment, a prompt is used to let the execution language model (LLM) assign pictures to the text and give titles of 5 pictures, and then proceed to step S0602; the sample code is as follows:
[0066]
[0067] prompt=prompt,
[0068] max_tokens=50,
[0069] n=5,
[0070] stop=None,
[0071] temperature=0.7,
[0072] top_p=1.0 )
[0074] # Output the generated title
[0075] print(["A woman is making a kiwi banana milkshake","a kiwi","a banana","a glass of milk","a glass with ice cubes"])
[0076] S0602: Perform CLIP model vector operation according to the picture title list to obtain the picture title vector, and proceed to step S0603; the example code is as follows:
[0077] # Load CLIP model
[0078] device="cuda"if torch.cuda.is_available()else"cpu"
[0079] model,preprocess=Clip.load("ViT-B / 32",device=device)
[0080] # Load text and preprocess it
[0081] query_title_list = ["A woman is making a kiwi banana milkshake", "a kiwi", "a banana", "a glass of milk", "a glass with ice cubes"]
[0082] texts=clip.tokenize(query_title_list).to(device)
[0083] #Generate text vector
[0084] with torch.no_grad():
[0085] query_vec=model.encode_text(texts)
[0086]
[0087] S0603: performing a COS cosine similarity operation according to the picture title vector list and the picture vector list to obtain a first article picture similarity list, and proceeding to step S0604;
[0088] S0604: performing a COS cosine similarity operation according to the picture title vector list and the text vector list to obtain a second article picture similarity list, and proceeding to step S0605;
[0089] The pseudo example codes for S0603 and S0604 are as follows:
[0090]
[0091] Picture list with two-way matching of picture title = two-way matching subroutine (picture title, picture list)
[0092] If the image title is not in the result_sim dictionary:
[0093] Add the picture title to the key of the result_sim dictionary, and the corresponding value is a list of pictures with bidirectional matching of the picture title
[0094] Returns the result_sim dictionary
[0095] S0605: According to the first article-picture similarity list and the second article-picture similarity list, a threshold similarity list with an average similarity greater than or equal to 0.7 is obtained, and step S0607 is performed. The average similarity list is obtained according to an average algorithm formula, and the average algorithm formula is:
[0096] Sim total =(Sim pic +Sim cap ) / 2
[0097] And Sim total >=0.7
[0098] S0607: Obtain a reverse ranking list according to the threshold similarity list and the normalized image library, wherein the sorting includes a reverse sorting of similarity, and proceed to step S0608;
[0099] S0608: Obtain the first N pictures according to the reverse ranking list, where N includes 5.
[0100] The present invention also provides an automatic article picture matching device, comprising:
[0101] P01: Image normalization module: used to execute the normalization instructions of the image library, including image pixel modification, image cropping instructions, and image scaling instructions. In this embodiment, a third-party tool is used to execute the image pixel modification, image cropping, and image scaling.
[0102] Image vector list preprocessing module: used to obtain the image vector list according to the CLIP model vector algorithm. The execution code example is as follows:
[0103] # Load CLIP model
[0104] device="cuda"if torch.cuda.is_available()else"cpu"
[0105] model,preprocess=Clip.load("ViT-B / 32",device=device)
[0106] # Load the image and preprocess it
[0107] image_path="path / to / your / image.jpg"
[0108] image=preprocess(Image.open(image_path)).unsqueeze(0).to(device)
[0109] #Generate image vector
[0110] with torch.no_grad():
[0111] image_vec=model.encode_image(image)
[0112] P02: Text vector list preprocessing module: used to obtain the title according to the picture library, perform vector operation to obtain the text vector list, obtain the text in the picture by the BLIP model according to the normalized picture of the picture library, and further obtain the text vector list according to the text; the algorithm executes the acquisition of the text vector list, and the model operation example is as follows:
[0113] #Load BLIP model
[0114] device="cuda"if torch.cuda.is_available()else"cpu"
[0115] model,preprocess=Blip.load("openai / blip-base",device=device)
[0116] # Load the image and preprocess it
[0117] image_path="path / to / image.jpg"
[0118]
[0119] P03: Vector deduplication preprocessing module: used to perform deduplication operation according to the preprocessed picture vector list and the text vector list to obtain the picture library metadata list, first calculate the MD5 string for the binary data of each picture in the picture library, then compare the MD5 string, only retain one picture with the same MD5 value, the deduplicated pictures are the first MD5 picture set, further perform the second deduplication calculation according to the first MD5 picture set, calculate the cos similarity of the picture vectors two by two, the similarity is greater than 90% to form the second COS similarity list set, further perform deduplication operation according to the second COS similarity list set to delete duplicate COS similarity picture vectors, after completing the above steps, perform the COS similarity picture vector disk save operation.
[0120] P04: Article content matching module: The user performs LLM model calculation and CLIP model vector calculation according to the library metadata list and article content matching to obtain article matching pictures. In this implementation, the article content matching pictures perform two-way matching to calculate similarity, including
[0121] Execute the CLIP model to convert each image caption into an image caption vector;
[0122] Further, performing a COS cosine similarity operation according to the picture title vector list and the picture vector list to obtain a first article picture similarity list;
[0123] Further performing a COS cosine similarity operation according to the picture title vector list and the text vector list to obtain a second article picture similarity list;
[0124] The similarities of the first article image similarity list and the second article image similarity list are added and averaged, and images with a value less than a minimum threshold (0.7 in this case) are filtered to prevent completely irrelevant images from entering the candidate list. The minimum threshold can be set according to your needs.
[0125] Sim_total = (title similarity vector + image similarity vector) / 2
[0126] And Sim_total>=0.7
[0127] According to Sim_total, the top n pictures in the gallery are taken (n=5 in this case), so that each picture title can get the top n pictures with the highest matching degree, and n can be set according to your needs; finally, the picture collection of the article is obtained.
[0128] The invention provides an automatic article picture matching method and device, which performs two-way matching from text to picture and then from picture to text, thereby improving accuracy and solving the problems of low efficiency and lack of flexibility in manual article picture matching.
[0129] The above description is only an embodiment of the present invention and is not intended to limit the scope of protection of the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the scope of protection of the present invention.
Claims
1. A method for automatically adding pictures to articles. It is characterized in that include: Image library preprocessing to obtain normalized image library, including image pixel modification, image cropping instruction, and image scaling instruction; Execute model vector operation according to the normalized image library to obtain a picture vector list; Performing a picture-to-text operation according to the normalized picture library to obtain a text vector list; The image-to-text operation includes a BLIP model operation and a CLIP model operation; Recursively traverse the normalized image library, compare the MD5 strings of the image files, and perform a first duplicate image deletion operation to obtain a first deduplicated image list; Perform a second deduplication operation according to the first deduplication picture list and the picture vector list to obtain a second deduplication picture list; The second deduplication operation includes: Extracting picture titles according to the first deduplicated picture list and performing a comparison operation; If the picture titles are equal, a picture similarity comparison operation is performed, wherein the similarity comparison is a cosine similarity comparison performed according to the picture vector list; If the image similarity is greater than 90%, it is determined to be a duplicate image; Execute the first deduplication picture list deletion and obtain the second deduplication picture library; Execute the text vector list and the second deduplication picture library storage instruction to save the picture library metadata list on a disk; Execute text-to-image instructions based on the article content, including: Execute LLM model operation to obtain the picture title list; Perform CLIP model vector operation according to the picture title list to obtain a picture title vector list; Perform a COS cosine similarity operation according to the picture title vector list and the picture vector list to obtain a first article picture similarity list; Perform a COS cosine similarity operation based on the picture title vector list and the text vector list to obtain a second article picture similarity list; Acquire a threshold similarity list with an average similarity greater than or equal to 0.7 according to the first article-picture similarity list and the second article-picture similarity list; Obtaining a reverse ranking list according to the threshold similarity list and the second deduplicated images; The first N pictures are obtained according to the reverse ranking list.
2. According to the method for automatically matching pictures to articles according to claim 1, It is characterized in that The first duplicate image deletion operation includes: Create a first deduplication MD5 dictionary, where the first deduplication MD5 dictionary is an empty dictionary; Read the normalized image library directory to obtain a file list; Traverse the file list to obtain picture elements; Perform MD5 operation according to the image element to obtain the element MD5 value; Perform the first deduplication MD5 dictionary search according to the element MD5 value; If the picture MD5 value does not exist in the first deduplication MD5 dictionary, append the picture MD5 value to the first deduplication MD5 dictionary; If the file list is traversed completely, the first deduplicated MD5 dictionary is output as the first deduplicated MD5 list.
3. According to the method for automatically matching pictures to articles according to claim 1, It is characterized in that The second deduplication operation further includes: Recursively traverse the first deduplicated picture list; The picture title comparison operation is a string value comparison.
4. According to the method for automatically matching pictures to articles according to claim 1, It is characterized in that The picture similarity comparison operation includes performing a COS similarity operation of two pictures according to a cosine similarity formula; The cosine similarity formula includes Among them, a=(x1,x2,x3,…,xi), b=(y1,y2,y3,…,yi), i=768.
5. According to the method for automatically matching pictures to articles according to claim 1, It is characterized in that The reverse ranking list is sorted in descending order according to the threshold similarity list.
6. The method for automatically matching pictures to articles according to claim 1, It is characterized in that The first N pictures are obtained according to the reverse ranking list, where N includes 5.
7. An automatic article picture matching device, It is characterized in that include: Image normalization module: used to execute the normalization instructions of the image library, including image pixel modification, image cropping instructions, and image scaling instructions; Image vector list preprocessing module: used to obtain the image vector list according to the CLIP model vector algorithm; A text vector list preprocessing module: used to obtain titles according to a picture library, perform vector operations to obtain a text title vector list, obtain text in the picture by a BLIP model according to a normalized picture of the picture library, and further obtain a text vector list according to the text; Vector deduplication preprocessing module: used for performing deduplication operation according to the preprocessed picture vector list and the text vector list to obtain a picture library metadata list; Article content illustration module: The user performs LLM model calculation and CLIP model vector calculation according to the image library metadata list and article content matching to obtain article illustrations.
8. The automatic article picture matching device according to claim 7, It is characterized in that The picture pixel modification, execution of picture cropping instructions, and execution of picture scaling instructions include execution of the instructions by a third-party tool.
9. The automatic article picture matching device according to claim 7, It is characterized in that The deduplication operation includes performing a first deduplication according to the MD5 value of the image to obtain a first deduplication MD5 dictionary, further obtaining second deduplication data according to the vector value equality comparison of the text vector list, further performing a COS similarity operation according to the second deduplication data to obtain a deduplication COS cosine similarity list, and further obtaining a gallery metadata list according to the deduplication COS cosine similarity list.
10. The automatic article picture matching device according to claim 7, It is characterized in that The LLM model operation is used to obtain a list of picture titles, and the CLIP model vector operation is used to obtain a list of picture vectors.
Citation Information
Patent Citations
Malicious picture interception method, device and equipment and storage medium
CN111444362A
Image-text data processing method and device, electronic equipment and computer storage medium
CN115129914A
PDF document processing method and device based on big data
CN115640267A
Computerized systems and methods for product integration and deduplication using artificial intelligence
US20210304121A1
