- 根据https://tianchi.aliyun.com/notebook-ai/detail?postId=107990 链接下载数据集
- 利用json库读取json文件,得到如下结果
with open('./dataset/mchar_val.json','r',encoding='utf8')as fp:
json_data = json.load(fp) print('这是文件中的json数据:',json_data)
3.利用opencv库函数实现数据的可视化。
全部代码如下:
import json
import cv2
img_path="./dataset/mchar_val/"
with open('./dataset/mchar_val.json','r',encoding='utf8')as fp:
json_data = json.load(fp)
print('这是文件中的json数据:',json_data)
cnt=0
#得到前5条数据并利用matplot可视化数据集
for tmp in json_data:
if cnt >5:
break;
temp_path=img_path+tmp
img = cv2.imread(temp_path)
cv2.namedWindow('test', cv2.WINDOW_NORMAL)
cv2.resizeWindow('test', 1000, 1000)
print(json_data[tmp]['label'])
label_len=len(json_data[tmp]['label'])
print("xxx:",json_data[tmp]['top'][0])
for i in range(0,label_len):
y=int(json_data[tmp]['top'][i])
x=int(json_data[tmp]['left'][i])
w=int(json_data[tmp]['width'][i])
h=int(json_data[tmp]['height'][i])
cv2.rectangle(img, (x,y),(x+w,y+h), (0, 0, 255), 1)
cv2.putText(img, str(json_data[tmp]['label'][i]), (x-3,y-3), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.3, (255, 0, 0))
cv2.imshow("test",img)
key = cv2.waitKey(0)
# if key == ord('m'):
# continue
cnt+=1
# train_label = [json_data[x]['label'] for x in json_data]
# print(train_label)
# print('这是读取到文件数据的数据类型:', type(json_data))