---
title: "Discussion on \"Image & Video Background Removal using Deep Learning\" | Hashnode"
description: "Discussion on \"Image & Video Background Removal using Deep Learning\". UPDATE:\n\nI made a web application for Image and Video Background Removal using Anvil. Preview below.\nhttps://www.canva.com/design/DAERVIWR404/view\nCode and Explanation in the next section.\n\n\nBackground Removal is a process of separating the main obje..."
url: "https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Image & Video Background Removal using Deep Learning](https://nisargkapkar.hashnode.dev/image-and-video-background-removal-using-deep-learning?utm_source=hashnode&utm_medium=feed)

UPDATE: I made a web application for Image and Video Background Removal using Anvil. Preview below. https://www.canva.com/design/DAERVIWR404/view Code and Explanation in the next section. Background Removal is a process of separating the main obje...

Dec 31, 2020 · 10 min read · nisargkapkar.hashnode.dev  
Updated Jan 2, 2021  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#machine-learning](https://hashnode.com/tag/machine-learning)
-   [#data-science](https://hashnode.com/tag/data-science)
-   [#deep-learning](https://hashnode.com/tag/deep-learning)
-   [#christmashackathon](https://hashnode.com/tag/christmashackathon)

## Written by

[Nisarg Kapkar](https://hashnode.com/@nisargkapkar)

@nisargkapkar · India

  
[View profile](https://hashnode.com/@nisargkapkar)

Twitter: @nnkkaapp | Software Engineering | Product Management

Eating JavaScript for breakfast. Building cool things on the web. Occasionally Blogging. Software Engineering | Product Management. "Ever tried. Ever failed. No matter. Try again. Fail again. Fail better"

## Responses 6

Markdown supported · ⌘↵ to post

[w yl](https://hashnode.com/@yulin6518)

[Apr 16, 2024](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/661e3e516b1c62b8e3998818)

error Traceback (most recent call last)

in () 6 original=cv2.imread('/content/gdrive/MyDrive/background\_removal\_DL/test\_data/videos/input\_frames/input'+str(i)+'.png') 7 #subimage ----> 8 subimage=cv2.subtract(u2netresult,original) 9 cv2.imwrite('/content/gdrive/MyDrive/background\_removal\_DL/test\_data/videos/output\_frames/output'+str(i)+'.png',subimage) 10

error: OpenCV(4.8.0) /io/opencv/modules/core/src/arithm.cpp:647: 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 'arithm\_op'

[w yl](https://hashnode.com/@yulin6518)

[Apr 16, 2024](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/661e3e516b1c62b8e3998818#reply-661e3e772d2115418c08ec53)

How should I resize my image

[Aashish Bhandari](https://hashnode.com/@aashishHasher)

[Apr 21, 2022](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/62610995313088464cfc5906)

Fantastic Job on the article. But please change the variable for second loop under 6B (Removing the background from all frames and saving them). This is causing issue later where only a single image is being saved and overridden after conversion to RGBA

[Khadim Hussain](https://hashnode.com/@KhadimHussain)

[Oct 5, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/615c1fd858a5294ce01af25a)

great article learned a lot!!!!!

[Social Finesse](https://hashnode.com/@randomuser)

[Jun 8, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/60bf38949cd17933741a03a0)

Hey man I need your help! Great work on this by the way it's amazing for us video editors, but i'm having trouble near the last part of the code. I'm definitely not an avid coder here at all so let me know what I'm doing wrong. Everything looks like it activated correctly but when I get towards this part:

-   Removing the background from all frames and saving them

When I try to run that code, this error comes up:

---

NameError Traceback (most recent call last)

<ipython-input-7-ad0006e31003> in <module>() 32 ht,wd,l=final\_img.shape 33 sz=(wd,ht) ---> 34 img\_array.append(final\_img)

NameError: name 'img\_array' is not defined

From that point on, I am totally stuck here and can't finish the process because I don't know what this means or how to fix it. However, from what I can see, everything before that has activated successfully.

If you know how to fix this part for me, I'd greatly appreciate it! Thank you

[hu xuan](https://hashnode.com/@xuanhu)

[Dec 30, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/60bf38949cd17933741a03a0#reply-61cdecf8b9f0453f6d320734)

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)

[Dvernitskiy Andrey](https://hashnode.com/@advern)

[Jun 2, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/60b776ac25dc276ffb940067)

Hello. 6B does not work. With the original file input.mp4 also does not work, output.mp4 is created but it is always the same size 23k and does not open. Although the frames in input\_frames output\_frames u2net\_results are rendered correctly, there is some problem when assembling the video outv.write (i)

[Al Murray](https://hashnode.com/@rogueyoshi)

[May 10, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/609972cc1ea29f2c341e35b3)

got a link to the anvil app?

[vaibhav kumar](https://hashnode.com/@vaibhavkumar)

[Jan 1, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/5fee958b017ebb59232dddc2)

Good One!

[Nisarg Kapkar](https://hashnode.com/@nisargkapkar)

[Jan 1, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/5fee958b017ebb59232dddc2#reply-5feeb660b2cf2458fe32770c)

Thank You! A question if you don't mind. Any tips on handling code blocks? I read a few of your articles and your code blocks are perfect. My code blogs have this weird coloring issue! (some part of the code are assigned random colors)

[vaibhav kumar](https://hashnode.com/@vaibhavkumar)

[Jan 1, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/5fee958b017ebb59232dddc2#reply-5feecaa7b2cf2458fe3278a9)

I only give proper spacing between blocks and mention the language name for each block. Also I first write my articles in Jupyter notebook, adjust the markdown accordingly and then copy paste into the blog

[Nisarg Kapkar](https://hashnode.com/@nisargkapkar)

[Nisarg Kapkar](https://hashnode.com/@nisargkapkar)

[Jan 1, 2021](https://hashnode.com/posts/image-and-video-background-removal-using-deep-learning/5fee022d017ebb59232dd71e/comment/5fee958b017ebb59232dddc2#reply-5feeced1b2cf2458fe3278ed)

[vaibhav kumar](https://hashnode.com/@vaibhavkumar)

Thank you! I didn't know you could mention the language in blocks!
