Spaces:
Running
Running
added training and new endpoint
Browse files- .DS_Store +0 -0
- matchCollection.py +32 -0
- output.json +0 -0
- server.py +26 -3
- todo.md +2 -0
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
matchCollection.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
def load_palettes(filepath):
|
5 |
+
with open(filepath, 'r') as file:
|
6 |
+
return json.load(file)
|
7 |
+
|
8 |
+
def hex_to_rgb(hex_color):
|
9 |
+
hex_color = hex_color.lstrip('#')
|
10 |
+
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
11 |
+
|
12 |
+
def rgb_distance(color1, color2):
|
13 |
+
return np.sqrt(sum((a - b) ** 2 for a, b in zip(color1, color2)))
|
14 |
+
|
15 |
+
def predict_palette(collection_name, hex_color, filepath='output.json'):
|
16 |
+
palettes_data = load_palettes(filepath)
|
17 |
+
|
18 |
+
target_rgb = hex_to_rgb(hex_color)
|
19 |
+
|
20 |
+
min_distance = float('inf')
|
21 |
+
best_palette = None
|
22 |
+
|
23 |
+
for collection in palettes_data['collections']:
|
24 |
+
if collection['collection'] == collection_name:
|
25 |
+
for palette in collection['colors']:
|
26 |
+
for color in palette:
|
27 |
+
distance = rgb_distance(target_rgb, hex_to_rgb(color))
|
28 |
+
if distance < min_distance:
|
29 |
+
min_distance = distance
|
30 |
+
best_palette = palette
|
31 |
+
|
32 |
+
return best_palette
|
output.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
server.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile, Form
|
2 |
from fastapi.responses import StreamingResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from typing import List
|
@@ -14,6 +14,7 @@ import recolorLumaConverterAlgo
|
|
14 |
import recolorPaletteBasedTransfer
|
15 |
import recolorReinhardV2Algo
|
16 |
import recolorLinearColorTransfer
|
|
|
17 |
from typing import Optional
|
18 |
|
19 |
app = FastAPI()
|
@@ -131,12 +132,32 @@ async def recolor(file: UploadFile = File(...), colors: str = Form(...), model:
|
|
131 |
|
132 |
img_file = open("./result.jpg", "rb")
|
133 |
return StreamingResponse(img_file, media_type="image/jpeg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
@app.get("/test/")
|
135 |
async def test():
|
136 |
"""
|
137 |
Test endpoint to check if the server is running.
|
138 |
"""
|
139 |
-
return {"message": "Server is running"}
|
140 |
|
141 |
|
142 |
|
@@ -152,4 +173,6 @@ if __name__ == "__main__":
|
|
152 |
|
153 |
#how to run:
|
154 |
#source env/bin/activate
|
155 |
-
#uvicorn server:app --reload
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Form, Body
|
2 |
from fastapi.responses import StreamingResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from typing import List
|
|
|
14 |
import recolorPaletteBasedTransfer
|
15 |
import recolorReinhardV2Algo
|
16 |
import recolorLinearColorTransfer
|
17 |
+
import matchCollection
|
18 |
from typing import Optional
|
19 |
|
20 |
app = FastAPI()
|
|
|
132 |
|
133 |
img_file = open("./result.jpg", "rb")
|
134 |
return StreamingResponse(img_file, media_type="image/jpeg")
|
135 |
+
|
136 |
+
|
137 |
+
# @app.post("/collection/")
|
138 |
+
# async def create_collection(collection: str = Body(...), colors: List[str] = Body(...)):
|
139 |
+
# """
|
140 |
+
# Endpoint to create a collection with items.
|
141 |
+
# """
|
142 |
+
# result = matchCollection.predict_palette(collection, colors[0])
|
143 |
+
# print(result)
|
144 |
+
# #preparar o dado pra ser respondido
|
145 |
+
# return {"collection": result}
|
146 |
+
|
147 |
+
@app.post("/collection/")
|
148 |
+
async def create_collection(collection: str = Body(...), colors: List[str] = Body(...)):
|
149 |
+
"""
|
150 |
+
Endpoint to create a collection with items.
|
151 |
+
"""
|
152 |
+
palettes = [matchCollection.predict_palette(collection, color) for color in colors]
|
153 |
+
return {"collection": collection, "palettes": palettes}
|
154 |
+
|
155 |
@app.get("/test/")
|
156 |
async def test():
|
157 |
"""
|
158 |
Test endpoint to check if the server is running.
|
159 |
"""
|
160 |
+
return {"message": "Server is running!"}
|
161 |
|
162 |
|
163 |
|
|
|
173 |
|
174 |
#how to run:
|
175 |
#source env/bin/activate
|
176 |
+
#uvicorn server:app --reload
|
177 |
+
|
178 |
+
#curl -X POST http://0.0.0.0:4201/collection/ \ -H "Content-Type: application/json" \ -d '{"collection": "FLORAL", "colors": ["#1f3b4a", "#597375", "#7f623e", "#5c453c"]}'
|
todo.md
CHANGED
@@ -11,4 +11,6 @@ source ./env/bin/activate
|
|
11 |
#source env/bin/activate
|
12 |
#uvicorn server:app --reload
|
13 |
#docker run -p 4201:7860 farm-recolor
|
|
|
|
|
14 |
`
|
|
|
11 |
#source env/bin/activate
|
12 |
#uvicorn server:app --reload
|
13 |
#docker run -p 4201:7860 farm-recolor
|
14 |
+
#docker build -t farm-recolor .
|
15 |
+
|
16 |
`
|