Spaces:
Sleeping
Sleeping
Update server1.py
Browse files- server1.py +38 -25
server1.py
CHANGED
|
@@ -2,54 +2,67 @@
|
|
| 2 |
|
| 3 |
import io
|
| 4 |
import numpy as np
|
| 5 |
-
import cv2
|
| 6 |
from fastapi import FastAPI, UploadFile, File
|
| 7 |
from fastapi.responses import JSONResponse
|
| 8 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Accudoctor Strip Analyzer")
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
np_img = np.array(img)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
approx = cv2.approxPolyDP(cnt, 0.04 * cv2.arcLength(cnt, True), True)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"bbox": [int(x), int(y), int(x+w), int(y+h)]
|
| 38 |
-
})
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
@app.post("/strip/")
|
| 44 |
async def strip(front: UploadFile = File(...)):
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
return JSONResponse(content={
|
| 50 |
"code": 200,
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
})
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return JSONResponse(content={"code": 500, "error": str(e)})
|
|
|
|
| 2 |
|
| 3 |
import io
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from fastapi import FastAPI, UploadFile, File
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
from PIL import Image
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
from transformers import SamModel, SamProcessor
|
| 11 |
|
| 12 |
app = FastAPI(title="Accudoctor Strip Analyzer")
|
| 13 |
|
| 14 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
model = SamModel.from_pretrained("facebook/sam-vit-base").to(DEVICE)
|
| 17 |
+
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
| 18 |
+
|
| 19 |
|
| 20 |
+
def detect_blocks(image_bytes):
|
|
|
|
| 21 |
|
| 22 |
+
img = Image.open(io.BytesIO(image_bytes))
|
| 23 |
+
if img.mode != "RGB":
|
| 24 |
+
img = img.convert("RGB")
|
| 25 |
|
| 26 |
+
# preparar imagen
|
| 27 |
+
inputs = processor(img, return_tensors="pt").to(DEVICE)
|
| 28 |
|
| 29 |
+
# obtener mascaras
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model(**inputs)
|
| 32 |
|
| 33 |
+
masks = processor.post_process_masks(
|
| 34 |
+
outputs.pred_masks.cpu(),
|
| 35 |
+
inputs["original_sizes"].cpu(),
|
| 36 |
+
inputs["reshaped_input_sizes"].cpu()
|
| 37 |
+
)[0].numpy()
|
| 38 |
|
| 39 |
+
blocks = []
|
|
|
|
| 40 |
|
| 41 |
+
# convertir mascaras en bounding boxes
|
| 42 |
+
for mask in masks:
|
| 43 |
+
ys, xs = np.where(mask > 0.5)
|
| 44 |
+
if len(xs) == 0:
|
| 45 |
+
continue
|
| 46 |
|
| 47 |
+
x1, x2 = int(xs.min()), int(xs.max())
|
| 48 |
+
y1, y2 = int(ys.min()), int(ys.max())
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
blocks.append([x1, y1, x2, y2])
|
| 51 |
+
|
| 52 |
+
return blocks
|
| 53 |
|
| 54 |
|
| 55 |
@app.post("/strip/")
|
| 56 |
async def strip(front: UploadFile = File(...)):
|
| 57 |
try:
|
| 58 |
+
img_bytes = await front.read()
|
| 59 |
+
blocks = detect_blocks(img_bytes)
|
| 60 |
|
| 61 |
return JSONResponse(content={
|
| 62 |
"code": 200,
|
| 63 |
+
"num_blocks": len(blocks),
|
| 64 |
+
"blocks": blocks
|
| 65 |
})
|
| 66 |
+
|
| 67 |
except Exception as e:
|
| 68 |
return JSONResponse(content={"code": 500, "error": str(e)})
|