ds
Browse files- 736-512x512.jpg +0 -0
- app.py +6 -4
- request.py +11 -3
736-512x512.jpg
ADDED
![]() |
app.py
CHANGED
@@ -26,20 +26,22 @@ async def segment_image(file: UploadFile = File(...)):
|
|
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, max_length=768)
|
31 |
|
32 |
# Model ile segmentasyon yap
|
33 |
with torch.no_grad():
|
34 |
outputs = model(**inputs)
|
35 |
|
36 |
-
#
|
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 |
-
#
|
42 |
-
|
|
|
|
|
43 |
|
44 |
# Maske şeklini kontrol et
|
45 |
if len(mask.shape) != 2:
|
|
|
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, max_length=768)
|
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ç (iou_scores baz alınarak)
|
42 |
+
iou_scores = outputs.iou_scores.detach().cpu().numpy() # Shape: (batch_size, num_masks)
|
43 |
+
best_mask_idx = np.argmax(iou_scores[0]) # En yüksek skora sahip maskeyi seç
|
44 |
+
mask = masks[0][best_mask_idx] # Shape: (height, width)
|
45 |
|
46 |
# Maske şeklini kontrol et
|
47 |
if len(mask.shape) != 2:
|
request.py
CHANGED
@@ -1,9 +1,18 @@
|
|
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)
|
@@ -12,9 +21,6 @@ try:
|
|
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))
|
@@ -25,5 +31,7 @@ try:
|
|
25 |
|
26 |
except FileNotFoundError:
|
27 |
print(f"Hata: {file_path} dosyası bulunamadı.")
|
|
|
|
|
28 |
except Exception as e:
|
29 |
print(f"Hata: {str(e)}")
|
|
|
1 |
import requests
|
2 |
+
from PIL import Image
|
3 |
+
import base64
|
4 |
+
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
|
11 |
+
img = Image.open(file_path)
|
12 |
+
if img.size[0] < 64 or img.size[1] < 64:
|
13 |
+
raise ValueError(f"Görüntü boyutu çok küçük: {img.size[0]}x{img.size[1]}. Minimum 64x64 piksel olmalı.")
|
14 |
+
print(f"Görüntü boyutu: {img.size[0]}x{img.size[1]}")
|
15 |
+
|
16 |
with open(file_path, "rb") as file:
|
17 |
files = {"file": file}
|
18 |
response = requests.post(url, files=files)
|
|
|
21 |
result = response.json()
|
22 |
print("Başarılı! Maske alındı.")
|
23 |
# Base64'ü PNG olarak kaydet
|
|
|
|
|
|
|
24 |
base64_string = result["mask"].split(",")[1] # "data:image/png;base64," kısmını atla
|
25 |
img_data = base64.b64decode(base64_string)
|
26 |
img = Image.open(BytesIO(img_data))
|
|
|
31 |
|
32 |
except FileNotFoundError:
|
33 |
print(f"Hata: {file_path} dosyası bulunamadı.")
|
34 |
+
except ValueError as ve:
|
35 |
+
print(f"Hata: {str(ve)}")
|
36 |
except Exception as e:
|
37 |
print(f"Hata: {str(e)}")
|