在python中遇到可变默认值的问题

时间:2014-03-07 23:06:00

标签: python opencv

所以这是我的代码部分,我认为最能描述我的问题。我使用OpenCV从8 *相机中捕获8 *帧。然后我想把这8个帧组合并放入一个更大的数组来存储从录制开始的所有帧(所以我可以倒回并查看前面的帧)。

tmp_vis = [0]*8
frame_tracker = []

while True:
  for i in xrange(8):
     rets, frame = cap[i].read()
     try:
       tmp_vis[i] = cv2.resize(frame, (self.w, self.h))
     except:
       continue

  print "\nbreak"

  frame_tracker.append(tmp_vis)

  print hashlib.sha1(frame_tracker[0][0].view(np.uint8)).hexdigest()

  if len(frame_tracker) > 1:
    print hashlib.sha1(frame_tracker[1][0].view(np.uint8)).hexdigest()   

  if len(frame_tracker) > 2:
    print hashlib.sha1(frame_tracker[2][0].view(np.uint8)).hexdigest() 

问题在于,当我回头查看前面的帧时,它们似乎只是我追加的最后一组8帧。所以我期望看到的是:

break
67bb20b6ad6c14bc99e9f27456d9ec898a1e0eeb
abb6c8046d877200ad85e6315ce4ec3c83ee5842
db27d915b189a89068dafb965f92ff3e4832473f

break
67bb20b6ad6c14bc99e9f27456d9ec898a1e0eeb
abb6c8046d877200ad85e6315ce4ec3c83ee5842
db27d915b189a89068dafb965f92ff3e4832473f

相反,我看到的是:

break
bc55af0bb9901c55b7ca63480d93f3b2ef604b02
bc55af0bb9901c55b7ca63480d93f3b2ef604b02
bc55af0bb9901c55b7ca63480d93f3b2ef604b02

break
67bb20b6ad6c14bc99e9f27456d9ec898a1e0eeb
67bb20b6ad6c14bc99e9f27456d9ec898a1e0eeb
67bb20b6ad6c14bc99e9f27456d9ec898a1e0eeb

现在我认为这是一个可变的默认值问题,我已经查看“Least Astonishment” in Python: The Mutable Default Argument,但无法找出解决方案。非常感谢帮助。我已经尝试将代码修改为所需的东西,但如果我已经修剪得太​​多,请告诉我。

谢谢,

编辑(解决方案):

tmp_vis = []
frame_tracker = []

while True:
  for i in xrange(8):
     rets, frame = cap[i].read()
     try:
       tmp_vis.append(cv2.resize(frame, (self.w, self.h)))
     except:
       continue

  print "\nbreak"

  frame_tracker.append(tmp_vis)

  print hashlib.sha1(frame_tracker[0][0].view(np.uint8)).hexdigest()

  if len(frame_tracker) > 1:
    print hashlib.sha1(frame_tracker[1][0].view(np.uint8)).hexdigest()   

  if len(frame_tracker) > 2:
    print hashlib.sha1(frame_tracker[2][0].view(np.uint8)).hexdigest() 

1 个答案:

答案 0 :(得分:0)

tmp_vis = []

frame_tracker = []

while True:
  for i in xrange(8):
     rets, frame = cap[i].read()
     try:
       tmp_vis.append(cv2.resize(frame, (self.w, self.h)))
     except:
       continue

  print "\nbreak"

  frame_tracker.append(tmp_vis)

  print hashlib.sha1(frame_tracker[0][0].view(np.uint8)).hexdigest()

  if len(frame_tracker) > 1:
    print hashlib.sha1(frame_tracker[1][0].view(np.uint8)).hexdigest()   

  if len(frame_tracker) > 2:
    print hashlib.sha1(frame_tracker[2][0].view(np.uint8)).hexdigest()