Forum in maintenance, we will back soon 🙂
@salman-qureshi He did already.
Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack
@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.
@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
@salman-qureshi you mean when you turn into an API you get different results?
@admin you said in your video that you will also give api code also and you do not give kindly help me
@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
@salman-qureshi There is no right prompt. There are good prompts, but also these good prompts sometime give different results on each run.
@husein @SSAdvisor @admin so how can i turn these into api please tell me
@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?
@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
@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
@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