59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
import os
|
|
import shutil
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
def cropImg(img_orginal:np.array,xyxy):
|
|
return img_orginal[xyxy[1]:xyxy[3],xyxy[0]:xyxy[2],:]
|
|
|
|
def cxywh2xyxy(cxywh):
|
|
xyxy = [0,0,0,0]
|
|
xyxy[0] = int(cxywh[0]-cxywh[2]/2)
|
|
xyxy[1] = int(cxywh[1]-cxywh[3]/2)
|
|
xyxy[2] = int(cxywh[0]+cxywh[2]/2)
|
|
xyxy[3] = int(cxywh[1]+cxywh[3]/2)
|
|
xyxy = [(num if num>0 else 0) for num in xyxy]
|
|
|
|
return xyxy
|
|
|
|
def getGallaryByIndex(cam_index:int):
|
|
gallary_index_path = f"./mydataset/gallery/{cam_index}"
|
|
if not os.path.exists(gallary_index_path):
|
|
os.mkdir(gallary_index_path)
|
|
count_frame = -1
|
|
video_source = cv2.VideoCapture(f"./video/cam{cam_index}.mp4")
|
|
ret = None
|
|
frame = None
|
|
with open(f"./label/cam{cam_index}.txt",'r') as flabel:
|
|
while True:
|
|
tmp = flabel.readline()
|
|
if len(tmp)<5:
|
|
break
|
|
elems = tmp.split(",")
|
|
elems = [int(elem) for elem in elems[0:6]]
|
|
while count_frame<elems[0]:
|
|
ret,frame = video_source.read()
|
|
if ret is False:
|
|
video_source.release()
|
|
break
|
|
count_frame += 1
|
|
if ret is False:
|
|
break
|
|
xxyy = cxywh2xyxy(elems[2:6])
|
|
print(xxyy)
|
|
img_tosave = cropImg(frame,xxyy)
|
|
print(img_tosave)
|
|
img_tosave = cv2.resize(img_tosave,(128,256))
|
|
cv2.imwrite(gallary_index_path+f"/g_{count_frame}_{elems[1]}.jpg",img=img_tosave)
|
|
|
|
if __name__ == '__main__':
|
|
gallary_path = "./mydataset/gallery"
|
|
if not os.path.exists(gallary_path):
|
|
os.mkdir(gallary_path)
|
|
else:
|
|
shutil.rmtree(gallary_path)
|
|
os.mkdir(gallary_path)
|
|
for i in range(1,6+1):
|
|
getGallaryByIndex(i)
|
|
|