xuanzang commited on
Commit
6f5bbbb
·
verified ·
1 Parent(s): 748dbfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -20
app.py CHANGED
@@ -11,7 +11,6 @@ import base64
11
 
12
  app = FastAPI(title="Waste Classification API")
13
 
14
- # Add CORS middleware
15
  app.add_middleware(
16
  CORSMiddleware,
17
  allow_origins=["*"],
@@ -20,12 +19,11 @@ app.add_middleware(
20
  allow_headers=["*"],
21
  )
22
 
23
- # Load the model (you'll need to place your model file in the api directory)
24
  try:
25
  model = load_model("predictWaste12.h5")
26
  except:
27
  model = None
28
- print("Model not found. Please place predictWaste12.h5 in the api directory.")
29
 
30
  output_class = [
31
  "baterai", "biologis", "kaca-coklat", "kardus", "pakaian",
@@ -40,21 +38,12 @@ output_class_english = [
40
  ]
41
 
42
  def preprocess_image(img_bytes):
43
- """Preprocess image for prediction"""
44
- # Convert bytes to PIL Image
45
  img = Image.open(io.BytesIO(img_bytes))
46
-
47
- # Convert to RGB if necessary
48
  if img.mode != 'RGB':
49
  img = img.convert('RGB')
50
-
51
- # Resize to model input size
52
  img = img.resize((224, 224))
53
-
54
- # Convert to array and normalize
55
  img_array = np.array(img) / 255.0
56
  img_array = np.expand_dims(img_array, axis=0)
57
-
58
  return img_array
59
 
60
  @app.get("/")
@@ -65,28 +54,20 @@ async def root():
65
  async def predict_waste(file: UploadFile = File(...)):
66
  if not model:
67
  raise HTTPException(status_code=500, detail="Model tidak tersedia")
68
-
69
  try:
70
- # Read image file
71
  img_bytes = await file.read()
72
-
73
- # Preprocess image
74
  processed_image = preprocess_image(img_bytes)
75
-
76
- # Make prediction
77
  predicted_array = model.predict(processed_image)
78
  predicted_index = np.argmax(predicted_array)
79
  predicted_class = output_class[predicted_index]
80
  predicted_class_en = output_class_english[predicted_index]
81
  predicted_accuracy = round(np.max(predicted_array) * 100, 2)
82
-
83
  return {
84
  "prediction": predicted_class,
85
  "prediction_en": predicted_class_en,
86
  "accuracy": predicted_accuracy,
87
  "confidence_scores": predicted_array[0].tolist()
88
  }
89
-
90
  except Exception as e:
91
  raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
92
 
 
11
 
12
  app = FastAPI(title="Waste Classification API")
13
 
 
14
  app.add_middleware(
15
  CORSMiddleware,
16
  allow_origins=["*"],
 
19
  allow_headers=["*"],
20
  )
21
 
 
22
  try:
23
  model = load_model("predictWaste12.h5")
24
  except:
25
  model = None
26
+ print("Model not found.")
27
 
28
  output_class = [
29
  "baterai", "biologis", "kaca-coklat", "kardus", "pakaian",
 
38
  ]
39
 
40
  def preprocess_image(img_bytes):
 
 
41
  img = Image.open(io.BytesIO(img_bytes))
 
 
42
  if img.mode != 'RGB':
43
  img = img.convert('RGB')
 
 
44
  img = img.resize((224, 224))
 
 
45
  img_array = np.array(img) / 255.0
46
  img_array = np.expand_dims(img_array, axis=0)
 
47
  return img_array
48
 
49
  @app.get("/")
 
54
  async def predict_waste(file: UploadFile = File(...)):
55
  if not model:
56
  raise HTTPException(status_code=500, detail="Model tidak tersedia")
 
57
  try:
 
58
  img_bytes = await file.read()
 
 
59
  processed_image = preprocess_image(img_bytes)
 
 
60
  predicted_array = model.predict(processed_image)
61
  predicted_index = np.argmax(predicted_array)
62
  predicted_class = output_class[predicted_index]
63
  predicted_class_en = output_class_english[predicted_index]
64
  predicted_accuracy = round(np.max(predicted_array) * 100, 2)
 
65
  return {
66
  "prediction": predicted_class,
67
  "prediction_en": predicted_class_en,
68
  "accuracy": predicted_accuracy,
69
  "confidence_scores": predicted_array[0].tolist()
70
  }
 
71
  except Exception as e:
72
  raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
73