David Ko commited on
Commit
e069cf4
ยท
1 Parent(s): 2e154ca

Add: Detailed debug logs for search-similar-objects API

Browse files
Files changed (1) hide show
  1. api.py +105 -39
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('nResults', 5)) # ๊ฒฐ๊ณผ ๊ฐœ์ˆ˜
 
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
- image = Image.open(BytesIO(base64.b64decode(image_data))).convert('RGB')
785
- query_embedding = generate_image_embedding(image)
 
 
 
 
 
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 'className' in data:
796
  # ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ
797
- class_name = data['className']
 
 
798
  filter_query = {"class": {"$eq": class_name}}
799
 
800
- # ํด๋ž˜์Šค๋กœ ํ•„ํ„ฐ๋งํ•˜์—ฌ ๊ฒ€์ƒ‰
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
801
  results = object_collection.query(
802
- query_embeddings=None,
803
- where=filter_query,
804
  n_results=n_results,
805
  include=["metadatas", "distances"]
806
  )
807
 
 
 
 
 
808
  return jsonify({
809
  "success": True,
810
- "searchType": "class",
811
- "results": format_object_results(results)
812
  })
813
-
814
- else:
815
- return jsonify({"error": "Invalid search parameters"})
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
- if len(results['ids']) > 0 and len(results['ids'][0]) > 0:
 
 
 
 
 
 
842
  for i, obj_id in enumerate(results['ids'][0]):
843
- result_item = {
844
- "id": obj_id,
845
- "metadata": results['metadatas'][0][i] if 'metadatas' in results else {}
846
- }
847
-
848
- if 'distances' in results:
849
- result_item["distance"] = float(results['distances'][0][i])
850
-
851
- formatted_results.append(result_item)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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