From ab7de82af32e7954c1375d2f3ef34544468c14b1 Mon Sep 17 00:00:00 2001 From: maeriil Date: Fri, 29 Sep 2023 15:31:51 -0400 Subject: [PATCH] added black to format code --- backend/main.py | 46 ++++++++++++++++++++-------------------- backend/requirements.txt | 1 + 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/backend/main.py b/backend/main.py index a33cc34..eaf51c0 100644 --- a/backend/main.py +++ b/backend/main.py @@ -6,12 +6,11 @@ # def hello_world(): # return "

asdasd!

" - #: flask to get data from frontend (assume we have data for now) (last thing we build in backend) #: OCR or Pytesseract for extracting text from image #: Pillow library to edit on the images #: googletrans to translate the text from language 1 to English -#: +#: import cv2 import pytesseract @@ -19,11 +18,11 @@ from manga_ocr import MangaOcr from PIL import Image -MOCKIMAGEPATH = 'japanese.png' +MOCKIMAGEPATH = "japanese.png" mockImage = cv2.imread(MOCKIMAGEPATH) -pytesseract_config = r'--oem 3 --psm 5 -l jpn_vert' +pytesseract_config = r"--oem 3 --psm 5 -l jpn_vert" -grayscale = cv2.cvtColor(mockImage, cv2.COLOR_BGR2GRAY) # convert image to grayscale +grayscale = cv2.cvtColor(mockImage, cv2.COLOR_BGR2GRAY) # convert image to grayscale blur = cv2.GaussianBlur(grayscale, (9, 9), 0) # threshold = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] @@ -33,47 +32,48 @@ # invert = 255 - opening img = cv2.imread(MOCKIMAGEPATH) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - + # Performing OTSU threshold ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV) - + # Specify structure shape and kernel size. # Kernel size increases or decreases the area # of the rectangle to be detected. # A smaller value like (10, 10) will detect # each word instead of a sentence. rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18)) - + # Applying dilation on the threshold image -dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1) -cv2.imshow('dilation ', dilation) - +dilation = cv2.dilate(thresh1, rect_kernel, iterations=1) +cv2.imshow("dilation ", dilation) + # Finding contours -contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, - cv2.CHAIN_APPROX_NONE) - +contours, hierarchy = cv2.findContours( + dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE +) + # Creating a copy of image im2 = img.copy() - + # A text file is created and flushed - + # Looping through the identified contours # Then rectangular part is cropped and passed on # to pytesseract for extracting text from it # Extracted text is then written into the text file for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) - + # Drawing a rectangle on copied image rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2) - cv2.imshow('rectangle is ', rect) - + cv2.imshow("rectangle is ", rect) + # Cropping the text block for giving input to OCR - cropped = im2[y:y + h, x:x + w] - + cropped = im2[y : y + h, x : x + w] + # Open the file in append mode file = open("recognized.txt", "a") - + # Apply OCR on the cropped image text = pytesseract.image_to_string(cropped) print(text) @@ -92,4 +92,4 @@ # translator = Translator() # translatedText = translator.translate(jp_text, dest="en", src="ja") -# print('[translated]: ', translatedText.text) \ No newline at end of file +# print('[translated]: ', translatedText.text) diff --git a/backend/requirements.txt b/backend/requirements.txt index 72e2c2e..270536c 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -5,3 +5,4 @@ numpy googletrans==4.0.0-rc1 opencv-python manga-ocr +black \ No newline at end of file