image_identifier.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 图文识别脚本
  5. 主要功能:使用 Coze API 分析图片内容
  6. """
  7. import os
  8. import json
  9. import time
  10. import sys
  11. from typing import Dict, Any, List, Optional
  12. from dotenv import load_dotenv
  13. # 导入自定义模块
  14. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  15. from coze.coze_hook import CozeHook
  16. class ImageIdentifier:
  17. def __init__(self):
  18. # 加载环境变量
  19. load_dotenv()
  20. # 初始化Coze客户端
  21. self.coze = CozeHook()
  22. def extract_image_urls(self, formatted_content: Dict[str, Any]) -> List[str]:
  23. """提取图片URL列表"""
  24. image_urls = []
  25. image_url_list = formatted_content.get('image_url_list', [])
  26. for img_data in image_url_list:
  27. if isinstance(img_data, dict) and 'image_url' in img_data:
  28. image_urls.append(img_data['image_url'])
  29. return image_urls
  30. def analyze_images_with_coze(self, image_urls: List[str]) -> Dict[str, Any]:
  31. """使用Coze API分析图片内容"""
  32. try:
  33. if not image_urls:
  34. return {"images_comprehension": [], "error": "没有图片需要分析"}
  35. print(f"正在使用Coze API分析 {len(image_urls)} 张图片...")
  36. response = self.coze.run(image_urls)
  37. # 解析Coze响应
  38. if response and 'data' in response:
  39. try:
  40. if isinstance(response['data'], str):
  41. data = json.loads(response['data'])
  42. else:
  43. data = response['data']
  44. return {
  45. "images_comprehension": data.get('images_comprehension', []),
  46. }
  47. except json.JSONDecodeError:
  48. return {"images_comprehension": [], "error": "Coze响应解析失败", "raw_response": response}
  49. else:
  50. return {"images_comprehension": [], "error": "Coze API响应异常", "raw_response": response}
  51. except Exception as e:
  52. print(f"Coze API调用失败: {e}")
  53. return {"images_comprehension": [], "error": f"Coze API调用失败: {str(e)}"}
  54. def process_images(self, formatted_content: Dict[str, Any]) -> Dict[str, Any]:
  55. """处理图片识别的主函数"""
  56. print("开始图片识别处理...")
  57. # 提取图片URL
  58. image_urls = self.extract_image_urls(formatted_content)
  59. print(f"提取到 {len(image_urls)} 张图片")
  60. if not image_urls:
  61. print("没有图片需要分析")
  62. return {"images_comprehension": [], "error": "没有图片需要分析"}
  63. # 分析图片
  64. result = self.analyze_images_with_coze(image_urls)
  65. if result.get("images_comprehension"):
  66. print(f"图片识别完成,共分析 {len(result['images_comprehension'])} 张图片")
  67. else:
  68. print("图片识别失败")
  69. return result
  70. def main():
  71. """测试函数"""
  72. # 模拟数据
  73. test_content = {
  74. "image_url_list": [
  75. {
  76. "image_type": 2,
  77. "image_url": "http://example.com/image1.jpg"
  78. }
  79. ]
  80. }
  81. identifier = ImageIdentifier()
  82. result = identifier.process_images(
  83. test_content
  84. )
  85. print(f"识别结果: {json.dumps(result, ensure_ascii=False, indent=2)}")
  86. if __name__ == '__main__':
  87. main()