gpt4 book ai didi

python - 程序未从while循环写入列表?

转载 作者:行者123 更新时间:2023-12-02 17:42:22 26 4
gpt4 key购买 nike

当像素强度差异高于预定义阈值时,我正在尝试将值写入我的VideoFlag列表。在输出上,但是我的输出'flag.txt'文件为空,我不确定为什么。有人知道我的代码以什么方式错误吗?

谢谢!

import cv2
import tkinter as tk
from tkinter.filedialog import askopenfilename
import numpy as np
import os
import matplotlib.pyplot as plt

MIList =[]
VideoFlag=[]

def frame_diff(prev_frame, cur_frame, next_frame):
diff_frames1 = cv2.absdiff(next_frame, cur_frame)

diff_frames2 = cv2.absdiff(cur_frame, prev_frame)

return cv2.bitwise_and(diff_frames1, diff_frames2)

def get_frame(cap):
ret, frame = cap.read()
if ret == True:
scaling_factor = 1
frame = cv2.resize(frame, None, fx = scaling_factor, fy = scaling_factor, interpolation = cv2.INTER_AREA)
return cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

def moving_average(MIList, n=30) :
ret = np.cumsum(MIList, dtype=float)
ret[n:] = ret[n:] - ret[:-n]
return ret[n - 1:] / n

def main():

root = tk.Tk()
root.withdraw()

selectedvideo = askopenfilename()
cap = cv2.VideoCapture(selectedvideo)
length = cap.get(cv2.CAP_PROP_FRAME_COUNT)
intlength = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
currentframenumber = cap.get(cv2.CAP_PROP_POS_FRAMES)
intcurrentframenumber = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
scaling_factor = 1
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter((selectedvideo + 'motionindexed.avi'),fourcc, 60.0, (640,478), isColor=False)
with open((selectedvideo + 'threshold' + '.txt'), 'r') as readthreshold:
threshold = float(readthreshold.readline())

prev_frame = get_frame(cap)
cur_frame = get_frame(cap)
next_frame = get_frame(cap)

while (cap.isOpened()):

try:
cv2.imshow("Object Movement", frame_diff(prev_frame, cur_frame, next_frame))
prev_frame = cur_frame
cur_frame = next_frame
next_frame = get_frame(cap)
differencesquared = (next_frame-cur_frame)**2
interframedifference = np.sum(differencesquared)
MIList.append(interframedifference)
print(interframedifference)
if interframedifference >= threshold:
out.write(cur_frame)
VideoFlag.append(str(intcurrentframenumber + '|' + 1))
print(VideoFlag)
elif interframedifference < threshold:
VideoFlag.append(str(intcurrentframenumber + '|' + 0))
print(VideoFlag)

key = cv2.waitKey(1)
if key == ord('q'):
break
except:
break

with open((selectedvideo + 'flag' + '.txt'), 'w') as f:
for item in VideoFlag:
f.write(str(item))


cap.release()
cv2.destroyAllWindows()

if __name__ == '__main__':
# this is called if this code was not imported ... ie it was directly run
# if this is called, that means there is no GUI already running, so we need to create a root
root = tk.Tk()
root.withdraw()
main()

最佳答案

更换

with open((selectedvideo + 'flag' + '.txt'), 'w') as f:
for item in VideoFlag:
f.write(str(item))


# Note that you need to append('a') data to the file instead of writing('w') to it for each iteration. 
# The last line will be empty string and that is what contains finally.
with open((selectedvideo + 'flag' + '.txt'), 'a') as f:
for item in VideoFlag:
f.write(str(item))

将解决问题。

关于python - 程序未从while循环写入列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42908846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com