I met the same problem:"NameError: name 'img_array' is not defined".①At first,I thought the package PIL was not imported successfully.I added "from PIL import Image" to the first line of the cell.However,it didn't work.②Then,given that the problem concerning "img_array",I added "img_array = []" before line 34(around line 29).It seemed to start working without error,the size of output.mp4 is only about 20K.After a while,I found that I fogot the "append()" function is used to add a new object to the end of the list.The addition of "img_array = []" was wrong.It is wise to add it just before the "for" loop(around line 2).③ However,new error jumped out:"error: (-209:Sizes of input arguments do not match) The operation is neither ‘array op array’ (where arrays have the same size and the same number of channels), nor ‘array op scalar’, nor ‘scalar op array’ in function ‘cv::arithm_op’".It means that the sizes do not match?After I deleted "img_array = []" and "from PIL import Image",the cell began to work properly.
It is a strange error that does not report an error when used alone.I don't know exactly what went wrong, so I wrote down the process of solving it. I hope it will help you. Good luck!
Here is my cell:
for i in range(count):
#u2netreusult
u2netresult=cv2.imread('/content/gdrive/MyDrive/background_removal_DL/test_data/videos/u2net_results/input'+str(i)+'.png')
#original
original=cv2.imread('/content/gdrive/MyDrive/background_removal_DL/test_data/videos/input_frames/input'+str(i)+'.png')
#subimage
subimage=cv2.subtract(u2netresult,original)
cv2.imwrite('/content/gdrive/MyDrive/background_removal_DL/test_data/videos/output_frames/output'+str(i)+'.png',subimage)
#subimage
subimage=Image.open('/content/gdrive/MyDrive/background_removal_DL/test_data/videos/output_frames/output'+str(i)+'.png')
#original
original=Image.open('/content/gdrive/MyDrive/background_removal_DL/test_data/videos/input_frames/input'+str(i)+'.png')
subimage=subimage.convert("RGBA")
original=original.convert("RGBA")
subdata=subimage.getdata()
ogdata=original.getdata()
newdata=[]
for i in range(subdata.size[0]*subdata.size[1]):
if subdata[i][0]==0 and subdata[i][1]==0 and subdata[i][2]==0:
newdata.append((255,255,255,0))
else:
newdata.append(ogdata[i])
subimage.putdata(newdata)
subimage.save('/content/gdrive/MyDrive/background_removal_DL/test_data/videos/output_frames/output'+str(i)+'.png',"PNG")
final_img=cv2.imread('/content/gdrive/MyDrive/background_removal_DL/test_data/videos/output_frames/output'+str(i)+'.png')
ht,wd,l=final_img.shape
sz=(wd,ht)
img_array.append(final_img)