import cv2
from PIL import Image
import numpy as np
import os
for file in os.listdir('/home/lixuan/Downloads/payactions/scanandpay/images/train'):
imgpath = '/home/lixuan/Downloads/payactions/scanandpay/images/train/{}'.format(file)
txtpath = '/home/lixuan/Downloads/payactions/scanandpay/labels/train/{}'.format(file.replace('jpg','txt'))
img = cv2.imread(imgpath)
datas = open(txtpath).read().strip().split(' ')[1:]
datas = [float(data) for data in datas]
datas[0] *= img.shape[1]
datas[1] *= img.shape[0]
datas[2] *= img.shape[1]
datas[3] *= img.shape[0]
x = img.shape[1] - datas[0]
y = img.shape[0] - datas[1]
w = datas[2]
h = datas[3]
img = Image.fromarray(img)
img = img.transpose(Image.ROTATE_180)
img = np.array(img)
x1 = int(x - w / 2)
y1 = int(y - h / 2)
x2 = int(x1 + w)
y2 = int(y1 + h)
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,255),3)
cv2.namedWindow('img',0)
cv2.imshow('img',img)
cv2.waitKey(0)
import os
import cv2
count = 0
for file in os.listdir('/home/lixuan/workspace/dataset/scenesDec/images/train'):
img = cv2.imread('/home/lixuan/workspace/dataset/scenesDec/images/train/{}'.format(file))
H,W,_ = img.shape
with open('/home/lixuan/workspace/dataset/scenesDec/labels/train/{}'.format(file.replace('jpg','txt'))) as f:
datas = f.readlines()
for data in datas:
data = data.strip().split(' ')
label = int(data[0]) + 1
x = float(data[1]) * W
y = float(data[2]) * H
w = float(data[3]) * W
h = float(data[4]) * H
x1 = int(x - w / 2)
y1 = int(y - h / 2)
x2 = int(x1 + w)
y2 = int(y1 + h)
patch = img[y1:y2,x1:x2]
cv2.imwrite('/home/lixuan/workspace/gz/{}/patch_{}.jpg'.format(label,count),patch)
count += 1