123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- import requests
- import json
- class CoverAnalyzer:
- @staticmethod
- def cover_list_data(cover_list):
- list_data = []
- for cover in cover_list:
- data_dict = CoverAnalyzer.analyze_covers(cover)
- list_data.append(data_dict)
- return list_data
- @staticmethod
- def analyze_covers(cover):
- for i in range(3):
- url = "http://aigc-api.cybertogether.net//aigc/dev/test/gpt"
- payload = json.dumps({
- "imageList": [cover],
- "prompt": "请帮我完成图片分析工作",
- "responseFormat": {
- "type": "json_schema",
- "json_schema": {
- "strict": True,
- "name": "comprehension_result",
- "schema": {
- "type": "object",
- "properties": {
- "是否包含老年人为画面主体": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否包含老年人为画面主体,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否有多人为画面主体": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否有多人为画面主体,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否有超过3人为画面主体": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否有超过3人为画面主体,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否充斥画面的密集人群为画面主体": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否充斥画面的密集人群为画面主体,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否存在知名历史、近代人物": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否存在知名历史、近代人物,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否存在人物脸部、头部未完整出现在画面的情况": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否存在人物脸部、头部未完整出现在画面的情况,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否是不以人为主体的鲜花、美景、知名建筑或风景": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否是不以人为主体的鲜花、美景、知名建筑或风景,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否是老照片、怀旧风格": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否是老照片、怀旧风格,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否是农村、军事、综艺演出、历史画面": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否是农村、军事、综艺演出、历史画面,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否有趣味、惊奇的形象或画面为主体": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否有趣味、惊奇的形象或画面为主体,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否以大号文字或密集文字为主体并且不包含人物": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否以大号文字或密集文字为主体并且不包含人物,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否是不包含人物的纯色画面": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否是不包含人物的纯色画面,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- },
- "是否是模糊的或清晰度、像素较低的": {
- "type": "object",
- "properties": {
- "result": {
- "description": "请分析所有图片,是否是模糊的或清晰度、像素较低的,如果是返回1,如果不是返回0",
- "type": "number",
- "enum": [1, 0]
- },
- "reason": {
- "description": "得出结论的客观的分析原因和依据",
- "type": "string"
- }
- },
- "required": ["result", "reason"],
- "additionalProperties": False
- }
- },
- "additionalProperties": False,
- "required": [
- "是否包含老年人为画面主体",
- "是否有多人为画面主体",
- "是否有超过3人为画面主体",
- "是否充斥画面的密集人群为画面主体",
- "是否存在知名历史、近代人物",
- "是否存在人物脸部、头部未完整出现在画面的情况",
- "是否是不以人为主体的鲜花、美景、知名建筑或风景",
- "是否是老照片、怀旧风格",
- "是否是农村、军事、综艺演出、历史画面",
- "是否有趣味、惊奇的形象或画面为主体",
- "是否以大号文字或密集文字为主体并且不包含人物",
- "是否是不包含人物的纯色画面",
- "是否是模糊的或清晰度、像素较低的"
- ]
- }
- }
- }
- })
- headers = {'Content-Type': 'application/json'}
- try:
- response = requests.post(url, headers=headers, data=payload)
- response_data = response.json()
- data = json.loads(response_data.get('data', '{}'))
- result_sum = sum(item['result'] for item in data.values()) # 获取总和
- data_dict = {
- "data": str(data),
- "cover_url": cover,
- "cover_sum": result_sum
- }
- return data_dict
- except requests.exceptions.RequestException as e:
- print(f"请求出错: {e}")
- continue
- except json.JSONDecodeError as e:
- print(f"JSON 解析错误: {e}")
- continue
- except Exception as e:
- print(f"其他错误: {e}")
- continue
- return None
|