Spaces:
Runtime error
Runtime error
VikramSingh178
commited on
Commit
•
f3cfe0c
1
Parent(s):
7b86905
refactor: Update import statement for accelerator in sdxl_text_to_image.py
Browse files- product_diffusion_api/routers/sdxl_text_to_image.py +1 -1
- product_diffusion_api/utils.py +0 -12
- requirements.txt +2 -1
- run.sh +1 -0
- scripts/inpainting-pipeline.py +0 -0
- scripts/utils.py +105 -0
product_diffusion_api/routers/sdxl_text_to_image.py
CHANGED
@@ -13,7 +13,7 @@ from functools import lru_cache
|
|
13 |
from s3_manager import S3ManagerService
|
14 |
from PIL import Image
|
15 |
import io
|
16 |
-
from utils import accelerator
|
17 |
|
18 |
device = accelerator()
|
19 |
torch._inductor.config.conv_1x1_as_mm = True
|
|
|
13 |
from s3_manager import S3ManagerService
|
14 |
from PIL import Image
|
15 |
import io
|
16 |
+
from scripts.utils import accelerator
|
17 |
|
18 |
device = accelerator()
|
19 |
torch._inductor.config.conv_1x1_as_mm = True
|
product_diffusion_api/utils.py
DELETED
@@ -1,12 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
def accelerator():
|
6 |
-
if torch.cuda.is_available():
|
7 |
-
device = 'cuda'
|
8 |
-
elif torch.backends.mps.is_available():
|
9 |
-
device = 'mps'
|
10 |
-
else :
|
11 |
-
device = 'cpu'
|
12 |
-
return device
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -19,4 +19,5 @@ tensorboard
|
|
19 |
Jinja2
|
20 |
datasets
|
21 |
peft
|
22 |
-
async-batcher
|
|
|
|
19 |
Jinja2
|
20 |
datasets
|
21 |
peft
|
22 |
+
async-batcher
|
23 |
+
ultralytics
|
run.sh
CHANGED
@@ -1 +1,2 @@
|
|
1 |
apt-get update && apt-get install python3-dev
|
|
|
|
1 |
apt-get update && apt-get install python3-dev
|
2 |
+
pip install -r requirements.txt
|
scripts/inpainting-pipeline.py
ADDED
File without changes
|
scripts/utils.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from ultralytics import YOLO
|
3 |
+
from transformers import pipeline
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
|
8 |
+
def accelerator():
|
9 |
+
"""
|
10 |
+
Determines the device accelerator to use based on availability.
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
str: The name of the device accelerator ('cuda', 'mps', or 'cpu').
|
14 |
+
"""
|
15 |
+
if torch.cuda.is_available():
|
16 |
+
device = 'cuda'
|
17 |
+
elif torch.backends.mps.is_available():
|
18 |
+
device = 'mps'
|
19 |
+
else:
|
20 |
+
device = 'cpu'
|
21 |
+
return device
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
def center_scaled_roi(image_path, bg_size, scale_factor):
|
27 |
+
"""
|
28 |
+
Center and scale the region of interest (ROI) within a background image.
|
29 |
+
|
30 |
+
Args:
|
31 |
+
image_path (str): The path to the original image.
|
32 |
+
bg_size (tuple): The size (width, height) of the background image.
|
33 |
+
scale_factor (float): The scaling factor to apply to the ROI.
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
numpy.ndarray: The background image with the scaled ROI centered.
|
37 |
+
|
38 |
+
"""
|
39 |
+
|
40 |
+
original_image = cv2.imread(image_path)
|
41 |
+
height, width = original_image.shape[:2]
|
42 |
+
|
43 |
+
# Convert the image to grayscale
|
44 |
+
gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
|
45 |
+
|
46 |
+
# Apply Gaussian blur to reduce noise
|
47 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
48 |
+
|
49 |
+
# Perform edge detection using Canny
|
50 |
+
edges = cv2.Canny(blurred, 50, 150)
|
51 |
+
|
52 |
+
# Find contours in the edged image
|
53 |
+
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
54 |
+
|
55 |
+
# Initialize variables to store ROI coordinates
|
56 |
+
roi_x, roi_y, roi_w, roi_h = 0, 0, 0, 0
|
57 |
+
|
58 |
+
# Loop over the contours
|
59 |
+
for contour in contours:
|
60 |
+
# Approximate the contour
|
61 |
+
peri = cv2.arcLength(contour, True)
|
62 |
+
approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
|
63 |
+
|
64 |
+
# If the contour has 4 vertices, it's likely a rectangle
|
65 |
+
if len(approx) == 4:
|
66 |
+
# Get the bounding box of the contour
|
67 |
+
x, y, w, h = cv2.boundingRect(approx)
|
68 |
+
roi_x, roi_y, roi_w, roi_h = x, y, w, h
|
69 |
+
break
|
70 |
+
|
71 |
+
# Calculate dimensions for the background
|
72 |
+
bg_width, bg_height = bg_size
|
73 |
+
|
74 |
+
# Resize the ROI based on the scale factor
|
75 |
+
scaled_roi_w = int(roi_w * scale_factor)
|
76 |
+
scaled_roi_h = int(roi_h * scale_factor)
|
77 |
+
|
78 |
+
# Calculate offsets to center the scaled ROI within the background
|
79 |
+
x_offset = (bg_width - scaled_roi_w) // 2
|
80 |
+
y_offset = (bg_height - scaled_roi_h) // 2
|
81 |
+
|
82 |
+
# Resize the original image
|
83 |
+
scaled_image = cv2.resize(original_image, (scaled_roi_w, scaled_roi_h))
|
84 |
+
|
85 |
+
# Create a blank background
|
86 |
+
background = np.zeros((bg_height, bg_width, 3), dtype=np.uint8)
|
87 |
+
|
88 |
+
# Place the scaled ROI onto the background
|
89 |
+
background[y_offset:y_offset+scaled_roi_h, x_offset:x_offset+scaled_roi_w] = scaled_image
|
90 |
+
|
91 |
+
return background
|
92 |
+
|
93 |
+
# Define dimensions for the background (larger than the ROI)
|
94 |
+
bg_width, bg_height = 800, 600
|
95 |
+
|
96 |
+
# Define the scale factor
|
97 |
+
scale_factor = 0.5 # Adjust this value as needed
|
98 |
+
|
99 |
+
# Call the function to center the scaled ROI within the background
|
100 |
+
centered_scaled_roi = center_scaled_roi('image.jpg', (bg_width, bg_height), scale_factor)
|
101 |
+
|
102 |
+
# Display the centered scaled ROI
|
103 |
+
cv2.imshow('Centered Scaled ROI', centered_scaled_roi)
|
104 |
+
cv2.waitKey(0)
|
105 |
+
cv2.destroyAllWindows()
|