felixflier03 commited on
Commit
f3d16e4
·
verified ·
1 Parent(s): da8f0df

Delete inference.py

Browse files
Files changed (1) hide show
  1. inference.py +0 -68
inference.py DELETED
@@ -1,68 +0,0 @@
1
- import torch
2
- from transformers import Pipeline
3
- from segment_anything import sam_model_registry, SamPredictor
4
- import numpy as np
5
-
6
- class CVATWrapper:
7
- def __init__(self, model_path):
8
- # Modell laden
9
- self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
-
11
- # Für SAM:
12
- self.model = sam_model_registry["vit_h"](checkpoint=model_path)
13
- self.model.to(self.device)
14
- self.predictor = SamPredictor(self.model)
15
-
16
- # Für eigene Modelle:
17
- # self.model = torch.load(model_path)
18
- # self.model.eval()
19
-
20
- def preprocess(self, image):
21
- # Bildvorverarbeitung
22
- if isinstance(image, str):
23
- # Falls Bildpfad übergeben wird
24
- image = cv2.imread(image)
25
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
26
- return image
27
-
28
- def predict(self, image):
29
- # Vorverarbeitung
30
- image = self.preprocess(image)
31
-
32
- # Für SAM:
33
- self.predictor.set_image(image)
34
- masks, scores, logits = self.predictor.predict()
35
-
36
- # Für eigene Modelle:
37
- # output = self.model(processed_image)
38
- # masks = process_output(output) # Je nach Modellausgabe
39
-
40
- # Konvertierung ins CVAT-Format
41
- results = []
42
- for mask, score in zip(masks, scores):
43
- if score > 0.5: # Konfidenz-Schwellenwert
44
- results.append({
45
- "confidence": float(score),
46
- "label": "object",
47
- "points": mask_to_polygons(mask), # Hilfsfunktion unten
48
- "type": "polygon"
49
- })
50
-
51
- return results
52
-
53
- def mask_to_polygons(mask):
54
- """Konvertiert eine binäre Maske in CVAT-Polygon-Format"""
55
- import cv2
56
- contours, _ = cv2.findContours(
57
- mask.astype(np.uint8),
58
- cv2.RETR_EXTERNAL,
59
- cv2.CHAIN_APPROX_SIMPLE
60
- )
61
- polygons = []
62
- for contour in contours:
63
- # Vereinfache die Konturen
64
- epsilon = 0.005 * cv2.arcLength(contour, True)
65
- approx = cv2.approxPolyDP(contour, epsilon, True)
66
- if len(approx) >= 3: # Mindestens 3 Punkte für ein Polygon
67
- polygons.append(approx.reshape(-1, 2).tolist())
68
- return polygons[0] if polygons else []