jupyter笔记本中的两个循环后内核崩溃

时间:2020-04-03 08:11:44

标签: python-3.x opencv jupyter-notebook

我有这段代码,在jupyter笔记本中出现2次循环后崩溃(内核重新启动)。我怀疑这可能是内存问题,需要在每个循环后清除内存。我已包含以下代码。我的机器有4Gb内存和双核CPU。任何人都可以在这方面提供帮助。

请在下面找到代码

@jit(nopython=True)
def square(list):
    return [i ** 2 for i in list]
#Extracting images from the faked videos no optimization. opencv4

for i in tqdm(range(0,100000,5),desc ='Images Created'):

    if str(df2['original'][i]).endswith(".mp4"):
        fake_frame_list = []
        orgi_frame_list = []
        cap1 = cv2.VideoCapture('/media/michael/extHDD/Kaggle/DeepFAke/DF_all/{}'.format(df2['fake_name'][i]))
        while(cap1.isOpened()):
            ret1, frame1 = cap1.read()
            if ret1 == False:
                break
            fake_frame_list.append(frame1) # list of all the frames in fake video
        cap1.release()
        cap2 = cv2.VideoCapture('/media/michael/extHDD/Kaggle/DeepFAke/DF_all/{}'.format(df2['original'][i]))
        while(cap2.isOpened()):
            ret2, frame2 = cap2.read()
            if ret2 == False:
                break
            orgi_frame_list.append(frame2) #  list of all the frames in original video 
        cap2.release()
        if len(orgi_frame_list)==len(fake_frame_list): #Check if real and fake videos have the same length (frames)
            d1 = []
            for c in range(len(fake_frame_list)):#to check if there is a difference in the images
                d1.append(fake_frame_list[c]-orgi_frame_list[c]) 
            list_of_dis = []
            for k in tqdm(range(len(d1)),desc = 'Frames Extracted',leave = False):
                sum_dist = []
                for j in tqdm(range(3),desc = 'Dim Distance Added',leave = False):
                    s = np.sum(square(d1[k][:,:,j].reshape(-1))) 
                    sum_dist.append(s) # square each value in the resulting list (dimenstion)
                distance = np.sum(sum_dist) # adding the total value for each dimension to a list
                list_of_dis.append(np.round(np.sqrt(distance)))  # Sum the values to get the total 
                                                                #squared values of residual images      
            max_value = max(list_of_dis)
            max_index = list_of_dis.index(max_value)
            cv2.imwrite('/home/michael/DFDC_New/FakeTrain/frame_'+str(df2['fake_name'][i])[:-4]+'.jpg',
                        fake_frame_list[max_index])         
            cv2.destroyAllWindows() 

感谢您的帮助。

致谢与问候

迈克尔

0 个答案:

没有答案
相关问题