我们的训练样本都需要自己来制作,目前的样本在多段视频的固定位置可以截取。我采用了多进程同时跑多个视频然后截图保存
为了区分是哪一个视频的截图,在命名上做了小区分。
import os
import numpy as np
import cv2
import time
from PIL import Image,ImageDraw
import matplotlib.pyplot as plt
- 切图的范围函数
def cut_finish(frame):
#切图的位置
img01 = frame
size=img01.shape
box = (int(1*size[0]/5), int(1.5*size[0]/5), int(3.5*size[1]/9), int(5.5*size[1]/9))
img02 = img01[box[0]:box[1], box[2]:box[3]]
return img02
- 使用图片测试截图位置:
a =cv2.imread('f5.png')
b= cut_finish_game(a)
fig, ax = plt.subplots()
ax.imshow(b)
plt.show()
进行切图
- 使用文件夹内的图片切图
def cut_image(path):
path = all_path(path)
N = 0
for i in path:
frame = cv2.imread(i)
cut = cut_finish(frame)
path = 'C:\\Users\\admin\\cut_images\\'+ '___' + str(N).zfill(0) + '.jpg'
cv2.imwrite(path,img)
N += 1
print('{}/{}...........\r'.format(N,len(path)),end='')
- 使用视频切图
def video_cutimage(url,num):
vc = cv2.VideoCapture(url)
video_is_open = vc.isOpened()
n = 0
count = 0
if video_is_open == False:
print('没有直播')
while video_is_open:
rval,frame = vc.read()
cut = cut_witness(frame)
count +=1
path = 'cut_images\\'+ num + str(n).zfill(0) + '.jpg'
if count == 1:
cv2.imwrite(path,cut)
n += 1
if count == 20:
count = 0
使用多开进程加大效率
注意:多开的函数必须从另外一个py函数调用。在这同一个jupyter notebook是使用不了的!!
from multiprocessing import Pool
#from cut_to_image import video_cutimage
p = Pool()
n = 0
for i in data:
n += 1
dd = 'az__' +str(n)+'__az'
p.apply_async(video_cutimage, args=(i))
p.close()
p.join()
网友评论