业务背景:对图片进行画框后调用排序方法按照“从上到下,从左到右”对已经画的框进行排序。排序方法当前有缺陷,修复后需要验证。
目前有如下信息:
- 原始图片地址
- 图片对应的每个框的坐标(xy左上起始点,wh宽高),且大框有可能是多个小框的组合(大框按照组合框的第一个框的位置来排序)
验证的过程就是找到原始图片,根据排序后的大框的顺序把它的小框在原图上画出来,验证顺序是否满足 “从上到下,从左到右”。由于原图比较多,每个图上的大框更多,想要验证全部数据会存在如下问题:
- 数据量大,一个个画效率比较低
- 容易画错、漏画
解决办法:使用opencv找到坐标按照排序后的顺序画在原始图片上,直接观察原始图片。
- 下载python-opencv
pip install opencv-python -i http://mirrors.aliyun.com/pypi/simple/
- 编码
# coding:utf8
import json
import os
from loguru import logger
import requests
import cv2 as cv
def parse_page_json(json_path, out_path):
# 结果目录
if not os.path.exists(out_path):
os.makedirs(os.path.join(out_path,'src')) # 存原图
# 解析原始导出数据
with open(json_path, 'r', encoding='utf8') as f_r:
for line in f_r:
page = json.loads(line.strip('\n'))
source_page_url = page['supplementaryPage']['pageImg']
p_no = page['supplementaryPage']['number']
q_id_li = page['supplementaryPage']['questionIds']
# 下载并存储原图
res = requests.get(source_page_url)
logger.info(f'download {source_page_url}')
file_name = source_page_url.split('/')[-1]
with open(os.path.abspath(os.path.join(out_path, 'src', file_name)), 'wb') as f_w:
for a in res.iter_content(chunk_size=32): # iter是iter
f_w.write(a)
# 获取题目框信息
q_gps_li = page['supplementaryPage']['abkQuestionLocationDTOs']
q_li_info = {} # {'d42b150d-f804-4d7c-b8e6-22140d0ad5a3': [((70.8743980738362, 406.7817014446228), (806.0670144462279, 1475.9831460674159))], '3b31243e-95e8-4111-9047-3dade17b0bb6': [((85.80216693418933, 1483.4470304975923), (807.932985553772, 2134.6709470304977)), ((820.994783306581, 376.92616372391655), (1556.1873996789727, 569.1211878009631))], '46481e96-aef4-409c-b75c-db5b1cdcf05a': [((822.8607544141252, 580.317014446228), (1569.2491974317818, 1729.7552166934192))], '1acb999a-cfa2-44a4-b144-5f71d2693214': [((820.994783306581, 1737.2191011235957), (1593.5068218298557, 2127.207062600321))]}
for g_gps in q_gps_li:
# 题目ID
q_id = g_gps['abkQuestionId']
locationDTOs = g_gps['locationDTOs']
# 题目如果有多个框,按顺序分别吧多个框添加进列表
lct_li = []
for location in locationDTOs:
pt_s = (round(float((location['x']))), round(float((location['y']))))
pt_e = (round(float((location['x']))) + round(float((location['w']))),
round(float((location['y']))) + round(float((location['h']))))
lct_li.append((pt_s, pt_e))
q_li_info.update({q_id: lct_li})
logger.info(f'start draw pic : {file_name}')
cv_write(os.path.abspath(os.path.join(out_path, 'src', file_name)), out_path + '/', p_no, q_li_info,q_id_li)
def cv_write(src, dst, page_no, q_li_info,q_id_li):
# 读取原图
src = cv.imread(src)
for q, gps_li in q_li_info.items():
# 框排序后的编号
q_no = q_id_li.index(q)
# 小框编号(如果有小框,把小框也画出来)
xt_no = 0
for gps in gps_li:
pt_s, pt_e = gps
p_color = (0, 0, 255)
# 画矩形:图,开始坐标,结束坐标,颜色...
cv.rectangle(src, pt_s, pt_e, p_color)
# 写题目题号
font = cv.FONT_HERSHEY_SIMPLEX
tihao = str(q_no + 1) + '-' + str(xt_no + 1) if len(gps_li) > 1 else str(q_no + 1)
# 写入框编号
cv.putText(src, tihao, pt_s, font, 1.2, (0, 0, 255), 2)
# cv.putText(src, str(q), (pt_s[0],pt_s[1]+20), font, 0.7, (0, 0, 255), 2) # 写入题目ID
xt_no += 1
q_no += 1
# 另存图片
cv.imwrite(dst + str(page_no) + '.jpg', src)
if __name__ == '__main__':
parse_page_json('page.json', 'ttt4')
出来的图片信息为: