Upload get_coordinate.py with huggingface_hub
Browse files- get_coordinate.py +112 -0
get_coordinate.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# %%
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
import cv2
|
| 6 |
+
import logging
|
| 7 |
+
from logging.handlers import RotatingFileHandler
|
| 8 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
|
| 11 |
+
# 配置日志记录到文件
|
| 12 |
+
# 日志配置
|
| 13 |
+
def setup_logger(log_file):
|
| 14 |
+
logger = logging.getLogger("ImageProcessingLogger")
|
| 15 |
+
logger.setLevel(logging.DEBUG)
|
| 16 |
+
|
| 17 |
+
# 设置文件滚动,每个日志文件最大10MB,保留5个文件
|
| 18 |
+
handler = RotatingFileHandler(log_file, maxBytes=10 * 1024 * 1024, backupCount=5)
|
| 19 |
+
handler.setLevel(logging.DEBUG)
|
| 20 |
+
|
| 21 |
+
# 设置日志格式
|
| 22 |
+
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
| 23 |
+
handler.setFormatter(formatter)
|
| 24 |
+
|
| 25 |
+
# 添加处理器
|
| 26 |
+
logger.addHandler(handler)
|
| 27 |
+
return logger
|
| 28 |
+
|
| 29 |
+
# 日志实例
|
| 30 |
+
logger = setup_logger("app.log")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def read_json(file_path):
|
| 34 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 35 |
+
data = json.load(file)
|
| 36 |
+
return data
|
| 37 |
+
|
| 38 |
+
def write_json(file_path, data):
|
| 39 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 40 |
+
json.dump(data, file, ensure_ascii=False, indent=4)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def get_corrdinate(data, image_path,logger, out_root=None):
|
| 45 |
+
try:
|
| 46 |
+
image_name = image_path.split('/')[-1]
|
| 47 |
+
out_path = os.path.join(out_root, image_name)
|
| 48 |
+
|
| 49 |
+
y_str, x_str = data['click_loc'].split(',')[2:]
|
| 50 |
+
y = float(y_str.split(':')[1])
|
| 51 |
+
x = float(x_str.strip())
|
| 52 |
+
# print(f"x: {x}, y: {y}")
|
| 53 |
+
|
| 54 |
+
image = cv2.imread(image_path)
|
| 55 |
+
if image is None:
|
| 56 |
+
logger.error(f"Failed to read image: {image_path}")
|
| 57 |
+
return
|
| 58 |
+
height, width, _ = image.shape
|
| 59 |
+
# print(f"height: {height}, width: {width}")
|
| 60 |
+
|
| 61 |
+
x = int(x * width)
|
| 62 |
+
y = int(y * height)
|
| 63 |
+
# print(f"x: {x}, y: {y}")
|
| 64 |
+
|
| 65 |
+
color = (0, 255, 0)
|
| 66 |
+
|
| 67 |
+
thickness = 5
|
| 68 |
+
center = (x, y)
|
| 69 |
+
length = 35
|
| 70 |
+
cv2.line(image, (center[0] - length, center[1] - length), (center[0] - length, center[1] + length), color, thickness)
|
| 71 |
+
cv2.line(image, (center[0] - length, center[1] + length), (center[0] + length, center[1] + length), color, thickness)
|
| 72 |
+
cv2.line(image, (center[0] + length, center[1] + length), (center[0] + length, center[1] - length), color, thickness)
|
| 73 |
+
cv2.line(image, (center[0] + length, center[1] - length), (center[0] - length, center[1] - length), color, thickness)
|
| 74 |
+
|
| 75 |
+
cv2.imwrite(out_path, image)
|
| 76 |
+
|
| 77 |
+
# 每处理100张记录日志
|
| 78 |
+
if count % 100 == 0:
|
| 79 |
+
logger.info(f"Processed {count} images successfully.")
|
| 80 |
+
except Exception as e:
|
| 81 |
+
logger.exception(f"Error processing data: {data}. Exception: {e}")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def process_data(data, root_path,logger, out_root):
|
| 85 |
+
image_path = os.path.join(root_path, data['image'])
|
| 86 |
+
get_corrdinate(data, image_path,logger, out_root)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
# 参数配置
|
| 91 |
+
json_file = r'/code/Auto-GUI/dataset/mind/general_blip_train_llava_coco.json'
|
| 92 |
+
root_path = r'/code/Auto-GUI/dataset'
|
| 93 |
+
out_root = r'/code/Auto-GUI/dataset/coco_corrdinate'
|
| 94 |
+
count = 0
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# 读取 JSON 数据
|
| 98 |
+
data = read_json(json_file)
|
| 99 |
+
data1 = [line for line in data if line['action_type'] == '#DUAL_POINT#'][1:]
|
| 100 |
+
logging.debug(f"total data: " +str(len(data1)))
|
| 101 |
+
|
| 102 |
+
# 并行处理数据
|
| 103 |
+
os.makedirs(out_root, exist_ok=True)
|
| 104 |
+
with ThreadPoolExecutor(max_workers=8) as executor:
|
| 105 |
+
list(tqdm(executor.map(lambda d: process_data(d, root_path, logger, out_root), data1), total=len(data1)))
|
| 106 |
+
|
| 107 |
+
# list(tqdm(executor.map(lambda d: process_data(d, root_path, out_root), data1), total=len(data1)))
|
| 108 |
+
|
| 109 |
+
logger.info("All processing complete.")
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|