nadermx / backgroundremover

Background Remover lets you Remove Background from images and video using AI with a simple command line interface that is free and open source.
https://www.backgroundremoverai.com
MIT License
6.46k stars 538 forks source link

JSONDecodeError: Expecting value: line 2 column 1 (char 1) "Remove the background from a video" #20

Closed AhmedEwis closed 2 years ago

AhmedEwis commented 2 years ago

Hello

Thanks for this great effort!

I am running the below code to remove a background from a video, but I am getting the below error.

JSONDecodeError: Expecting value: line 2 column 1 (char 1)

import requests

file_names = ["/path/to/file.mp4"] api_key = ''

domain = 'https://video.backgroundremover.app' api_version = 'api/v1/' headers = {'Authorization': api_key} def videos(): files = [('video', open(f, 'rb')) for f in file_names] r = requests.post("%s/%svideo/" % (domain, api_version), files=files, headers=headers) return r.json()

def get_results(job_name, count, type): r = requests.post("%s/%sresults/" % (domain, api_version), data={'job_name': job_name, 'count': count, "type": type}) return r.json()

def download_file(url): local_filename = url.split('/')[-1] with requests.get(url, stream=True) as r: r.raise_for_status() with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk)

image = videos() if not image.get('error'): results = get_results(image['job_name'], image['count'], image['type']) while not results['finished'] or not results['error']: results = get_results(word['job_name'], word['count'], word['type']) results['finished'] = results['finished'] time.sleep(5) if results['finished']: print(results['finished']) break for f in results['files']: file = download_file(f[0]) print(file)

nadermx commented 2 years ago

Hi, you are using the website, to remove a video from the command line, simply do

pip install --upgrade pip
pip install backgroundremover

then to remove the video simply in termianl do

backgroundremover -i "/path/to/video.mp4" -tv -o "output.mov"
nadermx commented 2 years ago

If you're trying to use the API from the website, you have to input your api key from your account page where it says <Yout api key>

AhmedEwis commented 2 years ago

Thanks.

The above code was done after collecting an API from the website.

still receiving that JSONDecodeError.

And I also tried the above comment for pip install --upgrade pip pip install backgroundremover and backgroundremover -i "/path/to/video.mp4" -tv -o "output.mov" in the anaconda prompt I received the below error:

PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000026B57F9EEF0>

nadermx commented 2 years ago

Hi, the code you posted, does not have the proper API key, you left the <api key> in the code, instead of replacing it with a string of text such as sdfkljsdlkfjdlkj234324 that would be showing in your admin panel.

In terms of cannot identify the image, are you sure you are inputting the right video? If you want to just remove an image it would be backgroundremover -i img.jpg -o stuff.png

AhmedEwis commented 2 years ago

the code works now I was putting normal strings with no <>.

but where can I find the result of the background removal?.

In the same directory I found the same original video

nadermx commented 2 years ago

correct. It should be placed in the directory you are running it, or where you specify to put it ie, /path/to/file.mov

AhmedEwis commented 2 years ago

I am not specifying any path and I could not find it in my running directory.

I have only run the below code in Jupiter Notebook.

import requests

file_names = ["C:/Users/eng_a/Downloads/bgrr/cut52.mkv"] api_key = '<54b10362########d3a4b5894af76cc>'

domain = 'https://video.backgroundremover.app' api_version = 'api/v1/' headers = {'Authorization': api_key} def videos(): files = [('video', open(f, 'rb')) for f in file_names] r = requests.post("%s/%svideo/" % (domain, api_version), files=files, headers=headers) return r.json()

def get_results(job_name, count, type): r = requests.post("%s/%sresults/" % (domain, api_version), data={'job_name': job_name, 'count': count, "type": type}) return r.json()

def download_file(url): local_filename = url.split('/')[-1] with requests.get(url, stream=True) as r: r.raise_for_status() with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk)

image = videos() if not image.get('error'): results = get_results(image['job_name'], image['count'], image['type']) while not results['finished'] or not results['error']: results = get_results(word['job_name'], word['count'], word['type']) results['finished'] = results['finished'] time.sleep(5) if results['finished']: print(results['finished']) break for f in results['files']: file = download_file(f[0]) print(file)

nadermx commented 2 years ago

It should output the file path in the end. Although, I'm unsure it works with MKV, I haven't actually tested it with an MKV

nadermx commented 2 years ago

Either way, at least in the mean time you can go into your account page and manually download them from there. I'll try and get later today to perhaps specify the output file in the documentation to make that a bit more clear. But the result once it's done (of course some files take longer) will print the path to the file

AhmedEwis commented 2 years ago

I think it is working for mkv and mp4.

but I could not find the output video.

I have also tried manually but always facing server error image image

nadermx commented 2 years ago

hrmm, let me check this. I'll try and get to this tonight/tomorrow morning and worse case update the documentation to make it explicitly show where the file is being saved.

AhmedEwis commented 2 years ago

Thanks for your cooperation and I will be waiting for you.

AhmedEwis commented 2 years ago

Hey @nadermx Is there any update? Still can not find the video nor able to upload it manually.

nadermx commented 2 years ago

I still haven't had time to check this, but try this, please note new variable local_file


file_names = ["/path/to/file.mp4"]
api_key = '<YOUR_API_KEY>'
local_file = '/path/to/file/file.mov'

domain = 'https://video.backgroundremover.app'
api_version = 'api/v1/'
headers = {'Authorization': api_key}
def videos():
    files = [('video', open(f, 'rb')) for f in file_names]
    r = requests.post("%s/%svideo/" % (domain, api_version), files=files, headers=headers)
    return r.json()

def get_results(job_name, count, type):
    r = requests.post("%s/%sresults/" % (domain, api_version), data={'job_name': job_name, 'count': count, "type": type})
    return r.json()

def download_file(url):
    local_filename = local_file
    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)

image = videos()
if not image.get('error'):
    results = get_results(image['job_name'], image['count'], image['type'])
    while not results['finished'] or not results['error']:
        results = get_results(word['job_name'], word['count'], word['type'])
        results['finished'] = results['finished']
        time.sleep(5)
        if results['finished']:
            print(results['finished'])
            break
    for f in results['files']:
        file = download_file(f[0])
        print(file)
AhmedEwis commented 2 years ago

I am still trying, however it is running but with no results as below. image image

nadermx commented 2 years ago

It shows the error of not enough credits. How long is the video

On Sat, Nov 13, 2021, 15:30 AhmedEwis @.***> wrote:

I am still trying, however it is running but with no results as below. [image: image] https://user-images.githubusercontent.com/65680238/141659561-43f046dc-3b8c-4396-8df4-b15b12077c0b.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nadermx/backgroundremover/issues/20#issuecomment-968138728, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYACXDCHWBJGLHQXWVKRBTUL3KGJANCNFSM5HXMELHQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

AhmedEwis commented 2 years ago

I didn’t use my credits which are 180 and the video is only 1 min and 5 seconds. Total 65 seconds.

nadermx commented 2 years ago

Try again, I set your account to 500 credits to see if that fixes it. If not I'll try and debug today as I have a bit more time now.

AhmedEwis commented 2 years ago

Thank you. It is working now manually only. but can I control the effect of the removal? the result is not satisfying a lot of actors are hidden. And again by code, I can not find the saved video.