Thompson001 commited on
Commit
303f396
ยท
verified ยท
1 Parent(s): d7f9a54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -23
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  from fastapi import FastAPI, UploadFile, File
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from ultralytics import YOLO
@@ -9,7 +8,6 @@ import uvicorn
9
 
10
  app = FastAPI()
11
 
12
- # CORS ํ—ˆ์šฉ
13
  app.add_middleware(
14
  CORSMiddleware,
15
  allow_origins=["*"],
@@ -18,58 +16,42 @@ app.add_middleware(
18
  allow_headers=["*"],
19
  )
20
 
21
- # ===========================================
22
- # YOLOv8 segmentation model (from keremberke)
23
- # ===========================================
24
  model = YOLO("best.pt")
25
  print("โœ… Model loaded")
26
 
27
  @app.post("/predict")
28
  async def predict(img: UploadFile = File(...)):
29
  try:
30
- # ์ด๋ฏธ์ง€ ๋กœ๋”ฉ
31
  bytes_data = await img.read()
32
  image = Image.open(io.BytesIO(bytes_data)).convert("RGB")
33
  np_img = np.array(image)
34
 
35
- # YOLO inference
36
  results = model(np_img)
37
  result = results[0]
38
 
39
- # segmentation mask ์—ฌ๋ถ€๋กœ crack ์œ ๋ฌด ํŒ๋‹จ
40
  has_mask = result.masks is not None
41
 
42
  if not has_mask:
43
  return {
44
- "data": [
45
- {"label": "normal", "confidence": 1.0}
46
- ]
47
  }
48
 
49
- # ๊ฐ€์žฅ ๋†’์€ confidence ์ถ”์ถœ
50
  if result.boxes is not None and len(result.boxes) > 0:
51
  conf = float(result.boxes.conf.max().item())
52
  else:
53
- conf = 0.85 # ๊ธฐ๋ณธ๊ฐ’
54
 
55
  return {
56
- "data": [
57
- {
58
- "label": "crack",
59
- "confidence": conf
60
- }
61
- ]
62
  }
63
 
64
  except Exception as e:
65
  print("โŒ Prediction error:", e)
66
  return {
67
- "data": [
68
- {"label": "normal", "confidence": 1.0}
69
- ],
70
  "error": str(e)
71
  }
72
 
73
-
74
  if __name__ == "__main__":
75
  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
 
8
 
9
  app = FastAPI()
10
 
 
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
 
16
  allow_headers=["*"],
17
  )
18
 
19
+ print("๐Ÿ”ต Loading model: best.pt")
 
 
20
  model = YOLO("best.pt")
21
  print("โœ… Model loaded")
22
 
23
  @app.post("/predict")
24
  async def predict(img: UploadFile = File(...)):
25
  try:
 
26
  bytes_data = await img.read()
27
  image = Image.open(io.BytesIO(bytes_data)).convert("RGB")
28
  np_img = np.array(image)
29
 
 
30
  results = model(np_img)
31
  result = results[0]
32
 
 
33
  has_mask = result.masks is not None
34
 
35
  if not has_mask:
36
  return {
37
+ "data": [{"label": "normal", "confidence": 1.0}]
 
 
38
  }
39
 
 
40
  if result.boxes is not None and len(result.boxes) > 0:
41
  conf = float(result.boxes.conf.max().item())
42
  else:
43
+ conf = 0.85
44
 
45
  return {
46
+ "data": [{"label": "crack", "confidence": conf}]
 
 
 
 
 
47
  }
48
 
49
  except Exception as e:
50
  print("โŒ Prediction error:", e)
51
  return {
52
+ "data": [{"label": "normal", "confidence": 1.0}],
 
 
53
  "error": str(e)
54
  }
55
 
 
56
  if __name__ == "__main__":
57
  uvicorn.run(app, host="0.0.0.0", port=7860)