Update api.py
Browse files
api.py
CHANGED
@@ -17,7 +17,8 @@ app.add_middleware(
|
|
17 |
allow_methods=["*"],
|
18 |
allow_headers=["*"],
|
19 |
)
|
20 |
-
|
|
|
21 |
def crop_image_from_gray(img, tol=7):
|
22 |
if img.ndim == 2:
|
23 |
mask = img > tol
|
@@ -35,7 +36,6 @@ def crop_image_from_gray(img, tol=7):
|
|
35 |
img = np.stack([img1, img2, img3], axis=-1)
|
36 |
return img
|
37 |
|
38 |
-
|
39 |
def load_ben_color(image, sigmaX=10):
|
40 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
41 |
image = crop_image_from_gray(image)
|
@@ -43,7 +43,6 @@ def load_ben_color(image, sigmaX=10):
|
|
43 |
image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0, 0), sigmaX), -4, 128)
|
44 |
return image
|
45 |
|
46 |
-
|
47 |
def clahe(image):
|
48 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
49 |
r, g, b = cv2.split(image)
|
@@ -53,18 +52,15 @@ def clahe(image):
|
|
53 |
result = cv2.merge((r, g, b))
|
54 |
return result
|
55 |
|
56 |
-
|
57 |
def filter1(image):
|
58 |
image = load_ben_color(image)
|
59 |
return image
|
60 |
|
61 |
-
|
62 |
def filter2(image):
|
63 |
image = clahe(image)
|
64 |
image = cv2.resize(image, (224, 224))
|
65 |
return image
|
66 |
|
67 |
-
|
68 |
def predict(image, model, filter_func):
|
69 |
model_image = filter_func(image)
|
70 |
model_image = np.array([model_image], dtype=np.float32) / 255.0
|
@@ -82,7 +78,7 @@ def result(predictions):
|
|
82 |
models_names = ["ODIR-B-2K-5Class-LastTrain-Xception", "ODIR-B-2K-6Class-LastTrain-Xception"]
|
83 |
model_paths = ["Bitirme/odirmodel/ODIR-B-2K-5Class-LastTrain-Xception", "Bitirme/odirmodel/ODIR-B-2K-6Class-LastTrain-Xception"]
|
84 |
|
85 |
-
filters = [filter1, filter2] tanımlandı
|
86 |
|
87 |
models = []
|
88 |
for model_path in model_paths:
|
@@ -104,4 +100,4 @@ async def predict_endpoint(file: UploadFile = File(...)):
|
|
104 |
prediction = predict(image, model, filters[i])
|
105 |
result_json[models_names[i]] = result(prediction)
|
106 |
|
107 |
-
return {"predictions": result_json}
|
|
|
17 |
allow_methods=["*"],
|
18 |
allow_headers=["*"],
|
19 |
)
|
20 |
+
|
21 |
+
# Filtre kısmı
|
22 |
def crop_image_from_gray(img, tol=7):
|
23 |
if img.ndim == 2:
|
24 |
mask = img > tol
|
|
|
36 |
img = np.stack([img1, img2, img3], axis=-1)
|
37 |
return img
|
38 |
|
|
|
39 |
def load_ben_color(image, sigmaX=10):
|
40 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
41 |
image = crop_image_from_gray(image)
|
|
|
43 |
image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0, 0), sigmaX), -4, 128)
|
44 |
return image
|
45 |
|
|
|
46 |
def clahe(image):
|
47 |
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
|
48 |
r, g, b = cv2.split(image)
|
|
|
52 |
result = cv2.merge((r, g, b))
|
53 |
return result
|
54 |
|
|
|
55 |
def filter1(image):
|
56 |
image = load_ben_color(image)
|
57 |
return image
|
58 |
|
|
|
59 |
def filter2(image):
|
60 |
image = clahe(image)
|
61 |
image = cv2.resize(image, (224, 224))
|
62 |
return image
|
63 |
|
|
|
64 |
def predict(image, model, filter_func):
|
65 |
model_image = filter_func(image)
|
66 |
model_image = np.array([model_image], dtype=np.float32) / 255.0
|
|
|
78 |
models_names = ["ODIR-B-2K-5Class-LastTrain-Xception", "ODIR-B-2K-6Class-LastTrain-Xception"]
|
79 |
model_paths = ["Bitirme/odirmodel/ODIR-B-2K-5Class-LastTrain-Xception", "Bitirme/odirmodel/ODIR-B-2K-6Class-LastTrain-Xception"]
|
80 |
|
81 |
+
filters = [filter1, filter2] # tanımlandı
|
82 |
|
83 |
models = []
|
84 |
for model_path in model_paths:
|
|
|
100 |
prediction = predict(image, model, filters[i])
|
101 |
result_json[models_names[i]] = result(prediction)
|
102 |
|
103 |
+
return {"predictions": result_json}
|