cover_gpt4o.py 16 KB

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