hussainraza commited on
Commit
ddb0516
·
verified ·
1 Parent(s): a083792

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -16,18 +16,27 @@ client = Groq(api_key=GROQ_API_KEY)
16
 
17
  # Step 1: Load YOLO model
18
  model = YOLO('yolov8n.pt') # Ensure the model path is correct
 
19
 
20
  # Function to detect weeds, classify types, and analyze severity
21
  def detect_weeds_and_classify(image):
22
  results = model(image) # Run inference on the image
23
  detections = results[0]
24
- image = Image.fromarray(np.uint8(image))
25
 
26
- boxes = detections.boxes.xyxy.cpu().numpy() # Bounding boxes
27
- labels = detections.boxes.cls.cpu().numpy() # Class labels
28
- confidences = detections.boxes.conf.cpu().numpy() # Confidence scores
 
 
 
 
 
 
 
29
 
 
30
  draw = ImageDraw.Draw(image)
 
31
  weed_count = 0
32
  crop_count = 0
33
 
@@ -44,7 +53,6 @@ def detect_weeds_and_classify(image):
44
  draw.text((x_min, y_min - 10), f"Crop: {confidence:.2f}", fill="green")
45
 
46
  severity = (weed_count / (weed_count + crop_count)) * 100 if (weed_count + crop_count) > 0 else 0
47
-
48
  return image, f"Weed Severity: {severity:.2f}%", weed_count, crop_count
49
 
50
  # Function to query Groq's Llama model for dynamic recommendations
 
16
 
17
  # Step 1: Load YOLO model
18
  model = YOLO('yolov8n.pt') # Ensure the model path is correct
19
+ model.conf = 0.3 # Lower confidence threshold to 30%
20
 
21
  # Function to detect weeds, classify types, and analyze severity
22
  def detect_weeds_and_classify(image):
23
  results = model(image) # Run inference on the image
24
  detections = results[0]
 
25
 
26
+ # Debugging output
27
+ print("Detection results:", detections)
28
+
29
+ boxes = detections.boxes.xyxy.cpu().numpy() if detections.boxes is not None else []
30
+ labels = detections.boxes.cls.cpu().numpy() if detections.boxes is not None else []
31
+ confidences = detections.boxes.conf.cpu().numpy() if detections.boxes is not None else []
32
+
33
+ if len(boxes) == 0:
34
+ print("No objects detected!")
35
+ return image, "Weed Severity: 0.00%", 0, 0
36
 
37
+ image = Image.fromarray(np.uint8(image))
38
  draw = ImageDraw.Draw(image)
39
+
40
  weed_count = 0
41
  crop_count = 0
42
 
 
53
  draw.text((x_min, y_min - 10), f"Crop: {confidence:.2f}", fill="green")
54
 
55
  severity = (weed_count / (weed_count + crop_count)) * 100 if (weed_count + crop_count) > 0 else 0
 
56
  return image, f"Weed Severity: {severity:.2f}%", weed_count, crop_count
57
 
58
  # Function to query Groq's Llama model for dynamic recommendations