arittrabag commited on
Commit
22d6743
·
verified ·
1 Parent(s): 3b05912

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -25
app.py CHANGED
@@ -17,49 +17,31 @@ async def root():
17
 
18
  @app.post("/predict")
19
  async def predict(file: UploadFile = File(...)):
20
- # Save temp file
21
  temp_file = Path(f"/tmp/{uuid.uuid4()}_{file.filename}")
22
  with temp_file.open("wb") as buffer:
23
  shutil.copyfileobj(file.file, buffer)
24
 
25
- # Run inference
26
  results = model.predict(source=str(temp_file), save=False, conf=0.5)
27
 
28
- # Collect detections (bounding boxes)
29
  detections = []
30
- frame_level = {"healthy": 0, "yellow_disease": 0}
31
 
32
  for r in results:
33
  for box in r.boxes:
34
  cls_name = model.names[int(box.cls)]
35
  conf = float(box.conf)
36
- frame_level[cls_name] += 1 # count detections
37
-
38
  detections.append({
39
  "class": cls_name,
40
  "confidence": conf,
41
- "bbox": [float(v) for v in box.xyxy[0].tolist()] # [x1,y1,x2,y2]
42
  })
 
 
43
 
44
- # Decide frame-level classification
45
- frame_class = "yellow_disease" if frame_level["yellow_disease"] > 0 else "healthy"
46
-
47
- # Optional: encode image to base64 (so you can preview annotated frame in Postman if needed)
48
- # r.save_dir contains annotated images if save=True
49
- img_b64 = None
50
- try:
51
- import cv2, base64
52
- annotated = results[0].plot() # numpy array (BGR)
53
- _, buf = cv2.imencode(".jpg", annotated)
54
- img_b64 = base64.b64encode(buf).decode("utf-8")
55
- except Exception:
56
- pass
57
-
58
- # Clean up
59
  temp_file.unlink(missing_ok=True)
60
 
61
  return {
62
- "frame_classification": frame_class, # overall yes/no for spraying
63
- "detections": detections, # bounding boxes if nozzle precision needed
64
- "image_base64": img_b64 # optional preview (can remove if not needed)
65
  }
 
17
 
18
  @app.post("/predict")
19
  async def predict(file: UploadFile = File(...)):
 
20
  temp_file = Path(f"/tmp/{uuid.uuid4()}_{file.filename}")
21
  with temp_file.open("wb") as buffer:
22
  shutil.copyfileobj(file.file, buffer)
23
 
 
24
  results = model.predict(source=str(temp_file), save=False, conf=0.5)
25
 
 
26
  detections = []
27
+ disease_found = False
28
 
29
  for r in results:
30
  for box in r.boxes:
31
  cls_name = model.names[int(box.cls)]
32
  conf = float(box.conf)
 
 
33
  detections.append({
34
  "class": cls_name,
35
  "confidence": conf,
36
+ "bbox": [float(v) for v in box.xyxy[0].tolist()]
37
  })
38
+ if cls_name == "yellow_disease":
39
+ disease_found = True
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  temp_file.unlink(missing_ok=True)
42
 
43
  return {
44
+ "spray_signal": 1 if disease_found else 0, # ESC reads this
45
+ "frame_classification": "yellow_disease" if disease_found else "healthy",
46
+ "detections": detections # keep for logs/debugging
47
  }