ds
Browse files- app.py +10 -66
- 736-512x512.jpg → input.jpg +0 -0
- request.py +1 -0
app.py
CHANGED
@@ -1,74 +1,18 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
from PIL import Image
|
4 |
-
import
|
5 |
from transformers import SamModel, SamProcessor
|
6 |
-
import io
|
7 |
-
import base64
|
8 |
-
import torch
|
9 |
-
import uvicorn
|
10 |
|
11 |
-
app = FastAPI(title="SAM-ViT-Base API")
|
12 |
-
|
13 |
-
# SAM modelini ve işlemciyi yükle
|
14 |
model = SamModel.from_pretrained("facebook/sam-vit-base")
|
15 |
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
original_width, original_height = image.size
|
26 |
-
if original_width < 64 or original_height < 64:
|
27 |
-
raise HTTPException(status_code=400, detail=f"Görüntü boyutu çok küçük: {original_width}x{original_height}. Minimum 64x64 piksel olmalı.")
|
28 |
-
|
29 |
-
# Görüntüyü işlemciye hazırla
|
30 |
-
inputs = processor(image, return_tensors="pt", do_rescale=True, do_resize=True)
|
31 |
-
|
32 |
-
# Model ile segmentasyon yap
|
33 |
-
with torch.no_grad():
|
34 |
-
outputs = model(**inputs)
|
35 |
-
|
36 |
-
# Maskeleri al
|
37 |
-
masks = outputs.pred_masks.detach().cpu().numpy() # Shape: (batch_size, num_masks, height, width)
|
38 |
-
if masks.shape[1] == 0:
|
39 |
-
raise HTTPException(status_code=500, detail="Hiç maske üretilmedi.")
|
40 |
-
|
41 |
-
# En iyi maskeyi seç
|
42 |
-
iou_scores = outputs.iou_scores.detach().cpu().numpy() # Shape: (batch_size, num_masks)
|
43 |
-
if iou_scores.shape[1] > 1:
|
44 |
-
best_mask_idx = np.argmax(iou_scores[0]) # En yüksek skora sahip maskeyi seç
|
45 |
-
else:
|
46 |
-
best_mask_idx = 0 # Tek maske varsa onu kullan
|
47 |
-
mask = masks[0][best_mask_idx] # Shape: (height, width)
|
48 |
-
|
49 |
-
# Maske şeklini kontrol et
|
50 |
-
if len(mask.shape) != 2:
|
51 |
-
raise HTTPException(status_code=500, detail=f"Hatalı maske şekli: {mask.shape}. 2D matris bekleniyor.")
|
52 |
-
|
53 |
-
# Maskeyi binary hale getir
|
54 |
-
mask = (mask > 0).astype(np.uint8) * 255
|
55 |
-
|
56 |
-
# Maskeyi orijinal görüntü boyutlarına yeniden boyutlandır
|
57 |
-
mask_image = Image.fromarray(mask).resize((original_width, original_height), Image.NEAREST)
|
58 |
-
|
59 |
-
# Maskeyi PNG olarak kaydet
|
60 |
-
buffered = io.BytesIO()
|
61 |
-
mask_image.save(buffered, format="PNG")
|
62 |
-
mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
63 |
-
|
64 |
-
return JSONResponse(content={"mask": f"data:image/png;base64,{mask_base64}"})
|
65 |
-
|
66 |
-
except Exception as e:
|
67 |
-
raise HTTPException(status_code=500, detail=str(e))
|
68 |
|
69 |
-
@app.get("/")
|
70 |
-
async def root():
|
71 |
-
return {"message": "SAM-ViT-Base API çalışıyor. /segment endpoint'ine görüntü yükleyin."}
|
72 |
|
73 |
-
|
74 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
1 |
from PIL import Image
|
2 |
+
import requests
|
3 |
from transformers import SamModel, SamProcessor
|
|
|
|
|
|
|
|
|
4 |
|
|
|
|
|
|
|
5 |
model = SamModel.from_pretrained("facebook/sam-vit-base")
|
6 |
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
7 |
|
8 |
+
img_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
9 |
+
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
|
10 |
+
input_points = [[[450, 600]]] # 2D localization of a window
|
11 |
+
|
12 |
+
inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
|
13 |
+
outputs = model(**inputs)
|
14 |
+
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
|
15 |
+
scores = outputs.iou_scores
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
17 |
|
18 |
+
print(scores)
|
|
736-512x512.jpg → input.jpg
RENAMED
File without changes
|
request.py
CHANGED
@@ -5,6 +5,7 @@ from io import BytesIO
|
|
5 |
|
6 |
url = "https://sezer91-sam.hf.space/segment/"
|
7 |
file_path = "img.jpeg"
|
|
|
8 |
|
9 |
try:
|
10 |
# Görüntü dosyasını kontrol et
|
|
|
5 |
|
6 |
url = "https://sezer91-sam.hf.space/segment/"
|
7 |
file_path = "img.jpeg"
|
8 |
+
file_path = "input.jpg"
|
9 |
|
10 |
try:
|
11 |
# Görüntü dosyasını kontrol et
|