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.43k stars 534 forks source link

API rotates image #144

Open daslicht opened 1 month ago

daslicht commented 1 month ago

Hello, is ther a way to stop teh API rotating images?

Input File:
Screenshot 2024-05-06 at 16 44 05

Processed Image:

Screenshot 2024-05-06 at 16 44 30

xzitlou commented 1 month ago

Hello @daslicht , I will check the API and I will let you know once the problem is fixed.

daslicht commented 1 month ago

Nice ! another issue is that batch processing isnt working, I replaced curl for downloading teh images since my server has outdated openssl, thre seams to go something wrong in the get_results function

<?

use Functions as F;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ERROR | E_PARSE);

class BackgroundRemoverAPI
{

    private $headers;
    private $api_url;
    private $results_url;

//    private function download_file($url, $filename) : void
//    {
//        $curl = curl_init();
//        $url = "https://api.backgroundremoverai.com" . $url;
//        echo $url;
//        curl_setopt($curl, CURLOPT_URL, $url);
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($curl, CURLOPT_SSLVERSION, 3);
//        $data = curl_exec($curl);
//        $error = curl_error($curl);
//        var_dump($data );
//        curl_close($curl);
//        # Make sure destination path exists
//        $destination_path = "./processed_images/";
//        $destination_file = fopen($destination_path . $filename, "w+");
//        fwrite($destination_file, $data);
//        fclose($destination_file);
//    }

    private function download_file($url, $filename) : void
    {
        $url = "https://api.backgroundremoverai.com" . $url;
        $destination_path = "./processed_images/";

        if (file_put_contents($destination_path.$filename, file_get_contents($url)))
        {
            echo "File downloaded successfully";
        }
        else
        {
            echo "File downloading failed.";
        }

    }

    private function get_results($params)
    {

        if (isset($params->error)) {
            print_r($params->error);
            return;
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->results_url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_decode(json_encode($params), true));
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $content = json_decode(curl_exec($curl));
        curl_close($curl);

        if ($content->finished == false) {
            if (intval($content->queue_count) > 0) {
                print_r("queue: $content->queue_count");
            }

            sleep(5);
            $results = $this->get_results($params);
            return;
        }

        foreach ($content->files as $f) {

            $this->download_file($f->url, $f->filename);
        }
    }

    public function convert_files(array $file_list) : void
    {

        $post_data['lang'] = 'en';
        $post_data['convert_to'] = 'image-backgroundremover';

        foreach ($file_list as $index => $file) {
            $post_data['file[' . $index . ']'] = curl_file_create(
                realpath($file),
                mime_content_type($file),
                basename($file)
            );
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->api_url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $content = curl_exec($curl);
        curl_close($curl);

        $this->get_results( json_decode($content) );
        //return json_decode($content);
    }

    function __construct()
    {
        //$this->file_list = $files_list;//['/path/to/files/blah.jpeg'];
        $this->headers = array("Authorization: 08144e1b39cc41a190d691242edaad61");
        $this->api_url = "https://api.backgroundremoverai.com/v1/convert/";
        $this->results_url = "https://api.backgroundremoverai.com/v1/results/";
    }

}

//$resp = convert_files($file_list, $headers, $api_url);
//get_results($resp, $results_url, $headers);
xzitlou commented 1 month ago

Let me check this issue too.

cfl-chenfangliang commented 1 month ago

I have the same problem as you, with high probability is your image exif info contains rotating info. you can try: from PIL import Image, ImageOps img = Image.open('path/to/image').convert('RGB')
 img = ImageOps.exif_transpose(img)

daslicht commented 1 month ago

thanks will try what is PIL?

cfl-chenfangliang commented 1 month ago

thanks will try what is PIL?

pythom Image library. The basic idea is to process the exif information of the image. you need to deal with it yourself if you use other language