Bitirme commited on
Commit
fc2cffa
1 Parent(s): edd1748

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +40 -6
api.py CHANGED
@@ -1,20 +1,51 @@
1
- import cv2
2
- import tensorflow as tf
3
  import numpy as np
4
- from fastapi import FastAPI, UploadFile, File
 
 
5
  from pydantic import BaseModel
6
  from huggingface_hub import from_pretrained_keras
7
 
8
  app = FastAPI()
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def load_ben_color(image, sigmaX=10):
11
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
12
  image = cv2.resize(image, (224, 224))
13
  image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0, 0), sigmaX), -4, 128)
14
  return image
15
 
 
16
  def clahe(image):
17
- clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
18
  r, g, b = cv2.split(image)
19
  r = clahe.apply(r)
20
  g = clahe.apply(g)
@@ -22,15 +53,18 @@ def clahe(image):
22
  result = cv2.merge((r, g, b))
23
  return result
24
 
 
25
  def filter1(image):
26
  image = load_ben_color(image)
27
  return image
28
 
 
29
  def filter2(image):
30
  image = clahe(image)
31
  image = cv2.resize(image, (224, 224))
32
  return image
33
 
 
34
  def predict(image, model, filter_func):
35
  model_image = filter_func(image)
36
  model_image = np.array([model_image], dtype=np.float32) / 255.0
@@ -48,7 +82,7 @@ def result(predictions):
48
  models_names = ["ODIR-B-2K-5Class-LastTrain-Xception", "ODIR-B-2K-6Class-LastTrain-Xception"]
49
  model_paths = ["Bitirme/odirmodel/ODIR-B-2K-5Class-LastTrain-Xception", "Bitirme/odirmodel/ODIR-B-2K-6Class-LastTrain-Xception"]
50
 
51
- filters = [filter1, filter2] # İhtiyacınıza göre filtre fonksiyonları burada tanımlandı
52
 
53
  models = []
54
  for model_path in model_paths:
@@ -70,4 +104,4 @@ async def predict_endpoint(file: UploadFile = File(...)):
70
  prediction = predict(image, model, filters[i])
71
  result_json[models_names[i]] = result(prediction)
72
 
73
- return {"predictions": result_json}
 
1
+ from fastapi import FastAPI, File, UploadFile
 
2
  import numpy as np
3
+ import tensorflow as tf
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ import cv2
6
  from pydantic import BaseModel
7
  from huggingface_hub import from_pretrained_keras
8
 
9
  app = FastAPI()
10
 
11
+ origins = ["*"]
12
+
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=origins,
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+ #filtre kısmı
21
+ def crop_image_from_gray(img, tol=7):
22
+ if img.ndim == 2:
23
+ mask = img > tol
24
+ return img[np.ix_(mask.any(1), mask.any(0))]
25
+ elif img.ndim == 3:
26
+ gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
27
+ mask = gray_img > tol
28
+ check_shape = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))].shape[0]
29
+ if check_shape == 0:
30
+ return img
31
+ else:
32
+ img1 = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))]
33
+ img2 = img[:, :, 1][np.ix_(mask.any(1), mask.any(0))]
34
+ img3 = img[:, :, 2][np.ix_(mask.any(1), mask.any(0))]
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)
42
  image = cv2.resize(image, (224, 224))
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)
50
  r = clahe.apply(r)
51
  g = clahe.apply(g)
 
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
  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
  prediction = predict(image, model, filters[i])
105
  result_json[models_names[i]] = result(prediction)
106
 
107
+ return {"predictions": result_json}