Spaces:
Runtime error
Runtime error
marwanelzainy
commited on
Commit
•
94c0acf
1
Parent(s):
8b87a69
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import base64
|
|
2 |
import io
|
3 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
4 |
import os
|
5 |
-
import shutil
|
6 |
from PIL import Image
|
7 |
from fastapi.responses import JSONResponse
|
8 |
from semantic_seg_model import segmentation_inference
|
@@ -29,7 +28,7 @@ temp_processing_dir = input_images_dir + "processed/"
|
|
29 |
|
30 |
# Define a function to handle the POST request at `image-analyzer`
|
31 |
@app.post("/image-analyzer")
|
32 |
-
def image_analyzer(image: UploadFile = File(...)):
|
33 |
"""
|
34 |
This function takes in an image filepath and will return the PolyHaven url addresses of the
|
35 |
top k materials similar to the wall, ceiling, and floor.
|
@@ -42,16 +41,25 @@ def image_analyzer(image: UploadFile = File(...)):
|
|
42 |
os.remove(file_path) # Remove the file
|
43 |
except Exception as e:
|
44 |
print(f"Failed to delete {file_path}. Reason: {e}")
|
45 |
-
|
46 |
# load image
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
print("image loaded successfully. Processing image for segmentation and similarity inference...", datetime.now())
|
|
|
52 |
# segment into components
|
53 |
-
segmentation_inference(
|
54 |
print("image segmented successfully. Starting similarity inference...", datetime.now())
|
|
|
55 |
# identify similar materials for each component
|
56 |
matching_textures = similarity_inference(temp_processing_dir)
|
57 |
print("done", datetime.now())
|
@@ -78,13 +86,17 @@ async def image_render(prompt: str, image: UploadFile = File(...)):
|
|
78 |
print(f"recieved prompt: {prompt} and image: {image}")
|
79 |
image_path = os.path.join(input_images_dir, image.filename+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+".png")
|
80 |
contents = await image.read()
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
86 |
# Convert image to grayscale
|
87 |
-
grayscale_image =
|
88 |
# Save the processed image to the specified path
|
89 |
grayscale_image.save(image_path)
|
90 |
result = client.predict(
|
@@ -115,4 +127,4 @@ async def image_render(prompt: str, image: UploadFile = File(...)):
|
|
115 |
except Exception as e:
|
116 |
print(str(e))
|
117 |
raise HTTPException(status_code=500, detail=str(e))
|
118 |
-
|
|
|
2 |
import io
|
3 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
4 |
import os
|
|
|
5 |
from PIL import Image
|
6 |
from fastapi.responses import JSONResponse
|
7 |
from semantic_seg_model import segmentation_inference
|
|
|
28 |
|
29 |
# Define a function to handle the POST request at `image-analyzer`
|
30 |
@app.post("/image-analyzer")
|
31 |
+
async def image_analyzer(image: UploadFile = File(...)):
|
32 |
"""
|
33 |
This function takes in an image filepath and will return the PolyHaven url addresses of the
|
34 |
top k materials similar to the wall, ceiling, and floor.
|
|
|
41 |
os.remove(file_path) # Remove the file
|
42 |
except Exception as e:
|
43 |
print(f"Failed to delete {file_path}. Reason: {e}")
|
44 |
+
|
45 |
# load image
|
46 |
+
contents = await image.read()
|
47 |
+
if contents.startswith(b"data:image/png;base64"):
|
48 |
+
# Remove the prefix "data:image/png;base64,"
|
49 |
+
image_data = contents.split(b";base64,")[1]
|
50 |
+
# Decode base64 data
|
51 |
+
decoded_image = base64.b64decode(image_data)
|
52 |
+
img = Image.open(io.BytesIO(decoded_image))
|
53 |
+
|
54 |
+
else:
|
55 |
+
img = Image.open(image.file)
|
56 |
+
|
57 |
print("image loaded successfully. Processing image for segmentation and similarity inference...", datetime.now())
|
58 |
+
|
59 |
# segment into components
|
60 |
+
segmentation_inference(img, temp_processing_dir)
|
61 |
print("image segmented successfully. Starting similarity inference...", datetime.now())
|
62 |
+
|
63 |
# identify similar materials for each component
|
64 |
matching_textures = similarity_inference(temp_processing_dir)
|
65 |
print("done", datetime.now())
|
|
|
86 |
print(f"recieved prompt: {prompt} and image: {image}")
|
87 |
image_path = os.path.join(input_images_dir, image.filename+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+".png")
|
88 |
contents = await image.read()
|
89 |
+
if contents.startswith(b"data:image/png;base64"):
|
90 |
+
# Remove the prefix "data:image/png;base64,"
|
91 |
+
image_data = contents.split(b";base64,")[1]
|
92 |
+
# Decode base64 data
|
93 |
+
decoded_image = base64.b64decode(image_data)
|
94 |
+
img = Image.open(io.BytesIO(decoded_image))
|
95 |
+
|
96 |
+
else:
|
97 |
+
img = Image.open(image.file)
|
98 |
# Convert image to grayscale
|
99 |
+
grayscale_image = img.convert('L')
|
100 |
# Save the processed image to the specified path
|
101 |
grayscale_image.save(image_path)
|
102 |
result = client.predict(
|
|
|
127 |
except Exception as e:
|
128 |
print(str(e))
|
129 |
raise HTTPException(status_code=500, detail=str(e))
|
130 |
+
|