Spaces:
Sleeping
Sleeping
Update modules/search_system.py
Browse files- 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,
|
| 17 |
"""Process a single image and add to knowledge graph"""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 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"""
|