sd
Browse files- __pycache__/requests.cpython-310.pyc +0 -0
- app.py +19 -10
- img.jpeg +0 -0
- request.py +29 -0
__pycache__/requests.cpython-310.pyc
ADDED
Binary file (488 Bytes). View file
|
|
app.py
CHANGED
@@ -5,7 +5,7 @@ import numpy as np
|
|
5 |
from transformers import SamModel, SamProcessor
|
6 |
import io
|
7 |
import base64
|
8 |
-
import
|
9 |
|
10 |
app = FastAPI(title="SAM-ViT-Base API")
|
11 |
|
@@ -20,21 +20,33 @@ async def segment_image(file: UploadFile = File(...)):
|
|
20 |
image_data = await file.read()
|
21 |
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
22 |
|
|
|
|
|
|
|
|
|
|
|
23 |
# Görüntüyü işlemciye hazırla
|
24 |
inputs = processor(image, return_tensors="pt")
|
25 |
|
26 |
# Model ile segmentasyon yap
|
27 |
-
|
|
|
28 |
|
29 |
# Maskeyi al
|
30 |
-
masks = outputs.pred_masks.detach().cpu().numpy()
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Maskeyi binary hale getir
|
34 |
mask = (mask > 0).astype(np.uint8) * 255
|
35 |
|
36 |
-
# Maskeyi görüntü
|
37 |
-
mask_image = Image.fromarray(mask)
|
|
|
|
|
38 |
buffered = io.BytesIO()
|
39 |
mask_image.save(buffered, format="PNG")
|
40 |
mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
@@ -46,7 +58,4 @@ async def segment_image(file: UploadFile = File(...)):
|
|
46 |
|
47 |
@app.get("/")
|
48 |
async def root():
|
49 |
-
return {"message": "SAM-ViT-Base API çalışıyor. /segment endpoint'ine görüntü yükleyin."}
|
50 |
-
|
51 |
-
if __name__ == "__main__":
|
52 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
5 |
from transformers import SamModel, SamProcessor
|
6 |
import io
|
7 |
import base64
|
8 |
+
import torch
|
9 |
|
10 |
app = FastAPI(title="SAM-ViT-Base API")
|
11 |
|
|
|
20 |
image_data = await file.read()
|
21 |
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
22 |
|
23 |
+
# Görüntü boyutlarını al
|
24 |
+
original_width, original_height = image.size
|
25 |
+
if original_width < 64 or original_height < 64:
|
26 |
+
raise HTTPException(status_code=400, detail="Görüntü boyutu çok küçük. Minimum 64x64 piksel olmalı.")
|
27 |
+
|
28 |
# Görüntüyü işlemciye hazırla
|
29 |
inputs = processor(image, return_tensors="pt")
|
30 |
|
31 |
# Model ile segmentasyon yap
|
32 |
+
with torch.no_grad():
|
33 |
+
outputs = model(**inputs)
|
34 |
|
35 |
# Maskeyi al
|
36 |
+
masks = outputs.pred_masks.detach().cpu().numpy() # Shape: (batch_size, num_masks, height, width)
|
37 |
+
if masks.shape[1] == 0:
|
38 |
+
raise HTTPException(status_code=500, detail="Hiç maske üretilmedi.")
|
39 |
+
|
40 |
+
# İlk maskeyi al
|
41 |
+
mask = masks[0][0] # Shape: (height, width)
|
42 |
|
43 |
# Maskeyi binary hale getir
|
44 |
mask = (mask > 0).astype(np.uint8) * 255
|
45 |
|
46 |
+
# Maskeyi orijinal görüntü boyutlarına yeniden boyutlandır
|
47 |
+
mask_image = Image.fromarray(mask).resize((original_width, original_height), Image.NEAREST)
|
48 |
+
|
49 |
+
# Maskeyi PNG olarak kaydet
|
50 |
buffered = io.BytesIO()
|
51 |
mask_image.save(buffered, format="PNG")
|
52 |
mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
58 |
|
59 |
@app.get("/")
|
60 |
async def root():
|
61 |
+
return {"message": "SAM-ViT-Base API çalışıyor. /segment endpoint'ine görüntü yükleyin."}
|
|
|
|
|
|
img.jpeg
ADDED
![]() |
request.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
url = "https://sezer91-sam.hf.space/segment/"
|
4 |
+
file_path = "img.jpeg"
|
5 |
+
|
6 |
+
try:
|
7 |
+
with open(file_path, "rb") as file:
|
8 |
+
files = {"file": file}
|
9 |
+
response = requests.post(url, files=files)
|
10 |
+
|
11 |
+
if response.status_code == 200:
|
12 |
+
result = response.json()
|
13 |
+
print("Başarılı! Maske alındı.")
|
14 |
+
# Base64'ü PNG olarak kaydet
|
15 |
+
import base64
|
16 |
+
from io import BytesIO
|
17 |
+
from PIL import Image
|
18 |
+
base64_string = result["mask"].split(",")[1] # "data:image/png;base64," kısmını atla
|
19 |
+
img_data = base64.b64decode(base64_string)
|
20 |
+
img = Image.open(BytesIO(img_data))
|
21 |
+
img.save("output_mask.png")
|
22 |
+
print("Maske 'output_mask.png' olarak kaydedildi.")
|
23 |
+
else:
|
24 |
+
print(f"Hata: {response.status_code}, {response.text}")
|
25 |
+
|
26 |
+
except FileNotFoundError:
|
27 |
+
print(f"Hata: {file_path} dosyası bulunamadı.")
|
28 |
+
except Exception as e:
|
29 |
+
print(f"Hata: {str(e)}")
|