EnginDev commited on
Commit
a738e6c
·
verified ·
1 Parent(s): cb3707d

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -52
app.py DELETED
@@ -1,52 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import torch
4
- import cv2
5
- from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
6
- from PIL import Image
7
-
8
-
9
- import os
10
- import urllib.request
11
-
12
- MODEL_URL = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth"
13
- MODEL_PATH = "sam_vit_b.pth"
14
-
15
- # Eğer model yoksa indir
16
- if not os.path.exists(MODEL_PATH):
17
- print("Model indiriliyor...")
18
- urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
19
- print("Model indirildi.")
20
-
21
- # Model yükle
22
- model_type = "vit_b"
23
- device = "cuda" if torch.cuda.is_available() else "cpu"
24
-
25
- sam = sam_model_registry[model_type](checkpoint=MODEL_PATH)
26
- sam.to(device=device)
27
-
28
- mask_generator = SamAutomaticMaskGenerator(sam)
29
-
30
- def segment_all_objects(image):
31
- image_np = np.array(image)
32
- masks = mask_generator.generate(image_np)
33
-
34
- # Maske üzerine çiz
35
- overlay = image_np.copy()
36
- for i, mask in enumerate(masks):
37
- m = mask["segmentation"]
38
- color = np.random.randint(0, 255, size=(3,))
39
- overlay[m] = overlay[m] * 0.3 + color * 0.7
40
- # Maske üstüne label yaz
41
- y, x = np.where(m)
42
- if len(x) > 0 and len(y) > 0:
43
- cx, cy = int(np.mean(x)), int(np.mean(y))
44
- cv2.putText(overlay, f"Obj {i+1}", (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,255,255), 2)
45
-
46
- return Image.fromarray(overlay.astype(np.uint8))
47
-
48
- gr.Interface(
49
- fn=segment_all_objects,
50
- inputs=gr.Image(type="pil"),
51
- outputs=gr.Image()
52
- ).launch()