Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -1,8 +1,6 @@
1
  from fastapi import FastAPI, UploadFile, File
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from ultralytics import YOLO
4
- from huggingface_hub import hf_hub_download
5
-
6
  import numpy as np
7
  from PIL import Image
8
  import io
@@ -10,7 +8,7 @@ import uvicorn
10
 
11
  app = FastAPI()
12
 
13
- # CORS
14
  app.add_middleware(
15
  CORSMiddleware,
16
  allow_origins=["*"],
@@ -19,16 +17,20 @@ app.add_middleware(
19
  allow_headers=["*"],
20
  )
21
 
22
- # ===========================================
23
- # DOWNLOAD CRACK MODEL FROM HF
24
- # ===========================================
25
-
26
  print("๐Ÿ”ต Loading local crack model...")
27
  model = YOLO("best.pt")
28
  print("โœ… Crack Model Loaded Successfully")
29
 
30
  # ===========================================
31
- # PREDICTION API
 
 
 
 
 
 
 
32
  # ===========================================
33
  @app.post("/predict")
34
  async def predict(img: UploadFile = File(...)):
@@ -36,21 +38,18 @@ async def predict(img: UploadFile = File(...)):
36
  bytes_data = await img.read()
37
  image = Image.open(io.BytesIO(bytes_data)).convert("RGB")
38
  np_img = np.array(image)
39
-
40
  results = model(np_img)
41
  result = results[0]
42
-
43
- # crack detection: check boxes
44
  if result.boxes is None or len(result.boxes) == 0:
45
  return {
46
  "data": [
47
  {"label": "normal", "confidence": 1.0}
48
  ]
49
  }
50
-
51
- # There are crack boxes
52
  conf = float(result.boxes.conf.max().item())
53
-
54
  return {
55
  "data": [
56
  {
@@ -59,7 +58,6 @@ async def predict(img: UploadFile = File(...)):
59
  }
60
  ]
61
  }
62
-
63
  except Exception as e:
64
  print("โŒ Prediction error:", e)
65
  return {
@@ -67,6 +65,5 @@ async def predict(img: UploadFile = File(...)):
67
  "error": str(e)
68
  }
69
 
70
-
71
  if __name__ == "__main__":
72
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi import FastAPI, UploadFile, File
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from ultralytics import YOLO
 
 
4
  import numpy as np
5
  from PIL import Image
6
  import io
 
8
 
9
  app = FastAPI()
10
 
11
+ # CORS ์„ค์ •
12
  app.add_middleware(
13
  CORSMiddleware,
14
  allow_origins=["*"],
 
17
  allow_headers=["*"],
18
  )
19
 
20
+ # ๋ชจ๋ธ ๋กœ๋“œ
 
 
 
21
  print("๐Ÿ”ต Loading local crack model...")
22
  model = YOLO("best.pt")
23
  print("โœ… Crack Model Loaded Successfully")
24
 
25
  # ===========================================
26
+ # 1. (์ถ”๊ฐ€๋จ) ๊ธฐ๋ณธ ๊ฒฝ๋กœ("/") ์ฒ˜๋ฆฌ - ๋ธŒ๋ผ์šฐ์ € ์ ‘์† ํ…Œ์ŠคํŠธ์šฉ
27
+ # ===========================================
28
+ @app.get("/")
29
+ def read_root():
30
+ return {"message": "ConcreteAI Crack Detection API is running!", "status": "OK"}
31
+
32
+ # ===========================================
33
+ # 2. ์˜ˆ์ธก API
34
  # ===========================================
35
  @app.post("/predict")
36
  async def predict(img: UploadFile = File(...)):
 
38
  bytes_data = await img.read()
39
  image = Image.open(io.BytesIO(bytes_data)).convert("RGB")
40
  np_img = np.array(image)
 
41
  results = model(np_img)
42
  result = results[0]
43
+
44
+ # ๊ท ์—ด ๊ฐ์ง€ ๋กœ์ง
45
  if result.boxes is None or len(result.boxes) == 0:
46
  return {
47
  "data": [
48
  {"label": "normal", "confidence": 1.0}
49
  ]
50
  }
51
+
 
52
  conf = float(result.boxes.conf.max().item())
 
53
  return {
54
  "data": [
55
  {
 
58
  }
59
  ]
60
  }
 
61
  except Exception as e:
62
  print("โŒ Prediction error:", e)
63
  return {
 
65
  "error": str(e)
66
  }
67
 
 
68
  if __name__ == "__main__":
69
+ uvicorn.run(app, host="0.0.0.0", port=7860)