aryan365 commited on
Commit
64b6ab5
·
verified ·
1 Parent(s): f25ea4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -9,9 +9,7 @@ from ultralytics import YOLO
9
  import time
10
  from transformers import DonutProcessor, VisionEncoderDecoderModel
11
  from PIL import Image
12
- import os
13
- # Set cache directory to a local path with write permissions
14
- os.environ['TRANSFORMERS_CACHE'] = '/.cache'
15
 
16
  # Load the Donut model and processor (adjust the model name if needed)
17
  processor = DonutProcessor.from_pretrained('naver-clova-ix/donut-base')
@@ -30,7 +28,7 @@ vegetables = [
30
  "corn", "cucumber", "drumstick", "garlic", "ginger", "ladies finger",
31
  "lemons", "Onion red", "potato", "sweet potato", "tomato", "Zuchini"
32
  ]
33
- model = keras.models.load_model('fresh_model.keras')
34
  # Load the YOLO model
35
  yolo_model = YOLO('another_model.pt') # Adjust the path as needed
36
 
@@ -51,25 +49,19 @@ def predict():
51
  # Convert the NumPy array into an OpenCV image (BGR format)
52
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
53
 
54
- # Convert to RGB format
55
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
56
-
57
- # Resize the image to the expected input size of the model (e.g., 225x225)
58
- image_resized = cv2.resize(image, (225, 225))
59
-
60
- # Normalize the image for vegetable classification
61
- image_resized = image_resized.astype('float32') / 255.0 # Normalize to [0, 1]
62
- image_resized = np.expand_dims(image_resized, axis=0) # Add batch dimension
63
 
64
- # Make a prediction using the vegetable classification model
65
- predictions = model.predict(image_resized)
66
- predicted_class = np.argmax(predictions, axis=1)[0]
67
- time.sleep(3)
68
 
69
- # Map the class index to the vegetable label
70
- prediction_label = vegetables[predicted_class]
 
 
71
 
72
- return jsonify({'prediction': prediction_label})
73
  except Exception as e:
74
  return jsonify({'error': str(e)}), 500
75
 
 
9
  import time
10
  from transformers import DonutProcessor, VisionEncoderDecoderModel
11
  from PIL import Image
12
+
 
 
13
 
14
  # Load the Donut model and processor (adjust the model name if needed)
15
  processor = DonutProcessor.from_pretrained('naver-clova-ix/donut-base')
 
28
  "corn", "cucumber", "drumstick", "garlic", "ginger", "ladies finger",
29
  "lemons", "Onion red", "potato", "sweet potato", "tomato", "Zuchini"
30
  ]
31
+ model = YOLO('fresh_model.pt')
32
  # Load the YOLO model
33
  yolo_model = YOLO('another_model.pt') # Adjust the path as needed
34
 
 
49
  # Convert the NumPy array into an OpenCV image (BGR format)
50
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
51
 
52
+ # Perform inference using the YOLO model
53
+ results = model(image) # Pass the image to the YOLO model
 
 
 
 
 
 
 
54
 
55
+ # Extract the detected classes
56
+ class_indices = results[0].boxes.cls.cpu().numpy().astype(int) # Get the class indices
57
+ unique_classes = np.unique(class_indices) # Get unique classes detected
 
58
 
59
+ # Map class indices to labels if needed
60
+ # Assuming you have a mapping for YOLO classes
61
+ detected_classes = [str(cls) for cls in unique_classes]
62
+ time.sleep(3)# Convert indices to strings
63
 
64
+ return jsonify({'predictions': detected_classes})
65
  except Exception as e:
66
  return jsonify({'error': str(e)}), 500
67