aryan365 commited on
Commit
b812fc9
·
verified ·
1 Parent(s): 829b96f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -37
app.py CHANGED
@@ -6,17 +6,15 @@ from tensorflow import keras
6
  import sys
7
  import io
8
  from ultralytics import YOLO
9
- from transformers import pipeline
10
 
11
- # Initialize image-to-text pipeline
12
- #image_to_text_pipe = pipeline("image-to-text", model="naver-clova-ix/donut-base")
13
 
14
  # Set the default encoding to utf-8
15
  sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
16
 
17
  app = Flask(__name__)
18
 
19
- # Pre-defined vegetable and name classes
20
  vegetables = [
21
  "banana", "beans broad", "beans cluster", "beans haricot", "beetroot",
22
  "bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage",
@@ -24,11 +22,8 @@ vegetables = [
24
  "corn", "cucumber", "drumstick", "garlic", "ginger", "ladies finger",
25
  "lemons", "Onion red", "potato", "sweet potato", "tomato", "Zuchini"
26
  ]
27
- names= ['Complan Classic Creme', 'Complan Kesar Badam', 'Complan Nutrigro Badam Kheer', 'Complan Pista Badam', 'Complan Royal Chocolate', 'Complan Royale Chocolate', 'EY AAAM TULSI TURMERIC FACEWASH50G', 'EY ADVANCED GOLDEN GLOW PEEL OFF M- 50G', 'EY ADVANCED GOLDEN GLOW PEEL OFF M- 90G', 'EY EXF WALNUT SCRUB AYR 200G', 'EY HALDICHANDAN FP HF POWDER 25G', 'EY HYD-EXF WALNT APR SCRUB AYR100G', 'EY HYDR - EXF WALNUT APRICOT SCRUB 50G', 'EY NAT GLOW ORANGE PEEL OFF AY 90G', 'EY NATURALS NEEM FACE WASH AY 50G', 'EY RJ CUCUMBER ALOEVERA FACEPAK50G', 'EY TAN CHOCO CHERRY PACK 50G', 'EY_SCR_PURIFYING_EXFOLTNG_NEEM_PAPAYA_50G', 'Everyuth Naturals Body Lotion Nourishing Cocoa 200ml', 'Everyuth Naturals Body Lotion Rejuvenating Flora 200ml', 'Everyuth Naturals Body Lotion Soothing Citrus 200ml', 'Everyuth Naturals Body Lotion Sun Care Berries SPF 15 200ml', 'Glucon D Nimbu Pani', 'Glucon D Nimbu Pani 1-KG', 'Glucon D Regular', 'Glucon D Regular 1-KG', 'Glucon D Regular 2-KG', 'Glucon D Tangy orange', 'Glucon D Tangy orange 1-KG', 'Nutralite ACHARI MAYO 300g-275g-25g-', 'Nutralite ACHARI MAYO 30g', 'Nutralite CHEESY GARLIC MAYO 300g-275g-25g-', 'Nutralite CHEESY GARLIC MAYO 30g', 'Nutralite CHOCO SPREAD CALCIUM 275g', 'Nutralite DOODHSHAKTHI PURE GHEE 1L', 'Nutralite TANDOORI MAYO 300g-275g-25g-', 'Nutralite TANDOORI MAYO 30g', 'Nutralite VEG MAYO 300g-275g-25g-', 'Nycil Prickly Heat Powder', 'SUGAR FREE GOLD 500 PELLET', 'SUGAR FREE GOLD POWDER 100GM', 'SUGAR FREE GOLD SACHET 50', 'SUGAR FREE GOLD SACHET 50 SUGAR FREE GOLD SACHET 50', 'SUGAR FREE GRN 300 PELLET', 'SUGAR FREE NATURA 500 PELLET', 'SUGAR FREE NATURA DIET SUGAR', 'SUGAR FREE NATURA DIET SUGAR 80GM', 'SUGAR FREE NATURA SACHET 50', 'SUGAR FREE NATURA SWEET DROPS', 'SUGAR FREE NATURA_ POWDER_CONC_100G', 'SUGAR FREE_GRN_ POWDER_CONC_100G', 'SUGARLITE POUCH 500G']
28
-
29
-
30
- # Load the YOLO models
31
  model = YOLO('fresh_model.pt')
 
32
  yolo_model = YOLO('another_model.pt') # Adjust the path as needed
33
 
34
  @app.route('/')
@@ -38,64 +33,60 @@ def index():
38
  @app.route('/predict', methods=['POST'])
39
  def predict():
40
  try:
 
41
  data = request.json
42
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
43
 
 
44
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
45
- image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
46
 
47
- results = model(image) # Perform inference using the YOLO model
48
- class_indices = results[0].boxes.cls.cpu().numpy().astype(int) # Get the class indices
49
 
50
- if len(class_indices) == 0:
51
- return jsonify({'predictions': []}) # No classes detected
52
 
 
 
53
  unique_classes = np.unique(class_indices) # Get unique classes detected
54
- detected_classes = [vegetables[cls] for cls in unique_classes if cls < len(vegetables)] # Ensure valid index
 
 
 
55
 
56
  return jsonify({'predictions': detected_classes})
57
  except Exception as e:
58
  return jsonify({'error': str(e)}), 500
59
 
60
- '''
61
- @app.route('/image_to_text', methods=['POST'])
62
- def image_to_text():
63
- try:
64
- data = request.json
65
- image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
66
 
67
- nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
68
- image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
69
-
70
- # Use your OCR model here (replace this with the actual call)
71
- result = image_to_text_pipe(image) # Replace with actual OCR processing
72
 
73
- is_text_detected = bool(result) # Check if any text was detected
74
- return jsonify({'text_detected': is_text_detected})
75
- except Exception as e:
76
- return jsonify({'error': str(e)}), 500
77
- '''
78
  @app.route('/detect', methods=['POST'])
79
  def detect():
80
  try:
 
81
  data = request.json
82
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
83
 
 
84
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
85
- image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
86
 
87
- results = yolo_model(image) # Perform inference using the YOLO model
88
- class_indices = results[0].boxes.cls.cpu().numpy().astype(int)
89
 
90
- if len(class_indices) == 0:
91
- return jsonify({'predictions': []}) # No classes detected
92
 
 
 
93
  unique_classes = np.unique(class_indices) # Get unique classes detected
94
- detected_classes = [names[cls] for cls in unique_classes if cls < len(names)] # Ensure valid index
 
 
 
95
 
96
  return jsonify({'predictions': detected_classes})
97
  except Exception as e:
98
  return jsonify({'error': str(e)}), 500
99
 
100
  if __name__ == '__main__':
101
- app.run(host='0.0.0.0', port=7860, debug=True)
 
6
  import sys
7
  import io
8
  from ultralytics import YOLO
9
+ import time
10
 
 
 
11
 
12
  # Set the default encoding to utf-8
13
  sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
14
 
15
  app = Flask(__name__)
16
 
17
+ # Load your pre-trained vegetable classification model
18
  vegetables = [
19
  "banana", "beans broad", "beans cluster", "beans haricot", "beetroot",
20
  "bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage",
 
22
  "corn", "cucumber", "drumstick", "garlic", "ginger", "ladies finger",
23
  "lemons", "Onion red", "potato", "sweet potato", "tomato", "Zuchini"
24
  ]
 
 
 
 
25
  model = YOLO('fresh_model.pt')
26
+ # Load the YOLO model
27
  yolo_model = YOLO('another_model.pt') # Adjust the path as needed
28
 
29
  @app.route('/')
 
33
  @app.route('/predict', methods=['POST'])
34
  def predict():
35
  try:
36
+ # Extract and decode image data
37
  data = request.json
38
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
39
 
40
+ # Decode the base64 string into a NumPy array
41
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
 
42
 
43
+ # Convert the NumPy array into an OpenCV image (BGR format)
44
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
45
 
46
+ # Perform inference using the YOLO model
47
+ results = model(image) # Pass the image to the YOLO model
48
 
49
+ # Extract the detected classes
50
+ class_indices = results[0].boxes.cls.cpu().numpy().astype(int) # Get the class indices
51
  unique_classes = np.unique(class_indices) # Get unique classes detected
52
+
53
+ # Map class indices to labels if needed
54
+ # Assuming you have a mapping for YOLO classes
55
+ detected_classes = [str(cls) for cls in unique_classes]
56
 
57
  return jsonify({'predictions': detected_classes})
58
  except Exception as e:
59
  return jsonify({'error': str(e)}), 500
60
 
 
 
 
 
 
 
61
 
 
 
 
 
 
62
 
 
 
 
 
 
63
  @app.route('/detect', methods=['POST'])
64
  def detect():
65
  try:
66
+ # Extract and decode image data
67
  data = request.json
68
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
69
 
70
+ # Decode the base64 string into a NumPy array
71
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
 
72
 
73
+ # Convert the NumPy array into an OpenCV image (BGR format)
74
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
75
 
76
+ # Perform inference using the YOLO model
77
+ results = yolo_model(image) # Pass the image to the YOLO model
78
 
79
+ # Extract the detected classes
80
+ class_indices = results[0].boxes.cls.cpu().numpy().astype(int) # Get the class indices
81
  unique_classes = np.unique(class_indices) # Get unique classes detected
82
+
83
+ # Map class indices to labels if needed
84
+ # Assuming you have a mapping for YOLO classes
85
+ detected_classes = [str(cls) for cls in unique_classes]
86
 
87
  return jsonify({'predictions': detected_classes})
88
  except Exception as e:
89
  return jsonify({'error': str(e)}), 500
90
 
91
  if __name__ == '__main__':
92
+ app.run(host='0.0.0.0', port=7860, debug=True)