rogerxavier commited on
Commit
c2df0d7
1 Parent(s): b12df33

Update 2magiDialogCut.py

Browse files
Files changed (1) hide show
  1. 2magiDialogCut.py +31 -37
2magiDialogCut.py CHANGED
@@ -3,27 +3,34 @@ import requests
3
  import json
4
  from PIL import Image
5
  import os
6
-
7
-
8
- def getImgCoordinatesByMagi(image_path:"包含后缀的文件路径") -> "全部对话坐标list,失败返回none":
9
- # headers = {
10
- # 'authority': 'rogerxavier-fastapi-t5-magi.hf.space',
11
- # 'method': 'GET',
12
- # 'scheme': 'https',
13
- # 'Accept': '*/*',
14
- # 'Accept-Encoding': 'gzip, deflate, br, zstd',
15
- # 'Accept-Language': 'zh-CN,zh;q=0.9',
16
- # 'Cookie': 'spaces-jwt=eyJhbGciOiJFZERTQSJ9.eyJyZWFkIjp0cnVlLCJwZXJtaXNzaW9ucyI6eyJyZXBvLmNvbnRlbnQucmVhZCI6dHJ1ZX0sIm9uQmVoYWxmT2YiOnsia2luZCI6InVzZXIiLCJfaWQiOiI2NDJhNTNiNTE2ZDRkODI5M2M5YjdiNzgiLCJ1c2VyIjoicm9nZXJ4YXZpZXIifSwiaWF0IjoxNzE2NjExMTE3LCJzdWIiOiIvc3BhY2VzL3JvZ2VyeGF2aWVyL2Zhc3RhcGlfdDVfbWFnaSIsImV4cCI6MTcxNjY5NzUxNywiaXNzIjoiaHR0cHM6Ly9odWdnaW5nZmFjZS5jbyJ9.W00jo8kiRgwCpq5aaGhaPE2RP2jLOyvfimjyIfHVhP1gs7NHkBkRzVTFqYv3TRtZoHNPTiFiI5Ehu12KP06sDQ',
17
- # 'Dnt': '1',
18
- # 'Priority': 'u=1, i',
19
- # 'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
20
- # 'Sec-Ch-Ua-Mobile': '?0',
21
- # 'Sec-Ch-Ua-Platform': '"Windows"',
22
- # 'Sec-Fetch-Dest': 'empty',
23
- # 'Sec-Fetch-Mode': 'cors',
24
- # 'Sec-Fetch-Site': 'same-origin',
25
- # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
26
- # }
 
 
 
 
 
 
 
27
  with open(image_path, 'rb') as file:
28
  image_bytes = file.read()
29
  files = {
@@ -31,8 +38,8 @@ def getImgCoordinatesByMagi(image_path:"包含后缀的文件路径") -> "全部
31
  }
32
  magi_fastapi_base = 'https://rogerxavier-fastapi-t5-magi.hf.space/getCoordinates'
33
  try:
34
- # resp = requests.post(magi_fastapi_base, files=files, headers=headers) ##是仓库public后好像就api可用了
35
- resp = requests.post(magi_fastapi_base, files=files) ##不使用header看看会不会还限制
36
  print(resp.json())
37
  return resp.json()["panels"]
38
  except Exception as e:
@@ -59,23 +66,10 @@ def save_img(new_save_path:"新文件的保存路径(包含后缀)",old_img_path
59
  print(new_save_path)
60
  # 原始照片文件名
61
  original_image = old_img_path
62
- #打开原始照片
63
- # img = Image.open(original_image)
64
- # text_bboxes_for_all_images = getImgCoordinatesByMagi(original_image)
65
- # if text_bboxes_for_all_images is not None:
66
- # for index, box in enumerate(text_bboxes_for_all_images):
67
- # cropped_img = img.crop(tuple(box))
68
- # # 保存裁剪后的图片,并按照数字大小依次排序命名(包括本身的也要3d,比如 0.jpg - >000.000_cropped.jpg)
69
- #
70
- # cropped_img.save(f"{os.path.splitext(new_save_path)[0]}_{index:03d}_cropped.jpg")
71
- # else:
72
- # print("图片识别有问题,准备删除")
73
- # os.remove(original_image)
74
-
75
 
76
  ##防止文件打开无法删除
77
 
78
- text_bboxes_for_all_images = getImgCoordinatesByMagi(original_image)
79
  if text_bboxes_for_all_images is not None:
80
  with Image.open(original_image) as img:
81
  for index, box in enumerate(text_bboxes_for_all_images):
 
3
  import json
4
  from PIL import Image
5
  import os
6
+ from faker import Faker
7
+ fake = Faker()
8
+ fake_user_agent = fake.user_agent()
9
+ fake_headers = {
10
+ 'scheme': 'https',
11
+ 'Accept': '*/*',
12
+ 'Accept-Encoding': 'gzip, deflate, br, zstd',
13
+ 'Accept-Language': 'zh-CN,zh;q=0.9',
14
+ 'Dnt': '1',
15
+ 'Priority': 'u=1, i',
16
+ 'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
17
+ 'Sec-Ch-Ua-Mobile': '?0',
18
+ 'Sec-Ch-Ua-Platform': '"Windows"',
19
+ 'Sec-Fetch-Dest': 'empty',
20
+ 'Sec-Fetch-Mode': 'cors',
21
+ 'Sec-Fetch-Site': 'same-origin',
22
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
23
+ }
24
+ fake_headers['User-Agent'] = fake_user_agent
25
+
26
+
27
+ def getImgCoordinatesByMagi(image_path:"包含后缀的文件路径",fake_headers:dict=None) -> "全部对话坐标list,失败返回none":
28
+ # 创建一个Session对象
29
+ session = requests.Session()
30
+ if fake_headers is not None:
31
+ session.headers = fake_headers
32
+
33
+
34
  with open(image_path, 'rb') as file:
35
  image_bytes = file.read()
36
  files = {
 
38
  }
39
  magi_fastapi_base = 'https://rogerxavier-fastapi-t5-magi.hf.space/getCoordinates'
40
  try:
41
+
42
+ resp = session.post(magi_fastapi_base, files=files) ##用faker header试试
43
  print(resp.json())
44
  return resp.json()["panels"]
45
  except Exception as e:
 
66
  print(new_save_path)
67
  # 原始照片文件名
68
  original_image = old_img_path
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  ##防止文件打开无法删除
71
 
72
+ text_bboxes_for_all_images = getImgCoordinatesByMagi(image_path =original_image,fake_headers = fake_headers)
73
  if text_bboxes_for_all_images is not None:
74
  with Image.open(original_image) as img:
75
  for index, box in enumerate(text_bboxes_for_all_images):