importmatplotlib.pyplot as pltimportcv2importnumpy as npimportjsondefprocess_api_return_value(str):classStack(object):def __init__(self):
self.stack=[]defisEmpty(self):return self.stack ==[]defpush(self, item):
self.stack.append(item)defpop(self):ifself.isEmpty():print('stack is empty')return
returnself.stack.pop()defpeek(self):return self.stack[-1]defsize(self):returnlen(self.stack)
my_list=[]
stack=Stack()
begin=0for index, value inenumerate(str):if value == '{':ifstack.isEmpty():
begin=index
stack.push(value)if value == '}':
stack.pop()ifstack.isEmpty():
end=index
my_list.append(str[begin:end+ 1])
boxes_list=[]
classes=[]for value inmy_list:
boxes_list_each=[]
dic=json.loads(value)
classes.append(dic['label'])
x1= dic['X1']
x2= dic['X2']
y1= dic['Y1']
y2= dic['Y2']
boxes_list_each.append(x1)
boxes_list_each.append(y1)
boxes_list_each.append(x2)
boxes_list_each.append(y2)
boxes_list_each.append(dic['score'])
boxes_list.append(boxes_list_each)
boxes=np.array(boxes_list)returnboxes, classesdefget_class_string(class_name, score):return class_name + '{:0.2f}'.format(score).lstrip('0')defvis_one_image(im, class_names,
boxes, classes,
thresh=0.9, dpi=100,
box_alpha=1.0, show_class=True,
filename=None, ext='png'):if boxes is None or boxes.shape[0] ==0 or max(boxes[:, 4])
fig= plt.figure(frameon=False)
fig.set_size_inches(im.shape[1] /dpi, im.shape[0] / dpi) #shape[1]宽 shape[1]高
ax=plt.Axes(fig, [0., 0., 1., 1.])
ax.axis('off')
fig.add_axes(ax)
ax.imshow(im)
areas=(boxes[:, 2] -boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
sorted_inds= np.argsort(-areas) #big to small
for i insorted_inds:
bbox= boxes[i, :4]
score= boxes[i, -1]if score
contiune
ax.add_patch(
plt.Rectangle((bbox[0], bbox[1]),
bbox[2] -bbox[0],
bbox[3] - bbox[1],
fill=False, edgecolor= 'g',
linewidth=1.0, alpha=box_alpha))ifshow_class:
ax.text(
bbox[0], bbox[1] - 2,
get_class_string(class_names[classes[i]], score),
fontsize=11,
family= 'serif',
bbox=dict(
facecolor='g', alpha=0.4, pad=0, edgecolor='none'),
color='white')if filename is notNone:
fig.savefig(filename+ '.' + ext, dpi =dpi)
plt.close('all')else:
plt.imshow(im)
plt.show()if __name__ == '__main__':
class_names= ['__background__', #always index 0
'bai_sui_shan','cestbon','cocacola','jing_tian','pepsi_cola','sprite', 'starbucks_black_tea','starbucks_matcha','starbucks_mocha', 'vita_lemon_tea', 'vita_soymilk_blue', 'wanglaoji_green']
raw_image= cv2.imread("/home/stt/data/wh_t/img/1.jpg")
api_return_value= '{"X1":317.0668,"X2":334.80807,"Y1":111.80373,"Y2":138.84392,"label":1,"rotate":0.0,"scale":1.0,"score":0.9973503},{"X1":209.34439,"X2":233.45705,"Y1":98.4779,"Y2":123.20604,"label":4,"rotate":0.0,"scale":1.0,"score":0.98664963},{"X1":120.77178,"X2":154.04027,"Y1":159.96211,"Y2":227.42447,"label":12,"rotate":0.0,"scale":1.0,"score":0.37864015},{"X1":161.31192,"X2":200.91737,"Y1":31.158348,"Y2":51.811104,"label":1,"rotate":0.0,"scale":1.0,"score":0.30787778}'boxes, classes=process_api_return_value(api_return_value)
vis_one_image(raw_image, class_names, boxes, classes, thresh=0.0, box_alpha=1.0, show_class=True)