cover_gpt4o.py 17 KB

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