Spaces:
Runtime error
Runtime error
David Ko
commited on
Commit
ยท
e069cf4
1
Parent(s):
2e154ca
Add: Detailed debug logs for search-similar-objects API
Browse files
api.py
CHANGED
@@ -759,30 +759,42 @@ def add_detected_objects():
|
|
759 |
@app.route('/api/search-similar-objects', methods=['POST'])
|
760 |
def search_similar_objects():
|
761 |
"""์ ์ฌํ ๊ฐ์ฒด ๊ฒ์ API"""
|
|
|
|
|
762 |
if clip_model is None or object_collection is None:
|
|
|
763 |
return jsonify({"error": "Image embedding model or vector DB not available"})
|
764 |
|
765 |
try:
|
766 |
# ์์ฒญ ๋ฐ์ดํฐ ์ถ์ถ
|
767 |
data = request.json
|
|
|
768 |
|
769 |
if not data:
|
|
|
770 |
return jsonify({"error": "Missing request data"})
|
771 |
|
772 |
# ๊ฒ์ ์ ํ ๊ฒฐ์
|
773 |
search_type = data.get('searchType', 'image')
|
774 |
-
n_results = int(data.get('
|
|
|
775 |
|
776 |
query_embedding = None
|
777 |
|
778 |
if search_type == 'image' and 'image' in data:
|
779 |
# ์ด๋ฏธ์ง๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
|
|
|
780 |
image_data = data['image']
|
781 |
if image_data.startswith('data:image'):
|
782 |
image_data = image_data.split(',')[1]
|
783 |
|
784 |
-
|
785 |
-
|
|
|
|
|
|
|
|
|
|
|
786 |
|
787 |
elif search_type == 'object' and 'objectId' in data:
|
788 |
# ๊ฐ์ฒด ID๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
|
@@ -792,43 +804,65 @@ def search_similar_objects():
|
|
792 |
if result and "embeddings" in result and len(result["embeddings"]) > 0:
|
793 |
query_embedding = result["embeddings"][0]
|
794 |
|
795 |
-
elif search_type == 'class' and '
|
796 |
# ํด๋์ค ์ด๋ฆ์ผ๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
|
797 |
-
|
|
|
|
|
798 |
filter_query = {"class": {"$eq": class_name}}
|
799 |
|
800 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
801 |
results = object_collection.query(
|
802 |
-
query_embeddings=
|
803 |
-
where=filter_query,
|
804 |
n_results=n_results,
|
805 |
include=["metadatas", "distances"]
|
806 |
)
|
807 |
|
|
|
|
|
|
|
|
|
808 |
return jsonify({
|
809 |
"success": True,
|
810 |
-
"searchType":
|
811 |
-
"results":
|
812 |
})
|
813 |
-
|
814 |
-
|
815 |
-
return jsonify({"error": "
|
816 |
-
|
817 |
-
if query_embedding is None:
|
818 |
-
return jsonify({"error": "Failed to generate query embedding"})
|
819 |
-
|
820 |
-
# ์ ์ฌ๋ ๊ฒ์ ์คํ
|
821 |
-
results = object_collection.query(
|
822 |
-
query_embeddings=[query_embedding],
|
823 |
-
n_results=n_results,
|
824 |
-
include=["metadatas", "distances"]
|
825 |
-
)
|
826 |
-
|
827 |
-
return jsonify({
|
828 |
-
"success": True,
|
829 |
-
"searchType": search_type,
|
830 |
-
"results": format_object_results(results)
|
831 |
-
})
|
832 |
|
833 |
except Exception as e:
|
834 |
print(f"Error in search-similar-objects API: {e}")
|
@@ -838,17 +872,49 @@ def format_object_results(results):
|
|
838 |
"""๊ฒ์ ๊ฒฐ๊ณผ ํฌ๋งทํ
"""
|
839 |
formatted_results = []
|
840 |
|
841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
842 |
for i, obj_id in enumerate(results['ids'][0]):
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
852 |
|
853 |
return formatted_results
|
854 |
|
|
|
759 |
@app.route('/api/search-similar-objects', methods=['POST'])
|
760 |
def search_similar_objects():
|
761 |
"""์ ์ฌํ ๊ฐ์ฒด ๊ฒ์ API"""
|
762 |
+
print("[DEBUG] Received request in search-similar-objects")
|
763 |
+
|
764 |
if clip_model is None or object_collection is None:
|
765 |
+
print("[DEBUG] Error: Image embedding model or vector DB not available")
|
766 |
return jsonify({"error": "Image embedding model or vector DB not available"})
|
767 |
|
768 |
try:
|
769 |
# ์์ฒญ ๋ฐ์ดํฐ ์ถ์ถ
|
770 |
data = request.json
|
771 |
+
print(f"[DEBUG] Request data keys: {list(data.keys()) if data else 'None'}")
|
772 |
|
773 |
if not data:
|
774 |
+
print("[DEBUG] Error: Missing request data")
|
775 |
return jsonify({"error": "Missing request data"})
|
776 |
|
777 |
# ๊ฒ์ ์ ํ ๊ฒฐ์
|
778 |
search_type = data.get('searchType', 'image')
|
779 |
+
n_results = int(data.get('n_results', 5)) # ๊ฒฐ๊ณผ ๊ฐ์
|
780 |
+
print(f"[DEBUG] Search type: {search_type}, n_results: {n_results}")
|
781 |
|
782 |
query_embedding = None
|
783 |
|
784 |
if search_type == 'image' and 'image' in data:
|
785 |
# ์ด๋ฏธ์ง๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
|
786 |
+
print("[DEBUG] Searching by image")
|
787 |
image_data = data['image']
|
788 |
if image_data.startswith('data:image'):
|
789 |
image_data = image_data.split(',')[1]
|
790 |
|
791 |
+
try:
|
792 |
+
image = Image.open(BytesIO(base64.b64decode(image_data))).convert('RGB')
|
793 |
+
query_embedding = generate_image_embedding(image)
|
794 |
+
print(f"[DEBUG] Generated image embedding: {type(query_embedding)}, shape: {len(query_embedding) if query_embedding is not None else 'None'}")
|
795 |
+
except Exception as e:
|
796 |
+
print(f"[DEBUG] Error generating image embedding: {e}")
|
797 |
+
return jsonify({"error": f"Error processing image: {str(e)}"}), 500
|
798 |
|
799 |
elif search_type == 'object' and 'objectId' in data:
|
800 |
# ๊ฐ์ฒด ID๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
|
|
|
804 |
if result and "embeddings" in result and len(result["embeddings"]) > 0:
|
805 |
query_embedding = result["embeddings"][0]
|
806 |
|
807 |
+
elif search_type == 'class' and 'class_name' in data:
|
808 |
# ํด๋์ค ์ด๋ฆ์ผ๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
|
809 |
+
print("[DEBUG] Searching by class name")
|
810 |
+
class_name = data['class_name']
|
811 |
+
print(f"[DEBUG] Class name: {class_name}")
|
812 |
filter_query = {"class": {"$eq": class_name}}
|
813 |
|
814 |
+
try:
|
815 |
+
# ํด๋์ค๋ก ํํฐ๋งํ์ฌ ๊ฒ์
|
816 |
+
print(f"[DEBUG] Querying with filter: {filter_query}")
|
817 |
+
results = object_collection.query(
|
818 |
+
query_embeddings=None,
|
819 |
+
where=filter_query,
|
820 |
+
n_results=n_results,
|
821 |
+
include=["metadatas", "distances"]
|
822 |
+
)
|
823 |
+
|
824 |
+
print(f"[DEBUG] Query results: {results['ids'][0] if 'ids' in results and len(results['ids']) > 0 else 'No results'}")
|
825 |
+
formatted_results = format_object_results(results)
|
826 |
+
print(f"[DEBUG] Formatted results count: {len(formatted_results)}")
|
827 |
+
|
828 |
+
return jsonify({
|
829 |
+
"success": True,
|
830 |
+
"searchType": "class",
|
831 |
+
"results": formatted_results
|
832 |
+
})
|
833 |
+
except Exception as e:
|
834 |
+
print(f"[DEBUG] Error in class search: {e}")
|
835 |
+
return jsonify({"error": f"Error in class search: {str(e)}"}), 500
|
836 |
+
|
837 |
+
else:
|
838 |
+
print(f"[DEBUG] Invalid search parameters: {data}")
|
839 |
+
return jsonify({"error": "Invalid search parameters"})
|
840 |
+
|
841 |
+
if query_embedding is None:
|
842 |
+
print("[DEBUG] Error: Failed to generate query embedding")
|
843 |
+
return jsonify({"error": "Failed to generate query embedding"})
|
844 |
+
|
845 |
+
try:
|
846 |
+
# ์ ์ฌ๋ ๊ฒ์ ์คํ
|
847 |
+
print(f"[DEBUG] Running similarity search with embedding of length {len(query_embedding)}")
|
848 |
results = object_collection.query(
|
849 |
+
query_embeddings=[query_embedding],
|
|
|
850 |
n_results=n_results,
|
851 |
include=["metadatas", "distances"]
|
852 |
)
|
853 |
|
854 |
+
print(f"[DEBUG] Query results: {results['ids'][0] if 'ids' in results and len(results['ids']) > 0 else 'No results'}")
|
855 |
+
formatted_results = format_object_results(results)
|
856 |
+
print(f"[DEBUG] Formatted results count: {len(formatted_results)}")
|
857 |
+
|
858 |
return jsonify({
|
859 |
"success": True,
|
860 |
+
"searchType": search_type,
|
861 |
+
"results": formatted_results
|
862 |
})
|
863 |
+
except Exception as e:
|
864 |
+
print(f"[DEBUG] Error in similarity search: {e}")
|
865 |
+
return jsonify({"error": f"Error in similarity search: {str(e)}"}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
866 |
|
867 |
except Exception as e:
|
868 |
print(f"Error in search-similar-objects API: {e}")
|
|
|
872 |
"""๊ฒ์ ๊ฒฐ๊ณผ ํฌ๋งทํ
"""
|
873 |
formatted_results = []
|
874 |
|
875 |
+
print(f"[DEBUG] Formatting results: {results.keys() if results else 'None'}")
|
876 |
+
|
877 |
+
if not results or 'ids' not in results or len(results['ids']) == 0 or len(results['ids'][0]) == 0:
|
878 |
+
print("[DEBUG] No results to format")
|
879 |
+
return formatted_results
|
880 |
+
|
881 |
+
try:
|
882 |
for i, obj_id in enumerate(results['ids'][0]):
|
883 |
+
try:
|
884 |
+
# ๊ฐ์ฒด ์ ๋ณด ์ถ์ถ
|
885 |
+
metadata = results['metadatas'][0][i] if 'metadatas' in results and len(results['metadatas']) > 0 else {}
|
886 |
+
|
887 |
+
# bbox๊ฐ JSON ๋ฌธ์์ด๋ก ์ ์ฅ๋์ด ์๋ ๊ฒฝ์ฐ ์ญ์ง๋ ฌํ
|
888 |
+
if 'bbox' in metadata and isinstance(metadata['bbox'], str):
|
889 |
+
try:
|
890 |
+
metadata['bbox'] = json.loads(metadata['bbox'])
|
891 |
+
except:
|
892 |
+
pass # ์ญ์ง๋ ฌํ ์คํจ ์ ๊ทธ๋๋ก ์ ์ง
|
893 |
+
|
894 |
+
result_item = {
|
895 |
+
"id": obj_id,
|
896 |
+
"metadata": metadata
|
897 |
+
}
|
898 |
+
|
899 |
+
if 'distances' in results and len(results['distances']) > 0:
|
900 |
+
result_item["distance"] = float(results['distances'][0][i])
|
901 |
+
|
902 |
+
# ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ ์๋
|
903 |
+
try:
|
904 |
+
# ์ด๋ฏธ์ง ID๋ฅผ ํตํด ์๋ณธ ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ
|
905 |
+
image_id = metadata.get('image_id')
|
906 |
+
if image_id:
|
907 |
+
print(f"[DEBUG] Getting image for object {obj_id} with image_id {image_id}")
|
908 |
+
# ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ ๋ก์ง ์ถ๊ฐ ํ์
|
909 |
+
|
910 |
+
except Exception as e:
|
911 |
+
print(f"[DEBUG] Error getting image for result {i}: {e}")
|
912 |
+
|
913 |
+
formatted_results.append(result_item)
|
914 |
+
except Exception as e:
|
915 |
+
print(f"[DEBUG] Error formatting result {i}: {e}")
|
916 |
+
except Exception as e:
|
917 |
+
print(f"[DEBUG] Error in format_object_results: {e}")
|
918 |
|
919 |
return formatted_results
|
920 |
|