import cv2
import numpy as np
import os
import json
count = 0
def seamless(src,dst,P,center):
src_mask = np.zeros(src.shape, src.dtype)
poly = np.array(P, np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))
output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
return output
backpaths = [os.path.join('background_out/{}'.format(file)) for file in os.listdir('background_out')]
pays = [file for file in os.listdir('/home/lixuan/data/466yuanshi/im')]
fww = open('log.txt','w')
count = 0
for bkpath in backpaths:
for dir in os.listdir('/home/lixuan/sku_data/sku110k/yolov5_sku/annotations'):
# dir = 'F04247916_1_20210319T114750Z_20210319T115941Z%5B00-07-51%5D%5B20210323-163755784%5D.json'
datas = json.load(open('/home/lixuan/sku_data/sku110k/yolov5_sku/annotations/{}'.format(dir)))
imagepath = datas['imagepath']
if imagepath.split('466yuanshi/imgs/')[-1] in pays:
originalimg = cv2.imread(imagepath)
try:
imageHeight,imageWidth,_ = originalimg.shape
except:
print(dir,'*'*30)
continue
P = datas['people']
if datas.get('pay') != None:
box = datas['pay']
lab = 'pay'
else:
box = datas['scanning']
lab = 'scan'
continue
P = datas['people']
imgback = cv2.imread(bkpath)
imgback = cv2.resize(imgback,(imageWidth,imageHeight))
img = np.zeros((imageHeight, imageWidth, 3), np.uint8)
area1 = np.array(P)
cv2.fillPoly(img, [area1], (255, 255, 255))
maskindex = np.where(img > 0)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
__,contours, _ = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
s = contours[0].shape[0]
cont = contours[0]
for co in contours:
if co.shape[0] > s:
cont = co
s = co.shape[0]
if True:
# 外接矩形
x, y, w, h = cv2.boundingRect(cont)
x1 = x
y1 = y
x2 = x1 + w
y2 = y1 + h
x = ((box[0][0] + box[1][0])/2)/imageWidth
y = ((box[0][1]+box[1][1])/2)/imageHeight
w = box[1][0] - box[0][0]
h = box[1][1] - box[0][1]
out = seamless(originalimg, imgback, P,((x1+x2)//2,(y1+y2)//2))
cv2.imwrite('/home/lixuan/data/466yuanshi/img/pay_{}.jpg'.format(count),out)
fw = open('/home/lixuan/data/466yuanshi/lab/pay_{}.txt'.format(count),'w')
fw.write('1 ' + str(x) + ' ' + str(y) + ' ' + str(w/imageWidth) + ' ' + str(h/imageHeight) + '\n')
fw.close()
count += 1
# out = cv2.rectangle(out, (box[0][0], box[0][1]), (box[1][0], box[1][1]), (0, 255, 0), 3)
# cv2.namedWindow('img',0)
# cv2.imshow('img',out)
# cv2.waitKey(0)