Occasionally you may want to use OpenCV or facenet to perform face recognition from an image and do some processing. Of course, those tools will recognize the face, but I found that unnecessary things such as the background in the image "may dramatically reduce the accuracy of face recognition". Now, let's remove the background in advance to stabilize the accuracy of face recognition! So we will remove the background. Well, any motive is fine.
remove.bg The background removal itself uses a web application called remove.bg. It's faster to see what it's like. before after remove.bg makes it easy to remove backgrounds with such high accuracy on the Web.
As part of the system I'm implementing, I'll automate the structure of looking at the "latest" images in the directory where the images are stored, removing the background, and returning the results to another directory. ..
remove.bg exposes an API that you can use to automate background removal. You can register as a member and call the API for free up to 50 times a month with a regular account.
# Requires "requests" to be installed (see python-requests.org)
import requests
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': open('/path/to/file.jpg', 'rb')},
data={'size': 'auto'},
headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
)
if response.status_code == requests.codes.ok:
with open('no-bg.png', 'wb') as out:
out.write(response.content)
else:
print("Error:", response.status_code, response.text)
Insert the API Key obtained by remove.bg in the part of'INSERT_YOUR_API_KEY_HERE'. You can easily automate the input and output of images by specifying the path. Specify the path of the image you want to remove the background in the part of'/path/to/file.jpg'. You can specify the directory path by deciding the file name of the image you want to output in the'no-bg.png'part.
This time, I want to input the "latest" image taken with the standard mac camera application called Photo Booth, so I will devise a little path specification.
i_path = '/Users/username/Pictures/Photo Booth library/Pictures/*'
list_of_files = glob.glob(i_path)
latest_file = max(list_of_files, key=os.path.getctime)
The directory where the image you want to get with i_path is stored is specified. I am getting a list of images in the directory with the glob function. You can get the maximum value of the date and time of the file, that is, the latest image by specifying the max function and options.
import glob
import os
import requests
i_path = '/Users/username/Pictures/Photo Booth library/Pictures/*'
list_of_files = glob.glob(i_path)
latest_file = max(list_of_files, key=os.path.getctime)
# RemoveBgAPI
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': open(latest_file, 'rb')},
data={'size': 'auto'},
headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
)
if response.status_code == requests.codes.ok:
with open('/Users/username/output/no-bg.png', 'wb') as out:
out.write(response.content)
print('Success!')
else:
print("Error:", response.status_code, response.text)
The remove.bg API made it very easy to implement background removal automation. In addition, I was able to quickly implement the trick of specifying the latest image file, and achieved my goal. It's nice to throw the troublesome work manually into the program.