jflo commited on
Commit
89ffd90
·
verified ·
1 Parent(s): 87906f0

Added classify car part URL

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -16,6 +16,10 @@ import torch
16
  import torch.nn as nn
17
  import torch.nn.functional as F
18
  from torchvision import transforms
 
 
 
 
19
 
20
  all_letters = string.ascii_letters + " .,;'"
21
  n_letters = len(all_letters)
@@ -167,22 +171,28 @@ def classify_img(img):
167
  return model_output
168
 
169
 
170
- @app.post("/upload")
171
  async def upload_image(file: UploadFile = File(...)):
172
- # Read the file into memory
173
- image_bytes = await file.read()
174
-
175
- # Convert to a PIL Image (or format expected by your ML model)
176
  try:
177
- image = Image.open(io.BytesIO(image_bytes))
178
- except Exception as e:
179
- return JSONResponse(content={"error": "Invalid image format"}, status_code=400)
180
 
181
- # Preprocess and run classification (example: dummy label here)
182
- # Replace this with your actual model inference
183
- #prediction = classify_img(image) # 👈 Define this function to use your ML model
 
 
 
184
 
185
- return JSONResponse(content=classify_img(image), status_code=201)
 
 
 
 
 
 
 
186
 
187
  @app.get("/")
188
  def api_home():
 
16
  import torch.nn as nn
17
  import torch.nn.functional as F
18
  from torchvision import transforms
19
+ from PIL import Image
20
+ import torchvision.transforms as transforms
21
+
22
+ import io
23
 
24
  all_letters = string.ascii_letters + " .,;'"
25
  n_letters = len(all_letters)
 
171
  return model_output
172
 
173
 
174
+ @app.post("/classifycarpart")
175
  async def upload_image(file: UploadFile = File(...)):
 
 
 
 
176
  try:
177
+ # Read and convert the image to a PIL Image
178
+ contents = await file.read()
179
+ image = Image.open(io.BytesIO(contents)).convert("RGB")
180
 
181
+ # Optional: Transform the image into a tensor for PyTorch
182
+ transform = transforms.Compose([
183
+ transforms.Resize((224, 224)), # Resize to your model's expected input
184
+ transforms.ToTensor(), # Convert to tensor
185
+ ])
186
+ tensor = transform(image).unsqueeze(0) # Add batch dimension
187
 
188
+ # Dummy model prediction
189
+ # Replace this with your actual PyTorch model
190
+ prediction = torch.rand(1).item()
191
+
192
+ return JSONResponse(content={"message": "Image received", "prediction": prediction})
193
+
194
+ except Exception as e:
195
+ return JSONResponse(content={"error": str(e)}, status_code=500)
196
 
197
  @app.get("/")
198
  def api_home():