cover_gpt4o.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import requests
  2. import json
  3. class CoverAnalyzer:
  4. @staticmethod
  5. def cover_list_data(cover_list):
  6. list_data = []
  7. for cover in cover_list:
  8. data_dict = CoverAnalyzer.analyze_covers(cover)
  9. if data_dict:
  10. list_data.append(data_dict)
  11. return list_data
  12. @staticmethod
  13. def analyze_covers(cover):
  14. for i in range(3):
  15. url = "http://aigc-api.cybertogether.net//aigc/dev/test/gpt"
  16. payload = json.dumps({
  17. "imageList": [cover],
  18. # "prompt": "请帮我完成图片分析工作",
  19. "prompt": "请帮我完成图片分析工作:你只需要理解图片正中心600 × 480的区域画面,根据该区域画面和特征,给出相应的回答",
  20. "responseFormat": {
  21. "type": "json_schema",
  22. "json_schema": {
  23. "strict": True,
  24. "name": "comprehension_result",
  25. "schema": {
  26. "type": "object",
  27. "properties": {
  28. "是否包含老年人为画面主体": {
  29. "type": "object",
  30. "properties": {
  31. "result": {
  32. "description": "请分析所有图片,是否包含老年人为画面主体,如果是返回1,如果不是返回0",
  33. "type": "number",
  34. "enum": [1, 0]
  35. },
  36. "reason": {
  37. "description": "得出结论的客观的分析原因和依据",
  38. "type": "string"
  39. }
  40. },
  41. "required": ["result", "reason"],
  42. "additionalProperties": False
  43. },
  44. "是否有多人为画面主体": {
  45. "type": "object",
  46. "properties": {
  47. "result": {
  48. "description": "请分析所有图片,是否有多人为画面主体,如果是返回1,如果不是返回0",
  49. "type": "number",
  50. "enum": [1, 0]
  51. },
  52. "reason": {
  53. "description": "得出结论的客观的分析原因和依据",
  54. "type": "string"
  55. }
  56. },
  57. "required": ["result", "reason"],
  58. "additionalProperties": False
  59. },
  60. "是否有超过3人为画面主体": {
  61. "type": "object",
  62. "properties": {
  63. "result": {
  64. "description": "请分析所有图片,是否有超过3人为画面主体,如果是返回1,如果不是返回0",
  65. "type": "number",
  66. "enum": [1, 0]
  67. },
  68. "reason": {
  69. "description": "得出结论的客观的分析原因和依据",
  70. "type": "string"
  71. }
  72. },
  73. "required": ["result", "reason"],
  74. "additionalProperties": False
  75. },
  76. "是否充斥画面的密集人群为画面主体": {
  77. "type": "object",
  78. "properties": {
  79. "result": {
  80. "description": "请分析所有图片,是否充斥画面的密集人群为画面主体,如果是返回1,如果不是返回0",
  81. "type": "number",
  82. "enum": [1, 0]
  83. },
  84. "reason": {
  85. "description": "得出结论的客观的分析原因和依据",
  86. "type": "string"
  87. }
  88. },
  89. "required": ["result", "reason"],
  90. "additionalProperties": False
  91. },
  92. "是否存在知名历史、近代人物": {
  93. "type": "object",
  94. "properties": {
  95. "result": {
  96. "description": "请分析所有图片,是否存在知名历史、近代人物,如果是返回1,如果不是返回0",
  97. "type": "number",
  98. "enum": [1, 0]
  99. },
  100. "reason": {
  101. "description": "得出结论的客观的分析原因和依据",
  102. "type": "string"
  103. }
  104. },
  105. "required": ["result", "reason"],
  106. "additionalProperties": False
  107. },
  108. "是否存在人物脸部、头部未完整出现在画面的情况": {
  109. "type": "object",
  110. "properties": {
  111. "result": {
  112. "description": "请分析所有图片,是否存在人物脸部、头部未完整出现在画面的情况,如果是返回1,如果不是返回0",
  113. "type": "number",
  114. "enum": [1, 0]
  115. },
  116. "reason": {
  117. "description": "得出结论的客观的分析原因和依据",
  118. "type": "string"
  119. }
  120. },
  121. "required": ["result", "reason"],
  122. "additionalProperties": False
  123. },
  124. "是否是不以人为主体的鲜花、美景、知名建筑或风景": {
  125. "type": "object",
  126. "properties": {
  127. "result": {
  128. "description": "请分析所有图片,是否是不以人为主体的鲜花、美景、知名建筑或风景,如果是返回1,如果不是返回0",
  129. "type": "number",
  130. "enum": [1, 0]
  131. },
  132. "reason": {
  133. "description": "得出结论的客观的分析原因和依据",
  134. "type": "string"
  135. }
  136. },
  137. "required": ["result", "reason"],
  138. "additionalProperties": False
  139. },
  140. "是否是老照片、怀旧风格": {
  141. "type": "object",
  142. "properties": {
  143. "result": {
  144. "description": "请分析所有图片,是否是老照片、怀旧风格,如果是返回1,如果不是返回0",
  145. "type": "number",
  146. "enum": [1, 0]
  147. },
  148. "reason": {
  149. "description": "得出结论的客观的分析原因和依据",
  150. "type": "string"
  151. }
  152. },
  153. "required": ["result", "reason"],
  154. "additionalProperties": False
  155. },
  156. "是否是农村、军事、综艺演出、历史画面": {
  157. "type": "object",
  158. "properties": {
  159. "result": {
  160. "description": "请分析所有图片,是否是农村、军事、综艺演出、历史画面,如果是返回1,如果不是返回0",
  161. "type": "number",
  162. "enum": [1, 0]
  163. },
  164. "reason": {
  165. "description": "得出结论的客观的分析原因和依据",
  166. "type": "string"
  167. }
  168. },
  169. "required": ["result", "reason"],
  170. "additionalProperties": False
  171. },
  172. "是否有趣味、惊奇的形象或画面为主体": {
  173. "type": "object",
  174. "properties": {
  175. "result": {
  176. "description": "请分析所有图片,是否有趣味、惊奇的形象或画面为主体,如果是返回1,如果不是返回0",
  177. "type": "number",
  178. "enum": [1, 0]
  179. },
  180. "reason": {
  181. "description": "得出结论的客观的分析原因和依据",
  182. "type": "string"
  183. }
  184. },
  185. "required": ["result", "reason"],
  186. "additionalProperties": False
  187. },
  188. "是否以大号文字或密集文字为主体并且不包含人物": {
  189. "type": "object",
  190. "properties": {
  191. "result": {
  192. "description": "请分析所有图片,是否以大号文字或密集文字为主体并且不包含人物,如果是返回1,如果不是返回0",
  193. "type": "number",
  194. "enum": [1, 0]
  195. },
  196. "reason": {
  197. "description": "得出结论的客观的分析原因和依据",
  198. "type": "string"
  199. }
  200. },
  201. "required": ["result", "reason"],
  202. "additionalProperties": False
  203. },
  204. "是否是不包含人物的纯色画面": {
  205. "type": "object",
  206. "properties": {
  207. "result": {
  208. "description": "请分析所有图片,是否是不包含人物的纯色画面,如果是返回1,如果不是返回0",
  209. "type": "number",
  210. "enum": [1, 0]
  211. },
  212. "reason": {
  213. "description": "得出结论的客观的分析原因和依据",
  214. "type": "string"
  215. }
  216. },
  217. "required": ["result", "reason"],
  218. "additionalProperties": False
  219. },
  220. "是否是模糊的或清晰度、像素较低的": {
  221. "type": "object",
  222. "properties": {
  223. "result": {
  224. "description": "请分析所有图片,是否是模糊的或清晰度、像素较低的,如果是返回1,如果不是返回0",
  225. "type": "number",
  226. "enum": [1, 0]
  227. },
  228. "reason": {
  229. "description": "得出结论的客观的分析原因和依据",
  230. "type": "string"
  231. }
  232. },
  233. "required": ["result", "reason"],
  234. "additionalProperties": False
  235. }
  236. },
  237. "additionalProperties": False,
  238. "required": [
  239. "是否包含老年人为画面主体",
  240. "是否有多人为画面主体",
  241. "是否有超过3人为画面主体",
  242. "是否充斥画面的密集人群为画面主体",
  243. "是否存在知名历史、近代人物",
  244. "是否存在人物脸部、头部未完整出现在画面的情况",
  245. "是否是不以人为主体的鲜花、美景、知名建筑或风景",
  246. "是否是老照片、怀旧风格",
  247. "是否是农村、军事、综艺演出、历史画面",
  248. "是否有趣味、惊奇的形象或画面为主体",
  249. "是否以大号文字或密集文字为主体并且不包含人物",
  250. "是否是不包含人物的纯色画面",
  251. "是否是模糊的或清晰度、像素较低的"
  252. ]
  253. }
  254. }
  255. }
  256. })
  257. headers = {'Content-Type': 'application/json'}
  258. try:
  259. response = requests.post(url, headers=headers, data=payload)
  260. response_data = response.json()
  261. data = json.loads(response_data.get('data', '{}'))
  262. result_sum = sum(item['result'] for item in data.values()) # 获取总和
  263. data_dict = {
  264. "data": str(data),
  265. "cover_url": cover,
  266. "cover_sum": result_sum
  267. }
  268. return data_dict
  269. except requests.exceptions.RequestException as e:
  270. print(f"请求出错: {e}")
  271. continue
  272. except json.JSONDecodeError as e:
  273. print(f"JSON 解析错误: {e}")
  274. continue
  275. except Exception as e:
  276. print(f"其他错误: {e}")
  277. continue
  278. return None