aryan365 commited on
Commit
a8d3868
·
verified ·
1 Parent(s): 98513a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -43
app.py CHANGED
@@ -6,18 +6,17 @@ from tensorflow import keras
6
  import sys
7
  import io
8
  from ultralytics import YOLO
9
- import time
10
  from transformers import pipeline
11
 
 
12
  image_to_text_pipe = pipeline("image-to-text", model="naver-clova-ix/donut-base")
13
 
14
-
15
  # Set the default encoding to utf-8
16
  sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
17
 
18
  app = Flask(__name__)
19
 
20
- # Load your pre-trained vegetable classification model
21
  vegetables = [
22
  "banana", "beans broad", "beans cluster", "beans haricot", "beetroot",
23
  "bitter guard", "bottle guard", "brinjal long", "brinjal[purple]", "cabbage",
@@ -27,8 +26,9 @@ vegetables = [
27
  ]
28
  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']
29
 
 
 
30
  model = YOLO('fresh_model.pt')
31
- # Load the YOLO model
32
  yolo_model = YOLO('another_model.pt') # Adjust the path as needed
33
 
34
  @app.route('/')
@@ -38,27 +38,20 @@ def index():
38
  @app.route('/predict', methods=['POST'])
39
  def predict():
40
  try:
41
- # Extract and decode image data
42
  data = request.json
43
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
44
 
45
- # Decode the base64 string into a NumPy array
46
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
47
-
48
- # Convert the NumPy array into an OpenCV image (BGR format)
49
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
50
 
51
- # Perform inference using the YOLO model
52
- results = model(image) # Pass the image to the YOLO model
53
-
54
- # Extract the detected classes
55
  class_indices = results[0].boxes.cls.cpu().numpy().astype(int) # Get the class indices
56
- unique_classes = np.unique(class_indices) # Get unique classes detected
57
 
58
- # Map class indices to labels if needed
59
- # Assuming you have a mapping for YOLO classes
60
- detected_classes = [str(cls) for cls in unique_classes]
61
- detected_classes=vegetables[detected_classes[0]]
 
62
 
63
  return jsonify({'predictions': detected_classes})
64
  except Exception as e:
@@ -67,57 +60,41 @@ def predict():
67
  @app.route('/image_to_text', methods=['POST'])
68
  def image_to_text():
69
  try:
70
- # Extract and decode image data
71
  data = request.json
72
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
73
 
74
- # Decode the base64 string into a NumPy array
75
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
76
-
77
- # Convert the NumPy array into an OpenCV image (BGR format)
78
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
79
 
80
- # Use PaddleOCR to detect and extract text
81
- result = ocr.ocr(image, cls=True) # Run OCR on the image
82
-
83
- # Check if any text is detected
84
- is_text_detected = any(word_info[1][0].strip() for line in result for word_info in line)
85
 
 
86
  return jsonify({'text_detected': is_text_detected})
87
  except Exception as e:
88
  return jsonify({'error': str(e)}), 500
89
 
90
-
91
-
92
-
93
  @app.route('/detect', methods=['POST'])
94
  def detect():
95
  try:
96
- # Extract and decode image data
97
  data = request.json
98
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
99
 
100
- # Decode the base64 string into a NumPy array
101
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
102
-
103
- # Convert the NumPy array into an OpenCV image (BGR format)
104
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
105
 
106
- # Perform inference using the YOLO model
107
- results = yolo_model(image) # Pass the image to the YOLO model
108
 
109
- # Extract the detected classes
110
- class_indices = results[0].boxes.cls.cpu().numpy().astype(int) # Get the class indices
111
- unique_classes = np.unique(class_indices) # Get unique classes detected
112
 
113
- # Map class indices to labels if needed
114
- # Assuming you have a mapping for YOLO classes
115
- detected_classes = [str(cls) for cls in unique_classes]
116
- detected_classes=names[detected_classes[0]]
117
 
118
  return jsonify({'predictions': detected_classes})
119
  except Exception as e:
120
  return jsonify({'error': str(e)}), 500
121
 
122
  if __name__ == '__main__':
123
- app.run(host='0.0.0.0', port=7860, debug=True)
 
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",
 
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
  @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:
 
60
  @app.route('/image_to_text', methods=['POST'])
61
  def image_to_text():
62
  try:
 
63
  data = request.json
64
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
65
 
 
66
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
 
 
67
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
68
 
69
+ # Use your OCR model here (replace this with the actual call)
70
+ result = image_to_text_pipe(image) # Replace with actual OCR processing
 
 
 
71
 
72
+ is_text_detected = bool(result) # Check if any text was detected
73
  return jsonify({'text_detected': is_text_detected})
74
  except Exception as e:
75
  return jsonify({'error': str(e)}), 500
76
 
 
 
 
77
  @app.route('/detect', methods=['POST'])
78
  def detect():
79
  try:
 
80
  data = request.json
81
  image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
82
 
 
83
  nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
 
 
84
  image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
85
 
86
+ results = yolo_model(image) # Perform inference using the YOLO model
87
+ class_indices = results[0].boxes.cls.cpu().numpy().astype(int)
88
 
89
+ if len(class_indices) == 0:
90
+ return jsonify({'predictions': []}) # No classes detected
 
91
 
92
+ unique_classes = np.unique(class_indices) # Get unique classes detected
93
+ detected_classes = [names[cls] for cls in unique_classes if cls < len(names)] # Ensure valid index
 
 
94
 
95
  return jsonify({'predictions': detected_classes})
96
  except Exception as e:
97
  return jsonify({'error': str(e)}), 500
98
 
99
  if __name__ == '__main__':
100
+ app.run(host='0.0.0.0', port=7860, debug=True)