Reputation: 188
I have a small code in python that detects text from Images as:
import cv2
image = cv2.imread("sample.jpg")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours
# for each contour found, draw a rectangle around it on original image
for contour in contours:
# get rectangle bounding contour
[x,y,w,h] = cv2.boundingRect(contour)
# discard areas that are too large
if h>300 and w>300:
continue
# discard areas that are too small
if h<40 or w<40:
continue
# draw rectangle around contour on original image
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
# write original image with added contours to disk
cv2.imwrite("contoured.jpg", image)
So the output is a new image that has rectangles over the detected text.
I have also a function that encodes the text from a static image and shows the encoded result onto the console, the function is shown below:
from pytesseract import image_to_string
val_1 = sys.argv[1]
text = image_to_string(Image.open(''+val_1+''))
def encode(key, string):
encode = []
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
encode.append(encoded_c)
encoded_string = "".join(encode)
return base64.urlsafe_b64encode(encoded_string)
encry = encode(key,text)
#print encry
So for eg.If I feed it an image containing text it will extract the text ,encode it (if we give it a key) and print the encoded string onto a console.However is it possible to encode the text on top of the image itself instead of printing it on the console.
Upvotes: 4
Views: 1250
Reputation: 794
Yes, it is possible.
You need the image containing the text and the coordinates of the area with the text. Then you could use the OpenCV function putText().
In order to do this you have to apply some changes to your implementation. You have two different options:
Peform the OCR in every rectangle containing the text, so you should do something like this:
import cv2
from pytesseract import image_to_string
# ..various image operations..
# for each contour found, draw a rectangle, perform OCR in that rectangle and write the result on the image
for contour in contours:
# get rectangle bounding contour
[x,y,w,h] = cv2.boundingRect(contour)
# discard areas that are too large
if h>300 and w>300:
continue
# discard areas that are too small
if h<40 or w<40:
continue
# draw rectangle around contour on original image
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
# get the image area with the text
text_image = image[y:y+h, x:x+w]
# perform OCR
text = image_to_string(text_image)
# encode the text with your function
encry = encode(key, text)
# write the encoded text on the image
cv2.putText(image, encry, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 4, (255,255,255), 2, cv2.LINE_AA)
pytesseract.image_to_boxes
or pytesseract.image_to_data
.I want to clarify that I've not tested the code, so probably there may be some inaccuracies.
Upvotes: 1
Reputation: 6666
If you have the text (which you do) and you have a location that it needs to go (which you do) then it shouldn't be too difficult to add the text over the image.
OpenCV provides the PutText function:
https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html
Here is an example:
font = cv2.FONT_HERSHEY_SIMPLEX
2 cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)
Upvotes: 0