NEXAS commited on
Commit
d3e2337
·
verified ·
1 Parent(s): 52fd07e

Update utils/ingest_image.py

Browse files
Files changed (1) hide show
  1. utils/ingest_image.py +51 -51
utils/ingest_image.py CHANGED
@@ -1,51 +1,51 @@
1
- #inget image
2
- import os
3
- import fitz
4
- import chromadb
5
- from chromadb.utils.data_loaders import ImageLoader
6
- from chromadb.utils.embedding_functions import OpenCLIPEmbeddingFunction # type: ignore
7
-
8
- path = "mm_vdb2"
9
- client = chromadb.PersistentClient(path=path)
10
-
11
- def extract_and_store_images(pdf_path,images_dir=r'extracted_images'):
12
- # Step 1: Extract images from PDF
13
- pdf_document = fitz.open(pdf_path)
14
- os.makedirs(images_dir, exist_ok=True)
15
-
16
- for page_num in range(len(pdf_document)):
17
- page = pdf_document.load_page(page_num)
18
- image_list = page.get_images(full=True)
19
-
20
- for image_index, img in enumerate(image_list):
21
- xref = img[0]
22
- base_image = pdf_document.extract_image(xref)
23
- image_bytes = base_image["image"]
24
- image_ext = base_image["ext"]
25
- image_filename = f"{images_dir}/page_{page_num+1}_img_{image_index+1}.{image_ext}"
26
-
27
- with open(image_filename, "wb") as image_file:
28
- image_file.write(image_bytes)
29
- print(f"Saved: {image_filename}")
30
-
31
- print("Image extraction complete.")
32
-
33
- # Step 2: Add extracted images to ChromaDB
34
- image_loader = ImageLoader()
35
- CLIP = OpenCLIPEmbeddingFunction()
36
- image_collection = client.get_or_create_collection(name="image", embedding_function=CLIP, data_loader=image_loader)
37
-
38
- ids = []
39
- uris = []
40
-
41
- for i, filename in enumerate(sorted(os.listdir(images_dir))):
42
- if filename.endswith('.jpeg') or filename.endswith('.png'):
43
- file_path = os.path.join(images_dir, filename)
44
- ids.append(str(i))
45
- uris.append(file_path)
46
-
47
- image_collection.add(ids=ids, uris=uris)
48
- print("Images added to the database.")
49
- # return image_vdb
50
- return image_collection
51
-
 
1
+ #inget image
2
+ import os
3
+ import fitz
4
+ import chromadb
5
+ from chromadb.utils.data_loaders import ImageLoader
6
+ from chromadb.utils.embedding_functions import OpenCLIPEmbeddingFunction # type: ignore
7
+
8
+ path = "mm_vdb2"
9
+ client = chromadb.PersistentClient(path=path)
10
+
11
+ def extract_and_store_images(pdf_path,images_dir=r'extracted_images'):
12
+ # Step 1: Extract images from PDF
13
+ pdf_document = fitz.open(pdf_path)
14
+ os.makedirs(images_dir, exist_ok=True)
15
+
16
+ for page_num in range(len(pdf_document)):
17
+ page = pdf_document.load_page(page_num)
18
+ image_list = page.get_images(full=True)
19
+
20
+ for image_index, img in enumerate(image_list):
21
+ xref = img[0]
22
+ base_image = pdf_document.extract_image(xref)
23
+ image_bytes = base_image["image"]
24
+ image_ext = base_image["ext"]
25
+ image_filename = f"{images_dir}/page_{page_num+1}_img_{image_index+1}.{image_ext}"
26
+
27
+ with open(image_filename, "wb") as image_file:
28
+ image_file.write(image_bytes)
29
+ print(f"Saved: {image_filename}")
30
+
31
+ print("Image extraction complete.")
32
+
33
+ # Step 2: Add extracted images to ChromaDB
34
+ image_loader = ImageLoader()
35
+ CLIP = OpenCLIPEmbeddingFunction()
36
+ image_collection = client.get_or_create_collection(name="image", embedding_function=CLIP, data_loader=image_loader)
37
+
38
+ ids = []
39
+ uris = []
40
+
41
+ for i, filename in enumerate(sorted(os.listdir(images_dir))):
42
+ if filename.endswith('.jpeg') or filename.endswith('.png'):
43
+ file_path = os.path.join(images_dir, filename)
44
+ ids.append(str(i))
45
+ uris.append(file_path)
46
+
47
+ image_collection.add(ids=ids, uris=uris)
48
+ print("Images added to the database.")
49
+ # return image_vdb
50
+ return image_collection
51
+