cover_gpt4o.py 17 KB

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