aryan365 commited on
Commit
5bfdcfe
·
verified ·
1 Parent(s): b775946

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -1
app.py CHANGED
@@ -7,11 +7,12 @@ 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
@@ -88,5 +89,32 @@ def detect():
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)
 
7
  import io
8
  from ultralytics import YOLO
9
  import time
10
+ import easyocr
11
 
12
 
13
  # Set the default encoding to utf-8
14
  sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
15
+ reader = easyocr.Reader(['en'])
16
  app = Flask(__name__)
17
 
18
  # Load your pre-trained vegetable classification model
 
89
  except Exception as e:
90
  return jsonify({'error': str(e)}), 500
91
 
92
+ @app.route('/ocr', methods=['POST'])
93
+ def ocr():
94
+ try:
95
+ # Extract and decode image data
96
+ data = request.json
97
+ image_data = data['image'].split(',')[1] # Remove data:image/jpeg;base64,
98
+
99
+ # Decode the base64 string into a NumPy array
100
+ nparr = np.frombuffer(base64.b64decode(image_data), np.uint8)
101
+
102
+ # Convert the NumPy array into an OpenCV image (BGR format)
103
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
104
+
105
+ # Convert the image to RGB (as easyocr expects RGB format)
106
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
107
+
108
+ # Perform OCR using easyocr
109
+ results = reader.readtext(image_rgb)
110
+
111
+ # Check if any text was detected
112
+ text_detected = bool(results) # True if text is detected, False otherwise
113
+
114
+ return jsonify({'text_detected': text_detected})
115
+ except Exception as e:
116
+ return jsonify({'error': str(e)}), 500
117
+
118
+
119
  if __name__ == '__main__':
120
  app.run(host='0.0.0.0', port=7860, debug=True)