main.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import random
  2. from string import ascii_lowercase, digits
  3. import cv2
  4. import numpy as np
  5. from flask import Flask, request, jsonify
  6. from pytesseract import pytesseract
  7. from waitress import serve
  8. app = Flask(__name__)
  9. app.config['SECRET_KEY'] = ''.join([random.choice(ascii_lowercase + digits) for _ in range(10)])
  10. def preprocess_image_first(image):
  11. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  12. gray = cv2.GaussianBlur(gray, (1, 1), 0)
  13. gray = cv2.convertScaleAbs(gray, alpha=1.6, beta=0)
  14. binary_image = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
  15. return binary_image
  16. def preprocess_image_second(image):
  17. gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  18. gaussian_img = cv2.GaussianBlur(gray_img, (5, 5), 0)
  19. ret, thresh_img = cv2.threshold(gaussian_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  20. return thresh_img
  21. def ocr_image(image):
  22. custom_config = r'--oem 3 --psm 6'
  23. text = pytesseract.image_to_string(image, config=custom_config, lang='rus')
  24. return text
  25. @app.route('/recognition', methods=['POST'])
  26. def text_recognition():
  27. if 'image' not in request.files:
  28. return jsonify({'error': 'No image file provided'}), 400
  29. file = request.files['image']
  30. img = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_COLOR)
  31. response = []
  32. preprocessed_image = preprocess_image_first(img)
  33. text = ocr_image(preprocessed_image)
  34. response.append(text)
  35. preprocessed_image = preprocess_image_second(img)
  36. text = ocr_image(preprocessed_image)
  37. response.append(text)
  38. return jsonify(response)
  39. def main():
  40. serve(app, host='0.0.0.0', port=6543)
  41. if __name__ == '__main__':
  42. main()