How to use opencv and pytesseract to extract text from image?
import cv2
import pytesseract
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
img = Image.open('test.jpg').convert('L')
img.show()
img.save('test','png')
img = cv2.imread('test.png',0)
edges = cv2.Canny(img,100,200)
#contour = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print pytesseract.image_to_string(Image.open(edges))
print pytesseract.image_to_string(edges)
But this is giving error-
Traceback (most recent call last):
File "open.py", line 14, in
print pytesseract.image_to_string(edges)
File "/home/sroy8091/.local/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 143, in image_to_string
if len(image.split()) == 4:
AttributeError: 'NoneType' object has no attribute 'split'
If you like to do some pre-processing using opencv (like you did some edge detection) and later on if you wantto extract text, you can use this command,
# All the imports and other stuffs goes here
img = cv2.imread('test.png',0)
edges = cv2.Canny(img,100,200)
img_new = Image.fromarray(edges)
text = pytesseract.image_to_string(img_new, lang='eng')
print (text)
You cannot use directly Opencv objects with tesseract methods.
Try:
from PIL import Image
from pytesseract import *
image_file = 'test.png'
print(pytesseract.image_to_string(Image.open(image_file)))
Related
I want to capture the box recognized while using YOLOv4 webcam recognition.
So i used this code.
import cv2
import detect as dt
from darknet import Darknet
from PIL import Image
vidcap = cv2.VideoCapture(0)
success, image = vidcap.read()
count = 0
m = Darknet('darknet/data/yolo-obj.cfg')
m.load_weights('darknet/backup/yolo-obj_30000.weights')
use_cuda = 1
m.cuda()
while success:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(image)
im_pil = im_pil.resize((m.width, m.height))
boxes = dt.do_detect(m, im_pil, 0.5, 0.4, use_cuda)
result = open('Desktop/captureyolobox/capture%04d.jpg'%(count), 'w')
for i in range(len(boxes)):
result.write(boxes[i])
count = count + 1
success, image = vidcap.read()
result.close()
I've encountered this problem. I surfed the web to solve the problem, but I couldn't find it. Can you help me?
Traceback (most recent call last):
File "yoloshort.py", line 2, in <module>
import detect as dt
ImportError: No module named detect
Do you mean detect_image in darknet.py? You can check the darknet.py which have you want or not.
I have been attempting to produce an OCR tool following this tutorial on youtube, and using the following script:
import os
import sys
import cv2
import numpy as np
input_f = 'letter.data'
img_resize_factor = 12
start, end = 6, -1
height, width = 16, 8
with open(input_f, 'r') as f:
for line in f.readlines():
data = np.array([255*float(x) for x in line.split('\t')[start:end]])
img = np.reshape(data, (height, width))
img_scaled = cv2.resize(img, None, fx=img_resize_factor, fy=img_resize_factor)
print(line)
cv2.imshow('img', img_scaled)
c = cv2.waitKey()
if c == 27:
break
The code falls over when attempting to use cv2.imshow('img', img_scaled) the window appears however is non responding and the image is not loaded into it.
I am using the most up to date version of OpenCV, I am running this in VisualStudio, and have had to add "python.linting.pylintArgs": ["--extension-pkg-whitelist=cv2"] to the user settings.
The error I get is:
Exception has occurred: cv2.error OpenCV(4.0.0)
c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261:
error: (-2:Unspecified error) in function '__cdecl
cv::CvtHelper,struct cv::Set<3,4,-1>,struct
cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class
cv::_OutputArray &,int)' > Unsupported depth of input image: >
'VDepth::contains(depth)' > where > 'depth' is 6 (CV_64F) File
"C:\Users\aofarrell\Desktop\Python\NeuralNetworks\SimpleOCR.py", line
23, in
break
Everything in your script is wrong.
Solution
1) If you are opening a file, open just file, get data and get out of with statement
2) The error you are experiencing is due to wrong shape
I opened the file and extracted images
import os
import sys
import cv2
import numpy as np
input_f = 'letter.data'
start, end = 6, -1
def file_opener(input_f):
with open(input_f,'r') as fl:
for line in fl.readlines():
yield np.array([255*float(x) for x in line.split('\t')[start:-1]])
iterator = file_opener(input_f)
images = np.array([row for row in iterator]).reshape(52152,16,8) # array with 52152 images
for image_index in range(images.shape[0]):
IMAGE = cv2.resize(images[image_index,:],(0,0),fx=5,fy=5)
cv2.imshow('image {}/{}'.format(image_index,images.shape[0]),IMAGE)
cv2.waitKey(0)
cv2.destroyAllWindows(0)
How can I create a function that takes an image file (not image filename) in python. Simply, like the following:
FaceController.py
import cv2
from Computer_Vision import Face_Detector as FD
def detectface():
img = cv2.imread('DSC_1902.JPG')
FD.detect(img)
detectface()
Face_Detector.py
import cv2
def detect(img):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.namedWindow('img',cv2.WINDOW_NORMAL)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('messigray.png', img)
return img
Error:
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
You can pass a pointer pointing to the image instead of the image or the filename of the image
EDIT
def image_function(imagePointer):
#DO SOMETHING WITH THE IMAGE
#HERE IS THE IMAGE POINTER
image = open('your_image.png')
#CALLING THE FUNCTION
image_function(image)
Sorry, I don't know opencv so I can not help in your code :(
You can actually pass the image as a tensor.
with cv2.imread() and torch.
Which is easy, useful.
short answer:
load with cv2.imread()
transform to tensor with
img = torch.Tensor(img)/255.
That works for my application.
Yours might be a little different.
Code answer:
from Computer_Vision import Face_Detector as FD
def detectface():
import cv2
import torch
folder = r"This Folder/"
image_file = folder+"image.png"
# or
# file = r"image.png"
# image_file = os.path.join(folder, file)
img = imread(image_file)
img = torch.Tensor(img)/255. # THE KEY LINE HERE.
FDdetect(img):
"""Do stuff with object detection..."""
result =
return result
I am trying to do a gender recognition program, below is the code..
import caffe
import os
import numpy as np
import sys
import cv2
import time
#Models root folder
models_path = "./models"
#Loading the mean image
mean_filename=os.path.join(models_path,'./mean.binaryproto')
proto_data = open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean_image = caffe.io.blobproto_to_array(a)[0]
#Loading the gender network
gender_net_pretrained=os.path.join(models_path,
'./gender_net.caffemodel')
gender_net_model_file=os.path.join(models_path,
'./deploy_gender.prototxt')
gender_net = caffe.Classifier(gender_net_model_file, gender_net_pretrained)
#Reshaping mean input image
mean_image = np.transpose(mean_image,(2,1,0))
#Gender labels
gender_list=['Male','Female']
#cv2 Haar Face detector
face_cascade=cv2.CascadeClassifier(os.path.join
(models_path,'haarcascade_frontalface_default.xml'))
#Getting prediction from live camera
cap = cv2.VideoCapture(0)
while True:
ret,frame = cap.read()
if ret is True:
start_time = time.time()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = face_cascade.detectMultiScale(frame_gray, 1.3, 5)
#Finding the largest face
if len(rects) >= 1:
rect_area = [rects[i][2]*rects[i][3] for i in xrange(len(rects))]
rect = rects[np.argmax(rect_area)]
x,y,w,h = rect
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_color = frame[y:y+h, x:x+w]
#Resizing the face image
crop = cv2.resize(roi_color, (256,256))
#Subtraction from mean file
#input_image = crop -mean_image
input_image = rect
#Getting the prediction
start_prediction = time.time()
prediction = gender_net.predict([input_image])
gender = gender_list[prediction[0].argmax()]
print("Time taken by DeepNet model: {}").format(time.time()-start_prediction)
print prediction,gender
cv2.putText(frame,gender,(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2)
print("Total Time taken to process: {}").format(time.time()-start_time)
#Showing output
cv2.imshow("Gender Detection",frame)
cv2.waitKey(1)
#Delete objects
cap.release()
cv2.killAllWindows()
When I am running the I am getting an error:
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
AttributeError: 'module' object has no attribute 'io'
How Can I solve it. I am using cnn_gender_age_prediction model. I want to make a real time gender recognition program using python and cnn_gender_age model.
io is a module in caffe package. Basically when you type import caffe, it will not automatically try to import all modules in caffe package including io. There are two solutions.
First one: import caffe.io manually
import caffe
import caffe.io
Second one: update to the latest caffe version, in which you should find a line in __init__.py under python/caffe directory:
from . import io
How can I read an image from an Internet URL in Python cv2?
This Stack Overflow answer,
import cv2.cv as cv
import urllib2
from cStringIO import StringIO
import PIL.Image as pil
url="some_url"
img_file = urllib2.urlopen(url)
im = StringIO(img_file.read())
is not good because Python reported to me:
TypeError: object.__new__(cStringIO.StringI) is not safe, use cStringIO.StringI.__new__
Since a cv2 image is not a string (save a Unicode one, yucc), but a NumPy array, - use cv2 and NumPy to achieve it:
import cv2
import urllib
import numpy as np
req = urllib.urlopen('http://answers.opencv.org/upfiles/logo_2.png')
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1) # 'Load it as it is'
cv2.imshow('lalala', img)
if cv2.waitKey() & 0xff == 27: quit()
The following reads the image directly into a NumPy array:
from skimage import io
image = io.imread('https://raw2.github.com/scikit-image/scikit-image.github.com/master/_static/img/logo.png')
in python3:
from urllib.request import urlopen
def url_to_image(url, readFlag=cv2.IMREAD_COLOR):
# download the image, convert it to a NumPy array, and then read
# it into OpenCV format
resp = urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, readFlag)
# return the image
return image
this is the implementation of url_to_image in imutils, so you can just call
import imutils
imutils.url_to_image(url)
If you're using requests, you can use this
import requests
import numpy as np
from io import BytesIO
from PIL import Image
def url_to_img(url, save_as=''):
img = Image.open(BytesIO(requests.get(url).content))
if save_as:
img.save(save_as)
return np.array(img)
img = url_to_img('https://xxxxxxxxxxxxxxxxxx')
img = url_to_img('https://xxxxxxxxxxxxxxxxxx', 'sample.jpg')
cv2.imshow(img)
Using requests:
def url_to_numpy(url):
img = Image.open(BytesIO(requests.get(url).content))
return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
Updated Answer
import urllib
import cv2 as cv2
import numpy as np
url = "https://pyimagesearch.com/wp-content/uploads/2015/01/opencv_logo.png"
url_response = urllib.request.urlopen(url)
img_array = np.array(bytearray(url_response.read()), dtype=np.uint8)
img = cv2.imdecode(img_array, -1)
cv2.imshow('URL Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()