Skip to content
site logo mobile

Forum in maintenance, we will back soon 🙂

how to make tool li...
 
Notifications
Clear all

how to make tool like remove.bg

47 Posts
4 Users
6 Likes
789 Views
(@husein)
Posts: 275
Member Moderator
Premium Member
Prompt Engineer
Pythonista Prodigy Badge
API Entrepreneur
Power Member
 

@salman-qureshi Check the courses, there is a full course about APIs

 
Posted : 02/24/2024 9:35 am
SSAdvisor
(@ssadvisor)
Posts: 960
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@salman-qureshi He did already.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 02/24/2024 2:14 pm
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@ssadvisor i have course where he made removing bg tool

 
Posted : 04/01/2024 5:34 am
(@husein)
Posts: 275
Member Moderator
Premium Member
Prompt Engineer
Pythonista Prodigy Badge
API Entrepreneur
Power Member
 

@salman-qureshi You mean the blog post. And, if you want to turn it into a tool, go over the steps @admin mentioned before in this thread.

Plus, you can try these courses; they will help you with every step.

 
Posted : 04/01/2024 9:53 am
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@admin i am trying to an api but it get error every time please help me

import os
import io
import uuid
from fastapi import FastAPI, File, UploadFile, HTTPException
from PIL import Image
import numpy as np
from skimage import transform
import torch
from torch.autograd import Variable
from model import U2NET
from pydantic import BaseModel

app = FastAPI()

# Load the trained model
model_name = 'u2net'
model_dir = os.path.join(os.path.dirname(__file__), 'saved_models', model_name, f'{model_name}.pth')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = U2NET(3, 1)
net.load_state_dict(torch.load(model_dir, map_location=device))
net.eval()
net.to(device)

# Function to save output image
def save_output(image_name, output_name, pred, output_dir, suffix='imagebgremoval.com'):
    predict_np = pred.cpu().squeeze().numpy() * 255
    im = Image.fromarray(predict_np.astype(np.uint8)).convert('RGB')
    image = Image.open(image_name)
    imo = im.resize((image.width, image.height))
    imo.save(os.path.join(output_dir, f"{os.path.splitext(output_name)[0]}_{suffix}.png"))

# Function to remove background
def remove_background(image):
    image = transform.resize(image, (320, 320), mode='constant')
    image = (image - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225]
    image = image.transpose((2, 0, 1))
    image = np.expand_dims(image, 0)
    image = torch.tensor(image, dtype=torch.float32).to(device)
    with torch.no_grad():
        d1, _, _, _, _, _, _ = net(image)
        pred = torch.sigmoid(d1[:, 0, :, :])
    return pred

# Define response model
class BackgroundRemovalResponse(BaseModel):
    message: str

# API endpoint for background removal
@app.post("/remove_bg/")
async def process_image(image_file: UploadFile = File(...)):
    try:
        # Validate file type
        if not image_file.content_type.startswith('image'):
            raise HTTPException(status_code=400, detail="File is not an image.")

        # Read and preprocess the image
        image = Image.open(io.BytesIO(await image_file.read())).convert('RGB')
        if image.mode != "RGB":
            raise HTTPException(status_code=400, detail="Image mode is not RGB.")
        
        # Remove background
        pred = remove_background(np.array(image))

        # Save output image
        output_dir = os.path.join(os.path.dirname(__file__), 'static', 'results')
        unique_filename = str(uuid.uuid4())
        save_output(image, unique_filename + '.png', pred, output_dir)

        return BackgroundRemovalResponse(message=f"Background removed successfully. Output file: {unique_filename}_imagebgremoval.com.png")
    except Exception as e:
        raise HTTPException(status_code=500, detail="Failed to remove background.") from e
 
Posted : 04/02/2024 6:52 am
(@husein)
Posts: 275
Member Moderator
Premium Member
Prompt Engineer
Pythonista Prodigy Badge
API Entrepreneur
Power Member
 

@salman-qureshi whats the error you're getting?

 
Posted : 04/02/2024 7:35 am
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@husein @admin sometime internal error some time bg not removing from image some time whole image become blueish that's all 

 

 

7adc2c1a 769e 495c a5c6 f678fa59e719
f33cecdf af62 457c 974b 7cf0ad014254
9b7fc4b1 3f70 4297 99d4 5aa96f2d4cc0
4dc19abb 0a5f 466a 8b2c eab58a311d25
eb872b14 dd50 40e8 8565 cebbc6b1ae8b
0413f78e 349b 4ce2 b0ba 9555077b7fc5
61eac963 8203 46e8 bfed 809553059635
81fa034a bbb1 4dcf b08e 7d149d8960e4

this result i am getting but script working perfectly but api is not 

 
Posted : 04/02/2024 8:39 am
Hasan Aboul Hasan
(@admin)
Posts: 992
Member Admin
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@salman-qureshi you mean when you turn into an API you get different results?

 
Posted : 04/02/2024 10:06 am
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@admin yes

 
Posted : 04/02/2024 11:04 am
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@admin you said in your video that you will also give api code also and you do not give kindly help me

 
Posted : 04/02/2024 11:05 am
SSAdvisor
(@ssadvisor)
Posts: 960
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@salman-qureshi OpenAI will never give you the same result for the same prompt.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 04/02/2024 1:02 pm
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@ssadvisor @admin can you give me right prompt please

 
Posted : 04/03/2024 5:43 am
(@husein)
Posts: 275
Member Moderator
Premium Member
Prompt Engineer
Pythonista Prodigy Badge
API Entrepreneur
Power Member
 

@salman-qureshi There is no right prompt. There are good prompts, but also these good prompts sometime give different results on each run.

 
Posted : 04/03/2024 8:53 am
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@husein @SSAdvisor @admin so how can i turn these into api please tell me

This post was modified 1 month ago 2 times by salman qureshi
 
Posted : 04/03/2024 9:59 am
Hasan Aboul Hasan
(@admin)
Posts: 992
Member Admin
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@salman-qureshi Friend, if you want to build an API, you should at least learn the basics of Python APIs and how they work. Do you know python? did you built an API before?

 

 
Posted : 04/03/2024 11:18 am
SSAdvisor
(@ssadvisor)
Posts: 960
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@salman-qureshi you need to review the courses. This is explained there.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 04/03/2024 12:22 pm
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@admin but you said in your video that you will give api also but you not give

brother

 
Posted : 04/03/2024 12:36 pm
SSAdvisor
(@ssadvisor)
Posts: 960
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@salman-qureshi link to the video you reference?

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 04/03/2024 2:48 pm
(@salman-qureshi)
Posts: 22
Eminent Member Customer Registered
Topic starter
 

@ssadvisor @admin https://youtu.be/HNu6FgWGcYM?si=PsSUhqZNhfJaqNvj

 
Posted : 04/03/2024 4:14 pm
SSAdvisor
(@ssadvisor)
Posts: 960
Noble Member
Premium Member
Pythonista Prodigy Badge
Prompt Engineer
API Entrepreneur
Power Member
 

@salman-qureshi he gave you the code. https://learnwithhasan.com/remove-image-background-with-python/

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 04/03/2024 10:00 pm
Page 2 / 3
Share: