ComDesignProject/gallery_pull.py

45 lines
1.3 KiB
Python

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
if __name__ == '__main__':
video_source = cv2.VideoCapture("video/cam1.mp4")
count_frame = -1
ret = None
frame = None
with open("label/cam1.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(f"gallary/g_{count_frame}_{elems[1]}.jpg",img=img_tosave)