ali_image_validation.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import os
  2. import base64
  3. import json
  4. import time
  5. from PIL import Image
  6. from io import BytesIO
  7. import logging
  8. from openai import OpenAI
  9. from retry import retry
  10. import requests
  11. class AliImageValidator:
  12. def __init__(self, api_key="sk-ccfcdd12fd434d0dab1406958663df9d"):
  13. self.client = OpenAI(
  14. base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
  15. api_key=api_key or os.environ.get("DASHSCOPE_API_KEY")
  16. )
  17. if not self.client.api_key:
  18. raise ValueError("未找到API密钥,请设置环境变量DASHSCOPE_API_KEY或通过参数传递")
  19. logging.basicConfig(
  20. format='%(asctime)s - %(levelname)s - %(message)s',
  21. level=logging.DEBUG
  22. )
  23. @retry(tries=3, delay=1, backoff=2, max_delay=5, exceptions=(Exception,))
  24. def analyze_image(self, image_url):
  25. if not image_url.startswith(('http://', 'https://')):
  26. return {
  27. 'is_false_positive': False,
  28. 'uav_detected': False,
  29. 'probability': 0.0,
  30. 'reason': '无效的图片URL',
  31. 'response_time': 0.0
  32. }
  33. system_prompt = """
  34. 你是一个专业的图像分析专家,请严格按照以下要求响应:
  35. 1. 输出必须为合法的JSON格式,不能是纯自然语言段落
  36. 2. 包含三个字段:
  37. probability(误报概率0-1)
  38. is_uav(是否是无人机 bool)
  39. reason(分析理由)
  40. 3. 不要包含任何额外说明
  41. 请分析该监控画面:
  42. - 判断是否为安全误报
  43. - 识别画面中是否存在无人机
  44. - 给出详细分析理由
  45. """
  46. try:
  47. start_time = time.time()
  48. model_name = "qwen-vl-max-latest"
  49. logging.info(f"发送请求到阿里云 | 端点: {self.client.base_url} | 模型: {model_name}")
  50. response = self.client.chat.completions.create(
  51. model=model_name,
  52. messages=[
  53. {"role": "system", "content": [{"type": "text", "text": system_prompt}]},
  54. {
  55. "role": "user",
  56. "content": [
  57. {
  58. "type": "image_url",
  59. "image_url": {
  60. "url": image_url
  61. }
  62. },
  63. {"type": "text", "text": "请分析该监控画面是否为误报"}
  64. ]
  65. }
  66. ]
  67. )
  68. end_time = time.time()
  69. response_time = end_time - start_time
  70. try:
  71. result = response.choices[0].message.content
  72. logging.debug(f"原始响应内容: {result}")
  73. try:
  74. result_json = json.loads(result.split('```json')[1].split('```')[0].strip()) # 提取markdown代码块中的JSON
  75. return {
  76. 'is_false_positive': result_json.get('is_false_positive', False),
  77. 'uav_detected': result_json.get('is_uav', False),
  78. 'probability': result_json.get('probability', 0.0),
  79. 'reason': result_json.get('reason', '无分析结果'),
  80. 'response_time': response_time
  81. }
  82. except (IndexError, json.JSONDecodeError, KeyError) as e:
  83. logging.error(f"响应解析失败 | 错误类型: {type(e).__name__} | 原始响应: {result}")
  84. return {
  85. 'is_false_positive': "误报" in result,
  86. 'uav_detected': False,
  87. 'probability': 0.0,
  88. 'reason': '响应格式异常',
  89. 'response_time': response_time
  90. }
  91. except (json.JSONDecodeError, ValueError) as e:
  92. logging.error(f"响应解析失败: {str(e)}")
  93. return {
  94. 'is_false_positive': "误报" in result,
  95. 'uav_detected': False,
  96. 'probability': 0.0,
  97. 'reason': '解析失败',
  98. 'response_time': response_time
  99. }
  100. except Exception as e:
  101. end_time = time.time()
  102. response_time = end_time - start_time
  103. logging.error(f"API调用失败 | 端点: {self.client.base_url} | 模型: {model_name} | 错误类型: {type(e).__name__} | 错误详情: {str(e)}")
  104. return {
  105. 'is_false_positive': False,
  106. 'uav_detected': False,
  107. 'probability': 0.0,
  108. 'reason': 'API调用失败',
  109. 'response_time': response_time
  110. }
  111. if __name__ == "__main__":
  112. import argparse
  113. parser = argparse.ArgumentParser(description='阿里云图像误报分析工具')
  114. parser.add_argument('image_url', help='需要分析的图片URL地址')
  115. args = parser.parse_args()
  116. validator = AliImageValidator()
  117. result = validator.analyze_image(args.image_url)
  118. print(f"综合分析结果:\n误报概率: {result['probability']:.2f}\n无人机识别: {'是' if result['uav_detected'] else '否'}\n判定结果: {'误报' if result['is_false_positive'] else '真实威胁'}\n响应时间: {result['response_time']:.2f}秒\n分析理由: {result['reason']}")