Chamin09 commited on
Commit
8425610
·
verified ·
1 Parent(s): f3989da

Update modules/search_system.py

Browse files
Files changed (1) hide show
  1. modules/search_system.py +20 -9
modules/search_system.py CHANGED
@@ -13,17 +13,28 @@ class ImageSearchSystem:
13
  self.captioner = ImageCaptioner()
14
  self.knowledge_graph = KnowledgeGraph()
15
 
16
- def process_image(self, image_path: str) -> Dict[str, str]:
17
  """Process a single image and add to knowledge graph"""
18
- # Load and caption image
19
- image = Image.open(image_path)
20
- caption = self.captioner.generate_caption(image)
21
-
22
- # Add to knowledge graph
23
- image_id = os.path.basename(image_path)
24
- self.knowledge_graph.add_image_node(image_id, caption)
 
 
 
 
 
 
 
 
25
 
26
- return {"image_id": image_id, "caption": caption}
 
 
 
27
 
28
  def process_directory(self, dir_path: str) -> List[Dict[str, str]]:
29
  """Process all images in a directory"""
 
13
  self.captioner = ImageCaptioner()
14
  self.knowledge_graph = KnowledgeGraph()
15
 
16
+ def process_image(self, image_path_or_img) -> Dict[str, str]:
17
  """Process a single image and add to knowledge graph"""
18
+ try:
19
+ # Handle both string paths and PIL Image objects
20
+ if isinstance(image_path_or_img, str):
21
+ image = Image.open(image_path_or_img)
22
+ else:
23
+ image = image_path_or_img # Already a PIL Image
24
+
25
+ # Generate caption
26
+ caption = self.captioner.generate_caption(image)
27
+
28
+ # Add to knowledge graph
29
+ image_id = str(hash(str(image))) # Generate unique ID for the image
30
+ self.knowledge_graph.add_image_node(image_id, caption)
31
+
32
+ return {"image_id": image_id, "caption": caption}
33
 
34
+ except Exception as e:
35
+ print(f"Error processing image: {e}")
36
+ return {"error": str(e)}
37
+
38
 
39
  def process_directory(self, dir_path: str) -> List[Dict[str, str]]:
40
  """Process all images in a directory"""