afdx2 commited on
Commit
882b1f1
·
verified ·
1 Parent(s): c87d020

Update server1.py

Browse files
Files changed (1) hide show
  1. server1.py +36 -23
server1.py CHANGED
@@ -6,7 +6,6 @@ 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")
@@ -17,44 +16,58 @@ 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
  inputs = processor(img, return_tensors="pt").to(DEVICE)
27
-
28
  with torch.no_grad():
29
- outputs = model(**inputs)
30
 
31
  masks = processor.post_process_masks(
32
- outputs.pred_masks.cpu(),
33
  inputs["original_sizes"].cpu(),
34
  inputs["reshaped_input_sizes"].cpu()
35
  )[0].numpy()
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  blocks = []
38
 
39
- for mask in masks:
 
 
 
 
 
 
 
 
 
40
 
41
- mask = np.array(mask)
42
 
43
- # forzar a 2D
44
- while mask.ndim > 2:
45
- mask = mask[0]
46
 
47
- if mask.ndim != 2:
48
- continue
49
 
50
- ys, xs = np.where(mask > 0.5)
51
- if len(xs) == 0:
52
- continue
53
 
54
- x1, x2 = int(xs.min()), int(xs.max())
55
- y1, y2 = int(ys.min()), int(ys.max())
56
 
57
- blocks.append([x1, y1, x2, y2])
 
58
 
59
  return blocks
60
 
 
6
  from fastapi.responses import JSONResponse
7
  from PIL import Image
8
  import torch
 
9
  from transformers import SamModel, SamProcessor
10
 
11
  app = FastAPI(title="Accudoctor Strip Analyzer")
 
16
  processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
17
 
18
 
19
+ def detect_strip_mask(img):
 
 
 
 
 
20
  inputs = processor(img, return_tensors="pt").to(DEVICE)
 
21
  with torch.no_grad():
22
+ out = model(**inputs)
23
 
24
  masks = processor.post_process_masks(
25
+ out.pred_masks.cpu(),
26
  inputs["original_sizes"].cpu(),
27
  inputs["reshaped_input_sizes"].cpu()
28
  )[0].numpy()
29
 
30
+ # cojo la mascara mas grande (la tira)
31
+ best_mask = max(masks, key=lambda m: np.sum(m))
32
+
33
+ best_mask = np.squeeze(best_mask)
34
+ ys, xs = np.where(best_mask > 0.5)
35
+
36
+ x1, x2 = xs.min(), xs.max()
37
+ y1, y2 = ys.min(), ys.max()
38
+
39
+ return x1, y1, x2, y2
40
+
41
+
42
+ def split_into_11(img_strip):
43
+ w, h = img_strip.size
44
+ block_h = h // 11
45
+
46
  blocks = []
47
 
48
+ for i in range(11):
49
+ y1 = i * block_h
50
+ y2 = (i + 1) * block_h
51
+ crop = img_strip.crop((0, y1, w, y2))
52
+ blocks.append({
53
+ "index": i + 1,
54
+ "bbox": [0, y1, w, y2]
55
+ })
56
+
57
+ return blocks
58
 
 
59
 
60
+ def detect_blocks(image_bytes):
 
 
61
 
62
+ img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
 
63
 
64
+ # 1) SAM detecta la tira completa
65
+ x1, y1, x2, y2 = detect_strip_mask(img)
 
66
 
67
+ strip = img.crop((x1, y1, x2, y2))
 
68
 
69
+ # 2) se divide en 11 bloques
70
+ blocks = split_into_11(strip)
71
 
72
  return blocks
73