-1

I am using Pytesseract OCR for text detection and I am using a jupyter notebook to run it right now so the output is just popping up as another window. My output for the program is an image and I was wondering if there is a way to use flask or django or something to put my program on a website.

I am trying to allow the user to input their own image and then the program will output another image on the website.

This is the main code:

def pytess(img):

   hImg,wImg,_ = img.shape
   boxes = pytesseract.image_to_boxes(img)
   for b in boxes.splitlines():
       print(b[0])
       b = b.split(' ')
       x,y,w,h= int(b[1]),int(b[2]),int(b[3]),int(b[4])
       cv2.rectangle(img,(x,hImg-y),(w,hImg-h),(0,0,255), 2)
       cv2.putText(img,b[0],(x,hImg-y+25), cv2.FONT_HERSHEY_COMPLEX,1,(50,50,255),2)



   ##Detecting Words
   hImg,wImg,_ = img.shape
   boxes = pytesseract.image_to_data(img)
   for x,b in enumerate(boxes.splitlines()):
       if x!=0:
           b = b.split()
           if len(b)==12:
               x,y,w,h= int(b[6]),int(b[7]),int(b[8]),int(b[9])
               cv2.rectangle(img,(x,y),(w+x,h+y),(0,0,255), 2)
               cv2.putText(img,b[11],(x,y), cv2.FONT_HERSHEY_COMPLEX,1,(50,50,255),2)


   cv2.imshow("Result",img)
   cv2.waitKey(0)
pytess(img)
davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

0

Flask would definitely work for this. You could use a POST request that takes in an image file and then returns an image with the OCR applied.

from flask import Flask, request, Response

# Initialize Flask application
app = Flask(__name__)

# POST request for running OCR
@app.route('/ocr', methods= ['POST'])
def run_ocr():
    image = request.files["image"]
    # Read the image via file.stream, returns PIL image (may need to convert)
    img = Image.open(image.stream)
    # run ocr on image (you will need to update your function to return img)
    processed_img = pytess(img)
    # prepare image for response
    _, img_encoded = cv2.imencode('.png', processed_img)
    response = img_encoded.tostring()
    # return png img with OCR shown
    return Response(response=response, status=200, mimetype='image/png')

if __name__ == "__main__":
    app.run(debug=True)

  • Hi, thanks for the help but I have one issue. When I run the code, I get a 400 bad request error on the website. It says this exactly: Bad Request The browser (or proxy) sent a request that this server could not understand. I also changed the @app.route('/ocr', methods= ['POST']) to @app.route('/', methods=['GET', 'POST']) because I was getting a weird error before. I would really appreciate if u could help. – Yajat Sharma Feb 28 '22 at 02:42
  • You need to make sure that when you send the request you have the format as “multipart/form-data” with the “image” field set as the file – The AI Guy Feb 28 '22 at 16:48
  • Hi I am really grateful for your help but I don't really know what to change. If you could please change it for me and send it here I would REALLY appreciate it. Thank u so much in advance – Yajat Sharma Mar 01 '22 at 02:08
  • I can’t change it here. It isn’t part of the code. Once you deploy the Flask server, it would be in the actual request to the server that you need to distinguish the data format. – The AI Guy Mar 01 '22 at 13:05
  • Hey, do you have discord or another messaging platform from which I can talk to you easier? – Yajat Sharma Mar 02 '22 at 06:16